Skip to content

Commit

Permalink
New configuration approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Apr 18, 2024
1 parent e7bb36c commit 7bc242d
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 168 deletions.
39 changes: 14 additions & 25 deletions biobalm/_pint_reachability.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,19 @@

from biobalm.petri_net_translation import (
place_to_variable,
_optimized_recursive_dnf_generator,
optimized_recursive_dnf_generator,
)
from biobalm.types import BooleanSpace
import biobalm
from biobalm.types import BooleanSpace, SuccessionDiagramConfiguration

if TYPE_CHECKING:
from biodivine_aeon import Bdd

GOAL_SIZE_LIMIT = 8192
"""
Pint is called using command line and can only accept a limited number of arguments.
This limit is applied when constructing goals to avoid reaching this argument limit.
The limit currently applies to the total number of literals that can be used to
represent a goal.
The default value was empirically tested as safe on Debian linux, but other operating
systems may need a different limit to stay safe. Nevertheless, this should not be
an issue on smaller/simpler networks.
"""


def _pint_reachability(
def pint_reachability(
petri_net: DiGraph,
initial_state: BooleanSpace,
target_states: Bdd,
config: SuccessionDiagramConfiguration,
) -> bool:
"""
Use Pint to check if a given `initial_state` can possibly reach some state
Expand All @@ -59,15 +46,17 @@ def _pint_reachability(
for var, level in initial_state.items():
pint_model.initial_state[var] = level

goal = _pint_build_symbolic_goal(target_states)
goal = _pint_build_symbolic_goal(target_states, config)

def failed(_model, _goal):
def failed(*_):
raise RuntimeError("Cannot verify.")

return pint_model.reachability(goal=goal, fallback=failed) # type: ignore


def _pint_build_symbolic_goal(states: Bdd) -> Goal:
def _pint_build_symbolic_goal(
states: Bdd, config: SuccessionDiagramConfiguration
) -> Goal:
"""
A helper method which (very explicitly) converts a set of states
represented through a BDD into a Pint `Goal`.
Expand All @@ -80,8 +69,8 @@ def _pint_build_symbolic_goal(states: Bdd) -> Goal:
assert not states.is_false()

goals: list[Goal] = []
limit = GOAL_SIZE_LIMIT
for clause in _optimized_recursive_dnf_generator(states):
limit = config["pint_goal_size_limit"]
for clause in optimized_recursive_dnf_generator(states):
named_clause = {
states.__ctx__().get_variable_name(var): int(value)
for var, value in clause.items()
Expand All @@ -92,7 +81,7 @@ def _pint_build_symbolic_goal(states: Bdd) -> Goal:
# If the goal is too large to be passed as a command line argument,
# break here and don't continue. This is not ideal but I'm not sure
# how to fix this other than modifying `pint` itself.
if biobalm.succession_diagram.DEBUG:
if config["debug"]:
print(
"WARNING: `pint` goal size limit exceeded. A partial goal is used."
)
Expand Down Expand Up @@ -127,8 +116,8 @@ def _petri_net_as_automata_network(petri_net: DiGraph) -> str:
if kind != "transition":
continue

predecessors = set(cast(Iterable[str], petri_net.predecessors(transition)))
successors = set(cast(Iterable[str], petri_net.successors(transition)))
predecessors = set(cast(Iterable[str], petri_net.predecessors(transition))) # type: ignore
successors = set(cast(Iterable[str], petri_net.successors(transition))) # type: ignore

# The value under modification is the only
# value that is different between successors and predecessors.
Expand Down
15 changes: 8 additions & 7 deletions biobalm/_sd_algorithms/expand_attractor_seeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
if TYPE_CHECKING:
from biobalm.succession_diagram import SuccessionDiagram

import biobalm
import biobalm.succession_diagram
from biobalm.types import BooleanSpace
from biobalm.space_utils import intersect
from biobalm.trappist_core import compute_fixed_point_reduced_STG
from biobalm._sd_attractors.attractor_candidates import make_heuristic_retained_set
Expand All @@ -25,7 +24,7 @@ def expand_attractor_seeds(sd: SuccessionDiagram, size_limit: int | None = None)
# motif-avoidant attractors.
sd.expand_minimal_spaces(size_limit)

if biobalm.succession_diagram.DEBUG:
if sd.config["debug"]:
print(
"Minimal trap space expansion finished. Proceeding to attractor expansion."
)
Expand Down Expand Up @@ -82,9 +81,11 @@ def expand_attractor_seeds(sd: SuccessionDiagram, size_limit: int | None = None)
intersect(successor_space, child) for child in expanded_motifs
]
avoid = [x for x in avoid_or_none if x is not None]
avoid_restricted = []
avoid_restricted: list[BooleanSpace] = []
for x in avoid:
y = {var: val for (var, val) in x.items() if var not in successor_space}
y: BooleanSpace = {
var: val for (var, val) in x.items() if var not in successor_space
}
avoid_restricted.append(y)

retained_set = make_heuristic_retained_set(
Expand All @@ -105,7 +106,7 @@ def expand_attractor_seeds(sd: SuccessionDiagram, size_limit: int | None = None)
successors.pop()
continue

if biobalm.succession_diagram.DEBUG:
if sd.config["debug"]:
print(
f"[{node}] Found successor with new attractor candidate seeds. Expand node {successors[-1]}."
)
Expand All @@ -114,7 +115,7 @@ def expand_attractor_seeds(sd: SuccessionDiagram, size_limit: int | None = None)

if len(successors) == 0:
# Everything is done for this `node` and we can continue to the next one.
if biobalm.succession_diagram.DEBUG:
if sd.config["debug"]:
print(f"[{node}] Finished node attractor expansion.")
continue

Expand Down
Loading

1 comment on commit 7bc242d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
biobalm
   _pint_reachability.py615018%24, 40–54, 69–93, 101–146
   control.py1141488%107, 119, 125, 129, 134, 143–159, 477, 480, 493
   interaction_graph_utils.py38489%11–13, 151–152
   petri_net_translation.py1491193%22–26, 79, 136, 308–309, 333–334, 343, 452
   space_utils.py1321688%26–28, 104–110, 133–139, 414, 462
   succession_diagram.py3816683%6, 118, 203–208, 216, 263–270, 374–381, 398–399, 409, 415, 531, 618–624, 740, 743, 861–879, 911, 921, 924, 961, 964, 971, 1022, 1036, 1116, 1299, 1310, 1318, 1346, 1361, 1373, 1378, 1384
   symbolic_utils.py32584%10, 39–44, 100, 128
   trappist_core.py1832785%14–18, 55, 57, 92, 215, 217, 219, 247–250, 254–256, 276–282, 340, 342, 372, 420, 422
biobalm/_sd_algorithms
   expand_attractor_seeds.py60788%6, 28, 42, 109–114, 119
   expand_bfs.py28196%6
   expand_dfs.py30197%6
   expand_minimal_spaces.py37295%6, 31
   expand_source_SCCs.py122497%14–16, 88, 133
   expand_to_target.py31390%6, 38, 43
biobalm/_sd_attractors
   attractor_candidates.py2638966%13–15, 26–27, 93, 101, 107–108, 130, 152, 187, 193–204, 223, 239–316, 321, 325, 331, 337, 348, 372, 377, 381, 387, 389–427, 500, 571–572, 673
   attractor_symbolic.py1141686%6–7, 75, 88–92, 103, 112, 144, 179, 191–193, 202, 230, 236
TOTAL186031683% 

Tests Skipped Failures Errors Time
357 0 💤 0 ❌ 0 🔥 45.819s ⏱️

Please sign in to comment.