From 60e324449074f57d5adf7f431f4c09763a2394c1 Mon Sep 17 00:00:00 2001
From: pgleeson
Date: Thu, 29 Aug 2024 13:59:15 +0100
Subject: [PATCH] Using cached info if owmeta not found
---
c302/__init__.py | 150 ++++++++++--------
c302/c302_utils.py | 3 +-
.../c302_C0_Full_elec_neurons_neurons.png | Bin 88897 -> 88897 bytes
.../images/c302_C0_Full_exc_to_muscles.png | Bin 70143 -> 70143 bytes
.../images/c302_C0_Full_exc_to_neurons.png | Bin 91205 -> 91205 bytes
.../images/c302_C0_Full_inh_to_muscles.png | Bin 68876 -> 68876 bytes
.../images/c302_C0_Full_inh_to_neurons.png | Bin 84799 -> 84799 bytes
.../c302_C0_Muscles_elec_neurons_neurons.png | Bin 93246 -> 93246 bytes
.../images/c302_C0_Muscles_exc_to_muscles.png | Bin 80481 -> 80481 bytes
.../images/c302_C0_Muscles_exc_to_neurons.png | Bin 93571 -> 93571 bytes
.../images/c302_C0_Muscles_inh_to_muscles.png | Bin 77565 -> 77565 bytes
.../images/c302_C0_Muscles_inh_to_neurons.png | Bin 90831 -> 90831 bytes
...302_C0_Oscillator_elec_neurons_neurons.png | Bin 53479 -> 53479 bytes
.../c302_C0_Oscillator_exc_to_neurons.png | Bin 53383 -> 53383 bytes
.../c302_C0_Oscillator_inh_to_neurons.png | Bin 51739 -> 51739 bytes
...302_C0_Pharyngeal_elec_neurons_neurons.png | Bin 56038 -> 56038 bytes
.../c302_C0_Pharyngeal_exc_to_neurons.png | Bin 56269 -> 56269 bytes
.../c302_C0_Social_elec_neurons_neurons.png | Bin 42519 -> 42519 bytes
.../images/c302_C0_Social_exc_to_neurons.png | Bin 42323 -> 42323 bytes
.../c302_C0_Syns_elec_neurons_neurons.png | Bin 41397 -> 41397 bytes
.../images/c302_C0_Syns_exc_to_muscles.png | Bin 29905 -> 29905 bytes
.../images/c302_C0_Syns_exc_to_neurons.png | Bin 41137 -> 41137 bytes
.../images/c302_C0_Syns_inh_to_neurons.png | Bin 40369 -> 40369 bytes
23 files changed, 85 insertions(+), 68 deletions(-)
diff --git a/c302/__init__.py b/c302/__init__.py
index 334c054f..6044d7e7 100755
--- a/c302/__init__.py
+++ b/c302/__init__.py
@@ -37,6 +37,8 @@
from lxml import etree
import re
+import json
+
import collections
try:
@@ -69,6 +71,8 @@
MUSCLE_RE = re.compile(r'M([VD][LR])(\d+)')
+OWMETA_CACHED_DATA_FILE = 'c302/data/owmeta_cache.json'
+
def print_(msg, print_it=True): # print_it=False when not verbose
if print_it:
@@ -449,73 +453,88 @@ def elem_in_coll_matches_conn(coll, conn):
return True
return False
+cached_owmeta_data = None
def _get_cell_info(bnd, cells):
- #print('Getting cell info for %s'%cells)
- if bnd is None:
- return None
+ global cached_owmeta_data
+ #print('------ Getting the cell info for %s'%cells)
all_neuron_info = collections.OrderedDict()
all_muscle_info = collections.OrderedDict()
- ctx = bnd(Context)(ident="http://openworm.org/data").stored
- # Go through our list and get the neuron object associated with each name.
- # Store these in another list.
- fixed_up_names = []
- for name in cells:
- match = is_muscle(name)
- if match:
- name = match.group(1) + str(int(match.group(2)))
- fixed_up_names.append(name)
- #fixed_up_names.remove('MANAL')
- #fixed_up_names.remove('MVULVA')
-
- for name in fixed_up_names:
- cell = next(ctx(Cell).query(name=name).load(), None)
- if cell is None:
- #print_("No matching cell for %s" % name)
- continue
-
- normalized_name = cell.name()
- short = ') %s' % normalized_name
- color = '.5 0 0'
- if isinstance(cell, Neuron):
- neuron_types = cell.type()
- if 'sensory' in neuron_types:
- short = 'Se%s' % short
- color = '1 .2 1'
- if 'interneuron' in neuron_types:
- short = 'In%s' % short
- color = '1 0 .4'
- if 'motor' in neuron_types:
- short = 'Mo%s' % short
- color = '.5 .4 1'
-
- neurotransmitter = cell.neurotransmitter()
- elif isinstance(cell, Muscle):
- neuron_types = ()
- neurotransmitter = ()
- color = '0 0.6 0'
- short = 'Mu%s' % short
- else:
- # At this point, we should only have Neurons and Muscles because the reader
- # filters them out
- raise Exception('Got an unexpected cell type')
-
- short = '(%s' % short
- receptor = cell.receptor()
- if 'GABA' in neurotransmitter:
- short = '- %s' % short
- elif len(neurotransmitter) == 0:
- short = '? %s' % short
- else:
- short = '+ %s' % short
- info = (cell, neuron_types, receptor, neurotransmitter, short, color)
+ if bnd is None:
+ if cached_owmeta_data == None:
+ print('Loading OWMeta data from: %s'%OWMETA_CACHED_DATA_FILE)
+ with open(OWMETA_CACHED_DATA_FILE) as f:
+ cached_owmeta_data = json.load(f)
+
+ for cell in cells:
+ if is_muscle(cell):
+ all_muscle_info[cell] = cached_owmeta_data['muscle_info'][cell]
+ else:
+ all_neuron_info[cell] = cached_owmeta_data['neuron_info'][cell]
+
+ else:
+ ctx = bnd(Context)(ident="http://openworm.org/data").stored
+ # Go through our list and get the neuron object associated with each name.
+ # Store these in another list.
+ fixed_up_names = []
+ for name in cells:
+ match = is_muscle(name)
+ if match:
+ name = match.group(1) + str(int(match.group(2)))
+ fixed_up_names.append(name)
+ #fixed_up_names.remove('MANAL')
+ #fixed_up_names.remove('MVULVA')
+
+ for name in fixed_up_names:
+ cell = next(ctx(Cell).query(name=name).load(), None)
+ if cell is None:
+ #print_("No matching cell for %s" % name)
+ continue
+
+ normalized_name = cell.name()
+ short = ') %s' % normalized_name
+ color = '.5 0 0'
+ if isinstance(cell, Neuron):
+ neuron_types = cell.type()
+ if 'sensory' in neuron_types:
+ short = 'Se%s' % short
+ color = '1 .2 1'
+ if 'interneuron' in neuron_types:
+ short = 'In%s' % short
+ color = '1 0 .4'
+ if 'motor' in neuron_types:
+ short = 'Mo%s' % short
+ color = '.5 .4 1'
+
+ neurotransmitter = cell.neurotransmitter()
+ elif isinstance(cell, Muscle):
+ neuron_types = ()
+ neurotransmitter = ()
+ color = '0 0.6 0'
+ short = 'Mu%s' % short
+ else:
+ # At this point, we should only have Neurons and Muscles because the reader
+ # filters them out
+ raise Exception('Got an unexpected cell type')
+
+ short = '(%s' % short
+ receptor = cell.receptor()
+ if 'GABA' in neurotransmitter:
+ short = '- %s' % short
+ elif len(neurotransmitter) == 0:
+ short = '? %s' % short
+ else:
+ short = '+ %s' % short
+
+ info = (cell, neuron_types, receptor, neurotransmitter, short, color)
- if isinstance(cell, Muscle):
- all_muscle_info[cell.name()] = info
- elif isinstance(cell, Neuron):
- all_neuron_info[cell.name()] = info
+ if isinstance(cell, Muscle):
+ all_muscle_info[cell.name()] = info
+ elif isinstance(cell, Neuron):
+ all_neuron_info[cell.name()] = info
+ #print('==== Returning %s; %s'%(all_neuron_info, all_muscle_info))
return all_neuron_info, all_muscle_info
@@ -733,13 +752,14 @@ def generate(net_id,
cells_vs_name = c302.backers.get_adopted_cell_names()
count = 0
+ bnd_ow = None
try:
with Bundle('openworm/owmeta-data', version=6) as bnd:
- all_neuron_info, all_muscle_info = _get_cell_info(bnd, set(cell_names))
+ bnd_ow = bnd
except Exception as e:
print_('Unable to open "openworm/owmeta-data" bundle: %s' % e)
- all_neuron_info = None
- all_muscle_info = None
+
+ all_neuron_info, all_muscle_info = _get_cell_info(bnd_ow, set(cell_names))
for cell in cell_names:
if cells is None or cell in cells:
@@ -1533,9 +1553,7 @@ def main():
all_info['muscle_info'][n] = (str(ami[ow_name][0]),sorted(list(ami[ow_name][1])),sorted(list(ami[ow_name][2])),sorted(list(ami[ow_name][3])),ami[ow_name][4],ami[ow_name][5])
- import json
-
- with open('c302/data/owmeta_cache.json', 'w') as fp:
+ with open(OWMETA_CACHED_DATA_FILE, 'w') as fp:
json.dump(all_info, fp, sort_keys=True, indent=4)
else:
diff --git a/c302/c302_utils.py b/c302/c302_utils.py
index b90d66cf..0ce89621 100644
--- a/c302/c302_utils.py
+++ b/c302/c302_utils.py
@@ -468,8 +468,7 @@ def generate_conn_matrix(nml_doc, save_fig_dir=None, verbose=False, figsize=defa
except Exception as e:
traceback.print_exc()
c302.print_('Unable to connect to the owmeta bundle: %s\n Proceeding anyway...' % e)
- all_neuron_info = {n:('cell?','neuron_types?', 'receptor?', 'neurotransmitter?', n, 'color?') for n in all_neurons}
- all_muscle_info = {m:('cell?','neuron_types?', 'receptor?', 'neurotransmitter?', m, 'color?') for m in all_muscles}
+ all_neuron_info, all_muscle_info = c302._get_cell_info(None, all_cells)
'''
diff --git a/examples/summary/images/c302_C0_Full_elec_neurons_neurons.png b/examples/summary/images/c302_C0_Full_elec_neurons_neurons.png
index e24c1f54bc29942607c8177527de6033ee3f00e7..38d630822ecc1797e74d2b8b3effa9134d87bd28 100644
GIT binary patch
delta 48
zcmX@OjrHI*)(LJ3mU;#{3K=CO1;tkS`nicE1v&X8Ihjd%`9C>G2V^>
E0J1s}=l}o!
delta 48
zcmX@OjrHI*)(LJ37J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9C>G0qeO
E0KnN1u>b%7
delta 48
zcmeyrnC1UsmI-bO7J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9C>G2TxG
E0J2PG*u`eo?x<35&2PG*u`eo?x<>BgC>G2Zk7
E0HzEPk^lez
delta 48
zcmdl#jdlMt)(LJ37J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9C>G2X}q
E0KG&KGynhq
delta 48
zcmdmYgLU5x)(LJ37J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9C>G5)p$
E0Mn`xp#T5?
delta 48
zcmaF(h2`NFmI-bO7J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`92PG*u`eo?x<35&2PG*u`eo?x<>BgC>F;3S5
E0MynJJOBUy
delta 48
zcmex+m*wwWmI-bO7J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9
diff --git a/examples/summary/images/c302_C0_Muscles_inh_to_neurons.png b/examples/summary/images/c302_C0_Muscles_inh_to_neurons.png
index 72c4d88b2339cc935b940c3d4a1390d8cb0d2453..3ff94e16fcfa25a2b3fc83b264a7c11cd819cd55 100644
GIT binary patch
delta 48
zcmX?ql=b{k)(LJ3mU;#{3K=CO1;tkS`nicE1v&X8Ihjd%`9C>F=i$K
E0K3T%xBvhE
delta 48
zcmX?ql=b{k)(LJ37J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9S0RW2>
B5=8(2
diff --git a/examples/summary/images/c302_C0_Oscillator_exc_to_neurons.png b/examples/summary/images/c302_C0_Oscillator_exc_to_neurons.png
index d6312bde7399f59caaa40869f5caa98ffdbcb60f..c6c13f2ffae6fdfc43729d93b6f3b07863e3d114 100644
GIT binary patch
delta 45
zcmZo)$lSh=d4ijQrJjL~LPkkRL9vy-er{q(K~8>2PG*u`eo?x<35&2PG*u`eo?x<>BgN3G003#U
B5hVZs
diff --git a/examples/summary/images/c302_C0_Oscillator_inh_to_neurons.png b/examples/summary/images/c302_C0_Oscillator_inh_to_neurons.png
index cc4f69636e9e8b932e49270dca9babe95d50d94e..d59cab89e6a09a61ccdbcbe9252f8908fbf1de86 100644
GIT binary patch
delta 45
zcmbO|g?aWA<_T^JmU;#{3K=CO1;tkS`nicE1v&X8Ihjd%`9@nV
B5U~IN
delta 45
zcmbO|g?aWA<_T^J7J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9K3jmR6
B5_A9n
diff --git a/examples/summary/images/c302_C0_Pharyngeal_exc_to_neurons.png b/examples/summary/images/c302_C0_Pharyngeal_exc_to_neurons.png
index b1f82c7f4f5b4c941b2d49780d01a5e536f232de..951c9bdc062e9344a4eb9d5fe36690121b367db4 100644
GIT binary patch
delta 45
zcmX@Ro%!r`<_T^JmU;#{3K=CO1;tkS`nicE1v&X8Ihjd%`9
B5gz~m
delta 45
zcmX@Ro%!r`<_T^J7J3Fc3K=CO1;tkS`nicE1v&X8Ihjd%`9_Bl
B60HCL
diff --git a/examples/summary/images/c302_C0_Syns_elec_neurons_neurons.png b/examples/summary/images/c302_C0_Syns_elec_neurons_neurons.png
index e8a00926800a30c04aba5ef7b8c6af137fe5a351..2a894dfb106299b3ac23091d19ab43eb69ade026 100644
GIT binary patch
delta 45
zcmdmbm}%=_rU`BemU;#{3K=CO1;tkS`nicE1v&X8Ihjd%`9