Skip to content

Commit

Permalink
tests: Updates .topostats file in tests to 0.2
Browse files Browse the repository at this point in the history
The `tests/resources/test_image/minicircle_small.topostats` was version `0.1` and failed the updates and tests that now
work with `0.2`. I've therefore updated this test file and tweaked the associated tests to work with these files.

All tests pass locally (watch them fail on CI!).
  • Loading branch information
slackline authored and ns-rse committed Jan 15, 2025
1 parent ee9b197 commit b794252
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ def load_scan_dummy() -> LoadScans:
@pytest.fixture()
def load_scan_topostats_test_file(tmp_path: Path, loading_config: dict) -> LoadScans:
"""Instantiate a LoadScans object for a temporarily saved test .topostats file."""
loading_config["extract"] = "all"
return LoadScans([tmp_path / "topostats_file_test.topostats"], **loading_config)


Expand All @@ -359,9 +360,9 @@ def load_scan(loading_config: dict) -> LoadScans:


@pytest.fixture()
def load_scan_data() -> LoadScans:
def load_scan_data(loading_config: dict) -> LoadScans:
"""Instance of a LoadScans object after applying the get_data func."""
scan_data = LoadScans([RESOURCES / "test_image" / "minicircle_small.topostats"], channel="Height")
scan_data = LoadScans([RESOURCES / "test_image" / "minicircle_small.topostats"], **loading_config)
scan_data.get_data()
return scan_data

Expand Down
Binary file modified tests/resources/test_image/minicircle_small.topostats
Binary file not shown.
27 changes: 23 additions & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
}

# pylint: disable=protected-access
# pylint: disable=too-many-arguments
# pylint: disable=too-many-lines
# pylint: disable=too-many-positional-arguments

Expand Down Expand Up @@ -1118,11 +1119,21 @@ def test_hdf5_to_dict_nested_dict_group_path(tmp_path: Path) -> None:


@pytest.mark.parametrize(
("image", "pixel_to_nm_scaling", "grain_mask_above", "grain_mask_below", "grain_trace_data"),
(
"image",
"pixel_to_nm_scaling",
"filename",
"img_path",
"grain_mask_above",
"grain_mask_below",
"grain_trace_data",
),
[
pytest.param(
np.arange(0, 100).reshape(10, 10),
3.14159265,
"below_grain_mask_with_grain_trace_data",
"./below_grain_mask_with_grain_trace_data.topostats",
None,
np.zeros((10, 10)),
{
Expand Down Expand Up @@ -1224,6 +1235,8 @@ def test_hdf5_to_dict_nested_dict_group_path(tmp_path: Path) -> None:
pytest.param(
np.arange(0, 100).reshape(10, 10),
3.14159265,
"above_grain_mask_without_grain_trace_data",
"./above_grain_mask_without_grain_trace_data.topostats",
np.zeros((10, 10)),
None,
None,
Expand All @@ -1232,6 +1245,8 @@ def test_hdf5_to_dict_nested_dict_group_path(tmp_path: Path) -> None:
pytest.param(
np.arange(0, 100).reshape(10, 10),
3.14159265,
"above_and_below_grain_masks_without_grain_trace_data",
"./above_and_below_grain_masks_without_grain_trace_data.topostats",
np.zeros((10, 10)),
np.zeros((10, 10)),
None,
Expand All @@ -1244,14 +1259,19 @@ def test_save_and_load_topostats_file(
tmp_path: Path,
image: np.ndarray,
pixel_to_nm_scaling: float,
filename: str,
img_path: str,
grain_mask_above: np.ndarray,
grain_mask_below: np.ndarray,
grain_trace_data: dict,
) -> None:
"""Test saving a .topostats file."""
topostats_object = {
"image_flattened": image,
"filename": filename,
"img_path": img_path,
"pixel_to_nm_scaling": pixel_to_nm_scaling,
"image_original": image,
"image_flattened": image,
"grain_masks": {"above": grain_mask_above, "below": grain_mask_below},
"grain_trace_data": grain_trace_data,
}
Expand All @@ -1273,8 +1293,7 @@ def test_save_and_load_topostats_file(
"grain_masks",
"grain_trace_data",
}

np.testing.assert_array_equal(image, loadscans.img_dict["topostats_file_test"]["image_original"])
np.testing.assert_array_equal(image, loadscans.img_dict["image_original"])
assert pixel_to_nm_scaling == loadscans.img_dict["pixel_to_nm_scaling"]
if grain_mask_above is not None:
np.testing.assert_array_equal(grain_mask_above, loadscans.img_dict["grain_masks"]["above"])
Expand Down
2 changes: 2 additions & 0 deletions tests/test_run_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def test_run_topostats_process_all(caplog) -> None:
"./tests/resources/test_image/",
"--file-ext",
".topostats",
"--extract",
"all",
"process",
]
)
Expand Down
5 changes: 0 additions & 5 deletions topostats/run_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,6 @@ def process(args: argparse.Namespace | None = None) -> None: # noqa: C901
# Keys are the image names
# Values are the individual image data dictionaries
scan_data_dict = all_scan_data.img_dict
# Pop elements added for user convenience by AFMReader.topostats.load_topostats(), irrelevant when processing
if config["file_ext"] == ".topostats":
scan_data_dict.pop("image")
scan_data_dict.pop("pixel_to_nm_scaling")
scan_data_dict.pop("topostats_file_version")

with Pool(processes=config["cores"]) as pool:
results = defaultdict()
Expand Down

0 comments on commit b794252

Please sign in to comment.