diff --git a/process_bigraph/composite.py b/process_bigraph/composite.py index 2f3ed86..97f83d1 100644 --- a/process_bigraph/composite.py +++ b/process_bigraph/composite.py @@ -94,8 +94,8 @@ class can provide a ``defaults`` class variable to specify the """ config_schema = {} - def __init__(self, config=None, local_types=None): - self.core = local_types or core + def __init__(self, config=None, core=None): + self.core = core or ProcessTypes() if config is None: config = {} @@ -395,8 +395,8 @@ class Composite(Process): 'global_time_precision': 'maybe[float]'} - def __init__(self, config=None, local_types=None): - super().__init__(config, local_types) + def __init__(self, config=None, core=None): + super().__init__(config, core) # insert global_time into schema if not present initial_composition = self.config.get('composition', {}) diff --git a/process_bigraph/experiments/minimal_gillespie.py b/process_bigraph/experiments/minimal_gillespie.py index 48d8460..de3ca93 100644 --- a/process_bigraph/experiments/minimal_gillespie.py +++ b/process_bigraph/experiments/minimal_gillespie.py @@ -9,7 +9,8 @@ import numpy as np -from process_bigraph import Step, Process, Composite # , core +import pytest +from process_bigraph import Step, Process, Composite, ProcessTypes # , core # 'map[float](default:1.0|apply:set)' @@ -163,7 +164,14 @@ def update(self, state, interval): return update -def test_gillespie_composite(): +@pytest.fixture +def core(): + core = ProcessTypes() + core.import_types(EXPORT) + return core + + +def test_gillespie_composite(core): composite_schema = { # This all gets inferred ------------- # ================================== @@ -272,9 +280,9 @@ def test_gillespie_composite(): # 'mRNA': { # 'C': '21.0'}}} - gillespie = Composite(composite_schema) - - import ipdb; ipdb.set_trace() + gillespie = Composite( + composite_schema, + core=core) updates = gillespie.update({ 'DNA': { @@ -291,18 +299,21 @@ def test_gillespie_composite(): assert 'mRNA' in updates[0] -def test_union_tree(): +def test_union_tree(core): tree_union = core.access('list[string]~tree[list[string]]') assert core.check( tree_union, {'a': ['what', 'is', 'happening']}) -def test_stochastic_deterministic_composite(): +def test_stochastic_deterministic_composite(core): # TODO make the demo for a hybrid stochastic/deterministic simulator pass if __name__ == '__main__': - test_gillespie_composite() - test_union_tree() + core = ProcessTypes() + core.import_types(EXPORT) + + test_gillespie_composite(core) + test_union_tree(core) diff --git a/process_bigraph/tests.py b/process_bigraph/tests.py index 670c812..3764cb9 100644 --- a/process_bigraph/tests.py +++ b/process_bigraph/tests.py @@ -3,9 +3,10 @@ """ import random +import pytest from process_bigraph.composite import Process, Step, Composite, merge_collections -from process_bigraph.type_system import core +from process_bigraph.type_system import ProcessTypes class IncreaseProcess(Process): @@ -15,8 +16,8 @@ class IncreaseProcess(Process): '_default': '0.1'}} - def __init__(self, config=None): - super().__init__(config) + def __init__(self, config=None, core=None): + super().__init__(config, core) def interface(self): @@ -38,12 +39,12 @@ def update(self, state, interval): 'level': state['level'] * self.config['rate']} -def test_default_config(): - process = IncreaseProcess() +def test_default_config(core): + process = IncreaseProcess(core=core) assert process.config['rate'] == 0.1 -def test_merge_collections(): +def test_merge_collections(core): a = {('what',): [1, 2, 3]} b = {('okay', 'yes'): [3, 3], ('what',): [4, 5, 11]} @@ -52,19 +53,27 @@ def test_merge_collections(): assert c[('what',)] == [1, 2, 3, 4, 5, 11] -def test_process(): - process = IncreaseProcess({'rate': 0.2}) +@pytest.fixture +def core(): + return ProcessTypes() + + +def test_process(core): + process = IncreaseProcess({'rate': 0.2}, core=core) interface = process.interface() state = core.fill(interface['inputs']) state = core.fill(interface['outputs']) update = process.update({'level': 5.5}, 1.0) - new_state = core.apply(interface['outputs'], state, update) + new_state = core.apply( + interface['outputs'], + state, + update) assert new_state['level'] == 1.1 -def test_composite(): +def test_composite(core): # TODO: add support for the various vivarium emitter # increase = IncreaseProcess({'rate': 0.3}) @@ -92,7 +101,7 @@ def test_composite(): 'interval': 1.0, 'inputs': {'level': ['value']}, 'outputs': {'level': ['value']}}, - 'value': '11.11'}}) + 'value': '11.11'}}, core=core) initial_state = {'exchange': 3.33} @@ -107,7 +116,7 @@ def test_composite(): assert updates[0]['exchange'] == 0.999 -def test_infer(): +def test_infer(core): composite = Composite({ 'state': { 'increase': { @@ -116,13 +125,13 @@ def test_infer(): 'config': {'rate': '0.3'}, 'inputs': {'level': ['value']}, 'outputs': {'level': ['value']}}, - 'value': '11.11'}}) + 'value': '11.11'}}, core=core) assert composite.composition['value']['_type'] == 'float' assert composite.state['value'] == 11.11 -def test_process_type(): +def test_process_type(core): assert core.access('process')['_type'] == 'process' @@ -154,7 +163,7 @@ def update(self, inputs): return {'c': c} -def test_step_initialization(): +def test_step_initialization(core): composite = Composite({ 'state': { 'A': 13, @@ -178,13 +187,12 @@ def test_step_initialization(): 'a': ['B'], 'b': ['C']}, 'outputs': { - 'c': ['D']}}}}) - + 'c': ['D']}}}}, core=core) assert composite.state['D'] == (13 + 21) * 21 -def test_dependencies(): +def test_dependencies(core): operation = { 'a': 11.111, 'b': 22.2, @@ -241,7 +249,9 @@ def test_dependencies(): 'outputs': { 'c': ['i']}}} - composite = Composite({'state': operation}) + composite = Composite( + {'state': operation}, + core=core) assert composite.state['h'] == -17396.469884 @@ -367,11 +377,13 @@ def test_reaction(): if __name__ == '__main__': - test_default_config() - test_merge_collections() - test_process() - test_composite() - test_infer() - test_step_initialization() - test_dependencies() + core = ProcessTypes() + + test_default_config(core) + test_merge_collections(core) + test_process(core) + test_composite(core) + test_infer(core) + test_step_initialization(core) + test_dependencies(core) # test_reaction()