|
1 | 1 | """Operations for template models."""
|
2 | 2 | import logging
|
3 | 3 | from copy import deepcopy
|
4 |
| -from collections import defaultdict, Counter |
| 4 | +from collections import defaultdict |
5 | 5 | import itertools as itt
|
6 | 6 | from typing import Callable, Collection, Iterable, List, Mapping, Optional, \
|
7 | 7 | Tuple, Type, Union
|
|
10 | 10 |
|
11 | 11 | from .template_model import TemplateModel, Initial, Parameter, Observable
|
12 | 12 | from .templates import *
|
13 |
| -from .comparison import get_dkg_refinement_closure |
14 | 13 | from .units import Unit
|
15 | 14 | from .utils import SympyExprStr
|
16 | 15 |
|
17 | 16 | __all__ = [
|
18 | 17 | "stratify",
|
19 | 18 | "simplify_rate_laws",
|
| 19 | + "check_simplify_rate_laws", |
20 | 20 | "aggregate_parameters",
|
21 | 21 | "get_term_roles",
|
22 | 22 | "counts_to_dimensionless",
|
@@ -571,6 +571,67 @@ def simplify_rate_laws(template_model: TemplateModel):
|
571 | 571 | return template_model
|
572 | 572 |
|
573 | 573 |
|
| 574 | +def check_simplify_rate_laws(template_model: TemplateModel) -> \ |
| 575 | + Mapping[str, Union[str, int, TemplateModel]]: |
| 576 | + """Return a summary of what changes upon rate law simplification |
| 577 | +
|
| 578 | + Parameters |
| 579 | + ---------- |
| 580 | + template_model : |
| 581 | + A template model |
| 582 | +
|
| 583 | + Returns |
| 584 | + ------- |
| 585 | + : |
| 586 | + A dictionary with the result of the check under the `result` key. |
| 587 | + The result can be one of the following: |
| 588 | + - {'result': 'NO_GROUP_CONTROLLERS'}: If there are no templates with |
| 589 | + grouped controllers |
| 590 | + - {'result': 'NO_CHANGE'}: If the model does contain templates with |
| 591 | + grouped controllers but simplification does not change the model. |
| 592 | + - {'result': 'NO_CHANGE_IN_MAX_CONTROLLERS', |
| 593 | + 'max_controller_count': n}: If the model is simplified but the |
| 594 | + maximum number of controllers remains the same so it might not be |
| 595 | + worth doing the simplification. In this case the max controller |
| 596 | + count in the model is returned. The simplified model |
| 597 | + itself is also returned. |
| 598 | + - {'result': 'MEANINGFUL_CHANGE', |
| 599 | + 'max_controller_decrease': n}: If the model is simplified and the |
| 600 | + maximum number of controllers also meaningfully changes. In this |
| 601 | + case the decrease in the maximum controller count is returned. |
| 602 | + The simplified model itself is also returned. |
| 603 | + """ |
| 604 | + if not any(isinstance(template, (GroupedControlledConversion, |
| 605 | + GroupedControlledProduction, |
| 606 | + GroupedControlledDegradation)) |
| 607 | + for template in template_model.templates): |
| 608 | + return {'result': 'NO_GROUP_CONTROLLERS'} |
| 609 | + simplified_model = simplify_rate_laws(template_model) |
| 610 | + old_template_count = len(template_model.templates) |
| 611 | + new_template_count = len(simplified_model.templates) |
| 612 | + if old_template_count == new_template_count: |
| 613 | + return {'result': 'NO_CHANGE'} |
| 614 | + |
| 615 | + def max_controller_count(template_model): |
| 616 | + max_count = 0 |
| 617 | + for template in template_model.templates: |
| 618 | + if hasattr(template, 'controllers'): |
| 619 | + max_count = max(len(template.get_controllers()), max_count) |
| 620 | + elif hasattr(template, 'controller'): |
| 621 | + max_count = max(1, max_count) |
| 622 | + return max_count |
| 623 | + |
| 624 | + old_max_count = max_controller_count(template_model) |
| 625 | + new_max_count = max_controller_count(simplified_model) |
| 626 | + if old_max_count == new_max_count: |
| 627 | + return {'result': 'NO_CHANGE_IN_MAX_CONTROLLERS', |
| 628 | + 'max_controller_count': old_max_count, |
| 629 | + 'simplified_model': simplified_model} |
| 630 | + return {'result': 'MEANINGFUL_CHANGE', |
| 631 | + 'max_controller_decrease': old_max_count - new_max_count, |
| 632 | + 'simplified_model': simplified_model} |
| 633 | + |
| 634 | + |
574 | 635 | def aggregate_parameters(template_model: TemplateModel) -> TemplateModel:
|
575 | 636 | """Return a template model after aggregating parameters for mass-action
|
576 | 637 | rate laws.
|
@@ -630,6 +691,7 @@ def aggregate_parameters(template_model: TemplateModel) -> TemplateModel:
|
630 | 691 | return template_model
|
631 | 692 |
|
632 | 693 |
|
| 694 | + |
633 | 695 | def simplify_rate_law(template: Template,
|
634 | 696 | parameters: Mapping[str, Parameter]) \
|
635 | 697 | -> Union[List[Template], None]:
|
|
0 commit comments