-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_thinlayer.py
More file actions
360 lines (293 loc) · 14.4 KB
/
test_thinlayer.py
File metadata and controls
360 lines (293 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from pyenzyme.thinlayers.base import BaseThinLayer
from pyenzyme.versions.v2 import EnzymeMLDocument, Equation, EquationType
# Mock data for creating test species measurements
MOCK_DATA = {
"initial": 1.0,
"time": [1.0, 2.0, 3.0, 4.0],
"data": [1.0, 2.0, 3.0, 4.0],
}
class TestThinLayer:
"""Test suite for BaseThinLayer functionality."""
def test_remove_unmodeled_species_reaction(self):
"""
Test that unmodeled species are removed when they're not part of any reaction.
This test verifies that:
- Species not referenced in reactions are removed from the document
- Measurements containing only unmodeled species are removed
- Measurements with mixed modeled/unmodeled species keep only modeled ones
"""
enzmldoc = self._create_enzmldoc()
# Add reaction with only Substrate and Product (Unmodeled is not included)
reaction = enzmldoc.add_to_reactions(id="R1", name="R1")
reaction.add_to_reactants(species_id="Substrate", stoichiometry=1)
reaction.add_to_products(species_id="Product", stoichiometry=1)
# Remove unmodeled species
thinlayer = MockThinLayer(enzmldoc)
tl_enzmldoc = thinlayer.optimize()
assert len(tl_enzmldoc.small_molecules) == 2, (
f"Unmodeled small molecules should be removed, but {len(tl_enzmldoc.small_molecules)} remain."
)
assert len(tl_enzmldoc.measurements) == 2, (
f"Unmodeled measurements should be removed, but {len(tl_enzmldoc.measurements)} remain."
)
measurement_has_unmodeled: list[str] = []
for measurement in tl_enzmldoc.measurements:
for species_data in measurement.species_data:
if species_data.species_id == "Unmodeled":
measurement_has_unmodeled.append(measurement.id)
assert len(measurement_has_unmodeled) == 0, (
f"Unmodeled species should be removed, but appears in measurements {measurement_has_unmodeled}."
)
def test_remove_unmodeled_species_odes(self):
"""
Test that unmodeled species are removed when they're not part of any ODE.
This test verifies that:
- Species not referenced in ODE equations are removed from the document
- Measurements containing only unmodeled species are removed
- Measurements with mixed modeled/unmodeled species keep only modeled ones
"""
enzmldoc = self._create_enzmldoc()
# Add ODEs with only Substrate and Product (Unmodeled is not included)
enzmldoc.add_to_equations(
species_id="Substrate",
equation_type=EquationType.ODE,
equation="-Substrate",
)
enzmldoc.add_to_equations(
species_id="Product",
equation_type=EquationType.ODE,
equation="Substrate",
)
# Remove unmodeled species
thinlayer = MockThinLayer(enzmldoc)
tl_enzmldoc = thinlayer.optimize()
assert len(tl_enzmldoc.small_molecules) == 2, (
f"Unmodeled small molecules should be removed, but {len(tl_enzmldoc.small_molecules)} remain."
)
assert len(tl_enzmldoc.measurements) == 2, (
f"Unmodeled measurements should be removed, but {len(tl_enzmldoc.measurements)} remain."
)
measurement_has_unmodeled: list[str] = []
for measurement in tl_enzmldoc.measurements:
for species_data in measurement.species_data:
if species_data.species_id == "Unmodeled":
measurement_has_unmodeled.append(measurement.id)
assert len(measurement_has_unmodeled) == 0, (
f"Unmodeled species should be removed, but appears in measurements {measurement_has_unmodeled}."
)
def test_includes_species_in_ode_equation(self):
"""
Test that species referenced in equations are kept in the document even when
they are not defined as ODEs or part of reactions.
This test verifies that:
- A species that appears in an equation (but not in reactions or ODEs) is kept
- The species can have initial values in measurements without time course data
- The filtering function correctly identifies equation-referenced species as modeled
"""
enzmldoc = self._create_enzmldoc()
# Add a new species that will only appear in equations (not reactions/ODEs)
equation_only_species = enzmldoc.add_to_small_molecules(
id="p1",
name="Equation Only Species",
)
# Add an initial for the species to each measurement
for i, measurement in enumerate(enzmldoc.measurements):
measurement.add_to_species_data(
species_id=equation_only_species.id,
initial=i * 2.0,
data=[],
time=[],
)
# Add an assignment equation that references this species
# This species is NOT an ODE and NOT part of any reaction
enzmldoc.add_to_equations(
species_id="Product", # Left side of equation
equation_type=EquationType.ODE,
equation="p1 * 2.0", # p1 appears in the equation
)
# Verify the species exists before filtering
assert equation_only_species.id in [s.id for s in enzmldoc.small_molecules]
# Remove unmodeled species
thinlayer = MockThinLayer(enzmldoc)
tl_enzmldoc = thinlayer.optimize()
assert equation_only_species.id in [
s.id for s in tl_enzmldoc.small_molecules
], (
f"Species '{equation_only_species.id}' should be kept because it appears in an equation, "
f"but it was removed. Remaining species: {[s.id for s in tl_enzmldoc.small_molecules]}"
)
def test_includes_species_in_assignment_equation(self):
"""
Test that species referenced in equations are kept in the document even when
they are not defined as ODEs or part of reactions.
This test verifies that:
- A species that appears in an equation (but not in reactions or ODEs) is kept
- The species can have initial values in measurements without time course data
- The filtering function correctly identifies equation-referenced species as modeled
"""
enzmldoc = self._create_enzmldoc()
# Add a new species that will only appear in equations (not reactions/ODEs)
equation_only_species = enzmldoc.add_to_small_molecules(
id="p1",
name="Equation Only Species",
)
# Add an initial for the species to each measurement
for i, measurement in enumerate(enzmldoc.measurements):
measurement.add_to_species_data(
species_id=equation_only_species.id,
initial=i * 2.0,
data=[],
time=[],
)
# Add an assignment equation that references this species
# This species is NOT an ODE and NOT part of any reaction
enzmldoc.add_to_equations(
species_id="Product", # Left side of equation
equation_type=EquationType.ASSIGNMENT,
equation="p1 * 2.0", # p1 appears in the equation
)
# Verify the species exists before filtering
assert equation_only_species.id in [s.id for s in enzmldoc.small_molecules]
# Remove unmodeled species
thinlayer = MockThinLayer(enzmldoc)
tl_enzmldoc = thinlayer.optimize()
assert equation_only_species.id in [
s.id for s in tl_enzmldoc.small_molecules
], (
f"Species '{equation_only_species.id}' should be kept because it appears in an equation, "
f"but it was removed. Remaining species: {[s.id for s in tl_enzmldoc.small_molecules]}"
)
def test_includes_species_in_initial_assignment_equation(self):
"""
Test that species referenced in equations are kept in the document even when
they are not defined as ODEs or part of reactions.
This test verifies that:
- A species that appears in an equation (but not in reactions or ODEs) is kept
- The species can have initial values in measurements without time course data
- The filtering function correctly identifies equation-referenced species as modeled
"""
enzmldoc = self._create_enzmldoc()
# Add a new species that will only appear in equations (not reactions/ODEs)
equation_only_species = enzmldoc.add_to_small_molecules(
id="p1",
name="Equation Only Species",
)
# Add an initial for the species to each measurement
for i, measurement in enumerate(enzmldoc.measurements):
measurement.add_to_species_data(
species_id=equation_only_species.id,
initial=i * 2.0,
data=[],
time=[],
)
# Add an assignment equation that references this species
# This species is NOT an ODE and NOT part of any reaction
enzmldoc.add_to_equations(
species_id="Product", # Left side of equation
equation_type=EquationType.INITIAL_ASSIGNMENT,
equation="p1 * 2.0", # p1 appears in the equation
)
# Verify the species exists before filtering
assert equation_only_species.id in [s.id for s in enzmldoc.small_molecules]
# Remove unmodeled species
thinlayer = MockThinLayer(enzmldoc)
tl_enzmldoc = thinlayer.optimize()
assert equation_only_species.id in [
s.id for s in tl_enzmldoc.small_molecules
], (
f"Species '{equation_only_species.id}' should be kept because it appears in an equation, "
f"but it was removed. Remaining species: {[s.id for s in tl_enzmldoc.small_molecules]}"
)
def test_includes_species_in_kinetic_law_equation(self):
"""
Test that species referenced in equations are kept in the document even when
they are not defined as ODEs or part of reactions.
This test verifies that:
- A species that appears in an equation (but not in reactions or ODEs) is kept
- The species can have initial values in measurements without time course data
- The filtering function correctly identifies equation-referenced species as modeled
"""
enzmldoc = self._create_enzmldoc()
# Add a new species that will only appear in equations (not reactions/ODEs)
equation_only_species = enzmldoc.add_to_small_molecules(
id="p1",
name="Equation Only Species",
)
# Add an initial for the species to each measurement
for i, measurement in enumerate(enzmldoc.measurements):
measurement.add_to_species_data(
species_id=equation_only_species.id,
initial=i * 2.0,
data=[],
time=[],
)
# Add a reaction that references this species
# This species is NOT an ODE and NOT part of any reaction
reaction = enzmldoc.add_to_reactions(id="R1", name="R1")
reaction.add_to_reactants(species_id="Substrate", stoichiometry=1)
reaction.add_to_products(species_id="Product", stoichiometry=1)
reaction.kinetic_law = Equation(
equation="p1 * 2.0",
equation_type=EquationType.RATE_LAW,
species_id="v",
)
# Verify the species exists before filtering
assert equation_only_species.id in [s.id for s in enzmldoc.small_molecules]
# Remove unmodeled species
thinlayer = MockThinLayer(enzmldoc)
tl_enzmldoc = thinlayer.optimize()
assert equation_only_species.id in [
s.id for s in tl_enzmldoc.small_molecules
], (
f"Species '{equation_only_species.id}' should be kept because it appears in a reaction, "
f"but it was removed. Remaining species: {[s.id for s in tl_enzmldoc.small_molecules]}"
)
def _create_enzmldoc(self) -> EnzymeMLDocument:
"""
Create a test EnzymeML document with various measurement scenarios.
Creates a document with:
- Three species: Substrate, Product, and Unmodeled
- Four measurements:
- M1: Contains all three species (mixed modeled/unmodeled)
- M2: Contains only modeled species (Substrate, Product)
- M3: Contains only unmodeled species (Unmodeled)
- M4: Empty measurement (no species data)
Returns:
EnzymeMLDocument: A test document for use in unit tests.
"""
enzmldoc = EnzymeMLDocument(name="Test")
# Add small molecules
substrate = enzmldoc.add_to_small_molecules(id="Substrate", name="Substrate")
product = enzmldoc.add_to_small_molecules(id="Product", name="Product")
unmodeled = enzmldoc.add_to_small_molecules(id="s1", name="Unmodeled")
# Add a measurement with unmodeled species
measurement = enzmldoc.add_to_measurements(id="M1", name="M1")
measurement.add_to_species_data(species_id=substrate.id, **MOCK_DATA)
measurement.add_to_species_data(species_id=product.id, **MOCK_DATA)
measurement.add_to_species_data(species_id=unmodeled.id, **MOCK_DATA)
# Add a Measurement only with modeled species
measurement = enzmldoc.add_to_measurements(id="M2", name="M2")
measurement.add_to_species_data(species_id=substrate.id, **MOCK_DATA)
measurement.add_to_species_data(species_id=product.id, **MOCK_DATA)
# Add a Measurement with only unmodeled species
measurement = enzmldoc.add_to_measurements(id="M3", name="M3")
measurement.add_to_species_data(species_id=unmodeled.id, **MOCK_DATA)
# Add an empty measurement
measurement = enzmldoc.add_to_measurements(id="M4", name="M4")
return enzmldoc
class MockThinLayer(BaseThinLayer):
"""
Mock implementation of BaseThinLayer for testing purposes.
This class provides minimal implementations of the abstract methods
to allow testing of the base class functionality without requiring
a full thin layer implementation.
"""
def integrate(self, *args, **kwargs):
"""Mock integration method that does nothing."""
pass
def optimize(self, *args, **kwargs):
"""Mock optimization method that does nothing."""
return self._remove_unmodeled_species(self.enzmldoc)
def write(self, *args, **kwargs):
"""Mock write method that does nothing."""
pass