From c90e3f0acb5079cb7b26e3def96574debb3b7113 Mon Sep 17 00:00:00 2001
From: MikeLippincott <1michaell2017@gmail.com>
Date: Sat, 25 Jan 2025 12:20:36 -0700
Subject: [PATCH 1/3] fix label image dimension mis match
---
src/nviz/image.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/nviz/image.py b/src/nviz/image.py
index 8cd8c29..6c2079e 100644
--- a/src/nviz/image.py
+++ b/src/nviz/image.py
@@ -280,12 +280,15 @@ def tiff_to_ometiff( # noqa: PLR0913
# Collect image data
for channel, stack in frame_zstacks["images"].items():
+ raw_image_dim = len(stack.shape)
images_data.append(stack)
channel_names.append(channel)
# Collect label data
if label_dir:
for compartment_name, stack in frame_zstacks["labels"].items():
+ if len(stack.shape) != raw_image_dim and len(stack.shape) == 3:
+ labels_data.append(np.expand_dims(stack, axis=0))
labels_data.append(stack)
label_names.append(f"{compartment_name} (labels)")
From a443cda4124fa879e9f6ca12d0809fa5b60e18ef Mon Sep 17 00:00:00 2001
From: MikeLippincott <1michaell2017@gmail.com>
Date: Wed, 29 Jan 2025 18:30:25 -0700
Subject: [PATCH 2/3] generate test
---
.pre-commit-config.yaml | 2 +-
docs/src/_static/coverage-badge.svg | 2 +-
src/nviz/image.py | 25 +++++++++++++++++++++----
tests/test_image.py | 25 +++++++++++++++++++++++--
tests/utils.py | 13 +++++++++++++
5 files changed, 59 insertions(+), 8 deletions(-)
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 51d3b3b..8883aea 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -23,7 +23,7 @@ repos:
hooks:
- id: validate-cff
- repo: https://github.com/codespell-project/codespell
- rev: v2.4.0
+ rev: v2.4.1
hooks:
- id: codespell
exclude: |
diff --git a/docs/src/_static/coverage-badge.svg b/docs/src/_static/coverage-badge.svg
index ad88956..4564fd6 100644
--- a/docs/src/_static/coverage-badge.svg
+++ b/docs/src/_static/coverage-badge.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/src/nviz/image.py b/src/nviz/image.py
index 6c2079e..df03f89 100644
--- a/src/nviz/image.py
+++ b/src/nviz/image.py
@@ -16,11 +16,31 @@
from .image_meta import extract_z_slice_number_from_filename, generate_ome_xml
+def check_image_dimensionality(
+ arrays: Dict[str, np.ndarray], expected_dim: int
+) -> Tuple[None, np.ndarray]:
+ print(arrays["images"])
+ print(arrays["labels"])
+ if arrays["images"].shape == arrays["labels"].shape == (expected_dim,):
+ return arrays["labels"].shape
+ elif (
+ arrays["images"].shape[0] == expected_dim
+ and arrays["labels"].shape[0] != expected_dim
+ ):
+ if arrays["labels"].shape[0] < expected_dim:
+ arrays["labels"].append(np.expand_dims(arrays["labels"], axis=0))
+ else:
+ raise ValueError("Labels have more dimensions than images.")
+ else:
+ raise ValueError("Images do not have the same dimensions as labels.")
+
+
def image_set_to_arrays(
image_dir: str,
label_dir: Optional[str],
channel_map: Dict[str, str],
ignore: Optional[List[str]] = ["Merge"],
+ expected_dim: int = 4,
) -> Dict[str, Dict[str, np.ndarray]]:
"""
Read a set of images as an array of images.
@@ -101,7 +121,7 @@ def image_set_to_arrays(
key=lambda x: x.name.split("_")[0],
)
}
-
+ # check_image_dimensionality(zstack_arrays, expected_dim)
return zstack_arrays
@@ -280,15 +300,12 @@ def tiff_to_ometiff( # noqa: PLR0913
# Collect image data
for channel, stack in frame_zstacks["images"].items():
- raw_image_dim = len(stack.shape)
images_data.append(stack)
channel_names.append(channel)
# Collect label data
if label_dir:
for compartment_name, stack in frame_zstacks["labels"].items():
- if len(stack.shape) != raw_image_dim and len(stack.shape) == 3:
- labels_data.append(np.expand_dims(stack, axis=0))
labels_data.append(stack)
label_names.append(f"{compartment_name} (labels)")
diff --git a/tests/test_image.py b/tests/test_image.py
index c13e97e..dad34d8 100644
--- a/tests/test_image.py
+++ b/tests/test_image.py
@@ -7,12 +7,33 @@
from pathlib import Path
from typing import Dict, List, Optional, Tuple
+import numpy as np
import pytest
import tifffile as tiff
import zarr
-from nviz.image import image_set_to_arrays, tiff_to_ometiff, tiff_to_zarr
-from tests.utils import example_data_for_image_tests
+from nviz.image import (
+ check_image_dimensionality,
+ image_set_to_arrays,
+ tiff_to_ometiff,
+ tiff_to_zarr,
+)
+from tests.utils import (
+ example_data_for_checking_image_dimensionality,
+ example_data_for_image_tests,
+)
+
+
+@pytest.mark.parametrize(
+ ("arrays, expected_dim"),
+ example_data_for_checking_image_dimensionality,
+)
+def test_check_image_dimensionality(arrays: Dict[str, np.ndarray], expected_dim: int):
+ """
+ Tests the check_image_dimensionality function.
+ """
+ # ensure no errors are raised
+ assert expected_dim == check_image_dimensionality(arrays, expected_dim)
@pytest.mark.parametrize(
diff --git a/tests/utils.py b/tests/utils.py
index d760a7c..7ece3b3 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -2,6 +2,19 @@
Utilities for testing.
"""
+import numpy as np
+
+example_data_for_checking_image_dimensionality = [
+ (
+ {
+ "images": np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]]]]),
+ "labels": np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]]]]),
+ },
+ 4,
+ )
+]
+
+
# data examples for use with pytest parameterized tests
# (creating a data value here because it's simpler to use
# than a fixture inside parameterized tests).
From 7d8933866ec8f486b7ff909def607ec397934bc6 Mon Sep 17 00:00:00 2001
From: MikeLippincott <1michaell2017@gmail.com>
Date: Thu, 30 Jan 2025 08:37:13 -0700
Subject: [PATCH 3/3] testing
---
docs/src/_static/coverage-badge.svg | 2 +-
src/nviz/image.py | 26 +++++---------------------
tests/test_image.py | 15 ---------------
tests/utils.py | 13 -------------
4 files changed, 6 insertions(+), 50 deletions(-)
diff --git a/docs/src/_static/coverage-badge.svg b/docs/src/_static/coverage-badge.svg
index 4564fd6..8a9b8bd 100644
--- a/docs/src/_static/coverage-badge.svg
+++ b/docs/src/_static/coverage-badge.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/src/nviz/image.py b/src/nviz/image.py
index df03f89..5ec6c3a 100644
--- a/src/nviz/image.py
+++ b/src/nviz/image.py
@@ -16,25 +16,6 @@
from .image_meta import extract_z_slice_number_from_filename, generate_ome_xml
-def check_image_dimensionality(
- arrays: Dict[str, np.ndarray], expected_dim: int
-) -> Tuple[None, np.ndarray]:
- print(arrays["images"])
- print(arrays["labels"])
- if arrays["images"].shape == arrays["labels"].shape == (expected_dim,):
- return arrays["labels"].shape
- elif (
- arrays["images"].shape[0] == expected_dim
- and arrays["labels"].shape[0] != expected_dim
- ):
- if arrays["labels"].shape[0] < expected_dim:
- arrays["labels"].append(np.expand_dims(arrays["labels"], axis=0))
- else:
- raise ValueError("Labels have more dimensions than images.")
- else:
- raise ValueError("Images do not have the same dimensions as labels.")
-
-
def image_set_to_arrays(
image_dir: str,
label_dir: Optional[str],
@@ -121,7 +102,6 @@ def image_set_to_arrays(
key=lambda x: x.name.split("_")[0],
)
}
- # check_image_dimensionality(zstack_arrays, expected_dim)
return zstack_arrays
@@ -254,6 +234,7 @@ def tiff_to_ometiff( # noqa: PLR0913
channel_map: Dict[str, str],
scaling_values: Union[List[int], Tuple[int]],
ignore: Optional[List[str]],
+ expected_dim: int = 4,
) -> str:
"""
Convert TIFF files to OME-TIFF format.
@@ -306,7 +287,10 @@ def tiff_to_ometiff( # noqa: PLR0913
# Collect label data
if label_dir:
for compartment_name, stack in frame_zstacks["labels"].items():
- labels_data.append(stack)
+ if stack.ndim != expected_dim and stack.ndim < expected_dim:
+ labels_data.append(np.expand_dims(stack, axis=0))
+ else:
+ labels_data.append(stack)
label_names.append(f"{compartment_name} (labels)")
# Stack the images and labels along a new axis for channels
diff --git a/tests/test_image.py b/tests/test_image.py
index dad34d8..1f9d4a9 100644
--- a/tests/test_image.py
+++ b/tests/test_image.py
@@ -7,35 +7,20 @@
from pathlib import Path
from typing import Dict, List, Optional, Tuple
-import numpy as np
import pytest
import tifffile as tiff
import zarr
from nviz.image import (
- check_image_dimensionality,
image_set_to_arrays,
tiff_to_ometiff,
tiff_to_zarr,
)
from tests.utils import (
- example_data_for_checking_image_dimensionality,
example_data_for_image_tests,
)
-@pytest.mark.parametrize(
- ("arrays, expected_dim"),
- example_data_for_checking_image_dimensionality,
-)
-def test_check_image_dimensionality(arrays: Dict[str, np.ndarray], expected_dim: int):
- """
- Tests the check_image_dimensionality function.
- """
- # ensure no errors are raised
- assert expected_dim == check_image_dimensionality(arrays, expected_dim)
-
-
@pytest.mark.parametrize(
(
"image_dir, label_dir, output_path, channel_map, "
diff --git a/tests/utils.py b/tests/utils.py
index 7ece3b3..d760a7c 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -2,19 +2,6 @@
Utilities for testing.
"""
-import numpy as np
-
-example_data_for_checking_image_dimensionality = [
- (
- {
- "images": np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]]]]),
- "labels": np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]]]]),
- },
- 4,
- )
-]
-
-
# data examples for use with pytest parameterized tests
# (creating a data value here because it's simpler to use
# than a fixture inside parameterized tests).