Skip to content

Commit

Permalink
Merge pull request #80 from openworm/development
Browse files Browse the repository at this point in the history
Allows c302 to be fully used without owmeta installed...
  • Loading branch information
pgleeson committed Aug 29, 2024
2 parents a058860 + aaeed7e commit 81b229a
Show file tree
Hide file tree
Showing 38 changed files with 6,202 additions and 89 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/non_omv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
runs-on: [ubuntu-latest, macos-12, macos-latest ]
python-version: [ 3.8, 3.9, "3.10" ]
python-version: [ 3.8, 3.9, "3.10", "3.11" ]
exclude:
- runs-on: macos-latest
python-version: "3.8"
Expand Down Expand Up @@ -43,7 +43,9 @@ jobs:
pip list
- name: Test OpenWormReader with owmeta
if: ${{ matrix.python-version != '3.11' }}
run: |
pip install owmeta>=0.12.3
owm bundle remote --user add ow 'https://raw.githubusercontent.com/openworm/owmeta-bundles/master/index.json'
python -m c302.OpenWormReader
Expand Down
14 changes: 9 additions & 5 deletions c302/OpenWormReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
from c302.NeuroMLUtilities import analyse_connections
from c302 import print_, MUSCLE_RE

from owmeta_core.bundle import Bundle
from owmeta_core.context import Context
from owmeta.neuron import Neuron
from owmeta.muscle import BodyWallMuscle
from owmeta.worm import Worm
try:
from owmeta_core.bundle import Bundle
from owmeta_core.context import Context
from owmeta.neuron import Neuron
from owmeta.muscle import BodyWallMuscle
from owmeta.worm import Worm
except:
print("owmeta not installed! Cannot run OpenWormReader")
exit()

############################################################

Expand Down
210 changes: 139 additions & 71 deletions c302/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@
from lxml import etree
import re

import json

import collections

from owmeta_core import __version__ as owc_version
from owmeta_core.bundle import Bundle
from owmeta_core.context import Context
from owmeta import __version__ as owmeta_version
from owmeta.cell import Cell
from owmeta.neuron import Neuron
from owmeta.muscle import Muscle
try:
from owmeta_core import __version__ as owc_version
from owmeta_core.bundle import Bundle
from owmeta_core.context import Context
from owmeta import __version__ as owmeta_version
from owmeta.cell import Cell
from owmeta.neuron import Neuron
from owmeta.muscle import Muscle
owmeta_installed = True
except:
print("owmeta not installed! Proceeding anyway...")
owmeta_installed = False

try:
from urllib2 import URLError # Python 2
Expand All @@ -64,6 +71,8 @@

MUSCLE_RE = re.compile(r'M([VD][LR])(\d+)')

OWMETA_CACHED_DATA_FILE = os.path.dirname(os.path.abspath(__file__))+'/data/owmeta_cache.json'


def print_(msg, print_it=True): # print_it=False when not verbose
if print_it:
Expand Down Expand Up @@ -444,72 +453,88 @@ def elem_in_coll_matches_conn(coll, conn):
return True
return False

cached_owmeta_data = None

