Repository navigation
feat: add dry run functionality to autopipeline and add sensitive dicom tags to metadata #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
54dac55
8c90996
37df848
c8a1c13
ce5f490
47567a8
4f06061
8cf0f35
70cc33d
86493d7
f14f3e1
9507a17
64958ad
2204176
1ecc5ba
91ef21a
f5e79b2
5e69eca
c1957ab
de9c9f8
42b8369
a913545
1bb850a
86bc10c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -266,7 +266,7 @@ def process_one_sample( | |
| SampleNumber=idx, | ||
| ) | ||
| result.output_files = list(saved_files) | ||
| if not result.output_files: | ||
| if not result.output_files and not sample_output.dry_run: | ||
| raise ValueError( | ||
| "No output files were saved. Check the output directory." | ||
| ) | ||
|
|
@@ -311,6 +311,7 @@ def __init__( | |
| window: float | None = None, | ||
| level: float | None = None, | ||
| ignore_existing_samples: bool = False, | ||
| dry_run: bool = False, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- autopipeline changed area ---'
sed -n '260,360p' src/imgtools/autopipeline.py
printf '%s\n' '--- directly bound definitions and call sites ---'
rg -n -C 5 'class Sample(Input|Output)|def build|def __call__|update_crawl|crawl_directory|dry_run|process_one_sample' src/imgtoolsRepository: bhklab/med-imagetools Length of output: 50377 🏁 Script executed: #!/bin/bash
set -e
crawler_file="$(fd -i -t f 'crawler' src | head -n 1)"
printf '%s\n' "--- crawler file: $crawler_file ---"
rg -n -C 8 'class Crawler|def crawl|output_dir|mkdir|write|to_csv|json|pickle|yaml|save' "$crawler_file"
printf '%s\n' '--- output call and error contract ---'
sed -n '1,90p' src/imgtools/io/sample_output.py
sed -n '175,225p' src/imgtools/io/sample_output.py
sed -n '166,285p' src/imgtools/autopipeline.pyRepository: bhklab/med-imagetools Length of output: 14266 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- parse_dicom_dir binding and implementation ---'
rg -n -C 12 'def parse_dicom_dir|parse_dicom_dir\(' src/imgtools
printf '%s\n' '--- CLI dry-run help and pipeline output wiring ---'
sed -n '150,235p' src/imgtools/cli/autopipeline.py
sed -n '350,382p' src/imgtools/autopipeline.pyRepository: bhklab/med-imagetools Length of output: 8130 🏁 Script executed: #!/bin/bash
set -e
sed -n '296,430p' src/imgtools/dicom/crawl/parse_dicoms.pyRepository: bhklab/med-imagetools Length of output: 5531 🏁 Script executed: #!/bin/bash
set -e
sed -n '175,275p' src/imgtools/io/sample_output.pyRepository: bhklab/med-imagetools Length of output: 2591 Make
🤖 Prompt for AI Agents |
||
| ) -> None: | ||
| """ | ||
| Initialize the Autopipeline. | ||
|
|
@@ -352,6 +353,8 @@ def __init__( | |
| If True, skip samples whose writer-resolved output already exists | ||
| before any loading or transformation. Unlike ``existing_file_mode=SKIP``, | ||
| this avoids processing those samples entirely. | ||
| dry_run : bool, optional | ||
| Whether to run the pipeline in dry run mode, by default False | ||
| """ | ||
| self.ignore_existing_samples = ignore_existing_samples | ||
| self.input = SampleInput.build( | ||
|
|
@@ -371,6 +374,7 @@ def __init__( | |
| filename_format=output_filename_format, | ||
| existing_file_mode=existing_file_mode, | ||
| extra_context={}, | ||
| dry_run=dry_run, | ||
| ) | ||
|
|
||
| transforms: list[BaseTransform] = [ | ||
|
|
||
|
katy-scott marked this conversation as resolved.
Resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,8 @@ def save( | |
| ---------- | ||
| data : np.ndarray | sitk.Image | dict[str, np.ndarray | sitk.Image] | ||
| The data to save. Can be a single image or a dictionary of images. | ||
| **kwargs : object | ||
| Additional context for filename generation. | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -95,29 +97,32 @@ def save( | |
| """ | ||
| out_path = self.resolve_path(**kwargs) | ||
|
|
||
| if isinstance(data, (np.ndarray, sitk.Image)): | ||
| # Single image or array | ||
| array, metadata = self._to_numpy(data) | ||
| np.savez_compressed(out_path, image_array=array, **metadata) | ||
| elif isinstance(data, dict): | ||
| # Multiple images or arrays | ||
| arrays = {} | ||
| metadata = {} | ||
| for key, value in data.items(): | ||
| array, meta = self._to_numpy(value) | ||
| arrays[key] = array | ||
| for meta_key, meta_value in meta.items(): | ||
| metadata[f"{key}_{meta_key}"] = meta_value | ||
| if self.compressed: | ||
| np.savez_compressed( | ||
| out_path, allow_pickle=False, **arrays, **metadata | ||
| ) | ||
| if not self.dry_run: | ||
| if isinstance(data, (np.ndarray, sitk.Image)): | ||
| # Single image or array | ||
| array, metadata = self._to_numpy(data) | ||
| np.savez_compressed(out_path, image_array=array, **metadata) | ||
| elif isinstance(data, dict): | ||
| # Multiple images or arrays | ||
| arrays = {} | ||
| metadata = {} | ||
| for key, value in data.items(): | ||
| array, meta = self._to_numpy(value) | ||
| arrays[key] = array | ||
| for meta_key, meta_value in meta.items(): | ||
| metadata[f"{key}_{meta_key}"] = meta_value | ||
| if self.compressed: | ||
| np.savez_compressed( | ||
| out_path, allow_pickle=False, **arrays, **metadata | ||
| ) | ||
| else: | ||
| np.savez( | ||
| out_path, allow_pickle=False, **arrays, **metadata | ||
| ) | ||
| else: | ||
| np.savez(out_path, allow_pickle=False, **arrays, **metadata) | ||
| else: | ||
| raise NumpyWriterValidationError( | ||
| "Data must be a NumPy array, SimpleITK image, or a dictionary of these types." | ||
| ) | ||
| raise NumpyWriterValidationError( | ||
| "Data must be a NumPy array, SimpleITK image, or a dictionary of these types." | ||
| ) | ||
|
Comment on lines
+100
to
+125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve data validation behavior under The dry-run guard cleanly skips actual filesystem writes, but it currently also skips all type validation:
To keep dry-run useful for catching pipeline issues while still avoiding I/O, consider validating - out_path = self.resolve_path(**kwargs)
-
- if not self.dry_run:
- if isinstance(data, (np.ndarray, sitk.Image)):
+ out_path = self.resolve_path(**kwargs)
+
+ # Validate input type regardless of dry-run to surface misuses early.
+ if isinstance(data, (np.ndarray, sitk.Image)):
+ to_save = ("single", data)
+ elif isinstance(data, dict):
+ to_save = ("dict", data)
+ else:
+ raise NumpyWriterValidationError(
+ "Data must be a NumPy array, SimpleITK image, or a dictionary of these types."
+ )
+
+ if not self.dry_run:
+ kind, payload = to_save
+ if kind == "single":
+ data_single = payload
# Single image or array
- array, metadata = self._to_numpy(data)
+ array, metadata = self._to_numpy(data_single)
np.savez_compressed(out_path, image_array=array, **metadata)
- elif isinstance(data, dict):
+ else: # dict
# Multiple images or arrays
- arrays = {}
- metadata = {}
- for key, value in data.items():
+ arrays: dict[str, np.ndarray] = {}
+ metadata: dict[str, object] = {}
+ for key, value in payload.items():
...
- else:
- raise NumpyWriterValidationError(
- "Data must be a NumPy array, SimpleITK image, or a dictionary of these types."
- )This keeps validation semantics identical between normal and dry-run modes while still avoiding writes. Optionally, to satisfy TRY003 and improve reuse, you could move the long error message into a class-level constant on
🧰 Tools🪛 Ruff (0.14.5)123-125: Avoid specifying long messages outside the exception class (TRY003) 🤖 Prompt for AI Agents |
||
|
|
||
| self.add_to_index( | ||
| out_path, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"no output files, check the directory" sounds a bit confusing? what are they checking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you send the full output