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

Remove nrrd IO #82

Merged
merged 1 commit into from
Jun 6, 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
27 changes: 1 addition & 26 deletions brainglobe_utils/IO/image/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pathlib import Path
from typing import Tuple

import nrrd
import numpy as np
import tifffile
from dask import array as da
Expand Down Expand Up @@ -50,7 +49,7 @@ def load_any(
Parameters
----------
src_path : str or pathlib.Path
Can be the path of a nifty file, nrrd file, tiff file, tiff files
Can be the path of a nifty file, tiff file, tiff files
folder, or text file containing a list of paths.

x_scaling_factor : float, optional
Expand Down Expand Up @@ -136,9 +135,6 @@ def load_any(
z_scaling_factor,
anti_aliasing=anti_aliasing,
)
elif src_path.suffix == ".nrrd":
logging.debug("Data type is: nrrd")
img = load_nrrd(src_path)
elif src_path.suffix in [".nii", ".nii.gz"]:
logging.debug("Data type is: NifTI")
img = load_nii(src_path, as_array=True, as_numpy=as_numpy)
Expand All @@ -150,27 +146,6 @@ def load_any(
return img


def load_nrrd(src_path):
"""
Load an .nrrd file as a numpy array.

Parameters
----------
src_path : str or pathlib.Path
The path of the image to be loaded.

Returns
-------
np.ndarray
The loaded brain array.
"""
if isinstance(src_path, Path):
src_path = str(src_path.resolve())

stack, _ = nrrd.read(src_path)
return stack


def load_img_stack(
stack_path,
x_scaling_factor,
Expand Down
24 changes: 1 addition & 23 deletions brainglobe_utils/IO/image/save.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings
from pathlib import Path

import nrrd
import numpy as np
import tifffile

Expand Down Expand Up @@ -99,24 +98,6 @@ def to_tiffs(img_volume, path_prefix, path_suffix="", extension=".tif"):
tifffile.imwrite(dest_path, img)


def to_nrrd(img_volume, dest_path):
"""
Save the image volume (numpy array) as nrrd.

Parameters
----------
img_volume : np.ndarray
The image to be saved.

dest_path : str or pathlib.Path
The file path where to save the nrrd image.
"""
if isinstance(dest_path, Path):
dest_path = str(dest_path.resolve())

nrrd.write(dest_path, img_volume)


def save_any(img_volume, dest_path):
"""
Save the image volume (numpy array) to the given file path, using the save
Expand All @@ -130,7 +111,7 @@ def save_any(img_volume, dest_path):
dest_path : str or pathlib.Path
The file path to save the image to.
Supports directories (will save a sequence of tiffs), .tif, .tiff,
.nrrd and .nii.
and .nii.
"""
dest_path = Path(dest_path)

Expand All @@ -140,9 +121,6 @@ def save_any(img_volume, dest_path):
elif dest_path.suffix in [".tif", ".tiff"]:
to_tiff(img_volume, dest_path)

elif dest_path.suffix == ".nrrd":
to_nrrd(img_volume, dest_path)

elif dest_path.suffix == ".nii":
to_nii(img_volume, dest_path)

Expand Down
20 changes: 0 additions & 20 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ dependencies = [
"pandas",
"psutil",
"pyarrow",
"pynrrd",
"PyYAML",
"scikit-image",
"scipy",
Expand Down Expand Up @@ -82,20 +81,6 @@ exclude = ["tests", "docs*"]

[tool.pytest.ini_options]
addopts = "--cov=brainglobe_utils"
filterwarnings = [
"error",
# Emitted by scikit-image on import, see https://github.com/scikit-image/scikit-image/issues/6663
# This filter should be removed when scikit-image 0.20 is released
"ignore:`np.bool8` is a deprecated alias for `np.bool_`",
# Emitted by nptyping, see https://github.com/ramonhagenaars/nptyping/issues/102
# for upstream issue
"ignore:`np.object0` is a deprecated alias for ``np.object0`",
"ignore:`np.int0` is a deprecated alias for `np.intp`",
"ignore:`np.uint0` is a deprecated alias for `np.uintp`",
"ignore:`np.void0` is a deprecated alias for `np.void`",
"ignore:`np.bytes0` is a deprecated alias for `np.bytes_`",
"ignore:`np.str0` is a deprecated alias for `np.str_`",
]

[tool.black]
target-version = ['py39', 'py310', 'py311']
Expand All @@ -115,11 +100,6 @@ select = ["I", "E", "F"]
[tool.mypy]
ignore_errors = true

# [[tool.mypy.overrides]]
# module = ["imlib.cells.*"]
# ignore_errors = false
# strict = true

[tool.tox]
legacy_tox_ini = """
[tox]
Expand Down
17 changes: 0 additions & 17 deletions tests/tests/test_IO/test_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,12 @@ def test_nii_read_to_numpy(tmp_path, array_3d):
np.testing.assert_array_equal(reloaded_array, array_3d)


@pytest.mark.parametrize("use_path", [True, False], ids=["Path", "String"])
def test_nrrd_io(tmp_path, array_3d, use_path):
"""
Test that a 3D image can be written and read correctly as nrrd, using both
str and pathlib.Path input.
"""
filename = "test_array.nrrd"
if use_path:
nrrd_path = tmp_path / filename
else:
nrrd_path = str(tmp_path / filename)

save.to_nrrd(array_3d, nrrd_path)
assert (load.load_nrrd(nrrd_path) == array_3d).all()


@pytest.mark.parametrize("use_path", [True, False], ids=["Path", "String"])
@pytest.mark.parametrize(
"file_name",
[
"test_array.tiff",
"test_array.tif",
"test_array.nrrd",
"test_array.nii",
pytest.param("", id="dir of tiffs"),
],
Expand Down
Loading