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

Fix multiple stratification of templates with excluded concepts #330

Merged
merged 2 commits into from
Apr 24, 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
51 changes: 24 additions & 27 deletions mira/metamodel/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,35 +156,32 @@ def stratify(

keep_unstratified_parameters = set()
for template in template_model.templates:
# If the template doesn't have any concepts that need to be stratified
# then we can just keep it as is and skip the rest of the loop
if not set(template.get_concept_names()) - exclude_concepts:
original_params = template.get_parameter_names()
for param in original_params:
keep_unstratified_parameters.add(param)
templates.append(deepcopy(template))
continue

# Generate a derived template for each strata
for stratum in strata:
new_template = None
if set(template.get_concept_names()) - exclude_concepts:
new_template = template.with_context(
do_rename=modify_names, exclude_concepts=exclude_concepts,
curie_to_name_map=strata_curie_to_name,
**{key: stratum},
)
rewrite_rate_law(template_model=template_model,
old_template=template,
new_template=new_template,
params_count=params_count,
params_to_stratify=params_to_stratify,
params_to_preserve=params_to_preserve)
# parameters = list(template_model.get_parameters_from_rate_law(template.rate_law))
# if len(parameters) == 1:
# new_template.set_mass_action_rate_law(parameters[0])
templates.append(new_template)

# This means that the template had no concepts to stratify. This implies
# that the template has no controllers that are stratified either
# so we can skip the rest of this loop
if not new_template:
original_params = template.get_parameter_names()
for param in original_params:
keep_unstratified_parameters.add(param)
templates.append(deepcopy(template))
continue
new_template = template.with_context(
do_rename=modify_names, exclude_concepts=exclude_concepts,
curie_to_name_map=strata_curie_to_name,
**{key: stratum},
)
rewrite_rate_law(template_model=template_model,
old_template=template,
new_template=new_template,
params_count=params_count,
params_to_stratify=params_to_stratify,
params_to_preserve=params_to_preserve)
# parameters = list(template_model.get_parameters_from_rate_law(template.rate_law))
# if len(parameters) == 1:
# new_template.set_mass_action_rate_law(parameters[0])
templates.append(new_template)

# assume all controllers have to get stratified together
# and mixing of strata doesn't occur during control
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,16 @@ def condition(template):

assert len(tm.templates) == 2
assert tm.templates[1].rate_law.args[0] == sympy.core.numbers.Zero()


def test_stratify_excluded_species():
from mira.examples.sir import sir_parameterized

tm = stratify(sir_parameterized,
key='vax',
strata=['vax', 'unvax'],
structure=[],
cartesian_control=True,
concepts_to_stratify=['susceptible_population'])

assert len(tm.templates) == 5, templates
Loading