Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if IPM contains unused compartments or requirements. #216

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions epymorph/compartment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
TypeVar,
final,
)
from warnings import warn

import numpy as np
from numpy.typing import NDArray
Expand Down Expand Up @@ -477,6 +478,17 @@ def validate_compartment_model(model: BaseCompartmentModel) -> None:
)
raise IpmValidationException(err)

# declared compartments minus used compartments is ideally empty,
# otherwise raise a warning
extra_comps = set(model.symbols.all_compartments).difference(trx_comps)
if len(extra_comps) > 0:
msg = (
f"Possible issue in {name}: "
"not all declared compartments are being used in transitions.\n"
f"Extra compartments: {', '.join(map(str, extra_comps))}"
)
warn(msg)

# transition requirements minus declared requirements should be empty
missing_reqs = trx_reqs.difference(model.symbols.all_requirements)
if len(missing_reqs) > 0:
Expand All @@ -487,6 +499,17 @@ def validate_compartment_model(model: BaseCompartmentModel) -> None:
)
raise IpmValidationException(err)

# declared requirements minus used requirements is ideally empty,
# otherwise raise a warning
extra_reqs = set(model.symbols.all_requirements).difference(trx_reqs)
if len(extra_reqs) > 0:
msg = (
f"Possible issue in {name}: "
"not all declared requirements are being used in transitions.\n"
f"Extra requirements: {', '.join(map(str, extra_reqs))}"
)
warn(msg)


####################################
# Single-strata Compartment Models #
Expand Down
53 changes: 52 additions & 1 deletion epymorph/test/compartment_model_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# pylint: disable=missing-docstring,unused-variable
import unittest
from typing import Sequence

Expand Down Expand Up @@ -214,6 +213,58 @@ def edges(self, symbols):

self.assertIn("invalid compartments", str(e.exception).lower())

def test_create_07(self):
# Test for warning: unused requirements.
with self.assertWarns(Warning) as w:

class MyIpm(CompartmentModel):
compartments = [
compartment("S", tags=["test_tag"]),
compartment("I"),
compartment("R"),
]
requirements = [
AttributeDef("beta", float, Shapes.N),
AttributeDef("gamma", float, Shapes.N),
AttributeDef("extraneous", float, Shapes.N),
]

def edges(self, symbols):
S, I, R = symbols.all_compartments
beta, gamma, extraneous = symbols.all_requirements
return [
edge(S, I, rate=beta * S * I),
edge(I, R, rate=gamma * I),
]

self.assertIn("extra requirements: extraneous", str(w.warning).lower())

def test_create_08(self):
# Test for warning: unused compartments.
with self.assertWarns(Warning) as w:

class MyIpm(CompartmentModel):
compartments = [
compartment("S", tags=["test_tag"]),
compartment("I"),
compartment("R"),
compartment("Q"),
]
requirements = [
AttributeDef("beta", float, Shapes.N),
AttributeDef("gamma", float, Shapes.N),
]

def edges(self, symbols):
S, I, R, Q = symbols.all_compartments
beta, gamma = symbols.all_requirements
return [
edge(S, I, rate=beta * S * I),
edge(I, R, rate=gamma * I),
]

self.assertIn("extra compartments: q", str(w.warning).lower())

def test_compartment_name(self):
# Test for compartment names that include spaces.
with self.assertRaises(ValueError):
Expand Down
Loading