Skip to content

Commit

Permalink
relative paths and output folder paths for dataset.csv
Browse files Browse the repository at this point in the history
  • Loading branch information
fishingguy456 committed Jun 7, 2022
1 parent aa36b2d commit 2ce5ade
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
32 changes: 20 additions & 12 deletions examples/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import glob
import pickle
from imgtools.io.common import file_name_convention
import numpy as np
import sys

Expand Down Expand Up @@ -45,8 +46,8 @@ def __init__(self,
warn_on_error=warn_on_error)
self.overwrite = overwrite
# pipeline configuration
self.input_directory = input_directory
self.output_directory = output_directory
self.input_directory = pathlib.Path(input_directory).as_posix()
self.output_directory = pathlib.Path(output_directory).as_posix()
self.spacing = spacing
self.existing = [None] #self.existing_patients()

Expand Down Expand Up @@ -110,8 +111,6 @@ def process_one_subject(self, subject_id):

print(output_stream)

print(i, colname, "asdflkjqroigj")

if read_results[i] is None:
print("The subject id: {} has no {}".format(subject_id, colname))
pass
Expand Down Expand Up @@ -222,9 +221,12 @@ def process_one_subject(self, subject_id):
metadata.update(pet.metadata)

print(subject_id, " SAVED PET")

metadata[f"output_folder_{colname}"] = pathlib.Path(subject_id, file_name_convention()[colname]).as_posix()
#Saving all the metadata in multiple text files
metadata["Modalities"] = str(list(subject_modalities))
metadata["numRTSTRUCTs"] = num_rtstructs

with open(pathlib.Path(self.output_directory,".temp",f'{subject_id}.pkl').as_posix(),'wb') as f:
pickle.dump(metadata,f)
return
Expand All @@ -237,6 +239,12 @@ def save_data(self):
with open(file,"rb") as f:
metadata = pickle.load(f)
self.output_df.loc[subject_id, list(metadata.keys())] = list(metadata.values())
folder_renames = {}
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}"
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 Expand Up @@ -265,16 +273,16 @@ def run(self):
# visualize=False,
# overwrite=True)

# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_testing/HNSCC",
# output_directory="C:/Users/qukev/BHKLAB/hnscc_testing_output",
# modalities="CT,RTSTRUCT",
# visualize=False,
# overwrite=True)
pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_pet/PET",
output_directory="C:/Users/qukev/BHKLAB/hnscc_pet_output",
modalities="CT,PT",
pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_testing/HNSCC",
output_directory="C:/Users/qukev/BHKLAB/hnscc_testing_output",
modalities="CT,RTSTRUCT",
visualize=False,
overwrite=True)
# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_pet/PET",
# output_directory="C:/Users/qukev/BHKLAB/hnscc_pet_output",
# modalities="CT,PT",
# visualize=False,
# overwrite=True)

print(f'starting Pipeline...')
pipeline.run()
Expand Down
15 changes: 9 additions & 6 deletions imgtools/io/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def read_dicom_series(path: str,
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'):
Expand All @@ -82,8 +84,8 @@ def read_dicom_series(path: str,
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)
if hasattr(dicom_data, 'ContrastBolusAgent'):
metadata["ContrastType"] = str(dicom_data.ContrastBolusAgent)
else: # MR
if hasattr(dicom_data, 'AcquisitionTime'):
metadata["AcquisitionTime"] = str(dicom_data.AcquisitionTime)
Expand All @@ -101,6 +103,10 @@ def read_dicom_series(path: str,
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?
Expand All @@ -118,9 +124,6 @@ def read_dicom_series(path: str,
metadata["ContrastType"] = str(dicom_data.ContrastBolusAgent)
if hasattr(dicom_data, 'Manufacturer'):
metadata["Manufacturer"] = str(dicom_data.Manufacturer)
# Which field of view?
# if hasattr(dicom_data, 'FieldOfViewDescription'):
# metadata["FieldOfViewDescription"] = str(dicom_data.FieldOfViewDescription)
# Scan Plane?
if hasattr(dicom_data, 'ScanOptions'):
metadata["ScanOptions"] = str(dicom_data.ScanOptions)
Expand Down Expand Up @@ -148,7 +151,7 @@ def read_dicom_pet(path,series=None):
def read_dicom_auto(path, series=None):
if path is None:
return None
print(path, "asdlfkjqeroigjodfklsjg")
# print(path, "asdlfkjqeroigjodfklsjg")
dcms = glob.glob(pathlib.Path(path, "*.dcm").as_posix())
meta = dcmread(dcms[0])
modality = meta.Modality
Expand Down
1 change: 1 addition & 0 deletions imgtools/modules/datagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def _get_df(self,
folder_save["patient_ID"] = df_connections["patient_ID_x"].iloc[0]
folder_save[f"series_{df_connections['modality_x'].iloc[0]}"] = comp[j]
folder_save[f"folder_{df_connections['modality_x'].iloc[0]}"] = df_connections["folder_x"].iloc[0]
print(f"folder_{df_connections['modality_x'].iloc[0]}", df_connections["folder_x"].iloc[0], "asdf")
temp_dfconn = df_connections[["series_y", "modality_y", "folder_y"]]
for k in range(len(temp_dfconn)):
#This loop stores connection of the CT
Expand Down
4 changes: 4 additions & 0 deletions imgtools/modules/dose.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ 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)
Expand Down
4 changes: 4 additions & 0 deletions imgtools/modules/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def from_dicom_pet(cls, path,series_id=None,type="SUV"):
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)
Expand Down
4 changes: 4 additions & 0 deletions imgtools/modules/structureset.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def from_dicom_rtstruct(cls, rtstruct_path: str) -> 'StructureSet':
if hasattr(rtstruct, 'StructureSetROISequence'):
metadata["numROIs"] = str(len(rtstruct.StructureSetROISequence))

if hasattr(rtstruct, 'BodyPartExamined'):
metadata["BodyPartExamined"] = str(rtstruct.BodyPartExamined)
if hasattr(rtstruct, 'DataCollectionDiameter'):
metadata["DataCollectionDiameter"] = str(rtstruct.DataCollectionDiameter)
# Number of Slices is avg. number slice?
if hasattr(rtstruct, 'NumberofSlices'):
metadata["NumberofSlices"] = str(rtstruct.NumberofSlices)
Expand Down

0 comments on commit 2ce5ade

Please sign in to comment.