Skip to content
Merged
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
61 changes: 61 additions & 0 deletions tests/test_pixelmask_collection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Callable
from unittest.mock import MagicMock

import numpy as np
import pytest
Expand Down Expand Up @@ -320,6 +321,47 @@ def test_apply_with_extra_kwargs_on_array_raises(labelled_boolean_mask: Callable
_ = collection.apply(array, sample_frequency="test")


def test_apply_unsupported_type():
pm = PixelMask([[True, False], [False, True]])
collection = PixelMaskCollection([pm])

# Passing a string should trigger the `case _:` TypeError
with pytest.raises(TypeError) as excinfo:
collection.apply("not a valid type")
assert "Unsupported data type" in str(excinfo.value)
Comment on lines +329 to +331

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with pytest.raises(TypeError) as excinfo:
collection.apply("not a valid type")
assert "Unsupported data type" in str(excinfo.value)
with pytest.raises(TypeError, match="Unsupported data type"):
collection.apply("not a valid type")



def test_apply_to_eitdata_branch():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this test add over test_apply_to_eitdata_labelled?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason test_apply_to_eitdata_labelled does not cover lines 308 and 309 in PixelMaskCollection.py:
308 case EITData(): 308 ↛ 309
309 return self._apply_mask_data(data, label_format,

This new test does but perhaps a better solution would be to understand why test_apply_to_eitdata_labelled doesn't....

@psomhorst psomhorst Aug 13, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does for me... Doesn't it skip/fail some tests because of missing data on your side?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh check, it does. That explains (blonde moment)...

# Create a MagicMock that is recognized as an instance of EITData
eit_data_mock = MagicMock(spec=EITData)
eit_data_mock.pixel_impedance = np.array([[1.0, 2.0], [3.0, 4.0]])

# Ensure .apply() on a PixelMask returns an EITData instance (or mock)
def mock_apply(
_self: "PixelMaskCollection",
_data: "EITData | np.ndarray | PixelMap",
*,
_label: str | None = None,
**_kwargs: object,
) -> EITData:
"""Mock apply method for PixelMaskCollection."""
return MagicMock(spec=EITData)

# Patch PixelMask.apply for this test
PixelMask.apply = mock_apply

pm1 = PixelMask([[True, False], [False, True]], label="mask1")
pm2 = PixelMask([[True, True], [False, False]], label="mask2")

collection = PixelMaskCollection([pm1, pm2])

result = collection.apply(eit_data_mock)

assert isinstance(result, dict)
assert set(result.keys()) == {"mask1", "mask2"}
assert all(isinstance(v, EITData) for v in result.values())


def test_empty_collection_behavior():
# Allow emtpy collection initialization
_ = PixelMaskCollection() # No masks provided
Expand Down Expand Up @@ -488,3 +530,22 @@ def test_combine_weighted():
multiplied_mask = collection.combine(method="product", label="combined_product")
assert multiplied_mask.label == "combined_product"
assert np.array_equal(multiplied_mask.mask, np.array([[np.nan, 0.1], [0.06, 0.2]]), equal_nan=True)


def test_combine_method_argument():
pm1 = PixelMask([[True, False], [False, True]])
pm2 = PixelMask([[True, True], [False, False]])
collection = PixelMaskCollection([pm1, pm2])

# Test sum method
summed = collection.combine(method="sum")
assert isinstance(summed, PixelMask)

# Test product method
product = collection.combine(method="product")
assert isinstance(product, PixelMask)
Comment on lines +540 to +546

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be done in another combine test? This function now tests three things that are not really related, while other tests already test the functioning of combine.


# Test unsupported method raises ValueError
with pytest.raises(ValueError) as excinfo:
collection.combine(method="invalid")
assert "Unsupported method" in str(excinfo.value)
Comment on lines +549 to +551

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with pytest.raises(ValueError) as excinfo:
collection.combine(method="invalid")
assert "Unsupported method" in str(excinfo.value)
with pytest.raises(ValueError, match="Unsupported method):
collection.combine(method="invalid")