def test_dvh_volume(self): logger.info('Test_DVH test_dvh_volume') tmpdirpath = tempfile.mkdtemp() filenameStruct = wget.download( "https://gitlab.in2p3.fr/opengate/gatetools_data/-/raw/master/rtstruct.dcm?inline=false", out=tmpdirpath, bar=None) structset = pydicom.read_file(os.path.join(tmpdirpath, filenameStruct)) filenameDose = wget.download( "https://gitlab.in2p3.fr/opengate/gatetools_data/-/raw/master/rtdose.dcm?inline=false", out=tmpdirpath, bar=None) doseImage = gt.read_3d_dicom([os.path.join(tmpdirpath, filenameDose)]) transformImage = gt.applyTransformation(input=doseImage, neworigin=[-176, -320, -235]) aroi = gt.region_of_interest(structset, "PTV") mask = aroi.get_mask(transformImage, corrected=False) doseValues, volumePercentage = createDVH(transformImage, mask, bins=100, useCm3=True) self.assertTrue(np.all(volumePercentage >= 0)) self.assertTrue(np.all(volumePercentage <= 115120)) self.assertTrue(len(doseValues) == 100) self.assertTrue(len(volumePercentage) == 100) shutil.rmtree(tmpdirpath)
def test_dvh_compute_d(self): logger.info('Test_DVH test_dvh_compute_d') tmpdirpath = tempfile.mkdtemp() filenameStruct = wget.download("https://github.com/OpenGATE/GateTools/raw/master/dataTest/rtstruct.dcm", out=tmpdirpath, bar=None) structset = pydicom.read_file(os.path.join(tmpdirpath, filenameStruct)) filenameDose = wget.download("https://github.com/OpenGATE/GateTools/raw/master/dataTest/rtdose.dcm", out=tmpdirpath, bar=None) doseImage = gt.read_3d_dicom([os.path.join(tmpdirpath, filenameDose)]) transformImage = gt.applyTransformation(input=doseImage, neworigin=[-176, -320, -235]) aroi = gt.region_of_interest(structset, "PTV") mask = aroi.get_mask(transformImage, corrected=False) doseValues, volumePercentage = createDVH(transformImage, mask) self.assertTrue(np.isclose(computeD(doseValues, volumePercentage, 95), 0.15428162737918327)) shutil.rmtree(tmpdirpath)
def test_write_dicom(self): logger.info('Test_Write_Dicom test_write_dicom') image = createImageExample() tmpdirpath = tempfile.mkdtemp() itk.imwrite(image, os.path.join(tmpdirpath, "input.mhd")) filenameDcm = wget.download("https://github.com/OpenGATE/GateTools/raw/master/dataTest/rtdose.dcm", out=tmpdirpath, bar=None) writeDicom(image, dicom = os.path.join(tmpdirpath, filenameDcm), output = os.path.join(tmpdirpath, "output.dcm")) convertedDicom = gt.read_3d_dicom([os.path.join(tmpdirpath, "output.dcm")]) itk.imwrite(convertedDicom, os.path.join(tmpdirpath, "output.mhd")) with open(os.path.join(tmpdirpath, "output.mhd"),"rb") as fnew: bytesNew = fnew.read() new_hash = hashlib.sha256(bytesNew).hexdigest() self.assertTrue("65fc74e082eb4a69b60c289435564488956ba73bb274637fe0969c2152176308" == new_hash) shutil.rmtree(tmpdirpath)
def test_dvh(self): logger.info('Test_DVH test_dvh') tmpdirpath = tempfile.mkdtemp() filenameStruct = wget.download("https://github.com/OpenGATE/GateTools/raw/master/dataTest/rtstruct.dcm", out=tmpdirpath, bar=None) structset = pydicom.read_file(os.path.join(tmpdirpath, filenameStruct)) filenameDose = wget.download("https://github.com/OpenGATE/GateTools/raw/master/dataTest/rtdose.dcm", out=tmpdirpath, bar=None) doseImage = gt.read_3d_dicom([os.path.join(tmpdirpath, filenameDose)]) transformImage = gt.applyTransformation(input=doseImage, neworigin=[-176, -320, -235]) aroi = gt.region_of_interest(structset, "PTV") mask = aroi.get_mask(transformImage, corrected=False) doseValues, volumePercentage = createDVH(transformImage, mask, bins=100) self.assertTrue(np.allclose(doseValues, np.array([0.10095047205686569, 0.3028514161705971, 0.5047523528337479, 0.7066532969474792, 0.9085542261600494, 1.1104551553726196, 1.3123561143875122, 1.5142570734024048, 1.7161580324172974, 1.9180589318275452, 2.119959831237793, 2.3218607902526855, 2.523761749267578, 2.7256627082824707, 2.9275636672973633, 3.129464626312256, 3.3313655853271484, 3.533266544342041, 3.7351675033569336, 3.9370683431625366, 4.13896918296814, 4.340870141983032, 4.542771100997925, 4.744672060012817, 4.94657301902771, 5.1484739780426025, 5.350374937057495, 5.552275896072388, 5.75417685508728, 5.956077814102173, 6.157978773117065, 6.359879732131958, 6.561780691146851, 6.763681650161743, 6.965582609176636, 7.167483568191528, 7.369384527206421, 7.5712854862213135, 7.773186445236206, 7.9750871658325195, 8.176988124847412, 8.378889083862305, 8.580790042877197, 8.78269100189209, 8.984591960906982, 9.186492919921875, 9.388393878936768, 9.59029483795166, 9.792195796966553, 9.994096755981445, 10.195997714996338, 10.39789867401123, 10.599799633026123, 10.801700592041016, 11.003601551055908, 11.2055025100708, 11.407403469085693, 11.609304428100586, 11.811205387115479, 12.013106346130371, 12.215007305145264, 12.416908264160156, 12.618809223175049, 12.820710182189941, 13.022610664367676, 13.224511623382568, 13.426412582397461, 13.628313541412354, 13.830214500427246, 14.032115459442139, 14.234016418457031, 14.435917377471924, 14.637818336486816, 14.839719295501709, 15.041620254516602, 15.243521213531494, 15.445422172546387, 15.64732313156128, 15.849224090576172, 16.051124572753906, 16.253026008605957, 16.454927444458008, 16.656827926635742, 16.858728408813477, 17.060629844665527, 17.262531280517578, 17.464431762695312, 17.666332244873047, 17.868233680725098, 18.07013511657715, 18.272035598754883, 18.473936080932617, 18.675837516784668, 18.87773895263672, 19.079639434814453, 19.281539916992188, 19.483440399169922, 19.685341835021973, 19.887243270874023, 20.089143753051758]))) self.assertTrue(np.all(volumePercentage >= 0)) self.assertTrue(np.all(volumePercentage <= 100)) self.assertTrue(len(doseValues) == 100) self.assertTrue(len(volumePercentage) == 100) shutil.rmtree(tmpdirpath)
def insert_image_from_dicom(db, dicom_series, dicom_path): ''' Convert one Dicom image to MHD image ''' # modality modality = dicom_series['modality'] #Try guessing pixel unit from dicom try: ds = pydicom.read_file(dicom_path) unit = ds[0x0054, 0x1001].value if unit == "BQML": pixel_unit = 'Bq/mL' else: pixel_unit = unit except: # guess pixel unit FIXME --> check dicom tag ? pixel_unit = 'undefined' if modality == 'CT': pixel_unit = 'HU' if modality == 'NM': pixel_unit = 'counts' if modality == 'RTDOSE': pixel_unit = 'Gy' # get dicom files files = syd.get_dicom_series_files(db, dicom_series) if len(files) == 0: s = 'Error, no file associated with this dicom serie' syd.raise_except(s) # get folder folder = dicom_series['folder'] folder = os.path.join(db.absolute_data_folder, folder) acquisition_id = int( os.path.basename(os.path.dirname(folder)).split('_')[1]) suid = dicom_series['series_uid'] PixelType = itk.ctype('float') Dimension = 3 namesGenerator = itk.GDCMSeriesFileNames.New() namesGenerator.SetUseSeriesDetails(False) namesGenerator.AddSeriesRestriction("0008|0021") namesGenerator.SetGlobalWarningDisplay(False) namesGenerator.SetDirectory(folder) seriesUID = namesGenerator.GetSeriesUIDs() fileNames = namesGenerator.GetFileNames(seriesUID[0]) # read dicom image if len(fileNames) > 1: itk_image = gt.read_dicom(fileNames) else: itk_image = gt.read_3d_dicom(fileNames) # pixel_type (ignored) # pixel_type = image.GetPixelIDTypeAsString() # GetNumberOfComponentsPerPixel # convert: assume only 2 type short for CT and float for everything else pixel_type = 'float' if modality == 'CT': pixel_type = 'signed_short' InputImageType = itk.Image[itk.F, Dimension] OutputImageType = itk.Image[itk.SS, Dimension] castImageFilter = itk.CastImageFilter[InputImageType, OutputImageType].New() castImageFilter.SetInput(itk_image) castImageFilter.Update() itk_image = castImageFilter.GetOutput() # else: # pixel_type = 'float' # try: # itk_image = sitk.Cast(itk_image, sitk.sitkFloat32) # except: # s = 'Cannot cast image. Ignoring '+str(dicom_series) # warning(s) # return None # injection ? injid = None if 'injection_id' in dicom_series: injid = dicom_series.injection_id # create Image #syd.update_nested_one(db, dicom_series) labels = '' if 'labels' in dicom_series: labels = dicom_series.labels dicom_study = syd.find_one(db['DicomStudy'], id=dicom_series.dicom_study_id) patient = syd.find_one(db['Patient'], id=dicom_study.patient_id) img = { 'patient_id': patient.id, 'injection_id': injid, 'dicom_series_id': dicom_series.id, 'acquisition_id': acquisition_id, 'pixel_type': pixel_type, 'pixel_unit': pixel_unit, 'frame_of_reference_uid': dicom_series.frame_of_reference_uid, 'modality': modality, 'acquisition_date': dicom_series.acquisition_date, 'labels': labels } # insert the image in the db img = syd.insert_new_image(db, img, itk_image) # write the mhd file p = syd.get_image_filename(db, img) itk.imwrite(itk_image, p) return img