Skip to content

Commit

Permalink
connect_all is working
Browse files Browse the repository at this point in the history
  • Loading branch information
eagmon committed Feb 29, 2024
1 parent 23e222a commit e4f90f4
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 38 deletions.
43 changes: 28 additions & 15 deletions builder/builder_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,22 @@ def connect(self, port=None, target=None):
if port in schema['_outputs']:
value['outputs'][port] = target

def connect_all(self, append_to_store_name=''):
def connect_all(self, append_to_store_name='_store'):
# Check if the current node is an edge and perform connections if it is
value = self.value()
schema = self.schema()
assert self.builder.core.check('edge', value), "connect_all only works on edges"

for port, port_schema in schema['_inputs'].items():
if port not in value['inputs']:
value['inputs'][port] = [port + append_to_store_name]
for port, port_schema in schema['_outputs'].items():
if port not in value['outputs']:
value['outputs'][port] = [port + append_to_store_name]
if self.builder.core.check('edge', value):
schema = self.schema()
for port in schema.get('_inputs', {}).keys():
if port not in value.get('inputs', {}):
value['inputs'][port] = [port + append_to_store_name]
for port in schema.get('_outputs', {}).keys():
if port not in value.get('outputs', {}):
value['outputs'][port] = [port + append_to_store_name]
# Optionally, update the current node value here if necessary

# Recursively apply connect_all to all child nodes
for child in self.branches.values():
child.connect_all(append_to_store_name=append_to_store_name)

def interface(self, print_ports=False):
value = self.value()
Expand Down Expand Up @@ -212,6 +217,9 @@ def list_processes(self):
def complete(self):
self.schema, self.tree = self.core.complete(self.schema, self.tree)

def connect_all(self, append_to_store_name='_store'):
self.node.connect_all(append_to_store_name=append_to_store_name)

def visualize(self, filename=None, out_dir=None, **kwargs):
return plot_bigraph(
state=self.tree,
Expand Down Expand Up @@ -291,10 +299,10 @@ def test_builder():
'_type': 'map[float]',
'A gene': 2.0,
'B gene': 1.0},
'RNA_store': {
'mRNA_store': {
'_type': 'map[float]',
'A rna': 0.0,
'B rna': 0.0},
'A mRNA': 0.0,
'B mRNA': 0.0},
}

builder = Builder(core=core, tree=initial_tree)
Expand Down Expand Up @@ -331,6 +339,10 @@ def test_builder():
# inputs={'port_id': ['store']} # we should be able to set the wires directly like this
)

## print the ports
print(f"EVENT PROCESS PORTS: {pf(builder['event_process'].interface())}")
print(f"INTERVAL PROCESS PORTS: {pf(builder['interval_process'].interface())}")

# make bigraph-viz diagram before connect
builder.visualize(filename='builder_test1',
show_values=True,
Expand All @@ -342,8 +354,9 @@ def test_builder():
# builder['interval_process'].connect(port='DNA', target=['DNA_store'])
# builder['interval_process'].connect(port='mRNA', target=['mRNA_store'])
builder['interval_process'].connect(port='interval', target=['event_process', 'interval']) # TODO -- viz needs to show interval in process
builder['interval_process'].connect_all(append_to_store_name='_store')
builder['event_process'].connect_all(append_to_store_name='_store')
# builder['interval_process'].connect_all(append_to_store_name='_store')
# builder['event_process'].connect_all(append_to_store_name='_store')
builder.connect_all(append_to_store_name='_store')

# make bigraph-viz diagram after connect
builder.visualize(filename='builder_test2',
Expand Down
21 changes: 21 additions & 0 deletions builder/dict_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

def custom_pf(d, indent=0):
"""Custom dictionary formatter to achieve specific indentation styles."""
items = []
for k, v in d.items():
key_str = f"{repr(k)}: "
if isinstance(v, dict):
if v: # Check if the dictionary is not empty
value_str = custom_pf(v, indent + 3)
else:
value_str = "{}"
else:
value_str = repr(v)
items.append(f"{' ' * indent}{key_str}{value_str}")

# final formatting
items_str = ','.join(items)
if indent > 0:
return f"{{\n{items_str}\n{' ' * (indent - 4)}}}"
else:
return f"{{\n{items_str}\n}}"
Loading

0 comments on commit e4f90f4

Please sign in to comment.