Skip to content

Data Import Export

locastre edited this page Jun 14, 2024 · 3 revisions

Importing to planC

CERR supports importing from DICOM and NIfTI file formats. Supported image modalities include: DICOM CT, PT, MR, RTSTRUCT, RTDOSE; and scans and segmentation stored in NIfTI. Currently, DICOM scans consisting of single-slice-per-DICOM-file are supported.

Reading a DICOM directory into planC

CERR can read all supported imaging objects from a directory and its sub-directories. For example, the following code snippet reads a directory into a planC object.

from cerr import plan_container as pc
dcm_dir = r"\\path\to\Data\dicom\directory"
planC = pc.load_dcm_dir(dcm_dir)

The above example extracts and adds metadata to an empty planC object. Users can pass an existing planC to pc.load_dcm_dir as a 2nd input to add contents to that object instead of starting from an empty instance of planC. For example, the following will sequentially read and add metadata from the two directories to a planC object.

from cerr import plan_container as pc
dcm_dir1 = r"\\path\to\Data\dicom\directory1"
dcm_dir2 = r"\\path\to\Data\dicom\directory2"
planC = pc.load_dcm_dir(dcm_dir1)
planC = pc.load_dcm_dir(dcm_dir2, planC)

Reading scan from a NIfTI file into planC

Scans stored in NIfTI format can be imported to planC as follows:

from cerr import plan_container as pc
niiScan = r'path/to/niiScan.nii.gz'
planC = pc.load_nii_scan(niiScan)
assocScanNum = 0 # index of the original scan
structName = 'tumor'
planC = pc.load_nii_structure(origNiiStr, assocScanNum, planC, labels_dict={1: structName})

Reading segmentation from a NIfTI file into planC

Segmentation stored in NIfTI format can be imported to planC as follows:

niiSeg = r'path/to/niiSeg.nii.gz'
planC = pc.load_nii_scan(niiSeg)
assocScanNum = 0 # index of scan to associate segmentation
structName = 'tumor'
planC = pc.load_nii_structure(niiSeg , assocScanNum, planC, labels_dict={1: structName})

Exporting from planC

CERR supports exporting to DICOM and NIfTI formats.

Exporting Segmentation to RTSTRUCT

from cerr.dcm_export import rtstruct_iod
rtstructFile = r'path/to/export/rtstructFile/ai_seg_test.dcm'
structNumV = [0, len(planC.structure)-1] # Export the first and the last structure in the list
seriesDescription = "AI Generated"
rtstruct_iod.create(structNumV, rtstructFile, planC, seriesDescription)

Exporting Segmentation to NIfTI

strNiiFile = r'path/to/export/niiSeg.nii.gz'
structNum = 0 # index of structure to export to nii
planC.structure[structNum].save_nii(strNiiFile, planC)

Exporting Scan to NIfTI

scanNiiFile = r'path/to/export/niiScan.nii.gz'
scanNum = 0 # index of scan to export to nii
planC.scan[scanNum].save_nii(scanNiiFile)