|
| 1 | +"""Tests for two-stage interdetrital EE calculation. |
| 2 | +
|
| 3 | +Verifies that unconsumed detritus routed between detritus groups via the |
| 4 | +detritus fate matrix correctly increases the receiving group's EE. |
| 5 | +""" |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import pandas as pd |
| 11 | + |
| 12 | +from pypath.core.ecopath import rpath |
| 13 | +from pypath.core.params import create_rpath_params, read_rpath_params |
| 14 | + |
| 15 | +_ECOPATH_DIR = str(Path(__file__).parent / "data" / "rpath_reference" / "ecopath") |
| 16 | + |
| 17 | + |
| 18 | +def _build_4group_params(detfate_cross=0.5): |
| 19 | + """Build a minimal 4-group model: Producer, Consumer, Detritus1, Detritus2, Fleet. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + detfate_cross : float |
| 24 | + Fraction of Detritus1's fate routed to Detritus2. |
| 25 | + """ |
| 26 | + groups = ["Producer", "Consumer", "Detritus1", "Detritus2", "Fleet"] |
| 27 | + types = [1, 0, 2, 2, 3] |
| 28 | + params = create_rpath_params(groups, types) |
| 29 | + |
| 30 | + m = params.model |
| 31 | + # Producer: B=100, PB=50, EE=0.5 |
| 32 | + m.loc[m["Group"] == "Producer", "Biomass"] = 100.0 |
| 33 | + m.loc[m["Group"] == "Producer", "PB"] = 50.0 |
| 34 | + m.loc[m["Group"] == "Producer", "EE"] = 0.5 |
| 35 | + |
| 36 | + # Consumer: B=10, PB=2, QB=10, EE missing (solve for it) |
| 37 | + m.loc[m["Group"] == "Consumer", "Biomass"] = 10.0 |
| 38 | + m.loc[m["Group"] == "Consumer", "PB"] = 2.0 |
| 39 | + m.loc[m["Group"] == "Consumer", "QB"] = 10.0 |
| 40 | + |
| 41 | + # Detritus groups: biomass missing (will be estimated) |
| 42 | + m.loc[m["Group"] == "Detritus1", "Biomass"] = np.nan |
| 43 | + m.loc[m["Group"] == "Detritus2", "Biomass"] = np.nan |
| 44 | + |
| 45 | + # Unassimilated consumption |
| 46 | + m.loc[m["Group"] == "Consumer", "Unassim"] = 0.2 |
| 47 | + |
| 48 | + # DetFate: living groups route to Detritus1 |
| 49 | + m.loc[m["Group"] == "Producer", "Detritus1"] = 1.0 |
| 50 | + m.loc[m["Group"] == "Producer", "Detritus2"] = 0.0 |
| 51 | + m.loc[m["Group"] == "Consumer", "Detritus1"] = 1.0 |
| 52 | + m.loc[m["Group"] == "Consumer", "Detritus2"] = 0.0 |
| 53 | + |
| 54 | + # Interdetrital fate: Detritus1 routes to Detritus2 |
| 55 | + m.loc[m["Group"] == "Detritus1", "Detritus1"] = 1.0 - detfate_cross |
| 56 | + m.loc[m["Group"] == "Detritus1", "Detritus2"] = detfate_cross |
| 57 | + m.loc[m["Group"] == "Detritus2", "Detritus1"] = 0.0 |
| 58 | + m.loc[m["Group"] == "Detritus2", "Detritus2"] = 1.0 |
| 59 | + |
| 60 | + # Fleet routes to Detritus1 |
| 61 | + m.loc[m["Group"] == "Fleet", "Detritus1"] = 1.0 |
| 62 | + m.loc[m["Group"] == "Fleet", "Detritus2"] = 0.0 |
| 63 | + |
| 64 | + # Diet: Consumer eats 40% Producer, 30% Detritus1, 30% Detritus2 |
| 65 | + d = params.diet |
| 66 | + d.loc[d["Group"] == "Producer", "Consumer"] = 0.4 |
| 67 | + d.loc[d["Group"] == "Detritus1", "Consumer"] = 0.3 |
| 68 | + d.loc[d["Group"] == "Detritus2", "Consumer"] = 0.3 |
| 69 | + d.loc[d["Group"] == "Import", "Consumer"] = 0.0 |
| 70 | + |
| 71 | + # Producer has no diet (primary producer) |
| 72 | + d.loc[:, "Producer"] = 0.0 |
| 73 | + |
| 74 | + return params |
| 75 | + |
| 76 | + |
| 77 | +def test_interdetrital_flow_increases_receiving_detritus_ee(): |
| 78 | + """Detritus2 should have positive EE when Detritus1 routes material to it.""" |
| 79 | + params = _build_4group_params(detfate_cross=0.5) |
| 80 | + result = rpath(params) |
| 81 | + |
| 82 | + # Detritus2 index |
| 83 | + det2_idx = list(result.Group).index("Detritus2") |
| 84 | + assert result.EE[det2_idx] > 0.0, ( |
| 85 | + f"Detritus2 EE should be > 0 with interdetrital flow, got {result.EE[det2_idx]}" |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def test_interdetrital_flow_zero_when_no_cross_fate(): |
| 90 | + """With zero cross-fate, Detritus2 EE should be 0 (no inputs, no consumption).""" |
| 91 | + params = _build_4group_params(detfate_cross=0.0) |
| 92 | + result = rpath(params) |
| 93 | + |
| 94 | + det2_idx = list(result.Group).index("Detritus2") |
| 95 | + # Detritus2 has no inputs from living groups and no cross-fate from Detritus1 |
| 96 | + # Consumer eats from Detritus1 only, so Detritus2 has zero consumption too |
| 97 | + assert result.EE[det2_idx] == 0.0, ( |
| 98 | + f"Detritus2 EE should be 0 with no cross-fate, got {result.EE[det2_idx]}" |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def test_interdetrital_ee_bounded(): |
| 103 | + """Detrital EE values should remain in [0, 1] even with interdetrital flows.""" |
| 104 | + params = _build_4group_params(detfate_cross=0.5) |
| 105 | + result = rpath(params) |
| 106 | + |
| 107 | + dead_mask = result.type == 2 |
| 108 | + det_ee = result.EE[dead_mask] |
| 109 | + assert np.all(det_ee >= 0.0), f"Detrital EE has negative values: {det_ee}" |
| 110 | + assert np.all(det_ee <= 1.0), f"Detrital EE has values > 1: {det_ee}" |
| 111 | + |
| 112 | + |
| 113 | +def test_existing_reference_model_unchanged(): |
| 114 | + """Reference model results should not change (it has zero/negligible interdetrital fate).""" |
| 115 | + ecopath_dir = _ECOPATH_DIR |
| 116 | + model_df = pd.read_csv(ecopath_dir + "/model_params.csv") |
| 117 | + diet_df = pd.read_csv(ecopath_dir + "/diet_matrix.csv") |
| 118 | + |
| 119 | + params = create_rpath_params(model_df["Group"].tolist(), model_df["Type"].tolist()) |
| 120 | + params.model = model_df |
| 121 | + params.diet = diet_df |
| 122 | + |
| 123 | + result = rpath(params) |
| 124 | + |
| 125 | + # Known reference EE values for the first few living groups (from previous runs) |
| 126 | + # Just verify EE values are finite and in valid range |
| 127 | + living_mask = result.type < 2 |
| 128 | + living_ee = result.EE[living_mask] |
| 129 | + assert np.all(np.isfinite(living_ee)), "Living EE has non-finite values" |
| 130 | + assert np.all(living_ee >= 0.0), "Living EE has negative values" |
| 131 | + |
| 132 | + dead_mask = result.type == 2 |
| 133 | + dead_ee = result.EE[dead_mask] |
| 134 | + assert np.all(np.isfinite(dead_ee)), "Dead EE has non-finite values" |
| 135 | + assert np.all(dead_ee >= 0.0), "Dead EE has negative values" |
| 136 | + assert np.all(dead_ee <= 1.0), "Dead EE has values > 1" |
0 commit comments