Skip to content

Commit

Permalink
yaml error check
Browse files Browse the repository at this point in the history
Former-commit-id: fdeabd1
  • Loading branch information
fishingguy456 committed Jun 23, 2022
1 parent 1eeb78e commit 9144dd6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
31 changes: 17 additions & 14 deletions examples/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def __init__(self,
train_size=1.0,
random_state=42,
read_yaml_label_names=False,
ignore_missing_regex=False):
ignore_missing_regex=False,
roi_yaml_path=""):
"""Initialize the pipeline.
Parameters
Expand Down Expand Up @@ -105,15 +106,17 @@ def __init__(self,
self.random_state = random_state
self.label_names = {}
self.ignore_missing_regex = ignore_missing_regex

roi_path = pathlib.Path(self.input_directory, "roi_names.yaml").as_posix()

if os.path.exists(roi_path):
with open(roi_path, "r") as f:
try:
self.label_names = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)

roi_path = pathlib.Path(self.input_directory, "roi_names.yaml").as_posix() if roi_yaml_path == "" else roi_yaml_path
if read_yaml_label_names:
if os.path.exists(roi_yaml_path):
with open(roi_path, "r") as f:
try:
self.label_names = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
else:
raise FileNotFoundError(f"No file named roi_names.yaml found at {roi_path}. If you did not intend on creating ROI regexes, run the CLI without --read_yaml_label_names")

if not isinstance(self.label_names, dict):
raise ValueError("roi_names.yaml must parse as a dictionary")
Expand Down Expand Up @@ -415,10 +418,10 @@ def run(self):
print("Dataset already processed...")
shutil.rmtree(pathlib.Path(self.output_directory, ".temp").as_posix())
else:
# Parallel(n_jobs=self.n_jobs, verbose=verbose)(
# delayed(self._process_wrapper)(subject_id) for subject_id in subject_ids)
for subject_id in subject_ids:
self._process_wrapper(subject_id)
Parallel(n_jobs=self.n_jobs, verbose=verbose)(
delayed(self._process_wrapper)(subject_id) for subject_id in subject_ids)
# for subject_id in subject_ids:
# self._process_wrapper(subject_id)
self.save_data()
all_patient_names = glob.glob(pathlib.Path(self.input_directory, "*"," ").as_posix()[0:-1])
all_patient_names = [os.path.split(os.path.split(x)[0])[1] for x in all_patient_names]
Expand Down
5 changes: 5 additions & 0 deletions imgtools/autopipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def __init__(self,
Whether to read dictionary representing the label that regexes are mapped to from YAML. 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.
roi_yaml_path: str, default=""
The path to the
"""
super().__init__(
n_jobs=n_jobs,
Expand All @@ -107,6 +109,9 @@ def __init__(self,
self.label_names = {}
self.ignore_missing_regex = ignore_missing_regex

if roi_yaml_path != "" and not read_yaml_label_names:
warnings.warn("The YAML will not be read since it has not been specified to read them. To use the file, run the CLI with --read_yaml_label_namesg")

roi_path = pathlib.Path(self.input_directory, "roi_names.yaml").as_posix() if roi_yaml_path == "" else roi_yaml_path
if read_yaml_label_names:
if os.path.exists(roi_yaml_path):
Expand Down

0 comments on commit 9144dd6

Please sign in to comment.