Skip to content

Commit

Permalink
tests(tracing): Split and update tests of disordered traces
Browse files Browse the repository at this point in the history
Splits `test_trace_image_disordered` into two tests, the original compares dictionaries, the newer (suffixed with
`_dataframes`) compares the dataframes/csv files that are produced, of which there are two and one needed updating to
include the `grain_width_mean` column that #999 introduces.
  • Loading branch information
ns-rse committed Nov 18, 2024
1 parent 09e10d1 commit 20cf3c0
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 75 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
image grain_number grain_endpoints grain_junctions total_branch_lengths grain_width_mean
0 test_image 0 0 14 5.7552e-07 4.3883e-09
1 test_image 1 0 12 5.7479e-07 4.4161e-09
image grain_number branch_distance branch_type connected_segments mean_pixel_value stdev_pixel_value min_value median_value middle_value
0 test_image 0 2.9858e+01 2 [1, 2, 6] 2.6683e+00 3.5767e-01 1.9935e+00 2.6465e+00 2.6744e+00
1 test_image 0 2.8645e+01 2 [0, 2, 6] 2.7290e+00 2.1545e-01 2.5507e+00 2.6838e+00 2.6580e+00
2 test_image 0 1.8683e+00 2 [0, 1, 3, 4] 4.0675e+00 1.6906e-01 3.7890e+00 4.1385e+00 4.1396e+00
3 test_image 0 1.4661e+02 2 [2, 4, 9] 2.6694e+00 2.9476e-01 1.2200e+00 2.7080e+00 2.9116e+00
4 test_image 0 2.6170e+01 2 [2, 3, 9] 2.8350e+00 3.4364e-01 2.5354e+00 2.7608e+00 2.7651e+00
5 test_image 0 2.2459e+02 2 [6, 7, 10, 11] 2.7010e+00 2.6534e-01 1.7874e+00 2.7330e+00 2.2574e+00
6 test_image 0 6.9504e+00 2 [0, 1, 5, 7] 3.6264e+00 6.4067e-01 2.5888e+00 3.8420e+00 3.8420e+00
7 test_image 0 3.1203e+01 2 [5, 6, 8, 11] 2.8436e+00 5.5729e-01 2.1307e+00 2.7052e+00 2.7530e+00
8 test_image 0 3.1287e+01 2 [7, 9, 10, 11] 2.7443e+00 3.8223e-01 2.4149e+00 2.6535e+00 2.4707e+00
9 test_image 0 5.5701e+00 2 [3, 4, 8, 10] 2.7309e+00 4.9010e-02 2.6526e+00 2.7379e+00 2.7495e+00
10 test_image 0 3.8188e+01 2 [5, 8, 9, 11] 2.7864e+00 1.5142e-01 2.4451e+00 2.7710e+00 2.6967e+00
11 test_image 0 4.5941e+00 2 [5, 7, 8, 10] 4.1266e+00 3.7134e-01 3.3511e+00 4.2387e+00 4.2387e+00
0 test_image 1 3.7700e+01 2 [1, 2, 3, 6] 2.7733e+00 1.3491e-01 2.4451e+00 2.7690e+00 2.7142e+00
1 test_image 1 2.2389e+02 2 [0, 2, 4, 9] 2.6966e+00 2.6111e-01 1.8203e+00 2.7314e+00 2.2744e+00
2 test_image 1 5.0821e+00 2 [0, 1, 3, 4] 4.0290e+00 4.6381e-01 3.0770e+00 4.1850e+00 4.1850e+00
3 test_image 1 3.1001e+01 2 [0, 2, 4, 6] 2.7462e+00 4.0205e-01 2.4439e+00 2.6516e+00 2.4439e+00
4 test_image 1 3.1405e+01 2 [1, 2, 3, 9] 2.8423e+00 5.6226e-01 2.1307e+00 2.6942e+00 2.7530e+00
5 test_image 1 1.4750e+02 2 [6, 7, 11] 2.6728e+00 2.9408e-01 1.2200e+00 2.7122e+00 2.9467e+00
6 test_image 1 5.7723e+00 2 [0, 3, 5, 7] 2.7226e+00 5.1362e-02 2.6526e+00 2.7128e+00 2.7181e+00
7 test_image 1 2.5682e+01 2 [5, 6, 11] 2.8513e+00 3.6310e-01 2.5354e+00 2.7618e+00 2.7705e+00
8 test_image 1 2.7871e+01 2 [9, 10, 11] 2.7169e+00 1.7984e-01 2.5507e+00 2.6864e+00 2.6580e+00
9 test_image 1 6.9504e+00 2 [1, 4, 8, 10] 3.6794e+00 5.9509e-01 2.6486e+00 3.8420e+00 3.8420e+00
10 test_image 1 2.9370e+01 2 [8, 9, 11] 2.6403e+00 3.1060e-01 1.9935e+00 2.6483e+00 2.6828e+00
11 test_image 1 2.5584e+00 2 [5, 7, 8, 10] 3.9748e+00 2.3924e-01 3.6041e+00 4.0751e+00 4.0751e+00
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
image grain_number grain_endpoints grain_junctions total_branch_lengths grain_width_mean
0 test_image 0 0 13 9.6852e-07 3.0318e-09
image grain_number branch_distance branch_type connected_segments mean_pixel_value stdev_pixel_value min_value median_value middle_value
0 test_image 0 1.7269e+02 2 [1, 2, 3, 8] 2.1474e+00 1.8580e-01 1.4118e+00 2.1479e+00 2.1939e+00
1 test_image 0 3.3804e+02 2 [0, 2, 7, 11] 2.1262e+00 1.8417e-01 1.1279e+00 2.1329e+00 2.2363e+00
2 test_image 0 6.9014e-01 2 [0, 1, 3, 4] 3.5129e+00 2.4417e-01 3.2687e+00 3.5129e+00 3.5129e+00
3 test_image 0 7.5415e+01 2 [0, 2, 4, 8] 2.1781e+00 2.2953e-01 1.4118e+00 2.1784e+00 2.2734e+00
4 test_image 0 3.1893e+01 2 [2, 3, 5, 6] 2.1379e+00 3.3898e-01 1.7556e+00 2.0944e+00 2.2880e+00
5 test_image 0 5.1517e+01 2 [4, 6, 7] 2.2339e+00 2.3611e-01 1.7265e+00 2.2070e+00 2.0221e+00
6 test_image 0 1.1781e+00 2 [4, 5, 7] 3.4336e+00 2.9158e-01 3.0268e+00 3.5782e+00 3.6957e+00
7 test_image 0 1.5383e+02 2 [1, 5, 6, 11] 2.1708e+00 2.3399e-01 1.3202e+00 2.1774e+00 2.1908e+00
8 test_image 0 5.8754e+01 2 [0, 3, 10, 11] 2.0020e+00 3.3701e-01 8.8592e-01 2.0826e+00 2.2475e+00
9 test_image 0 3.8962e+01 3 [10] 2.2736e+00 1.7805e-01 2.0016e+00 2.2595e+00 2.2033e+00
10 test_image 0 4.8800e-01 2 [8, 9, 11] 2.8751e+00 9.8813e-03 2.8652e+00 2.8751e+00 2.8751e+00
11 test_image 0 4.5069e+01 2 [1, 7, 8, 10] 2.1855e+00 2.2638e-01 1.2893e+00 2.2271e+00 2.2271e+00
141 changes: 109 additions & 32 deletions tests/tracing/test_disordered_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import numpy as np
import numpy.typing as npt
import pandas as pd
import pytest

