Skip to content

Commit

Permalink
put metadata stuff into a util file
Browse files Browse the repository at this point in the history
  • Loading branch information
fishingguy456 committed Jun 8, 2022
1 parent 2ce5ade commit 69f4ee7
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 200 deletions.
2 changes: 1 addition & 1 deletion examples/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def save_data(self):
for col in self.output_df.columns:
if col.startswith("folder"):
self.output_df[col] = self.output_df[col].apply(lambda x: pathlib.Path(x).as_posix().split(self.input_directory)[1][1:]) # rel path, exclude the slash at the beginning
folder_renames[col] = f"output_{col}"
folder_renames[col] = f"input_{col}"
self.output_df.rename(columns=folder_renames, inplace=True)
self.output_df.to_csv(self.output_df_path)
shutil.rmtree(pathlib.Path(self.output_directory, ".temp").as_posix())
Expand Down
165 changes: 91 additions & 74 deletions imgtools/io/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from ..modules import StructureSet
from ..modules import Dose
from ..modules import PET
from ..modules import CTMRScan
from ..modules import Scan
from ..utils.crawl import *
from ..utils.dicomutils import *



Expand All @@ -30,7 +31,7 @@ def read_header(path):
def read_dicom_series(path: str,
series_id: Optional[str] = None,
recursive: bool = False,
modality: str = "CT") -> CTMRScan:
modality: str = "CT") -> Scan:
"""Read DICOM series as SimpleITK Image.
Parameters
Expand Down Expand Up @@ -69,74 +70,74 @@ 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 CTMRScan(reader.Execute(), 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)


def read_dicom_rtstruct(path):
Expand All @@ -155,14 +156,30 @@ def read_dicom_auto(path, series=None):
dcms = glob.glob(pathlib.Path(path, "*.dcm").as_posix())
meta = dcmread(dcms[0])
modality = meta.Modality
all_modality_metadata = all_modalities_metadata(meta)
if modality == 'CT' or modality == 'MR':
return read_dicom_series(path,series, modality=modality)
dicom_series = read_dicom_series(path,series, modality=modality)
if modality == 'CT':
dicom_series.metadata.update(ct_metadata(meta))
dicom_series.metadata.update(all_modality_metadata)
else:
dicom_series.metadata.update(mr_metadata(meta))
dicom_series.metadata.update(all_modality_metadata)
return dicom_series
elif modality == 'PT':
return read_dicom_pet(path,series)
pet = read_dicom_pet(path,series)
pet.metadata.update(pet_metadata(meta))
pet.metadata.update(all_modality_metadata)
return pet
elif modality == 'RTSTRUCT':
return read_dicom_rtstruct(dcms[0])
rtstruct = read_dicom_rtstruct(dcms[0])
rtstruct.metadata.update(rtstruct_metadata(meta))
rtstruct.metadata.update(all_modality_metadata)
return rtstruct
elif modality == 'RTDOSE':
return read_dicom_rtdose(path)
rtdose = read_dicom_rtdose(path)
rtdose.metadata.update(all_modality_metadata)
return rtdose
else:
if len(dcms)==1:
raise NotImplementedError
Expand Down
2 changes: 1 addition & 1 deletion imgtools/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .dose import *
from .datagraph import *
from .sparsemask import *
from .ctmrscan import *
from .scan import *
78 changes: 40 additions & 38 deletions imgtools/modules/dose.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __init__(self, img_dose, df, metadata: Optional[Dict[str, T]] = None):
self.df = df
if metadata:
self.metadata = metadata
else:
self.metadata = {}

@classmethod
def from_dicom_rtdose(cls, path):
Expand All @@ -50,44 +52,44 @@ def from_dicom_rtdose(cls, path):
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)
# 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)

def resample_dose(self,
ct_scan: sitk.Image) -> sitk.Image:
Expand Down
Loading

0 comments on commit 69f4ee7

Please sign in to comment.