From 7894042b6320d9c05054ff5ad12820f9a383ce09 Mon Sep 17 00:00:00 2001 From: Thomas BAUDIER Date: Thu, 31 Oct 2024 15:58:48 +0100 Subject: [PATCH 1/3] Pydicom change dcm_read to dcmread --- bin/gt_dicom_rt_struct_to_image | 2 +- gatetools/dvh.py | 8 ++++---- gatetools/image_convert.py | 20 ++++++++++---------- gatetools/roi_utils.py | 2 +- setup.py | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/bin/gt_dicom_rt_struct_to_image b/bin/gt_dicom_rt_struct_to_image index d23b0b2..9977789 100755 --- a/bin/gt_dicom_rt_struct_to_image +++ b/bin/gt_dicom_rt_struct_to_image @@ -40,7 +40,7 @@ def gt_dicom_rt_struct_to_image(list_roi, filename_struct, filename_img, roi, cr gt.logging_conf(verbose=(verbose or list_roi),logfile=logfile) # read dicom struct - structset = pydicom.read_file(filename_struct, force=True) + structset = pydicom.dcmread(filename_struct, force=True) # print roi names roi_names = gt.list_roinames(structset) diff --git a/gatetools/dvh.py b/gatetools/dvh.py index 30d5a5e..268e365 100644 --- a/gatetools/dvh.py +++ b/gatetools/dvh.py @@ -104,7 +104,7 @@ def test_dvh(self): logger.info('Test_DVH test_dvh') 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)) + structset = pydicom.dcmread(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]) @@ -123,7 +123,7 @@ 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)) + structset = pydicom.dcmread(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]) @@ -141,7 +141,7 @@ def test_dvh_compute_v(self): logger.info('Test_DVH test_dvh_compute_v') 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)) + structset = pydicom.dcmread(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]) @@ -156,7 +156,7 @@ def test_dvh_compute_d(self): logger.info('Test_DVH test_dvh_compute_d') 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)) + structset = pydicom.dcmread(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]) diff --git a/gatetools/image_convert.py b/gatetools/image_convert.py index 37ca38a..5e2dafa 100644 --- a/gatetools/image_convert.py +++ b/gatetools/image_convert.py @@ -105,9 +105,9 @@ def separate_series(dicomFiles): #Load dicom files for file in dicomFiles: try: - seriesInstanceUID = pydicom.read_file(file)[0x0020, 0x000e].value + seriesInstanceUID = pydicom.dcmread(file)[0x0020, 0x000e].value except pydicom.errors.InvalidDicomError: - ds = pydicom.read_file(file, force=True) + ds = pydicom.dcmread(file, force=True) ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian seriesInstanceUID = ds[0x0020, 0x000e].value print(seriesInstanceUID) @@ -125,9 +125,9 @@ def separate_accessionNumber_series(series): for serie in series.keys(): for file in series[serie]: try: - accessionNumber = pydicom.read_file(file)[0x0020, 0x0012].value + accessionNumber = pydicom.dcmread(file)[0x0020, 0x0012].value except pydicom.errors.InvalidDicomError: - ds = pydicom.read_file(file, force=True) + ds = pydicom.dcmread(file, force=True) ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian accessionNumber = ds[0x0020, 0x0012].value new_key = str(serie) + "_" + str(accessionNumber) @@ -145,9 +145,9 @@ def separate_sequenceName_series(series): for serie in series.keys(): for file in series[serie]: try: - sequenceName = pydicom.read_file(file)[0x0018, 0x0024].value + sequenceName = pydicom.dcmread(file)[0x0018, 0x0024].value except pydicom.errors.InvalidDicomError: - ds = pydicom.read_file(file, force=True) + ds = pydicom.dcmread(file, force=True) ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian sequenceName = ds[0x0018, 0x0024].value new_key = str(serie) + "_" + str(sequenceName) @@ -165,9 +165,9 @@ def read_dicom(dicomFiles): #Load dicom files for file in dicomFiles: try: - files.append(pydicom.read_file(file)) + files.append(pydicom.dcmread(file)) except pydicom.errors.InvalidDicomError: - ds = pydicom.read_file(file, force=True) + ds = pydicom.dcmread(file, force=True) ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian files.append(ds) @@ -242,9 +242,9 @@ def read_3d_dicom(dicomFile, flip=False): """ files = [] try: - files.append(pydicom.read_file(dicomFile[0])) + files.append(pydicom.dcmread(dicomFile[0])) except pydicom.errors.InvalidDicomError: - ds = pydicom.read_file(dicomFile[0], force=True) + ds = pydicom.dcmread(dicomFile[0], force=True) ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian files.append(ds) diff --git a/gatetools/roi_utils.py b/gatetools/roi_utils.py index 9669a03..c6ce62b 100644 --- a/gatetools/roi_utils.py +++ b/gatetools/roi_utils.py @@ -783,7 +783,7 @@ class Test_ROI(LoggedTestCase): def test_roi(self): 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)) + structset = pydicom.dcmread(os.path.join(tmpdirpath, filenameStruct)) # roi names roi_names = list_roinames(structset) diff --git a/setup.py b/setup.py index 40b3ada..633129f 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ 'click', 'numpy', 'scipy', - 'pydicom', + 'pydicom>=3.0.0', 'tqdm', 'colored', 'itk>=5.2.0', From 24d8099efab3f05b134f8c5e9cd6620c6f04b16d Mon Sep 17 00:00:00 2001 From: Thomas BAUDIER Date: Thu, 31 Oct 2024 16:12:56 +0100 Subject: [PATCH 2/3] Change version of pydicom For python < 3.10 pydicom 3.0 is not available 'ITK is now > 5.4' --- .github/workflows/main.yml | 6 +----- setup.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6496df2..78d954b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,6 @@ jobs: fail-fast: false matrix: python-version: [3.7, 3.8, 3.9, '3.10', '3.11'] - ITK_VERSION: [5.3.0, pre] steps: - name: Checkout github repo @@ -33,10 +32,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - if [ ${{ matrix.ITK_VERSION }} == 'pre' ]; - then pip install itk --pre; - else pip install itk==${{ matrix.ITK_VERSION }}; - fi + pip install itk pip install -e . pip install uproot3 pip freeze diff --git a/setup.py b/setup.py index 633129f..40b3ada 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ 'click', 'numpy', 'scipy', - 'pydicom>=3.0.0', + 'pydicom', 'tqdm', 'colored', 'itk>=5.2.0', From ed6adb2ad5625589a5a71c1dd2ff149819afa6e0 Mon Sep 17 00:00:00 2001 From: Thomas BAUDIER Date: Thu, 31 Oct 2024 16:41:30 +0100 Subject: [PATCH 3/3] With numpy>2.0 need to change float32 to float64 to have a consistent hash between numpy version --- gatetools/image_uncertainty.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gatetools/image_uncertainty.py b/gatetools/image_uncertainty.py index b344b49..297074f 100644 --- a/gatetools/image_uncertainty.py +++ b/gatetools/image_uncertainty.py @@ -202,9 +202,9 @@ def test_image_uncertainty(self): xx, yy, zz = np.meshgrid(x, y, z) npImage = 10*xx+4.5 npsImage = 10*xx**2+9*xx+2.85 - image = itk.image_from_array(np.float32(npImage)) + image = itk.image_from_array(np.float64(npImage)) images = [image] - simage = itk.image_from_array(np.float32(npsImage)) + simage = itk.image_from_array(np.float64(npsImage)) simages = [simage] uncertainty = image_uncertainty(images, simages, N=1000000000000) tmpdirpath = tempfile.mkdtemp() @@ -212,8 +212,8 @@ def test_image_uncertainty(self): with open(os.path.join(tmpdirpath, "uncertainty.mha"),"rb") as fnew: bytesNew = fnew.read() new_hash = hashlib.sha256(bytesNew).hexdigest() - self.assertTrue("0a2dc7a0e28509c569cecde6b6252507936b29365cb4db0c75ab3c0fab3b2bc4" == new_hash) - shutil.rmtree(tmpdirpath) + self.assertTrue("0e1f8e0f0d7d7d3921c726dc33409345e6d9b8bfcc53b797d67f8b48997fa1a5" == new_hash) + print(tmpdirpath) def test_image_uncertainty_by_slice(self): x = np.arange(0, 1, 0.01) y = np.arange(0, 1, 0.01) @@ -221,9 +221,9 @@ def test_image_uncertainty_by_slice(self): xx, yy, zz = np.meshgrid(x, y, z) npImage = 10*xx+4.5 npsImage = 10*xx**2+9*xx+2.85 - image = itk.image_from_array(np.float32(npImage)) + image = itk.image_from_array(np.float64(npImage)) images = [image] - simage = itk.image_from_array(np.float32(npsImage)) + simage = itk.image_from_array(np.float64(npsImage)) simages = [simage] uncertainty, mean, nb = image_uncertainty_by_slice(images, simages, N=1000000000000) tmpdirpath = tempfile.mkdtemp() @@ -234,7 +234,7 @@ def test_image_uncertainty_by_slice(self): with open(os.path.join(tmpdirpath, "uncertainty.mha"),"rb") as fnew: bytesNew = fnew.read() new_hash = hashlib.sha256(bytesNew).hexdigest() - self.assertTrue("0a2dc7a0e28509c569cecde6b6252507936b29365cb4db0c75ab3c0fab3b2bc4" == new_hash) + self.assertTrue("0e1f8e0f0d7d7d3921c726dc33409345e6d9b8bfcc53b797d67f8b48997fa1a5" == new_hash) shutil.rmtree(tmpdirpath) def test_image_uncertainty_Poisson(self): x = np.arange(0, 1, 0.01)