Repository navigation
Commit 026d79f
authored
Keep SaveImage channel options local to each call (#9243)
Fixes #9242.
The contributor explanation below was written by leecoring in Korean and
translated with OpenAI Codex. The technical details and diagnostic
report were prepared with Codex. Code and tests were developed with
Codex assistance after a friend suggested the bug candidate.
### Description
When saving multiple images with the same saver, the channel-axis
setting from the first image can remain in the saver. A setting that
should apply only to that image is then reused for later images, even
when those images have a different channel layout, causing incorrect
saving in SaveImage.
Previously, the saver kept and applied that modified setting. With the
fix, it retains the original configuration and uses a separate copy of
the settings for the current image during the actual save.
### Technical details — assisted by Codex
The verified trigger is a channel-less input followed by a channel-first
input. Metadata-driven inference of no channel currently leaves
`self.data_kwargs["channel_dim"] = None` behind. Copy the data options
for each call, apply inference to that copy, and pass it to the writer.
This also preserves caller settings made through `set_options`,
including a non-default channel dimension.
Add NiBabel file round-trip regression tests for NumPy arrays, torch
tensors, MetaTensor inputs, multiple channels, a metadata-free later
call, a non-default channel axis, and mixed `SaveImaged` keys in both
orders and over repeated calls. NiBabel is already an optional
dependency; the new tests explicitly skip when it is unavailable.
### Validation — Codex-run local diagnostics
- The same 9 new regression cases fail against the original source and
pass with the fix.
- The focused command below reports 52 passed and 18 skipped. The
skipped tests require ITK, which is not installed.
```sh
python -m unittest tests.transforms.test_save_image tests.transforms.test_save_imaged tests.data.test_nifti_rw tests.data.test_itk_writer -v
```
- Whole-checkout `./runtests.sh --black`, `--isort`, `--ruff`,
`--copyright` and `--pyrefly` each exit successfully. Pyrefly reports 0
errors, 781 suppressed diagnostics and 72 warnings under the repository
configuration.
- `git diff --check` and all applicable pre-commit hooks on the three
changed files pass.
- Local environment: macOS 15.5 arm64, Python 3.14.7, PyTorch 2.15.0,
NumPy 2.5.4 and NiBabel 5.4.2.
- Full integration/distributed tests, other OS/Python configurations and
the documentation build were not run locally. Public API signatures and
documentation files are unchanged.
### Types of changes
- [x] Non-breaking change (bug fix).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated and `make html` tested.
---------
Signed-off-by: leecoring <109904107+leecoring@users.noreply.github.com>1 parent 8c31f2e commit 026d79f
3 files changed
Lines changed: 109 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
531 | 531 | | |
532 | 532 | | |
533 | 533 | | |
| 534 | + | |
534 | 535 | | |
535 | 536 | | |
536 | 537 | | |
537 | | - | |
538 | | - | |
| 538 | + | |
| 539 | + | |
539 | 540 | | |
540 | 541 | | |
541 | | - | |
| 542 | + | |
542 | 543 | | |
543 | 544 | | |
544 | 545 | | |
545 | 546 | | |
546 | 547 | | |
547 | 548 | | |
548 | | - | |
| 549 | + | |
549 | 550 | | |
550 | 551 | | |
551 | 552 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
79 | 133 | | |
80 | 134 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
126 | 128 | | |
127 | 129 | | |
128 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
129 | 179 | | |
130 | 180 | | |
0 commit comments