Conversion#

Data Conversion#

class pydicer.convert.data.ConvertData(working_directory='.')#

Class that facilitates the conversion of the data into its intended final type

Parameters:

working_directory (-) – Main working directory for pydicer. Defaults to “.”.

add_entry(entry: dict)#

Add an entry of a converted data object to the patient’s converted dataframe.

Parameters:

entry (dict) – A dictionary object describing the object converted.

convert(patient: Optional[Union[str, list]] = None, force: bool = True)#

Converts the DICOM which was preprocessed into the pydicer output directory.

Parameters:
  • patient (str|list, optional) – Patient ID or list of patient IDs to convert. Defaults to None.

  • force (bool, optional) – When True objects will be converted even if the output files already exist. Defaults to True.

pydicer.convert.data.get_object_type(sop_class_uid: str) str#

Get the type of the object (used for the output path)

Parameters:

sop_class_uid (str) – The SOP Class UID of the object

Returns:

The object type

Return type:

str

pydicer.convert.data.handle_missing_slice(files: Union[DataFrame, list], ignore_duplicates: bool = False) list#

function to interpolate missing slices in an image

Example usage:

from pydicer.convert.data import handle_missing_slice

input_dic = [
    {
        "file_path" : "path/to/dicom_file_1.dcm",
        "slice_location: -100
    },
    {
        "file_path" : "path/to/dicom_file_2.dcm",
        "slice_location: -98
    },
    {
        "file_path" : "path/to/dicom_file_3.dcm",
        "slice_location: -96
    },
    ...
]
file_paths_list = handle_missing_slices(input_dict)
Parameters:
  • df_files (pd.DataFrame|list) – the DataFrame which was produced by PreprocessData

  • slices (or list of filepaths to dicom) –

  • ignore_duplicates (booleanbool, optional) – specifices whether the function is to ignore

  • ones (duplicate slices when handling missing) –

Returns:

a list of the interpolated file paths

Return type:

file_paths(list)

Find the image series linked to this FOR

Parameters:
  • for_uid (str) – The Frame of Reference UID

  • df_preprocess (pd.DataFrame) – The DataFrame containing the preprocessed DICOM data.

Returns:

DataFrame of the linked series entries

Return type:

pd.DataFrame

Header Conversion#

pydicer.convert.headers.convert_dicom_headers(dcm_file: Union[str, Path], binary_path: str, json_file: Union[str, Path])#

Save the DICOM Headers as a JSON file

Parameters:
  • dcm_file (str|pathlib.Path) – The files from which to save the headers.

  • binary_path (str) – Relative path to binary data which will be placed into JSON.

  • json_file (str|pathlib.Path) – Path to JSON file to save output.

RTSTRUCT Conversion#

pydicer.convert.rtstruct.convert_rtstruct(dcm_img, dcm_rt_file, prefix='Struct_', output_dir='.', output_img=None, spacing=None)#

Convert a DICOM RTSTRUCT to NIFTI masks.

The masks are stored as NIFTI files in the output directory

Parameters:
  • dcm_img (list|SimpleITK.Image) – List of DICOM paths (as str) to use as the reference image series or a SimpleITK image of the already converted image.

  • dcm_rt_file (str|pathlib.Path) – Path to the DICOM RTSTRUCT file

  • prefix (str, optional) – The prefix to give the output files. Defaults to “Struct” + underscore.

  • output_dir (str|pathlib.Path, optional) – Path to the output directory. Defaults to “.”.

  • output_img (str|pathlib.Path, optional) – If set, write the reference image to this file as in NIFTI format. Defaults to None.

  • spacing (list, optional) – Values of image spacing to override. Defaults to None.

pydicer.convert.rtstruct.write_nrrd_from_mask_directory(mask_directory, output_file, colormap=<matplotlib.colors.LinearSegmentedColormap object>)#

Produce a NRRD file from a directory of masks in Nifti format

Parameters:
  • mask_directory (pathlib.Path|str) – Path object of directory containing masks

  • output_file (pathlib.Path|str) – The output NRRD file to write to.

  • color_map (matplotlib.colors.Colormap | dict, optional) – Colormap to use for output. Defaults to matplotlib.colormaps.get_cmap(“rainbow”).

PET Conversion#

pydicer.convert.pt.convert_dicom_to_nifti_pt(input_filepaths, output_filepath)#

Function to convert the dicom files contained in input_filepaths to one NIFTI image.

Parameters:
  • input_filepaths (list) – list of the dicom paths

  • output_filepath (str) – path to the output file path where to store the NIFTI file.

Raises:
  • ValueError – Error to alert when the weight is missing from the PT, to compute the SUV.

  • RuntimeError – Error to alert when one or more slices are missing

  • ValueError – Raised when a modality or a unit (for the PT) is not handled.

Returns:

The numpy image, used to compute the bounding boxes

Return type:

numpy.array

pydicer.convert.pt.get_physical_values_pt(slices)#

Function to Get physical values from raw PET

Parameters:

slices – all pet slices of this patient

Returns:

extract physical values for pet

pydicer.convert.pt.get_sitk_volume_from_np(np_image, pixel_spacing, image_position_patient)#

Function to get sitk volume from np image

Parameters:
  • np_image – extracted pet data with numpy format

  • pixel_spacing – extracted pixel spacing information

  • image_position_patient – extracted image position about this patient

Returns:

a pet sitk data

pydicer.convert.pt.get_suv_from_bqml(slices, decay_time, patient_weight)#

Function to Get SUV from raw PET if units = “BQML”

Parameters:
  • slices – all pet slices from this patient

  • decay_time – decay time

  • patient_weight – patient weight information

Returns:

suv results

pydicer.convert.pt.get_suv_philips(slices)#

Function to Get SUV from raw PET if units = “CNTS”

Parameters:

slices – all pet slices from this patient

Returns:

suv philips results

pydicer.convert.pt.is_approx_equal(x, y, tolerance=0.05)#

Function to know is_approx_equal

Parameters:

y (x and) – two values to be compared

Returns:

True or False