From 70431e738679110fcf263b490145f4fb91ba291a Mon Sep 17 00:00:00 2001 From: Jordan Rozum Date: Fri, 1 Mar 2024 13:03:12 -0500 Subject: [PATCH] merged sd import methods and updated tests --- balm/control.py | 6 ++-- balm/drivers.py | 4 +-- balm/interaction_graph_utils.py | 2 +- balm/succession_diagram.py | 46 ++++++++++++++++++----------- tests/control_test.py | 8 ++--- tests/drivers_test.py | 3 +- tests/motif_avoidant_test.py | 2 +- tests/petri_net_translation_test.py | 2 +- tests/space_utils_test.py | 6 ++-- tests/succession_diagram_test.py | 6 ++-- tests/trappist_core_test.py | 8 ++--- 11 files changed, 50 insertions(+), 43 deletions(-) diff --git a/balm/control.py b/balm/control.py index 84dbddc..6a71358 100644 --- a/balm/control.py +++ b/balm/control.py @@ -58,7 +58,7 @@ def __init__( Example ------- >>> import balm - >>> sd = balm.SuccessionDiagram.from_bnet( + >>> sd = balm.SuccessionDiagram.from_rules( ... \"\"\" ... A, B & C ... B, A & C @@ -198,7 +198,7 @@ def succession_control( ------- >>> import balm >>> from balm.control import succession_control - >>> sd = balm.SuccessionDiagram.from_bnet( + >>> sd = balm.SuccessionDiagram.from_rules( ... \"\"\" ... S, S ... A, S | B @@ -237,7 +237,7 @@ def succession_control( ------- >>> import balm >>> from balm.control import succession_control - >>> sd = balm.SuccessionDiagram.from_bnet( + >>> sd = balm.SuccessionDiagram.from_rules( ... \"\"\" ... S, S ... A, S | B diff --git a/balm/drivers.py b/balm/drivers.py index 445626a..3cfeebc 100644 --- a/balm/drivers.py +++ b/balm/drivers.py @@ -40,7 +40,7 @@ def find_single_node_LDOIs( Example _______ >>> import balm - >>> sd = balm.SuccessionDiagram.from_bnet('A, A\\nB, A') + >>> sd = balm.SuccessionDiagram.from_rules('A, A\\nB, A') >>> ldois = balm.drivers.find_single_node_LDOIs(sd.network) >>> for k in sorted(ldois): ... print(f'{k} ==> {sorted(ldois[k].items())}') @@ -107,7 +107,7 @@ def find_single_drivers( Example ------- >>> import balm - >>> sd = balm.SuccessionDiagram.from_bnet('A, A\\nB, A') + >>> sd = balm.SuccessionDiagram.from_rules('A, A\\nB, A') >>> drivers = balm.drivers.find_single_drivers({'B': 0}, sd.network) >>> sorted(list(drivers)) [('A', 0), ('B', 0)] diff --git a/balm/interaction_graph_utils.py b/balm/interaction_graph_utils.py index da6dcc4..7b58cfd 100644 --- a/balm/interaction_graph_utils.py +++ b/balm/interaction_graph_utils.py @@ -96,7 +96,7 @@ def feedback_vertex_set( -------- >>> import balm >>> from balm.interaction_graph_utils import feedback_vertex_set - >>> sd = balm.SuccessionDiagram.from_bnet(\"\"\" + >>> sd = balm.SuccessionDiagram.from_rules(\"\"\" ... A, B ... B, A ... C, D diff --git a/balm/succession_diagram.py b/balm/succession_diagram.py index b4ce6ea..c2fa3fb 100644 --- a/balm/succession_diagram.py +++ b/balm/succession_diagram.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, cast +from typing import TYPE_CHECKING, Any, Literal, cast if TYPE_CHECKING: from typing import Iterator @@ -44,7 +44,7 @@ class SuccessionDiagram: Examples -------- >>> import balm - >>> sd = balm.SuccessionDiagram.from_bnet(\""" + >>> sd = balm.SuccessionDiagram.from_rules(\""" ... A, B ... B, A & C ... C, !A | B @@ -158,25 +158,35 @@ def __len__(self) -> int: return self.dag.number_of_nodes() @staticmethod - def from_aeon(model: str) -> SuccessionDiagram: + def from_rules( + rules: str, + format: Literal["bnet", "aeon", "sbml"] = "bnet", + ) -> SuccessionDiagram: """ - Read a `BooleanNetwork` from the string contents of an `.aeon` model. - """ - return SuccessionDiagram(BooleanNetwork.from_aeon(model)) + Generate a succession diagram from the given string. - @staticmethod - def from_bnet(model: str) -> SuccessionDiagram: - """ - Read a `BooleanNetwork` from the string contents of a `.bnet` model. - """ - return SuccessionDiagram(BooleanNetwork.from_bnet(model)) + Parameters + ---------- + rules : str + The string representation of the network rules. + format : Literal['bnet', 'aeon', 'sbml'] + The format of the string. One of `"bnet"`, `"aeon"`, or `"sbml"`. + Defaults to `"bnet"`. - @staticmethod - def from_sbml(model: str) -> SuccessionDiagram: - """ - Read a `BooleanNetwork` from the string contents of an `.sbml` model. + Returns + ------- + SuccessionDiagram + The generated succession diagram. Initially unexpanded. """ - return SuccessionDiagram(BooleanNetwork.from_sbml(model)) + + if format == "bnet": + return SuccessionDiagram(BooleanNetwork.from_bnet(rules)) + elif format == "aeon": + return SuccessionDiagram(BooleanNetwork.from_aeon(rules)) + elif format == "sbml": + return SuccessionDiagram(BooleanNetwork.from_sbml(rules)) + else: + raise ValueError(f"Unknown format: {format}") @staticmethod def from_file(path: str) -> SuccessionDiagram: @@ -207,7 +217,7 @@ def expanded_attractor_seeds(self) -> dict[int, list[BooleanSpace]]: Example ------- >>> import balm - >>> sd = balm.SuccessionDiagram.from_bnet(\""" + >>> sd = balm.SuccessionDiagram.from_rules(\""" ... A, B ... B, A & C ... C, !A | B diff --git a/tests/control_test.py b/tests/control_test.py index de9f4f1..37c01c3 100644 --- a/tests/control_test.py +++ b/tests/control_test.py @@ -101,7 +101,7 @@ def test_basic_succession_finding(): def test_internal_succession_control(): - sd = SuccessionDiagram.from_bnet( + sd = SuccessionDiagram.from_rules( """ S, S A, S | B @@ -143,7 +143,7 @@ def test_internal_succession_control(): def test_all_succession_control(): - sd = SuccessionDiagram.from_bnet( + sd = SuccessionDiagram.from_rules( """ S, S A, S | B @@ -185,7 +185,7 @@ def test_all_succession_control(): def test_forbidden_drivers(): - sd = SuccessionDiagram.from_bnet( + sd = SuccessionDiagram.from_rules( """ A, B & C B, A & C @@ -255,7 +255,7 @@ def test_forbidden_drivers(): def test_size_restriction(): - sd = SuccessionDiagram.from_bnet( + sd = SuccessionDiagram.from_rules( """ A, B & C B, A & C diff --git a/tests/drivers_test.py b/tests/drivers_test.py index 1e9efe6..bd5997d 100644 --- a/tests/drivers_test.py +++ b/tests/drivers_test.py @@ -1,6 +1,7 @@ -from balm.drivers import find_single_drivers, find_single_node_LDOIs from biodivine_aeon import BooleanNetwork +from balm.drivers import find_single_drivers, find_single_node_LDOIs + def test_find_single_node_LDOIs(): bn = BooleanNetwork.from_bnet( diff --git a/tests/motif_avoidant_test.py b/tests/motif_avoidant_test.py index f4c9b48..2f24c40 100644 --- a/tests/motif_avoidant_test.py +++ b/tests/motif_avoidant_test.py @@ -1,4 +1,4 @@ -from biodivine_aeon import BooleanNetwork, AsynchronousGraph +from biodivine_aeon import AsynchronousGraph, BooleanNetwork from balm.motif_avoidant import _filter_candidates # type: ignore from balm.motif_avoidant import _Pint_reachability # type: ignore diff --git a/tests/petri_net_translation_test.py b/tests/petri_net_translation_test.py index f5e3f3b..4bfa5eb 100644 --- a/tests/petri_net_translation_test.py +++ b/tests/petri_net_translation_test.py @@ -1,6 +1,6 @@ # type: ignore import pytest -from biodivine_aeon import BooleanNetwork, RegulatoryGraph +from biodivine_aeon import BooleanNetwork from networkx import DiGraph, is_isomorphic # type: ignore from balm.petri_net_translation import ( diff --git a/tests/space_utils_test.py b/tests/space_utils_test.py index 739c91a..73dbf22 100644 --- a/tests/space_utils_test.py +++ b/tests/space_utils_test.py @@ -1,13 +1,13 @@ -from biodivine_aeon import BooleanNetwork, BooleanExpression, AsynchronousGraph +from biodivine_aeon import AsynchronousGraph, BooleanExpression, BooleanNetwork from balm.space_utils import ( expression_to_space_list, is_subspace, - percolate_network, - percolation_conflicts, percolate_expression, + percolate_network, percolate_space, percolate_space_strict, + percolation_conflicts, space_unique_key, ) diff --git a/tests/succession_diagram_test.py b/tests/succession_diagram_test.py index 0e8caa8..9418cb0 100644 --- a/tests/succession_diagram_test.py +++ b/tests/succession_diagram_test.py @@ -109,8 +109,8 @@ def test_succession_diagram_structure(self): def test_state(): - sd1 = SuccessionDiagram.from_bnet("A, B\nB, A") - sd2 = SuccessionDiagram.from_bnet("A, B\nB, A\nC, C & B") + sd1 = SuccessionDiagram.from_rules("A, B\nB, A") + sd2 = SuccessionDiagram.from_rules("A, B\nB, A\nC, C & B") sd1.build() sd2.__setstate__(sd1.__getstate__()) slots1 = [x + str(sd1.__getattribute__(x)) for x in sd1.__slots__] @@ -311,7 +311,7 @@ def test_attractor_expansion(network_file: str): def test_attractor_extraction(): - sd = balm.SuccessionDiagram.from_bnet( + sd = balm.SuccessionDiagram.from_rules( """ A, B B, A & C diff --git a/tests/trappist_core_test.py b/tests/trappist_core_test.py index 33eda6c..cb77e7e 100644 --- a/tests/trappist_core_test.py +++ b/tests/trappist_core_test.py @@ -1,12 +1,8 @@ -from biodivine_aeon import ( - BooleanNetwork, - FixedPoints, - AsynchronousGraph, -) +from biodivine_aeon import AsynchronousGraph, BooleanNetwork, FixedPoints +from balm.interaction_graph_utils import cleanup_network from balm.petri_net_translation import network_to_petrinet from balm.trappist_core import compute_fixed_point_reduced_STG, trappist -from balm.interaction_graph_utils import cleanup_network from balm.types import BooleanSpace