Skip to content

Commit

Permalink
no global core
Browse files Browse the repository at this point in the history
  • Loading branch information
prismofeverything committed Feb 27, 2024
1 parent 24a3fc9 commit eddbeaa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
8 changes: 4 additions & 4 deletions process_bigraph/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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', {})
Expand Down
29 changes: 20 additions & 9 deletions process_bigraph/experiments/minimal_gillespie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
Expand Down Expand Up @@ -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 -------------
# ==================================
Expand Down Expand Up @@ -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': {
Expand All @@ -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)
64 changes: 38 additions & 26 deletions process_bigraph/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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]}

Expand All @@ -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})
Expand Down Expand Up @@ -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}

Expand All @@ -107,7 +116,7 @@ def test_composite():
assert updates[0]['exchange'] == 0.999


def test_infer():
def test_infer(core):
composite = Composite({
'state': {
'increase': {
Expand All @@ -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'


Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()

0 comments on commit eddbeaa

Please sign in to comment.