Skip to content
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

handle nii.gz correctly #93

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion brainglobe_utils/IO/image/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def load_any(
z_scaling_factor,
anti_aliasing=anti_aliasing,
)
elif src_path.suffix in [".nii", ".nii.gz"]:
elif src_path.suffix == ".nii" or str(src_path).endswith(".nii.gz"):
logging.debug("Data type is: NifTI")
img = load_nii(src_path, as_array=True, as_numpy=as_numpy)
else:
Expand Down
2 changes: 1 addition & 1 deletion brainglobe_utils/IO/image/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def save_any(img_volume, dest_path):
elif dest_path.suffix in [".tif", ".tiff"]:
to_tiff(img_volume, dest_path)

elif dest_path.suffix == ".nii":
elif dest_path.suffix == ".nii" or str(dest_path).endswith(".nii.gz"):
to_nii(img_volume, dest_path)

else:
Expand Down
12 changes: 9 additions & 3 deletions tests/tests/test_IO/test_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ def test_sort_img_sequence_from_txt(shuffled_txt_path, array_3d, sort):


@pytest.mark.parametrize("use_path", [True, False], ids=["Path", "String"])
def test_nii_io(tmp_path, array_3d, use_path):
@pytest.mark.parametrize(
"nifti_suffix", [".nii.gz", ".nii"], ids=["compressed", "uncompressed"]
)
def test_nii_io(tmp_path, array_3d, use_path, nifti_suffix):
"""
Test that a 3D image can be written and read correctly as nii with scale
(keeping it as a nifty object with no numpy conversion on loading).
Expand All @@ -265,11 +268,14 @@ def test_nii_io(tmp_path, array_3d, use_path):
assert reloaded.header.get_zooms() == scale


def test_nii_read_to_numpy(tmp_path, array_3d):
@pytest.mark.parametrize(
"nifti_suffix", [".nii.gz", ".nii"], ids=["compressed", "uncompressed"]
)
def test_nii_read_to_numpy(tmp_path, array_3d, nifti_suffix):
"""
Test that conversion of loaded nii image to an in-memory numpy array works
"""
nii_path = tmp_path / "test_array.nii"
nii_path = tmp_path / f"test_array{nifti_suffix}"
save.save_any(array_3d, nii_path)
reloaded_array = load.load_any(nii_path, as_numpy=True)

Expand Down
Loading