Commit 862f3a6
## Summary
- `NibabelReader._get_array_data` forced `np.asanyarray(img.dataobj,
order="C")`, triggering a full dense memory reorder on every load on top
of the file read/decompression step. This is the hot path reported in
#8107.
- Drop the forced C-order conversion and keep nibabel's native (F-order)
layout. This aligns the CPU path with the existing GPU/cupy branch just
above, which already returns F-order via `.reshape(data_shape,
order="F")`.
- Downstream MONAI conversion paths
(`convert_to_tensor`/`convert_to_numpy` in
`monai/utils/type_conversion.py`, `monai/data/image_writer.py`, recon
utils) already call `ascontiguousarray` where they actually need
C-contiguous memory, so the reader does not need to pay that cost
eagerly at load time.
Biggest wins are on uncompressed `.nii`, where nibabel's memmap view is
returned lazily rather than being materialized by a forced reorder.
Compressed `.nii.gz` still pays the decompression cost but skips the
subsequent reorder pass, which matches the "twice as long" observation
from @ericspod in the issue thread.
## Compatibility note
The returned array's memory layout changes from C-contiguous to whatever
nibabel provides (typically F-contiguous). Any external caller consuming
`reader.get_data(...)[0]` directly via `.tobytes()` or a raw C-extension
buffer without first calling `ascontiguousarray` would see a different
byte order. All in-repo consumers already guard themselves.
## Test plan
- [x] New regression test in `tests/data/test_init_reader.py` loads a
small NIfTI through `NibabelReader` for both `.nii` and `.nii.gz`,
asserts array equality, and asserts the returned data is not
C-contiguous (i.e. no eager C copy).
- [x] `pytest tests/data/test_init_reader.py` passes locally.
- [x] `runtests.sh`-equivalent checks: `ruff`, `black
--skip-magic-trailing-comma --check`, `isort --check`, `pycln`,
pre-commit hooks — all clean on touched files.
Fixes #8107
---------
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 098a443 commit 862f3a6
2 files changed
Lines changed: 27 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1102 | 1102 | | |
1103 | 1103 | | |
1104 | 1104 | | |
1105 | | - | |
| 1105 | + | |
| 1106 | + | |
1106 | 1107 | | |
1107 | 1108 | | |
1108 | 1109 | | |
| |||
1217 | 1218 | | |
1218 | 1219 | | |
1219 | 1220 | | |
1220 | | - | |
| 1221 | + | |
1221 | 1222 | | |
1222 | 1223 | | |
1223 | 1224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
| |||
76 | 80 | | |
77 | 81 | | |
78 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
79 | 103 | | |
80 | 104 | | |
81 | 105 | | |
0 commit comments