PyMAIA.utils.file_utils module#
- PyMAIA.utils.file_utils.convert_nifti_pred_to_dicom_seg(nifti_pred_file, patient_dicom_folder, template_file, output_dicom_seg, study_id)[source]#
Convert a NIFTI prediction file (segmentation mask), into a single DICOM SEG file.
patient_dicom_folderandtemplate_fileare used to extract DICOM metadata and information to use when saving the DICOM SEG file.- Parameters:
study_id – Study ID used to match the appropriate DICOM folder.
nifti_pred_file (
Union[str,PathLike]) – NIFTI prediction file (segmentation mask) to convert.patient_dicom_folder (
Union[str,PathLike]) – Original patient DICOM folder, used to retrieve DICOM Metadata.template_file (
Union[str,PathLike]) – Template JSON file for the prediction model/algorithm used. Generated from : http://qiicr.org/dcmqi/#/homeoutput_dicom_seg (
Union[str,PathLike]) – Output DICOM SEG file to save.
- PyMAIA.utils.file_utils.copy_data_from_dict_to_dataset_folder(input_data_file, step, image_folder, config_dict, label_folder=None, num_threads=None, save_label_instance_config=False)[source]#
- Parameters:
input_data_file (
Union[str,PathLike]) – JSON dict path of the input datasetstep (
str) – string list for the step of the dataset (train/test).image_folder (
Union[str,PathLike]) – folder path where to store images (imagesTr/imagesTs).config_dict (
Dict[str,object]) – dictionary with dataset and experiment configuration parameters.label_folder (
Union[str,PathLike,None]) – folder path where to store labels (labelsTr/labelsTs). Default:None. If label_suffix isNone, the label files are not saved.num_threads (
Optional[int]) – number of threads to use in multiprocessing ( Default:os.environ['N_THREADS'])save_label_instance_config (
bool) –- Flag to save label mask together with an instance dictionary as JSON file. NOTE: All the instances are assigned
to instance class
1.
- PyMAIA.utils.file_utils.copy_data_to_dataset_folder(input_data_folder, subjects, image_folder, config_dict, label_folder=None, num_threads=None, save_label_instance_config=False)[source]#
- Parameters:
input_data_folder (
Union[str,PathLike]) – folder path of the input datasetsubjects (
List[str]) – string list containing subject IDs.image_folder (
Union[str,PathLike]) – folder path where to store images (imagesTr/imagesTs).config_dict (
Dict[str,object]) – dictionary with dataset and experiment configuration parameters.label_folder (
Union[str,PathLike,None]) – folder path where to store labels (labelsTr/labelsTs). Default:None. If label_suffix isNone, the label files are not saved.num_threads (
Optional[int]) – number of threads to use in multiprocessing ( Default:os.environ['N_THREADS'])save_label_instance_config (
bool) –- Flag to save label mask together with an instance dictionary as JSON file. NOTE: All the instances are assigned
to instance class
1.
- PyMAIA.utils.file_utils.copy_image_file(input_filepath, output_filepath)[source]#
Copy image file.
- Parameters:
input_filepath (
Union[str,PathLike]) – file path for the file to copyoutput_filepath (
Union[str,PathLike]) – file path where to copy the file
- PyMAIA.utils.file_utils.copy_label_file(input_image, input_label, output_filepath)[source]#
Copy label file, verifying the image information (spacing, orientation).
- Parameters:
input_image (
Union[str,PathLike]) – file path for the input image, to be used as reference when copying image informationinput_label (
Union[str,PathLike]) – file path for the input label to be copiedoutput_filepath (
Union[str,PathLike]) – file location where to save the label image
- PyMAIA.utils.file_utils.copy_subject_folder_to_data_folder(input_data_folder, subjects, data_folder)[source]#
Copy all the specified subject sub-folders to a new data folder.
- Parameters:
input_data_folder (
Union[str,PathLike]) – Input data folder.subjects (
List[str]) – Subjects to copy.data_folder (
Union[str,PathLike]) – Destination data folder.
- PyMAIA.utils.file_utils.create_nndet_data_folder_tree(data_folder, task_name, task_id)[source]#
Create nnDetection folder tree, ready to be populated with the dataset.
nnDetection folder tree:
[raw_data_base]
- [Task000_Example]
dataset.yaml # dataset.json works too
- [raw_splitted]
- [imagesTr]
case0000_0000.nii.gz # case0000 modality 0
case0000_0001.nii.gz # case0000 modality 1
case0001_0000.nii.gz # case0001 modality 0
case0000_0001.nii.gz # case0001 modality 1
- [labelsTr]
case0000.nii.gz # instance segmentation case0000
case0000.json # properties of case0000
case0001.nii.gz # instance segmentation case0001
case0001.json # properties of case0001
[imagesTs] # optional, same structure as imagesTr …
[labelsTs] # optional, same structure as labelsTr …
[preprocessed]
[results]
- [Task001_Example1]
…
- Parameters:
data_folder (
Union[str,PathLike]) – folder path corresponding to the raw_data_base environment variable.task_name (
str) – string used as task_name when creating task foldertask_id (
str) – string used as task_id when creating task folder
- PyMAIA.utils.file_utils.create_nnunet_data_folder_tree(data_folder, task_name, task_id)[source]#
Create nnUnet folder tree, ready to be populated with the dataset.
nnUnet folder tree:
[raw_data_base]
- [Dataset000_Example]
dataset.yaml # dataset.json works too
- [imagesTr]
case0000_0000.nii.gz # case0000 modality 0
case0000_0001.nii.gz # case0000 modality 1
case0001_0000.nii.gz # case0001 modality 0
case0000_0001.nii.gz # case0001 modality 1
- [labelsTr]
case0000.nii.gz # instance segmentation case0000
case0000.json # properties of case0000
case0001.nii.gz # instance segmentation case0001
case0001.json # properties of case0001
[imagesTs] # optional, same structure as imagesTr
…
[labelsTs] # optional, same structure as labelsTr
- [Dataset001_Example1]
…
- Parameters:
data_folder (
str) – folder path corresponding to the raw_data_base environment variable.task_name (
str) – string used as task_name when creating task foldertask_id (
str) – string used as task_id when creating task folder
- PyMAIA.utils.file_utils.generate_dataset_json(output_file, train_subjects, test_subjects, modalities, labels, task_name, file_extension, region_class_order=None, nnunet_format=False)[source]#
Generates and saves a Dataset JSON file.
- Parameters:
nnunet_format (
bool) – Flag to specify which modality key to use.file_extension (
str) – Dataset file extensionoutput_file (
Union[str,PathLike]) – This needs to be the full path to the dataset.json you intend to write, sothe (output_file='DATASET_PATH/dataset.json' where the folder DATASET_PATH points to is the one with)
subfolders. (imagesTr and labelsTr)
train_subjects (
List[str]) – List of subjects in the train set.test_subjects (
List[str]) – List of subjects in the test set.modalities (
Tuple) – tuple of strings with modality names. must be in the same order as the images (first entry_0000.nii.gz (corresponds to)
Example (supposed to be background!)
labels (
Union[Dict,List]) – dict with int->str (key->value) mapping the label IDs to label names. Note that 0 is alwaysExample – the dictionaries for each label task are nested into a list.
task_name (
str) – The name of the dataset.region_class_order – Optional list of strings with the region class order, used for region-based training. Default:
None.
- PyMAIA.utils.file_utils.order_data_folder_by_patient(folder_path, file_pattern)[source]#
Order all the files in the root folder into corresponding subdirectories, according to the specified file pattern.
- Parameters:
folder_path (
Union[str,PathLike]) – Root folder path.file_pattern (
str) – File pattern to group the files and create the corresponding subdirectories.
- PyMAIA.utils.file_utils.order_data_in_single_folder(root_path, output_path, assign_parent_dir_name=False, file_extension='')[source]#
Moves all the sub-files, found iteratively from the root directory, to the output folder. Recursively removes all the empty subdirectories. If the assign_parent_dir_name flag is set to True, the parent directory name for each file will be used as suffix appended to the filename (used when images and masks are divided in different subfolders).
- Parameters:
file_extension (
str) – File extension for the files in the selected folder.assign_parent_dir_name (
bool) – Flag to set if to assign the parent directory name as suffix.root_path (
Union[str,PathLike]) – Root folder.output_path (
Union[str,PathLike]) – Output folder.
- PyMAIA.utils.file_utils.remove_empty_folder_recursive(folder_path)[source]#
Recursively removes all the empty subdirectories of the root folder.
- Parameters:
folder_path (
Union[str,PathLike]) – Root folder path.
- PyMAIA.utils.file_utils.save_config_json(config_dict, output_json)[source]#
Save dictionary as JSON file.
- Parameters:
output_json (
Union[str,PathLike]) – JSON file path to be savedconfig_dict (
Dict[str,object]) – dictionary to be saved in JSON format in the RESULTS_FOLDER
- PyMAIA.utils.file_utils.split_dataset(input_data_folder, test_split_ratio, seed, patient_class_file=None, classes=None)[source]#
Split dataset into a train/test split, given the specified ratio.
- Parameters:
classes (
Optional[List[str]]) – List of Patient classes to include in the experiment.patient_class_file (
Union[str,PathLike,None]) – File path to JSON Patient Class map.input_data_folder (
Union[str,PathLike]) – folder path of the input dataset.test_split_ratio (
int) – integer value in the range 0-100, specifying the split ratio to be used for the test set.seed (
int) – integer value to be used as random seed.
- Return type:
Tuple[List[str],List[str]]- Returns:
lists of strings containing subject IDs for train set and test set respectively.
- PyMAIA.utils.file_utils.subfiles(folder, join=True, prefix=None, suffix=None, sort=True)[source]#
Given a folder path, returns a list with all the files in the folder.
- Parameters:
folder (
Union[str,PathLike]) – Folder path.join (
bool) – Flag to return the complete file paths or only the relative file names.prefix (
Optional[str]) – Filter the files with the specified prefix.suffix (
Optional[str]) – Filter the files with the specified suffix.sort (
bool) – Flag to sort the files in the list by alphabetical order.
- Return type:
List[str]- Returns:
Filename list.
- PyMAIA.utils.file_utils.subfolders(folder, join=True, sort=True)[source]#
Given a folder path, returns a list with all the subfolders in the folder.
- Parameters:
folder (
Union[str,PathLike]) – Folder path.join (
bool) – Flag to return the complete folder paths or only the relative folder names.sort (
bool) – Flag to sort the sub folders in the list by alphabetical order.
- Return type:
List[str]- Returns:
Sub folder list.