from topostats.io import dict_almost_equal # pylint: disable=no-name-in-module import-error
Expand All @@ -16,6 +15,7 @@
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-lines
# pylint: disable=too-many-positional-arguments
# pylint: disable=unspecified-encoding

BASE_DIR = Path.cwd()
Expand Down Expand Up @@ -1294,9 +1294,7 @@ def test_disordered_trace_grain(
"skeletonisation_params",
"pruning_params",
"expected_disordered_crop_data_filename",
"expected_disordered_tracing_grainstats_filename",
"expected_all_images_filename",
"expected_disordered_tracing_stats_filename",
),
[
pytest.param(
Expand Down Expand Up @@ -1326,9 +1324,7 @@ def test_disordered_trace_grain(
"method_outlier": "mean_abs",
},
"catenanes_disordered_tracing_crop_data.pkl",
"catenanes_disordered_tracing_grainstats.csv",
"catenanes_disordered_tracing_all_images.pkl",
"catenanes_disordered_tracing_stats.csv",
id="catenane",
),
pytest.param(
Expand Down Expand Up @@ -1358,9 +1354,7 @@ def test_disordered_trace_grain(
"method_outlier": "mean_abs",
},
"rep_int_disordered_tracing_crop_data.pkl",
"rep_int_disordered_tracing_grainstats.csv",
"rep_int_disordered_tracing_all_images.pkl",
"rep_int_disordered_tracing_stats.csv",
id="replication intermediate",
),
],
Expand All @@ -1374,9 +1368,7 @@ def test_trace_image_disordered(
skeletonisation_params: dict,
pruning_params: dict,
expected_disordered_crop_data_filename: str,
expected_disordered_tracing_grainstats_filename: str,
expected_all_images_filename: str,
expected_disordered_tracing_stats_filename: str,
) -> None:
"""Test the trace image disordered method."""
# Load the image
Expand All @@ -1385,9 +1377,9 @@ def test_trace_image_disordered(

(
result_disordered_crop_data,
result_disordered_tracing_grainstats,
_,
result_all_images,
result_disordered_tracing_stats,
_,
) = disordered_tracing.trace_image_disordered(
image=image,
grains_mask=mask,
Expand All @@ -1407,42 +1399,127 @@ def test_trace_image_disordered(
# variable_pruned_skeleton = result_all_images["pruned_skeleton"]
# variable_branch_types = result_all_images["branch_types"]

# Update expected values - CHECK RESULTS WITH EXPERT BEFORE UPDATING
# Pickle result_disordered_crop_data
# # Update expected values - CHECK RESULTS WITH EXPERT BEFORE UPDATING
# # Pickle result_disordered_crop_data
# with open(DISORDERED_TRACING_RESOURCES / expected_disordered_crop_data_filename, "wb") as f:
# pkl.dump(result_disordered_crop_data, f)

# # Save result_disordered_tracing_grainstats as a csv
# result_disordered_tracing_grainstats.to_csv(
# DISORDERED_TRACING_RESOURCES / expected_disordered_tracing_grainstats_filename
# )

# # Save result_all_images as a pickle
# with open(DISORDERED_TRACING_RESOURCES / expected_all_images_filename, "wb") as f:
# pkl.dump(result_all_images, f)

# # Save result_disordered_tracing_stats dataframe as a csv
# result_disordered_tracing_stats.to_csv(DISORDERED_TRACING_RESOURCES / expected_disordered_tracing_stats_filename)

# Load expected values
with Path.open(DISORDERED_TRACING_RESOURCES / expected_disordered_crop_data_filename, "rb") as f:
expected_disordered_crop_data = pkl.load(f)

expected_disordered_tracing_grainstats = pd.read_csv(
DISORDERED_TRACING_RESOURCES / expected_disordered_tracing_grainstats_filename, index_col=0
)

with Path.open(DISORDERED_TRACING_RESOURCES / expected_all_images_filename, "rb") as f:
expected_all_images = pkl.load(f)

expected_disordered_tracing_stats = pd.read_csv(
DISORDERED_TRACING_RESOURCES / expected_disordered_tracing_stats_filename, index_col=0
)

assert dict_almost_equal(result_disordered_crop_data, expected_disordered_crop_data, abs_tol=1e-11)
pd.testing.assert_frame_equal(result_disordered_tracing_grainstats, expected_disordered_tracing_grainstats)
assert dict_almost_equal(result_all_images, expected_all_images, abs_tol=1e-11)
pd.testing.assert_frame_equal(result_disordered_tracing_stats, expected_disordered_tracing_stats)


@pytest.mark.parametrize(
(
"image_filename",
"mask_filename",
"pixel_to_nm_scaling",
"min_skeleton_size",
"mask_smoothing_params",
"skeletonisation_params",
"pruning_params",
),
[
pytest.param(
"example_catenanes.npy",
"example_catenanes_labelled_grain_mask_thresholded.npy",
# Pixel to nm scaling
0.488,
# Min skeleton size
10,
# Mask smoothing parameters
{
"gaussian_sigma": 2,
"dilation_iterations": 2,
"holearea_min_max": [10, None],
},
# Skeletonisation parameters
{
"method": "topostats",
"height_bias": 0.6,
},
# Pruning parameters
{
"method": "topostats",
"max_length": 7.0,
"height_threshold": None,
"method_values": "mid",
"method_outlier": "mean_abs",
},
id="catenane",
),
pytest.param(
"example_rep_int.npy",
"example_rep_int_labelled_grain_mask_thresholded.npy",
# Pixel to nm scaling
0.488,
# Min skeleton size
10,
# Mask smoothing parameters
{
"gaussian_sigma": 2,
"dilation_iterations": 2,
"holearea_min_max": [10, None],
},
# Skeletonisation parameters
{
"method": "topostats",
"height_bias": 0.6,
},
# Pruning parameters
{
"method": "topostats",
"max_length": 20.0,
"height_threshold": None,
"method_values": "mid",
"method_outlier": "mean_abs",
},
id="replication intermediate",
),
],
)
def test_trace_image_disordered_dataframes(
image_filename: str,
mask_filename: str,
pixel_to_nm_scaling: float,
min_skeleton_size: int,
mask_smoothing_params: dict,
skeletonisation_params: dict,
pruning_params: dict,
regtest: callable,
) -> None:
"""Test the trace image disordered method produces correct dataframes (/csv files)."""
# Load the image
image = np.load(GENERAL_RESOURCES / image_filename)
mask = np.load(GENERAL_RESOURCES / mask_filename)

(
_,
result_disordered_tracing_grainstats,
_,
result_disordered_tracing_stats,
) = disordered_tracing.trace_image_disordered(
image=image,
grains_mask=mask,
filename="test_image",
pixel_to_nm_scaling=pixel_to_nm_scaling,
min_skeleton_size=min_skeleton_size,
mask_smoothing_params=mask_smoothing_params,
skeletonisation_params=skeletonisation_params,
pruning_params=pruning_params,
pad_width=1,
)
print(result_disordered_tracing_grainstats.to_string(float_format="{:.4e}".format), file=regtest)
print(result_disordered_tracing_stats.to_string(float_format="{:.4e}".format), file=regtest)


@pytest.mark.skip(reason="Awaiting test to be written 2024-10-15.")
Expand Down

0 comments on commit 20cf3c0

Please sign in to comment.