def write_output_data_to_disk(output_data_dict, output_directory='./', output_file_suffix='.nii.gz', overwrite_existing_files=False):
    '\n    Write output to disk\n    '
    if (output_data_dict is None):
        return
    filename_fields = [i for i in output_data_dict.keys() if (i != 'parent_sorting_data')]
    parent_sorting_data = output_data_dict['parent_sorting_data']
    files_written = {}
    '\n    Write the the converted images to disk\n\n    ! CONSIDER\n    We could simply write as we go?\n    Pro: save memory, important if processing very large files\n    Con: Reading as we go allows proper indexing\n\n    '
    for field in filename_fields:
        logger.info(f'  Writing files for field: {field}')
        p = ((pathlib.Path(output_directory) / parent_sorting_data) / field)
        p.mkdir(parents=True, exist_ok=True)
        files_written[field] = []
        for (field_filename_base, field_list) in output_data_dict[field].items():
            if isinstance(field_list, (tuple, list)):
                field_list_flat = list(flatten(field_list))
                for (suffix, file_to_write) in enumerate(field_list_flat):
                    field_filename = (field_filename_base + f'_{suffix}')
                    while ('__' in field_filename):
                        field_filename = field_filename.replace('__', '_')
                    while (field_filename[(- 1)] == '_'):
                        field_filename = field_filename[:(- 1)]
                    output_name = (((pathlib.Path(output_directory) / parent_sorting_data) / field) / (field_filename + output_file_suffix))
                    files_written[field].append(output_name)
                    if output_name.is_file():
                        logger.warning(f'  File exists: {output_name}')
                        if overwrite_existing_files:
                            logger.warning('  You have selected to overwrite existing files.')
                        else:
                            logger.info('  You have selected to NOT overwrite existing files. Continuing.')
                            continue
                    sitk.WriteImage(file_to_write, output_name.as_posix())
            else:
                field_filename = field_filename_base
                file_to_write = field_list
                while ('__' in field_filename):
                    field_filename = field_filename.replace('__', '_')
                while (field_filename[(- 1)] == '_'):
                    field_filename = field_filename[:(- 1)]
                '\n                ! TO DO\n                Use pathlib, and perform some checks so we don"t overwrite anything!\n                '
                output_name = (((pathlib.Path(output_directory) / parent_sorting_data) / field) / (field_filename + output_file_suffix))
                files_written[field].append(output_name)
                if output_name.is_file():
                    logger.warning(f'  File exists: {output_name}')
                    if overwrite_existing_files:
                        logger.warning('  You have selected to overwrite existing files.')
                    else:
                        logger.info('  You have selected to NOT overwrite existing files. Continuing.')
                        continue
                sitk.WriteImage(file_to_write, output_name.as_posix())
    return files_written
Write output to disk
def write_output_data_to_disk(output_data_dict, output_directory='./', output_file_suffix='.nii.gz', overwrite_existing_files=False):
    '\n    \n    '
    if (output_data_dict is None):
        return
    filename_fields = [i for i in output_data_dict.keys() if (i != 'parent_sorting_data')]
    parent_sorting_data = output_data_dict['parent_sorting_data']
    files_written = {}
    '\n    Write the the converted images to disk\n\n    ! CONSIDER\n    We could simply write as we go?\n    Pro: save memory, important if processing very large files\n    Con: Reading as we go allows proper indexing\n\n    '
    for field in filename_fields:
        logger.info(f'  Writing files for field: {field}')
        p = ((pathlib.Path(output_directory) / parent_sorting_data) / field)
        p.mkdir(parents=True, exist_ok=True)
        files_written[field] = []
        for (field_filename_base, field_list) in output_data_dict[field].items():
            if isinstance(field_list, (tuple, list)):
                field_list_flat = list(flatten(field_list))
                for (suffix, file_to_write) in enumerate(field_list_flat):
                    field_filename = (field_filename_base + f'_{suffix}')
                    while ('__' in field_filename):
                        field_filename = field_filename.replace('__', '_')
                    while (field_filename[(- 1)] == '_'):
                        field_filename = field_filename[:(- 1)]
                    output_name = (((pathlib.Path(output_directory) / parent_sorting_data) / field) / (field_filename + output_file_suffix))
                    files_written[field].append(output_name)
                    if output_name.is_file():
                        logger.warning(f'  File exists: {output_name}')
                        if overwrite_existing_files:
                            logger.warning('  You have selected to overwrite existing files.')
                        else:
                            logger.info('  You have selected to NOT overwrite existing files. Continuing.')
                            continue
                    sitk.WriteImage(file_to_write, output_name.as_posix())
            else:
                field_filename = field_filename_base
                file_to_write = field_list
                while ('__' in field_filename):
                    field_filename = field_filename.replace('__', '_')
                while (field_filename[(- 1)] == '_'):
                    field_filename = field_filename[:(- 1)]
                '\n                ! TO DO\n                Use pathlib, and perform some checks so we don"t overwrite anything!\n                '
                output_name = (((pathlib.Path(output_directory) / parent_sorting_data) / field) / (field_filename + output_file_suffix))
                files_written[field].append(output_name)
                if output_name.is_file():
                    logger.warning(f'  File exists: {output_name}')
                    if overwrite_existing_files:
                        logger.warning('  You have selected to overwrite existing files.')
                    else:
                        logger.info('  You have selected to NOT overwrite existing files. Continuing.')
                        continue
                sitk.WriteImage(file_to_write, output_name.as_posix())
    return files_written