Skip to content

Commit

Permalink
got rid of file_name_convention stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
fishingguy456 committed Jun 15, 2022
1 parent d65c6d8 commit effac09
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
22 changes: 16 additions & 6 deletions examples/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def __init__(self,
is_nnunet=False,
train_size=1.0,
random_state=42,
label_names=None):
label_names=None,
ignore_missing_regex=False):
"""Initialize the pipeline.
Parameters
Expand Down Expand Up @@ -76,6 +77,10 @@ def __init__(self,
Proportion of the dataset to use for training, as a decimal
random_state: int, default=42
Random state for train_test_split
label_names: dict of str:str, default=None
Dictionary representing the label that regexes are mapped to. For example, "GTV": "GTV.*" will combine all regexes that match "GTV.*" into "GTV"
ignore_missing_regex: bool, default=False
Whether to ignore missing regexes. Will raise an error if none of the regexes in label_names are found for a patient.
"""
super().__init__(
n_jobs=n_jobs,
Expand All @@ -96,6 +101,7 @@ def __init__(self,
self.train_size = train_size
self.random_state = random_state
self.label_names = label_names
self.ignore_missing_regex = ignore_missing_regex

if self.train_size == 1.0:
warnings.warn("Train size is 1, all data will be used for training")
Expand Down Expand Up @@ -246,12 +252,15 @@ def process_one_subject(self, subject_id):

# make_binary_mask relative to ct/pet
if conn_to == "CT" or conn_to == "MR":
mask = self.make_binary_mask(structure_set, image, self.existing_roi_names)
mask = self.make_binary_mask(structure_set, image, self.existing_roi_names, self.ignore_missing_regex)
elif conn_to == "PT":
mask = self.make_binary_mask(structure_set, pet, self.existing_roi_names)
mask = self.make_binary_mask(structure_set, pet, self.existing_roi_names, self.ignore_missing_regex)
else:
raise ValueError("You need to pass a reference CT or PT/PET image to map contours to.")

if mask is None: #ignored the missing regex
return

for name in mask.roi_names.keys():
if name not in self.existing_roi_names.keys():
self.existing_roi_names[name] = len(self.existing_roi_names)
Expand Down Expand Up @@ -312,7 +321,7 @@ def process_one_subject(self, subject_id):

print(subject_id, " SAVED PET")

metadata[f"output_folder_{colname}"] = pathlib.Path(subject_id, file_name_convention()[colname]).as_posix()
metadata[f"output_folder_{colname}"] = pathlib.Path(subject_id, colname).as_posix()
#Saving all the metadata in multiple text files
metadata["Modalities"] = str(list(subject_modalities))
metadata["numRTSTRUCTs"] = num_rtstructs
Expand Down Expand Up @@ -390,9 +399,10 @@ def run(self):
modalities="CT,RTSTRUCT",
visualize=False,
overwrite=True,
is_nnunet=True,
# is_nnunet=True,
train_size=0.5,
label_names={"GTV":"GTV.*", "Brainstem": "Brainstem.*"})
# label_names={"GTV":"GTV.*", "Brainstem": "Brainstem.*"},
ignore_missing_regex=True)

# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/dataset/manifest-1598890146597/NSCLC-Radiomics-Interobserver1",
# output_directory="C:/Users/qukev/BHKLAB/autopipelineoutput",
Expand Down
8 changes: 6 additions & 2 deletions imgtools/modules/structureset.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def to_segmentation(self, reference_image: sitk.Image,
roi_names: Dict[str, str] = None,
force_missing: bool = False,
continuous: bool = True,
existing_roi_names: Dict[str, int] = None) -> Segmentation:
existing_roi_names: Dict[str, int] = None,
ignore_missing_regex: bool = False) -> Segmentation:
"""Convert the structure set to a Segmentation object.
Parameters
Expand Down Expand Up @@ -142,7 +143,10 @@ def to_segmentation(self, reference_image: sitk.Image,
labels = self._assign_labels(roi_names, force_missing)
print("labels:", labels)
if not labels:
raise ValueError(f"No ROIs matching {roi_names} found in {self.roi_names}.")
if ignore_missing_regex:
raise ValueError(f"No ROIs matching {roi_names} found in {self.roi_names}.")
else:
return None

# size = reference_image.GetSize()[::-1] + (max(labels.values()) + 1,)
size = reference_image.GetSize()[::-1] + (len(labels),)
Expand Down
11 changes: 5 additions & 6 deletions imgtools/ops/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,16 @@ def __init__(self,
nnunet_info: Dict = None):

# File types
self.file_name = file_name_convention()
# print(self.file_name)

self.output = {}
print(output_streams)
for colname in output_streams:
# Not considering colnames ending with alphanumeric
colname_process = ("_").join([item for item in colname.split("_") if item.isnumeric()==False])
extension = self.file_name[colname_process]
if not nnunet_info:
self.output[colname_process] = ImageSubjectFileOutput(pathlib.Path(root_directory,"{subject_id}",extension.split(".")[0]).as_posix(),
filename_format=colname_process+"{}.nii.gz".format(extension))
self.output[colname_process] = ImageSubjectFileOutput(pathlib.Path(root_directory,"{subject_id}",colname_process).as_posix(),
filename_format=colname_process+".nii.gz")
else:
self.output[colname_process] = ImageSubjectFileOutput(pathlib.Path(root_directory,"{label_or_image}{train_or_test}").as_posix(),
filename_format="{subject_id}_{modality_index}.nii.gz")
Expand Down Expand Up @@ -1469,7 +1467,7 @@ def __init__(self,
self.force_missing = force_missing
self.continuous = continuous

def __call__(self, structure_set: StructureSet, reference_image: sitk.Image, existing_roi_names: Dict[str, int]) -> Segmentation:
def __call__(self, structure_set: StructureSet, reference_image: sitk.Image, existing_roi_names: Dict[str, int], ignore_missing_regex: bool) -> Segmentation:
"""Convert the structure set to a Segmentation object.
Parameters
Expand All @@ -1488,7 +1486,8 @@ def __call__(self, structure_set: StructureSet, reference_image: sitk.Image, exi
roi_names=self.roi_names,
force_missing=self.force_missing,
continuous=self.continuous,
existing_roi_names=existing_roi_names)
existing_roi_names=existing_roi_names,
ignore_missing_regex=ignore_missing_regex)

class MapOverLabels(BaseOp):
"""MapOverLabels operation class:
Expand Down
3 changes: 3 additions & 0 deletions imgtools/utils/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ def parser():
parser.add_argument("--random_state", type=int, default=42,
help="The random state to be used for the train-test-split.")

parser.add_argument("--ignore_missing_roi_regex", default=False, action="store_true",
help="Whether to ignore patients with no ROI regex's that match the given ones. Will throw an error on patients without matches if this is not set.")

return parser.parse_known_args()[0]

0 comments on commit effac09

Please sign in to comment.