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 e24c1f54..38d63082 100644 Binary files a/examples/summary/images/c302_C0_Full_elec_neurons_neurons.png and b/examples/summary/images/c302_C0_Full_elec_neurons_neurons.png differ diff --git a/examples/summary/images/c302_C0_Full_exc_to_muscles.png b/examples/summary/images/c302_C0_Full_exc_to_muscles.png index 63a44222..27738fc1 100644 Binary files a/examples/summary/images/c302_C0_Full_exc_to_muscles.png and b/examples/summary/images/c302_C0_Full_exc_to_muscles.png differ diff --git a/examples/summary/images/c302_C0_Full_exc_to_neurons.png b/examples/summary/images/c302_C0_Full_exc_to_neurons.png index 2da1501e..3a54e975 100644 Binary files a/examples/summary/images/c302_C0_Full_exc_to_neurons.png and b/examples/summary/images/c302_C0_Full_exc_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Full_inh_to_muscles.png b/examples/summary/images/c302_C0_Full_inh_to_muscles.png index 01f0ed30..39b3fbe3 100644 Binary files a/examples/summary/images/c302_C0_Full_inh_to_muscles.png and b/examples/summary/images/c302_C0_Full_inh_to_muscles.png differ diff --git a/examples/summary/images/c302_C0_Full_inh_to_neurons.png b/examples/summary/images/c302_C0_Full_inh_to_neurons.png index 90aa3212..d2393017 100644 Binary files a/examples/summary/images/c302_C0_Full_inh_to_neurons.png and b/examples/summary/images/c302_C0_Full_inh_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Muscles_elec_neurons_neurons.png b/examples/summary/images/c302_C0_Muscles_elec_neurons_neurons.png index 30219d7b..050023a6 100644 Binary files a/examples/summary/images/c302_C0_Muscles_elec_neurons_neurons.png and b/examples/summary/images/c302_C0_Muscles_elec_neurons_neurons.png differ diff --git a/examples/summary/images/c302_C0_Muscles_exc_to_muscles.png b/examples/summary/images/c302_C0_Muscles_exc_to_muscles.png index d2505875..86bcaad2 100644 Binary files a/examples/summary/images/c302_C0_Muscles_exc_to_muscles.png and b/examples/summary/images/c302_C0_Muscles_exc_to_muscles.png differ diff --git a/examples/summary/images/c302_C0_Muscles_exc_to_neurons.png b/examples/summary/images/c302_C0_Muscles_exc_to_neurons.png index d6c32a75..9c005d11 100644 Binary files a/examples/summary/images/c302_C0_Muscles_exc_to_neurons.png and b/examples/summary/images/c302_C0_Muscles_exc_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Muscles_inh_to_muscles.png b/examples/summary/images/c302_C0_Muscles_inh_to_muscles.png index 6f69d0b4..aead6750 100644 Binary files a/examples/summary/images/c302_C0_Muscles_inh_to_muscles.png and b/examples/summary/images/c302_C0_Muscles_inh_to_muscles.png differ 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 72c4d88b..3ff94e16 100644 Binary files a/examples/summary/images/c302_C0_Muscles_inh_to_neurons.png and b/examples/summary/images/c302_C0_Muscles_inh_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Oscillator_elec_neurons_neurons.png b/examples/summary/images/c302_C0_Oscillator_elec_neurons_neurons.png index b0507671..55007542 100644 Binary files a/examples/summary/images/c302_C0_Oscillator_elec_neurons_neurons.png and b/examples/summary/images/c302_C0_Oscillator_elec_neurons_neurons.png differ 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 d6312bde..c6c13f2f 100644 Binary files a/examples/summary/images/c302_C0_Oscillator_exc_to_neurons.png and b/examples/summary/images/c302_C0_Oscillator_exc_to_neurons.png differ 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 cc4f6963..d59cab89 100644 Binary files a/examples/summary/images/c302_C0_Oscillator_inh_to_neurons.png and b/examples/summary/images/c302_C0_Oscillator_inh_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Pharyngeal_elec_neurons_neurons.png b/examples/summary/images/c302_C0_Pharyngeal_elec_neurons_neurons.png index 1ad2b621..acade5ab 100644 Binary files a/examples/summary/images/c302_C0_Pharyngeal_elec_neurons_neurons.png and b/examples/summary/images/c302_C0_Pharyngeal_elec_neurons_neurons.png differ 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 b1f82c7f..951c9bdc 100644 Binary files a/examples/summary/images/c302_C0_Pharyngeal_exc_to_neurons.png and b/examples/summary/images/c302_C0_Pharyngeal_exc_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Social_elec_neurons_neurons.png b/examples/summary/images/c302_C0_Social_elec_neurons_neurons.png index c7c0f1a6..1f5a1ca1 100644 Binary files a/examples/summary/images/c302_C0_Social_elec_neurons_neurons.png and b/examples/summary/images/c302_C0_Social_elec_neurons_neurons.png differ diff --git a/examples/summary/images/c302_C0_Social_exc_to_neurons.png b/examples/summary/images/c302_C0_Social_exc_to_neurons.png index c161e890..ffbb6bc6 100644 Binary files a/examples/summary/images/c302_C0_Social_exc_to_neurons.png and b/examples/summary/images/c302_C0_Social_exc_to_neurons.png differ 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 e8a00926..2a894dfb 100644 Binary files a/examples/summary/images/c302_C0_Syns_elec_neurons_neurons.png and b/examples/summary/images/c302_C0_Syns_elec_neurons_neurons.png differ diff --git a/examples/summary/images/c302_C0_Syns_exc_to_muscles.png b/examples/summary/images/c302_C0_Syns_exc_to_muscles.png index 58a29868..1d64c1f9 100644 Binary files a/examples/summary/images/c302_C0_Syns_exc_to_muscles.png and b/examples/summary/images/c302_C0_Syns_exc_to_muscles.png differ diff --git a/examples/summary/images/c302_C0_Syns_exc_to_neurons.png b/examples/summary/images/c302_C0_Syns_exc_to_neurons.png index 0c3d6885..552d140b 100644 Binary files a/examples/summary/images/c302_C0_Syns_exc_to_neurons.png and b/examples/summary/images/c302_C0_Syns_exc_to_neurons.png differ diff --git a/examples/summary/images/c302_C0_Syns_inh_to_neurons.png b/examples/summary/images/c302_C0_Syns_inh_to_neurons.png index 810fe7ea..3afa8845 100644 Binary files a/examples/summary/images/c302_C0_Syns_inh_to_neurons.png and b/examples/summary/images/c302_C0_Syns_inh_to_neurons.png differ