|
| 1 | +"""Tests for Measurement._pad_species_arrays() and from_enzymeml().""" |
| 2 | +import jax.numpy as jnp |
| 3 | +import pyenzyme as pe |
| 4 | +import pytest |
| 5 | + |
| 6 | +from catalax.dataset.measurement import Measurement |
| 7 | + |
| 8 | + |
| 9 | +def _make_pe_measurement(species: list[dict]) -> pe.Measurement: |
| 10 | + """Build a pe.Measurement with multiple species_data entries. |
| 11 | +
|
| 12 | + Each entry in ``species`` is a dict with keys: |
| 13 | + species_id, initial, data, time (all required). |
| 14 | + """ |
| 15 | + doc = pe.EnzymeMLDocument(name="test") |
| 16 | + meas = doc.add_to_measurements(id="m1", name="m1") |
| 17 | + for sp in species: |
| 18 | + sm = doc.add_to_small_molecules(id=sp["species_id"], name=sp["species_id"]) |
| 19 | + meas.add_to_species_data( |
| 20 | + species_id=sm.id, |
| 21 | + name=sp["species_id"], |
| 22 | + initial=sp["initial"], |
| 23 | + data=sp["data"], |
| 24 | + time=sp["time"], |
| 25 | + ) |
| 26 | + return meas |
| 27 | + |
| 28 | + |
| 29 | +class TestPadSpeciesArrays: |
| 30 | + def test_homogeneous_no_padding_needed(self): |
| 31 | + """When all species already have the same length, arrays are unchanged.""" |
| 32 | + meas = _make_pe_measurement([ |
| 33 | + {"species_id": "s1", "initial": 10.0, "data": [10, 8, 6], "time": [0, 1, 2]}, |
| 34 | + {"species_id": "s2", "initial": 0.0, "data": [0, 2, 4], "time": [0, 1, 2]}, |
| 35 | + ]) |
| 36 | + time, data = Measurement._pad_species_arrays(meas, global_max_len=3) |
| 37 | + assert len(time) == 3 |
| 38 | + assert len(data["s1"]) == len(data["s2"]) == 3 |
| 39 | + assert not jnp.any(jnp.isnan(data["s1"])) |
| 40 | + assert not jnp.any(jnp.isnan(data["s2"])) |
| 41 | + |
| 42 | + def test_subset_species_aligned_with_nan(self): |
| 43 | + """Shorter species (subset time) get NaN at positions they were not measured.""" |
| 44 | + # s1: [0,1,2,3,4] — canonical (longest) |
| 45 | + # s2: [0,2,4] — subset, measured at every other point |
| 46 | + meas = _make_pe_measurement([ |
| 47 | + {"species_id": "s1", "initial": 10.0, "data": [10, 8, 6, 4, 2], "time": [0, 1, 2, 3, 4]}, |
| 48 | + {"species_id": "s2", "initial": 0.0, "data": [0, 4, 8], "time": [0, 2, 4]}, |
| 49 | + ]) |
| 50 | + time, data = Measurement._pad_species_arrays(meas, global_max_len=5) |
| 51 | + assert list(time) == [0, 1, 2, 3, 4] |
| 52 | + assert float(data["s2"][0]) == 0.0 |
| 53 | + assert jnp.isnan(data["s2"][1]) # t=1 not measured |
| 54 | + assert float(data["s2"][2]) == 4.0 |
| 55 | + assert jnp.isnan(data["s2"][3]) # t=3 not measured |
| 56 | + assert float(data["s2"][4]) == 8.0 |
| 57 | + |
| 58 | + def test_cross_measurement_padding_to_global_max(self): |
| 59 | + """When global_max_len > local canonical length, time and data are extended.""" |
| 60 | + meas = _make_pe_measurement([ |
| 61 | + {"species_id": "s1", "initial": 10.0, "data": [10, 8, 6], "time": [0, 1, 2]}, |
| 62 | + ]) |
| 63 | + time, data = Measurement._pad_species_arrays(meas, global_max_len=5) |
| 64 | + assert len(time) == 5 |
| 65 | + assert float(time[3]) == 3.0 # monotonic continuation |
| 66 | + assert float(time[4]) == 4.0 |
| 67 | + assert jnp.isnan(data["s1"][3]) # data padded with NaN |
| 68 | + assert jnp.isnan(data["s1"][4]) |
| 69 | + |
| 70 | + def test_raises_when_species_time_not_subset(self): |
| 71 | + """Raises ValueError when a species has time points outside the canonical axis.""" |
| 72 | + meas = _make_pe_measurement([ |
| 73 | + {"species_id": "s1", "initial": 10.0, "data": [10, 8, 6, 4], "time": [0, 1, 2, 3]}, |
| 74 | + {"species_id": "s2", "initial": 0.0, "data": [0, 2, 4], "time": [0, 1.5, 3]}, |
| 75 | + # t=1.5 is NOT in s1's time array -> should raise |
| 76 | + ]) |
| 77 | + with pytest.raises(ValueError, match="not found in the canonical time"): |
| 78 | + Measurement._pad_species_arrays(meas, global_max_len=4) |
| 79 | + |
| 80 | + |
| 81 | +class TestMeasurementFromEnzymeML: |
| 82 | + def test_homogeneous_roundtrip(self): |
| 83 | + """Homogeneous species (same time arrays) round-trip without NaN.""" |
| 84 | + meas = _make_pe_measurement([ |
| 85 | + {"species_id": "s1", "initial": 10.0, "data": [10, 8, 6], "time": [0, 1, 2]}, |
| 86 | + {"species_id": "s2", "initial": 0.0, "data": [0, 2, 4], "time": [0, 1, 2]}, |
| 87 | + ]) |
| 88 | + m = Measurement.from_enzymeml(meas, global_max_len=3) |
| 89 | + assert list(m.time) == [0, 1, 2] |
| 90 | + assert not jnp.any(jnp.isnan(m.data["s1"])) |
| 91 | + assert not jnp.any(jnp.isnan(m.data["s2"])) |
| 92 | + |
| 93 | + def test_inhomogeneous_no_validation_error(self): |
| 94 | + """The original bug: inhomogeneous lengths must not raise ValidationError.""" |
| 95 | + meas = _make_pe_measurement([ |
| 96 | + {"species_id": "s1", "initial": 10.0, "data": [10, 8, 6, 4, 2], "time": [0, 1, 2, 3, 4]}, |
| 97 | + {"species_id": "s2", "initial": 0.0, "data": [0, 4, 8], "time": [0, 2, 4]}, |
| 98 | + ]) |
| 99 | + m = Measurement.from_enzymeml(meas, global_max_len=5) # must not raise |
| 100 | + assert len(m.time) == 5 |
| 101 | + assert len(m.data["s1"]) == len(m.data["s2"]) == 5 |
| 102 | + assert jnp.isnan(m.data["s2"][1]) |
| 103 | + assert jnp.isnan(m.data["s2"][3]) |
0 commit comments