|
| 1 | +"""The water column bounds z for every 3-D metric, not only IL-15.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from tests.test_api import _make_psess, _make_session |
| 9 | +from track2data.calibration.water_column import apply_water_column |
| 10 | +from track2data.core.models import WaterColumn |
| 11 | +from track2data.metrics.geometry3d import positions_cm, vertical_cm |
| 12 | +from track2data.metrics.group import NearestNeighbourDistance3D |
| 13 | +from track2data.metrics.individual import PathLength3D, Speed3D |
| 14 | + |
| 15 | +PX_PER_CM = 10.0 |
| 16 | + |
| 17 | + |
| 18 | +def _native(z, *, column: WaterColumn | None = None, n_animals: int = 1): |
| 19 | + """A native 3-D psess: x = 3 cm/frame, y = 4 cm/frame, z as given, 10 px/cm.""" |
| 20 | + z = np.asarray(z, dtype=float) |
| 21 | + n = z.shape[0] |
| 22 | + z = np.broadcast_to(z.reshape(n, 1), (n, n_animals)).copy() |
| 23 | + psess = _make_psess(_make_session(n_frames=n, n_animals=n_animals)) |
| 24 | + t = np.arange(n, dtype=float)[:, None] |
| 25 | + psess.xy = np.stack( |
| 26 | + [np.broadcast_to(3.0 * t * PX_PER_CM, (n, n_animals)), |
| 27 | + np.broadcast_to(4.0 * t * PX_PER_CM, (n, n_animals))], |
| 28 | + axis=-1, |
| 29 | + ) |
| 30 | + psess.px_per_cm = PX_PER_CM |
| 31 | + psess.z = z |
| 32 | + return apply_water_column(psess, column) if column is not None else psess |
| 33 | + |
| 34 | + |
| 35 | +def test_vertical_cm_is_z_when_there_is_no_water_column() -> None: |
| 36 | + psess = _native([0.0, 1.0, 5.0, 2.0]) |
| 37 | + np.testing.assert_array_equal(vertical_cm(psess)[:, 0], [0.0, 1.0, 5.0, 2.0]) |
| 38 | + |
| 39 | + |
| 40 | +def test_vertical_cm_drops_samples_outside_the_water_column() -> None: |
| 41 | + psess = _native([0.0, 1.0, 5.0, 2.0], column=WaterColumn(surface_z=0.0, floor_z=3.0)) |
| 42 | + np.testing.assert_array_equal(vertical_cm(psess)[:, 0], [0.0, 1.0, np.nan, 2.0]) |
| 43 | + # the stored z is the data and is not modified |
| 44 | + np.testing.assert_array_equal(psess.z[:, 0], [0.0, 1.0, 5.0, 2.0]) |
| 45 | + |
| 46 | + |
| 47 | +def test_vertical_cm_of_a_fused_session_is_depth_times_height() -> None: |
| 48 | + psess = _make_psess(_make_session(n_frames=3, n_animals=1)) |
| 49 | + psess.depth = np.array([[0.1], [0.5], [np.nan]]) |
| 50 | + psess.depth_height_cm = 20.0 |
| 51 | + np.testing.assert_allclose(vertical_cm(psess)[:, 0], [2.0, 10.0, np.nan]) |
| 52 | + |
| 53 | + |
| 54 | +def test_vertical_cm_is_none_without_a_vertical_coordinate() -> None: |
| 55 | + assert vertical_cm(_make_psess(_make_session(n_frames=3, n_animals=1))) is None |
| 56 | + |
| 57 | + |
| 58 | +def test_positions_cm_has_no_position_where_z_is_outside_the_column() -> None: |
| 59 | + psess = _native([0.0, 1.0, 5.0, 2.0], column=WaterColumn(surface_z=0.0, floor_z=3.0)) |
| 60 | + pos = positions_cm(psess) |
| 61 | + assert np.isnan(pos[2]).all() and np.isfinite(pos[[0, 1, 3]]).all() |
| 62 | + |
| 63 | + |
| 64 | +def test_il16_leaves_out_every_step_touching_an_outside_sample() -> None: |
| 65 | + # steps: 0->1 (3,4,1) = 5.099, 1->2 and 2->3 touch the outside sample, so only one is valid |
| 66 | + inside = _native([0.0, 1.0, 5.0, 2.0], column=WaterColumn(surface_z=0.0, floor_z=3.0)) |
| 67 | + row = PathLength3D().compute(inside).iloc[0] |
| 68 | + assert row["n_valid_steps"] == 1 |
| 69 | + assert row["path_length_3d_cm"] == pytest.approx(np.sqrt(9 + 16 + 1)) |
| 70 | + |
| 71 | + |
| 72 | +def test_il16_is_unchanged_when_the_column_contains_every_sample() -> None: |
| 73 | + z = [0.0, 1.0, 5.0, 2.0] |
| 74 | + plain = PathLength3D().compute(_native(z)).iloc[0] |
| 75 | + wide = PathLength3D().compute( |
| 76 | + _native(z, column=WaterColumn(surface_z=0.0, floor_z=10.0)) |
| 77 | + ).iloc[0] |
| 78 | + assert plain["path_length_3d_cm"] == wide["path_length_3d_cm"] |
| 79 | + assert plain["n_valid_steps"] == wide["n_valid_steps"] == 3 |
| 80 | + |
| 81 | + |
| 82 | +def test_il17_has_no_speed_where_z_is_outside_the_column() -> None: |
| 83 | + z = [0.0, 1.0, 5.0, 2.0, 3.0, 2.0, 1.0] |
| 84 | + column = WaterColumn(surface_z=0.0, floor_z=3.0) |
| 85 | + with_column = Speed3D().compute(_native(z, column=column)).iloc[0] |
| 86 | + without = Speed3D().compute(_native(z)).iloc[0] |
| 87 | + assert with_column["max_speed_3d_cm_s"] < without["max_speed_3d_cm_s"] # the spike is gone |
| 88 | + assert np.isfinite(with_column["mean_speed_3d_cm_s"]) |
| 89 | + |
| 90 | + |
| 91 | +def test_gl16_leaves_out_frames_with_an_animal_outside_the_column() -> None: |
| 92 | + column = WaterColumn(surface_z=0.0, floor_z=3.0) |
| 93 | + psess = _native([0.0, 1.0, 5.0, 2.0], column=column, n_animals=2) |
| 94 | + psess.xy[:, 1] += 50.0 # separate the two animals in the horizontal plane |
| 95 | + row = NearestNeighbourDistance3D().compute(psess).iloc[0] |
| 96 | + assert row["n_skipped_frames_3d"] == 1 |
| 97 | + assert row["frac_frames_used_3d"] == pytest.approx(0.75) |
0 commit comments