def _get_cell_info(bnd, 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 cached 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 @@ -618,8 +643,8 @@ def generate(net_id,
info = "\n\nParameters and setting used to generate this network:\n\n"+\
" Data reader: %s\n" % data_reader+\
" c302 version: %s\n" % __version__+\
" owmeta version: %s\n" % owmeta_version+\
" owmeta_core version: %s\n" % owc_version+\
" owmeta version: %s\n" % ('- not installed -' if not owmeta_installed else owmeta_version) +\
" owmeta_core version: %s\n" % ('- not installed -' if not owmeta_installed else owc_version) +\
" Cells: %s\n" % (cells if cells is not None else "All cells")+\
" Cell stimulated: %s\n" % (cells_to_stimulate if cells_to_stimulate is not None else "All neurons")+\
" Connection: %s\n" % (conns_to_include if conns_to_include is not None else "All connections") + \
Expand Down Expand Up @@ -727,13 +752,14 @@ def generate(net_id,
cells_vs_name = c302.backers.get_adopted_cell_names()

count = 0

try:
with Bundle('openworm/owmeta-data', version=6) as bnd:
all_neuron_info, all_muscle_info = _get_cell_info(bnd, set(cell_names))
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(None, set(cell_names))

for cell in cell_names:
if cells is None or cell in cells:
Expand Down Expand Up @@ -1489,4 +1515,46 @@ def main():


if __name__ == '__main__':
main()

import sys

if '-cache' in sys.argv:
print('Starting c302...')
from c302.ConnectomeReader import PREFERRED_MUSCLE_NAMES
from c302.ConnectomeReader import PREFERRED_NEURON_NAMES

all_info = {'neuron_info':{}, 'muscle_info':{}}


from owmeta_core.bundle import Bundle

from owmeta_core import __version__ as owc_version
from owmeta import __version__ as owmeta_version

ver_info = 'owmeta v%s (owmeta core v%s)'%(owmeta_version,owc_version)
all_info['comment'] = 'Information exported from '+ver_info

with Bundle('openworm/owmeta-data', version=6) as bnd:

for n in PREFERRED_NEURON_NAMES:

ani, _all_muscle_info = _get_cell_info(bnd, [n])

print(' > %s; %s'%(ani[n], _all_muscle_info))
all_info['neuron_info'][n] = (str(ani[n][0]),sorted(list(ani[n][1])),sorted(list(ani[n][2])),sorted(list(ani[n][3])),ani[n][4],ani[n][5])

for n in PREFERRED_MUSCLE_NAMES:

_all_neuron_info, ami = _get_cell_info(bnd, [n])

if not n == 'MVULVA' and not n == 'MANAL':
ow_name = n[1:] if n[3]!='0' else '%s%s'%(n[1:3],n[-1])
print(' > %s (%s): %s; %s'%(n, ow_name, _all_neuron_info, ami))

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])

with open(OWMETA_CACHED_DATA_FILE, 'w') as fp:
json.dump(all_info, fp, sort_keys=True, indent=4)

else:
main()
2 changes: 1 addition & 1 deletion c302/backers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
This method reads a generated list of cells vs. names as assigned by OpenWorm backers
This information will eventually be moved to owmeta
This information will eventually be moved to owmeta/elsewhere...
'''

import os
Expand Down
22 changes: 12 additions & 10 deletions c302/c302_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import numpy as np
from pyneuroml import pynml
from pyneuroml import plot as pyneuroml_plot
from owmeta_core.bundle import Bundle

import c302

Expand All @@ -18,7 +17,6 @@

def plots(a_n, info, cells, dt):


c302.print_('Generating plots for: %s'%info)

heightened = False
Expand Down Expand Up @@ -455,12 +453,23 @@ def generate_conn_matrix(nml_doc, save_fig_dir=None, verbose=False, figsize=defa

all_cells = sorted(all_cells)

all_neurons = []
all_muscles = []
for c in all_cells:
if c302.is_muscle(c):
all_muscles.append(c)
else:
all_neurons.append(c)

try:
from owmeta_core.bundle import Bundle
with Bundle('openworm/owmeta-data', version=6) as bnd:
all_neuron_info, all_muscle_info = c302._get_cell_info(bnd, all_cells)
except Exception as e:
c302.print_('Unable to connect to owmeta bundle: %s' % e)
traceback.print_exc()
c302.print_('Unable to connect to the owmeta bundle: %s\n Proceeding anyway...' % e)
all_neuron_info, all_muscle_info = c302._get_cell_info(None, all_cells)


'''
if order_by_type:
Expand All @@ -478,13 +487,6 @@ def generate_conn_matrix(nml_doc, save_fig_dir=None, verbose=False, figsize=defa
print('Swapping %s with %s'%(all_neuron_info,ordered_all_neuron_info))
all_neuron_info = ordered_all_neuron_info'''

all_neurons = []
all_muscles = []
for c in all_cells:
if c302.is_muscle(c):
all_muscles.append(c)
else:
all_neurons.append(c)


data_exc_n = np.zeros((len(all_neurons),len(all_neurons)))
Expand Down
Loading

0 comments on commit 81b229a

Please sign in to comment.