From 83f35e72465ba61888e732a220cce604b68598ec Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 3 Dec 2024 12:06:34 -0500 Subject: [PATCH] update to allow latest version of pydicom which changed read_file to dcmread --- requirements.txt | 2 +- setup.py | 2 +- src/DicomRTTool/ReaderWriter.py | 16 ++++++++++------ src/DicomRTTool/Services/DicomBases.py | 8 ++++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index 18af339..55a6cff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ opencv-python openpyxl pandas Pillow -pydicom==2.4.4 +pydicom scikit-image scipy SimpleITK diff --git a/setup.py b/setup.py index 69af2e5..4d7b2d2 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ name='DicomRTTool', author='Brian Mark Anderson', author_email='b5anderson@health.ucsd.edu', - version='2.1.9', + version='2.2.0', description='Services for reading dicom files, RT structures, and dose files, as well as tools for ' 'converting numpy prediction masks back to an RT structure', long_description=long_description, diff --git a/src/DicomRTTool/ReaderWriter.py b/src/DicomRTTool/ReaderWriter.py index c6b2071..91d67ce 100644 --- a/src/DicomRTTool/ReaderWriter.py +++ b/src/DicomRTTool/ReaderWriter.py @@ -11,6 +11,10 @@ import pydicom import numpy as np from pydicom.tag import Tag +if hasattr(pydicom, 'read_file'): + dcmread_function = pydicom.read_file +else: + dcmread_function = pydicom.dcmread import SimpleITK as sitk from skimage.measure import label, regionprops, find_contours from threading import Thread @@ -258,7 +262,7 @@ def add_dicom_to_dictionary_from_path(self, dicom_path, images_dictionary: Dict[ rt_files = [file for file in file_list if file not in all_names] for i in rt_files: lstRSFile = os.path.join(dicom_path, i) - rt = pydicom.read_file(lstRSFile) + rt = dcmread_function(lstRSFile) modality = rt.Modality if modality.lower().find('struct') != -1: add_rt_to_dictionary(ds=rt, path=lstRSFile, rt_dictionary=rt_dictionary) @@ -1122,7 +1126,7 @@ def get_images(self) -> None: print('Loading images for {} at \n {}\n'.format(self.series_instances_dictionary[index].Description, self.series_instances_dictionary[index].path)) dicom_names = self.series_instances_dictionary[index].files - self.ds = pydicom.read_file(dicom_names[0]) + self.ds = dcmread_function(dicom_names[0]) self.reader.SetFileNames(dicom_names) self.dicom_handle = self.reader.Execute() if self.verbose: @@ -1194,7 +1198,7 @@ def get_dose(self, dose_type="PLAN") -> None: def __characterize_RT__(self, RT: RTBase): if self.RS_struct_uid != RT.SeriesInstanceUID: self.structure_references = {} - self.RS_struct = pydicom.read_file(RT.path) + self.RS_struct = dcmread_function(RT.path) self.RS_struct_uid = RT.SeriesInstanceUID for contour_number in range(len(self.RS_struct.ROIContourSequence)): self.structure_references[ @@ -1348,7 +1352,7 @@ def use_template(self) -> None: if not os.path.exists(self.template_dir): self.template_dir = os.path.join('..', '..', 'Shared_Drive', 'Auto_Contour_Sites', 'template_RS.dcm') self.key_list = self.template_dir.replace('template_RS.dcm', 'key_list.txt') - self.RS_struct = pydicom.read_file(self.template_dir) + self.RS_struct = dcmread_function(self.template_dir) print('Running off a template') self.change_template() @@ -1401,7 +1405,7 @@ def prediction_array_to_RT(self, prediction_array: np.array, output_dir: typing. elif self.RS_struct_uid != self.series_instances_dictionary[index].SeriesInstanceUID: rt_structures = self.series_instances_dictionary[index].RTs for uid_key in rt_structures: - self.RS_struct = pydicom.read_file(rt_structures[uid_key].path) + self.RS_struct = dcmread_function(rt_structures[uid_key].path) self.RS_struct_uid = self.series_instances_dictionary[index].SeriesInstanceUID break @@ -1607,7 +1611,7 @@ def change_template(self): def rewrite_RT(self, lstRSFile: typing.Union[str, bytes, os.PathLike] = None): if lstRSFile is not None: - self.RS_struct = pydicom.read_file(lstRSFile) + self.RS_struct = dcmread_function(lstRSFile) if Tag((0x3006, 0x020)) in self.RS_struct.keys(): self.ROI_Structure = self.RS_struct.StructureSetROISequence else: diff --git a/src/DicomRTTool/Services/DicomBases.py b/src/DicomRTTool/Services/DicomBases.py index e197c82..2a4c076 100644 --- a/src/DicomRTTool/Services/DicomBases.py +++ b/src/DicomRTTool/Services/DicomBases.py @@ -1,5 +1,9 @@ import typing import pydicom +if hasattr(pydicom, 'read_file'): + dcmread_function = pydicom.read_file +else: + dcmread_function = pydicom.dcmread from pydicom.tag import Tag, BaseTag import SimpleITK as sitk from typing import List, Dict @@ -41,7 +45,7 @@ def __init__(self): def load_info(self, sitk_dicom_reader, sitk_string_keys: SitkDicomKeys = None): file_name = sitk_dicom_reader.GetFileName() - ds = pydicom.read_file(file_name) + ds = dcmread_function(file_name) self.SeriesInstanceUID = ds.SeriesInstanceUID self.DoseType = ds.DoseType self.DoseUnits = ds.DoseUnits @@ -68,7 +72,7 @@ def load_info(self, sitk_dicom_reader, sitk_string_keys: SitkDicomKeys = None): def add_beam(self, sitk_dicom_reader): file_name = sitk_dicom_reader.GetFileName() - ds = pydicom.read_file(file_name) + ds = dcmread_function(file_name) if self.SeriesInstanceUID == ds.SeriesInstanceUID: """ Means these are compatible beams