def pointset_to_objects(pointset): """ This function is just for visualization of intermediate data (i.e. debugging). """ doc = neuroml.NeuroMLDocument() for i in range(len(pointset)): point = pointset[i] p = neuroml.Point3DWithDiam(x=point[0], y=point[1], z=point[2], diameter=1) soma = neuroml.Segment(proximal=p, distal=p) soma.name = "Soma" soma.id = 0 sg = neuroml.SegmentGroup() sg.id = "Soma" sgm = neuroml.Member(segments=0) sg.members.append(sgm) morphology = neuroml.Morphology() morphology.segments.append(soma) morphology.segment_groups.append(sg) cell = neuroml.Cell() cell.id = "pseudocell for bbpt " + str(i) cell.morphology = morphology doc.cells.append(cell) writers.NeuroMLWriter.write(doc, "backbone.nml")
def _morphology(self): """Return the morphology of the cell. Currently this is restricted to `Neuron <#neuron>`_ objects. """ morph_name = "morphology_" + str(next(self.name())) # Query for segments query = segment_query.substitute(morph_name=morph_name) qres = self.rdf.query(query, initNs=ns) morph = neuroml.Morphology(id=morph_name) for r in qres: par = False if r['par_id']: par = neuroml.SegmentParent(segments=str(r['par_id'])) s = neuroml.Segment(name=str(r['seg_name']), id=str(r['seg_id']), parent=par) else: s = neuroml.Segment(name=str(r['seg_name']), id=str(r['seg_id'])) if r['x_prox']: loop_prox = neuroml.Point3DWithDiam( *(r[x] for x in ['x_prox', 'y_prox', 'z_prox', 'd_prox'])) s.proximal = loop_prox loop = neuroml.Point3DWithDiam(*(r[x] for x in ['x', 'y', 'z', 'd'])) s.distal = loop morph.segments.append(s) # Query for segment groups query = segment_group_query.substitute(morph_name=morph_name) qres = self.rdf.query(query, initNs=ns) for r in qres: s = neuroml.SegmentGroup(id=r['gid']) if r['member']: m = neuroml.Member() m.segments = str(r['member']) s.members.append(m) elif r['include']: i = neuroml.Include() i.segment_groups = str(r['include']) s.includes.append(i) morph.segment_groups.append(s) return morph
def splitSegmentAlongFraction(cell, parentName, parentGroup, fractionAlong, childName): child = [seg for seg in cell.morphology.segments if seg.name == childName][0] parent = [ seg for seg in cell.morphology.segments if seg.name == parentName ][0] newSegID = max([int(seg.id) for seg in cell.morphology.segments]) + 1 parentA = copy.deepcopy(parent) parentB = copy.deepcopy(parent) parentB.id = newSegID parentB.parent.segments = parentA.id parentB.name = parentName + "_B" breakPoint = copy.deepcopy(parent.distal) breakPoint.x = parent.proximal.x + (parent.distal.x - parent.proximal.x) * fractionAlong breakPoint.y = parent.proximal.y + (parent.distal.y - parent.proximal.y) * fractionAlong breakPoint.z = parent.proximal.z + (parent.distal.z - parent.proximal.z) * fractionAlong parentA.distal = copy.deepcopy(breakPoint) parentB.proximal = copy.deepcopy(breakPoint) parentB.proximal.original_tagname_ = "proximal" i = 0 for seg in cell.morphology.segments: if seg == parent: cell.morphology.segments[i] = parentA break i = i + 1 cell.morphology.segments.append(parentB) [g for g in cell.morphology.segment_groups if g.id == parentGroup][0]\ .members\ .append(neuroml.Member(segments=newSegID)) child.parent.segments = parent.id
def morphology(self): morph_name = "morphology_" + str(self.name()) # Query for segments query = segment_query.substitute(morph_name=morph_name) qres = self['semantic_net'].query(query, initNs=ns) morph = neuroml.Morphology(id=morph_name) for r in qres: par = False if r['par_id']: par = neuroml.SegmentParent(segments=str(r['par_id'])) s = neuroml.Segment(name=str(r['seg_name']), id=str(r['seg_id']), parent=par) else: s = neuroml.Segment(name=str(r['seg_name']), id=str(r['seg_id'])) if r['x_prox']: loop_prox = neuroml.Point3DWithDiam( *(r[x] for x in ['x_prox', 'y_prox', 'z_prox', 'd_prox'])) s.proximal = loop_prox loop = neuroml.Point3DWithDiam(*(r[x] for x in ['x', 'y', 'z', 'd'])) s.distal = loop morph.segments.append(s) # Query for segment groups query = segment_group_query.substitute(morph_name=morph_name) qres = self['semantic_net'].query(query, initNs=ns) for r in qres: s = neuroml.SegmentGroup(id=r['gid']) if r['member']: m = neuroml.Member() m.segments = str(r['member']) s.members.append(m) elif r['include']: i = neuroml.Include() i.segment_groups = str(r['include']) s.includes.append(i) morph.segment_groups.append(s) return morph
def parse_templates_json(templates_json="templates.json", ignore_chans = [], save_example_files=False, verbose=False): with open(templates_json, "r") as templates_json_file: json_cells = json.load(templates_json_file) concentrationModels = '' for firing_type_u in json_cells: if verbose: print("\n --------------- %s "%(firing_type_u)) firing_type = str(firing_type_u) cell_dict = json_cells[firing_type] nml_doc = neuroml.NeuroMLDocument(id=firing_type) # Membrane properties # included_channels[firing_type] = [] channel_densities = [] channel_density_nernsts = [] channel_density_non_uniform_nernsts = [] channel_density_non_uniforms = [] species = [] for section_list in cell_dict['forsecs']: for parameter_name in cell_dict['forsecs'][section_list]: value = cell_dict['forsecs'][section_list][parameter_name] if verbose: print(" --- %s, %s: %s "%(section_list,parameter_name,value)) if parameter_name == 'g_pas': channel = 'pas' arguments = {} cond_density = "%s S_per_cm2" % value if verbose: print(' - Adding %s with %s'%(channel, cond_density)) channel_nml2_file = "%s.channel.nml"%channel if channel_nml2_file not in included_channels[firing_type]: nml_doc.includes.append( neuroml.IncludeType( href="../../NeuroML2/%s" % channel_nml2_file)) included_channels[firing_type].append(channel_nml2_file) erev = cell_dict['forsecs'][section_list]['e_pas'] erev = "%s mV" % erev arguments["cond_density"] = cond_density arguments['ion_channel'] = channel arguments["ion"] = "non_specific" arguments["erev"] = erev arguments["id"] = "%s_%s" % (section_list, parameter_name) channel_class = 'ChannelDensity' density = getattr(neuroml, channel_class)(**arguments) channel_densities.append(density) for section_list in cell_dict['parameters']: for parameter_name in cell_dict['parameters'][section_list]: if parameter_name != 'e_pas' and 'CaDynamics_E2' not in parameter_name: parameter_dict = cell_dict['parameters'][section_list][parameter_name] if verbose: print(" --- %s, %s: %s "%(section_list,parameter_name,parameter_dict)) channel = parameter_dict['channel'] if channel not in ignore_chans: arguments = {} cond_density = None variable_parameters = None if parameter_dict['distribution']['disttype'] == "uniform": value = float(parameter_dict['distribution']['value']) if channel in density_scales: value = value * density_scales[channel] cond_density = "%s S_per_cm2" % value else: new_expr = '1e4 * (%s)'%parameter_dict['distribution']['value'].replace('x','p').replace('epp','exp') iv = neuroml.InhomogeneousValue(inhomogeneous_parameters="PathLengthOver_%s"%section_list, value=new_expr) variable_parameters = [ neuroml.VariableParameter( segment_groups=section_list, parameter='condDensity', inhomogeneous_value=iv)] channel_name = channel if channel_substitutes.has_key(channel): channel_name = channel_substitutes[channel] channel_nml2_file = "%s.channel.nml"%channel_name if channel_nml2_file not in included_channels[firing_type]: nml_doc.includes.append( neuroml.IncludeType( href="../../NeuroML2/%s" % channel_nml2_file)) included_channels[firing_type].append(channel_nml2_file) arguments['ion'] = channel_ions[channel] erev = ion_erevs[arguments["ion"]] channel_class = 'ChannelDensity' if erev == "nernst": erev = None channel_class = 'ChannelDensityNernst' elif erev == "pas": erev = cell_dict['parameters'] \ [section_list]['e_pas']['distribution']\ ['value'] erev = "%s mV" % erev arguments["ion"] = "non_specific" if variable_parameters is not None: channel_class += 'NonUniform' else: arguments["segment_groups"] = section_list if erev is not None: arguments["erev"] = erev arguments["id"] = "%s_%s" % (section_list, parameter_name) if cond_density is not None: arguments["cond_density"] = cond_density arguments['ion_channel'] = channel_name if variable_parameters is not None: arguments['variable_parameters'] = variable_parameters density = getattr(neuroml, channel_class)(**arguments) if channel_class == "ChannelDensityNernst": channel_density_nernsts.append(density) elif channel_class == "ChannelDensityNernstNonUniform": channel_density_non_uniform_nernsts.append(density) elif channel_class == "ChannelDensityNonUniform": channel_density_non_uniforms.append(density) else: channel_densities.append(density) elif 'gamma_CaDynamics_E2' in parameter_name: parameter_dict = cell_dict['parameters'][section_list][parameter_name] model = 'CaDynamics_E2_NML2__%s_%s'%(firing_type,section_list) value = parameter_dict['distribution']['value'] concentrationModels+='<concentrationModel id="%s" ion="ca" '%model +\ 'type="concentrationModelHayEtAl" minCai="1e-4 mM" ' +\ 'gamma="%s" '%value elif 'decay_CaDynamics_E2' in parameter_name: # calcium_model = \ # neuroml.DecayingPoolConcentrationModel(ion='ca') model = 'CaDynamics_E2_NML2__%s_%s'%(firing_type,section_list) species.append(neuroml.Species( id='ca', ion='ca', initial_concentration='5.0E-11 mol_per_cm3', initial_ext_concentration='2.0E-6 mol_per_cm3', concentration_model=model, segment_groups=section_list)) channel_nml2_file = 'CaDynamics_E2_NML2.nml' if channel_nml2_file not in included_channels[firing_type]: included_channels[firing_type].append(channel_nml2_file) parameter_dict = cell_dict['parameters'][section_list][parameter_name] value = parameter_dict['distribution']['value'] concentrationModels+='decay="%s ms" depth="0.1 um"/> <!-- For group %s in %s-->\n\n'%(value,section_list,firing_type) capacitance_overwrites = {} for section_list in cell_dict['forsecs']: for parameter_name in cell_dict['forsecs'][section_list]: if parameter_name == "cm" and section_list != 'all': value = cell_dict['forsecs'][section_list][parameter_name] capacitance_overwrites[ section_list] = "%s uF_per_cm2" % value specific_capacitances = [] for section_list in default_capacitances: if section_list in capacitance_overwrites: capacitance = capacitance_overwrites[section_list] else: capacitance = default_capacitances[section_list] specific_capacitances.append( neuroml.SpecificCapacitance(value=capacitance, segment_groups=section_list)) init_memb_potentials = [neuroml.InitMembPotential( value="-80 mV", segment_groups='all')] membrane_properties = neuroml.MembraneProperties( channel_densities=channel_densities, channel_density_nernsts=channel_density_nernsts, channel_density_non_uniform_nernsts=channel_density_non_uniform_nernsts, channel_density_non_uniforms=channel_density_non_uniforms, specific_capacitances=specific_capacitances, init_memb_potentials=init_memb_potentials) # Intracellular Properties # resistivities = [] resistivities.append(neuroml.Resistivity( value="100 ohm_cm", segment_groups='all')) intracellular_properties = neuroml.IntracellularProperties( resistivities=resistivities, species=species) # Cell construction # biophysical_properties = \ neuroml.BiophysicalProperties(id="biophys", intracellular_properties= intracellular_properties, membrane_properties= membrane_properties) biophysical_properties_vs_types[firing_type] = biophysical_properties if save_example_files: cell = neuroml.Cell(id=firing_type, notes="\n*************************\nThis is not a physiologically constrained cell model!!\n"+\ "It is only for testing formatting of the biophysicalProperties extracted from templates.json\n*************************\n", biophysical_properties=biophysical_properties) nml_doc.cells.append(cell) cell.morphology = neuroml.Morphology(id="morph") cell.morphology.segments.append(neuroml.Segment(id='0', name='soma', proximal=neuroml.Point3DWithDiam(x=0,y=0,z=0,diameter=10), distal=neuroml.Point3DWithDiam(x=0,y=20,z=0,diameter=10))) cell.morphology.segment_groups.append(neuroml.SegmentGroup(id="soma", neuro_lex_id="sao864921383", members=[neuroml.Member("0")])) cell.morphology.segments.append(neuroml.Segment(id='1', name='axon', parent=neuroml.SegmentParent(segments='0',fraction_along="0"), proximal=neuroml.Point3DWithDiam(x=0,y=0,z=0,diameter=2), distal=neuroml.Point3DWithDiam(x=0,y=-50,z=0,diameter=2))) cell.morphology.segment_groups.append(neuroml.SegmentGroup(id="axon", neuro_lex_id="sao864921383", members=[neuroml.Member("1")])) cell.morphology.segments.append(neuroml.Segment(id='2', name='basal_dend', parent=neuroml.SegmentParent(segments='0'), proximal=neuroml.Point3DWithDiam(x=0,y=20,z=0,diameter=3), distal=neuroml.Point3DWithDiam(x=50,y=20,z=0,diameter=3))) cell.morphology.segment_groups.append(neuroml.SegmentGroup(id="basal_dend", neuro_lex_id="sao864921383", members=[neuroml.Member("2")])) cell.morphology.segments.append(neuroml.Segment(id='3', name='apical_dend1', parent=neuroml.SegmentParent(segments='0'), proximal=neuroml.Point3DWithDiam(x=0,y=20,z=0,diameter=3), distal=neuroml.Point3DWithDiam(x=0,y=120,z=0,diameter=3))) cell.morphology.segments.append(neuroml.Segment(id='4', name='apical_dend2', parent=neuroml.SegmentParent(segments='0'), proximal=neuroml.Point3DWithDiam(x=0,y=120,z=0,diameter=3), distal=neuroml.Point3DWithDiam(x=0,y=220,z=0,diameter=3))) cell.morphology.segment_groups.append(neuroml.SegmentGroup(id="apical_dend", neuro_lex_id="sao864921383", members=[neuroml.Member("3"),neuroml.Member("4")])) cell.morphology.segment_groups.append(neuroml.SegmentGroup(id="somatic",includes=[neuroml.Include("soma")])) cell.morphology.segment_groups.append(neuroml.SegmentGroup(id="axonal", includes=[neuroml.Include("axon")])) sg = neuroml.SegmentGroup(id="basal", includes=[neuroml.Include("basal_dend")]) sg.inhomogeneous_parameters.append(neuroml.InhomogeneousParameter(id="PathLengthOver_"+"basal", variable="x", metric="Path Length from root", proximal=neuroml.ProximalDetails(translation_start="0"))) cell.morphology.segment_groups.append(sg) sg = neuroml.SegmentGroup(id="apical", includes=[neuroml.Include("apical_dend")]) sg.inhomogeneous_parameters.append(neuroml.InhomogeneousParameter(id="PathLengthOver_"+"apical", variable="x", metric="Path Length from root", proximal=neuroml.ProximalDetails(translation_start="0"))) cell.morphology.segment_groups.append(sg) nml_filename = 'test/%s.cell.nml' % firing_type neuroml.writers.NeuroMLWriter.write(nml_doc, nml_filename) logging.debug("Written cell file to: %s", nml_filename) neuroml.utils.validate_neuroml2(nml_filename) conc_mod_file = open('test/concentrationModel.txt','w') conc_mod_file.write(concentrationModels) conc_mod_file.close()