Skip to content

Commit

Permalink
Using cached info if owmeta not found
Browse files Browse the repository at this point in the history
  • Loading branch information
pgleeson committed Aug 29, 2024
1 parent 3a233d5 commit 60e3244
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 68 deletions.
150 changes: 84 additions & 66 deletions c302/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
from lxml import etree
import re

import json

import collections

try:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions c302/c302_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


'''
Expand Down
Binary file modified examples/summary/images/c302_C0_Full_elec_neurons_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Full_exc_to_muscles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Full_exc_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Full_inh_to_muscles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Full_inh_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Muscles_elec_neurons_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Muscles_exc_to_muscles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Muscles_exc_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Muscles_inh_to_muscles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Muscles_inh_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Oscillator_exc_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Oscillator_inh_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Pharyngeal_exc_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Social_elec_neurons_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Social_exc_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Syns_elec_neurons_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Syns_exc_to_muscles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Syns_exc_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/summary/images/c302_C0_Syns_inh_to_neurons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 60e3244

Please sign in to comment.