Converting your data¶
See Querying your database on how to create a query used to plan out a conversion.
plan = db.plan('{patient_id}/prostateX_{series_description}_{instance_creation_time}', query_0000)
# ensure these properties are set
plan.target_dir = 'path/to/output_directory'
plan.extension = '.mha'
# print out a detailed structure of our intended conversion
print(plan)
plan.execute(max_workers=4)
# optionally pass a custom reader, independently of the one used during database creation
# plan.execute(max_workers=4, reader_cls=MyDICOMReader)
The print gives:
dicomselect conversion plan
source_dir C:\Repos\dicomselect\tests\input\ProstateX
target_dir C:\Repos\dicomselect\tests\output\example
extension .mha
.
└── ProstateX-0000
├── prostateX_ep2d_diff_tra_DYNDISTCALC_BVAL_120403.703.mha
├── prostateX_ep2d_diff_tra_DYNDIST_120403.515.mha
├── prostateX_ep2d_diff_tra_DYNDIST_ADC_120403.625.mha
├── prostateX_t2_tse_tra_115754.609.mha
└── prostateX_tfl_3d PD ref_tra_1.5x1.5_t3_120615.109.mha
Conversion API¶
- class dicomselect.convert.Plan(*args)¶
Bases:
LoggerThis object is created from Database.plan, ensure the target_dir property has a target prior to performing Plan.execute().
- execute(reader_cls: Type[Reader] = <class 'dicomselect.readers.dicom.reader.DICOMImageReader'>, max_workers: int = 4, overwrite=False)¶
Execute the conversion plan.
- Parameters:
reader_cls (Type[Reader]) – File reader to use during conversion. Defaults to DICOMImageReader. You can pass a custom subclass here independently of the reader used during database creation.
max_workers (int) – Max number of workers for parallel execution of this conversion.
overwrite – If overwrite is true, conversion will overwrite existing files.
Examples
>>> plan = Database(db_path).plan(template_str, query_000) >>> TODO: update this example >>> def my_postprocess_func(reader: DICOMImageReader) -> DICOMImageReader: >>> dicom_slice_paths_per_bvalue = {} >>> for path in reader.dicom_slice_paths: >>> img = sitk.ReadImage(str(path)) >>> bvalue = int(img.GetMetaData("0018|9087")) >>> (...) >>> dicom_slice_paths_per_bvalue[bvalue] = dicom_slice_paths_per_bvalue.get(bvalue, []) + [path] >>> >>> # convert each b-value to a single image >>> diffusion_images = {} >>> for bvalue, dicom_slice_paths in dicom_slice_paths_per_bvalue.items(): >>> with tempfile.TemporaryDirectory() as tmpdirname: >>> # copy DICOM slices to temporary directory >>> (...) >>> >>> # read DICOM sequence >>> (...) >>> >>> # read metadata from the last DICOM slice and set metadata >>> ifr = sitk.ImageFileReader() >>> (...) >>> >>> # store image >>> diffusion_images[str(bvalue)] = image >>> >>> return diffusion_images >>> >>> plan.execute()
- property extension: str¶
The extension defines the converted filetype. See here for possible file formats to convert to.
- property ignore_validation_errors: bool¶
Ignore validation errors. They will still be logged, but not raised. This is useful if you are handling all errors in the postprocess_func of your Plan.execute function.
- property source_dir: Path¶
Source directory, containing your data that is to be converted. Defaults to the same directory used when the database was created, and so it is not recommended to change this value.
- property target_dir: Path¶
Target directory, to contain the converted data.
- to_string() str¶
The conversion plan printed as a string, in a tree representation.
- Return type:
str