Skip to content

Commit

Permalink
changed all mutable defaults to None
Browse files Browse the repository at this point in the history
Former-commit-id: 41630cd
  • Loading branch information
fishingguy456 committed Jun 17, 2022
1 parent a9f0e87 commit f626aaa
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 160 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/med-imagetools.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 18 additions & 73 deletions imgtools/io/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def read_header(path):

def read_dicom_series(path: str,
series_id: Optional[str] = None,
recursive: bool = False,
modality: str = "CT") -> Scan:
recursive: bool = False) -> Scan:
"""Read DICOM series as SimpleITK Image.
Parameters
Expand Down Expand Up @@ -70,72 +69,6 @@ def read_dicom_series(path: str,
reader.LoadPrivateTagsOn()

metadata = {}
# dicom_data = dcmread(dicom_names[0])
# if modality == 'CT':
# if hasattr(dicom_data, 'KVP'):
# metadata["KVP"] = str(dicom_data.KVP)
# if hasattr(dicom_data, 'XRayTubeCurrent'):
# metadata["XRayTubeCurrent"] = str(dicom_data.XRayTubeCurrent)
# if hasattr(dicom_data, 'ScanOptions'):
# metadata["ScanOptions"] = str(dicom_data.ScanOptions)
# if hasattr(dicom_data, 'ReconstructionAlgorithm'):
# metadata["ReconstructionAlgorithm"] = str(dicom_data.ReconstructionAlgorithm)
# if hasattr(dicom_data, 'ContrastFlowRate'):
# metadata["ContrastFlowRate"] = str(dicom_data.ContrastFlowRate)
# if hasattr(dicom_data, 'ContrastFlowDuration'):
# metadata["ContrastFlowDuration"] = str(dicom_data.ContrastFlowDuration)
# # is this contrast type?
# if hasattr(dicom_data, 'ContrastBolusAgent'):
# metadata["ContrastType"] = str(dicom_data.ContrastBolusAgent)
# else: # MR
# if hasattr(dicom_data, 'AcquisitionTime'):
# metadata["AcquisitionTime"] = str(dicom_data.AcquisitionTime)
# if hasattr(dicom_data, 'AcquisitionContrast'):
# metadata["AcquisitionContrast"] = str(dicom_data.AcquisitionContrast)
# if hasattr(dicom_data, 'AcquisitionType'):
# metadata["AcquisitionType"] = str(dicom_data.AcquisitionType)
# if hasattr(dicom_data, 'RepetitionTime'):
# metadata["RepetitionTime"] = str(dicom_data.RepetitionTime)
# if hasattr(dicom_data, 'EchoTime'):
# metadata["EchoTime"] = str(dicom_data.EchoTime)
# if hasattr(dicom_data, 'ImagingFrequency'):
# metadata["ImagingFrequency"] = str(dicom_data.ImagingFrequency)
# if hasattr(dicom_data, 'MagneticFieldStrength'):
# metadata["MagneticFieldStrength"] = str(dicom_data.MagneticFieldStrength)

# # Number of Slices is avg. number slice?
# if hasattr(dicom_data, 'BodyPartExamined'):
# metadata["BodyPartExamined"] = str(dicom_data.BodyPartExamined)
# if hasattr(dicom_data, 'DataCollectionDiameter'):
# metadata["DataCollectionDiameter"] = str(dicom_data.DataCollectionDiameter)
# if hasattr(dicom_data, 'NumberofSlices'):
# metadata["NumberofSlices"] = str(dicom_data.NumberofSlices)
# # Slice Thickness is avg. slice thickness?
# if hasattr(dicom_data, 'SliceThickness'):
# metadata["SliceThickness"] = str(dicom_data.SliceThickness)
# if hasattr(dicom_data, 'ScanType'):
# metadata["ScanType"] = str(dicom_data.ScanType)
# # Scan Progression Direction is Scan Direction?
# if hasattr(dicom_data, 'ScanProgressionDirection'):
# metadata["ScanProgressionDirection"] = str(dicom_data.ScanProgressionDirection)
# if hasattr(dicom_data, 'PatientPosition'):
# metadata["PatientPosition"] = str(dicom_data.PatientPosition)
# # is this contrast type?
# if hasattr(dicom_data, 'ContrastBolusAgent'):
# metadata["ContrastType"] = str(dicom_data.ContrastBolusAgent)
# if hasattr(dicom_data, 'Manufacturer'):
# metadata["Manufacturer"] = str(dicom_data.Manufacturer)
# # Scan Plane?
# if hasattr(dicom_data, 'ScanOptions'):
# metadata["ScanOptions"] = str(dicom_data.ScanOptions)
# if hasattr(dicom_data, 'RescaleType'):
# metadata["RescaleType"] = str(dicom_data.RescaleType)
# if hasattr(dicom_data, 'RescaleSlope'):
# metadata["RescaleSlope"] = str(dicom_data.RescaleSlope)
# if hasattr(dicom_data, 'PixelSpacing') and hasattr(dicom_data, 'SliceThickness'):
# pixel_size = copy.copy(dicom_data.PixelSpacing)
# pixel_size.append(dicom_data.SliceThickness)
# metadata["PixelSize"] = str(tuple(pixel_size))

return Scan(reader.Execute(), metadata)

Expand Down Expand Up @@ -216,11 +149,18 @@ def get(self, subject_id, default=None):
class ImageCSVLoader(BaseLoader):
def __init__(self,
csv_path_or_dataframe,
colnames=[],
seriesnames=[],
colnames=None,
seriesnames=None,
id_column=None,
expand_paths=False,
readers=[read_image]):
readers=None):

if colnames is None:
colnames = []
if seriesnames is None:
seriesnames = []
if readers is None:
readers = [read_image] # no mutable defaults https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/

self.expand_paths = expand_paths
self.readers = readers
Expand Down Expand Up @@ -270,8 +210,13 @@ def __init__(self,
root_directory,
get_subject_id_from="filename",
subdir_path=None,
exclude_paths=[],
reader=read_image):
exclude_paths=None,
reader=None):

if exclude_paths is None:
exclude_paths = []
if reader is None:
reader = read_image # no mutable defaults https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/

self.root_directory = root_directory
self.get_subject_id_from = get_subject_id_from
Expand Down
38 changes: 0 additions & 38 deletions imgtools/modules/dose.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,6 @@ def from_dicom_rtdose(cls, path):
img_dose = img_dose * factor

metadata = {}
# metadata["DoseType"] = df.DoseType
#return cls(img_dose, df, metadata)
# if hasattr(df, 'BodyPartExamined'):
# metadata["BodyPartExamined"] = str(df.BodyPartExamined)
# if hasattr(df, 'DataCollectionDiameter'):
# metadata["DataCollectionDiameter"] = str(df.DataCollectionDiameter)
# # Number of Slices is avg. number slice?
# if hasattr(df, 'NumberofSlices'):
# metadata["NumberofSlices"] = str(df.NumberofSlices)
# # Slice Thickness is avg. slice thickness?
# if hasattr(df, 'SliceThickness'):
# metadata["SliceThickness"] = str(df.SliceThickness)
# if hasattr(df, 'ScanType'):
# metadata["ScanType"] = str(df.ScanType)
# # Scan Progression Direction is Scan Direction?
# if hasattr(df, 'ScanProgressionDirection'):
# metadata["ScanProgressionDirection"] = str(df.ScanProgressionDirection)
# if hasattr(df, 'PatientPosition'):
# metadata["PatientPosition"] = str(df.PatientPosition)
# # is this contrast type?
# if hasattr(df, 'ContrastBolusAgent'):
# metadata["ContrastType"] = str(df.ContrastBolusAgent)
# if hasattr(df, 'Manufacturer'):
# metadata["Manufacturer"] = str(df.Manufacturer)
# # Which field of view?
# # if hasattr(df, 'FieldOfViewDescription'):
# # metadata["FieldOfViewDescription"] = str(df.FieldOfViewDescription)
# # Scan Plane?
# if hasattr(df, 'ScanOptions'):
# metadata["ScanOptions"] = str(df.ScanOptions)
# if hasattr(df, 'RescaleType'):
# metadata["RescaleType"] = str(df.RescaleType)
# if hasattr(df, 'RescaleSlope'):
# metadata["RescaleSlope"] = str(df.RescaleSlope)
# if hasattr(df, 'PixelSpacing') and hasattr(df, 'SliceThickness'):
# pixel_size = copy.copy(df.PixelSpacing)
# pixel_size.append(df.SliceThickness)
# metadata["PixelSize"] = str(tuple(pixel_size))

return cls(img_dose, df, metadata)

Expand Down
45 changes: 0 additions & 45 deletions imgtools/modules/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,51 +64,6 @@ def from_dicom_pet(cls, path,series_id=None,type="SUV"):
img_pet = sitk.Abs(img_pet * factor)

metadata = {}
# metadata["factor"] = df.factor

# if hasattr(pet, 'RescaleType'):
# metadata["RescaleType"] = str(pet.RescaleType)
# if hasattr(pet, 'RescaleSlope'):
# metadata["RescaleSlope"] = str(pet.RescaleSlope)
# if hasattr(pet, 'RadionuclideTotalDose'):
# metadata["RadionuclideTotalDose"] = str(pet.RadionuclideTotalDose)
# if hasattr(pet, 'RadionuclideHalfLife'):
# metadata["RadionuclideHalfLife"] = str(pet.RadionuclideHalfLife)

# if hasattr(pet, 'BodyPartExamined'):
# metadata["BodyPartExamined"] = str(pet.BodyPartExamined)
# if hasattr(pet, 'DataCollectionDiameter'):
# metadata["DataCollectionDiameter"] = str(pet.DataCollectionDiameter)
# # Number of Slices is avg. number slice?
# if hasattr(pet, 'NumberofSlices'):
# metadata["NumberofSlices"] = str(pet.NumberofSlices)
# # Slice Thickness is avg. slice thickness?
# if hasattr(pet, 'SliceThickness'):
# metadata["SliceThickness"] = str(pet.SliceThickness)
# if hasattr(pet, 'ScanType'):
# metadata["ScanType"] = str(pet.ScanType)
# # Scan Progression Direction is Scan Direction?
# if hasattr(pet, 'ScanProgressionDirection'):
# metadata["ScanProgressionDirection"] = str(pet.ScanProgressionDirection)
# if hasattr(pet, 'PatientPosition'):
# metadata["PatientPosition"] = str(pet.PatientPosition)
# # is this contrast type?
# if hasattr(pet, 'ContrastBolusAgent'):
# metadata["ContrastType"] = str(pet.ContrastBolusAgent)
# if hasattr(pet, 'Manufacturer'):
# metadata["Manufacturer"] = str(pet.Manufacturer)
# # Scan Plane?
# if hasattr(pet, 'ScanOptions'):
# metadata["ScanOptions"] = str(pet.ScanOptions)
# if hasattr(pet, 'RescaleType'):
# metadata["RescaleType"] = str(pet.RescaleType)
# if hasattr(pet, 'RescaleSlope'):
# metadata["RescaleSlope"] = str(pet.RescaleSlope)
# if hasattr(pet, 'PixelSpacing') and hasattr(pet, 'SliceThickness'):
# pixel_size = copy.copy(pet.PixelSpacing)
# pixel_size.append(pet.SliceThickness)
# metadata["PixelSize"] = str(tuple(pixel_size))

return cls(img_pet, df, factor, calc, metadata)
# return cls(img_pet, df, factor, calc)

Expand Down
16 changes: 12 additions & 4 deletions imgtools/ops/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ class ImageCSVInput(BaseInput):
"""
def __init__(self,
csv_path_or_dataframe: str,
colnames: List[str] = [],
colnames: List[str] = None,
id_column: Optional[str] = None,
expand_paths: bool = True,
readers: List[LoaderFunction] = [read_image]):
readers: List[LoaderFunction] = None): # no mutable defaults: https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/
if colnames is None:
colnames = []
if readers is None:
readers = [read_image]
self.csv_path_or_dataframe = csv_path_or_dataframe
self.colnames = colnames
self.id_column = id_column
Expand Down Expand Up @@ -195,8 +199,12 @@ def __init__(self,
root_directory: str,
get_subject_id_from: str = "filename",
subdir_path: Optional[str] = None,
exclude_paths: Optional[List[str]] =[],
reader: LoaderFunction =read_image):
exclude_paths: Optional[List[str]] = None, # no mutable defaults https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/
reader: LoaderFunction = None):
if exclude_paths is None:
exclude_paths = []
if reader is None:
reader = read_image
self.root_directory = root_directory
self.get_subject_id_from = get_subject_id_from
self.subdir_path = subdir_path
Expand Down

0 comments on commit f626aaa

Please sign in to comment.