def get_evset(self, obj): # Get the results by string: if isinstance(obj, basestring): return SeqUtils.filter_expect_single( self.evsets, lambda s: s.name == obj) else: assert False
def get_trace(self, obj): # Get the results by string: if isinstance(obj, basestring): return SeqUtils.filter_expect_single( self.traces, lambda s: s.name == obj) # Get the results by Recordable object: else: return self.get_trace(obj.name)
def get_cell(self, cellname=None): """ Either return a cell by name if there is more than one cell, otherwise the single cell """ if cellname: return SeqUtils.filter_expect_single(self.cells, lambda s: s.name == cellname) else: return SeqUtils.expect_single(self.cells)
def get_trace(self, obj): # Get the results by string: if isinstance(obj, basestring): return SeqUtils.filter_expect_single(self.traces, lambda s: s.name == obj) # Get the results by Recordable object: else: return self.get_trace(obj.name)
def record_from_mechanism(sim, mechanism_name, cell_location, what, on_error_skip=False, user_tags = None, **kwargs): user_tags = user_tags or [] assert on_error_skip == False mechs = cell_location.cell.get_biophysics().get_all_mechanisms_applied_to_cell() chl = SeqUtils.filter_expect_single(seq=mechs, filter_func=lambda m:m.name==mechanism_name) r = sim.record(chl, what=what, cell_location=cell_location, user_tags = user_tags + [chl.name], **kwargs) return r
def load_cell(cls, cellNode, regions=None): cableIDToRegionName, segmentListInfo = cls.load_cell_dictionaries(cellNode, regions=regions) # Make all the regions: regionNames = list(set(Filter(cableIDToRegionName.values(), lambda e: e is not None))) print "RegionNames:", regionNames regionNamesClean = [_clean_name(str(region_name)) for region_name in regionNames] rgns = [Region(name=region_name) for region_name in regionNamesClean] region_nameToRegionDict = dict([(rgn.name, rgn) for rgn in rgns]) cableIDToRegionDict = dict( [ (cableId, region_nameToRegionDict[_clean_name(region_name)]) if region_name is not None else (cableId, None) for cableId, region_name in cableIDToRegionName.iteritems() ] ) # Find the node without a parent: rN = SeqUtils.filter_expect_single(segmentListInfo.values(), lambda s: not s[3]) # Recursively Construct by finding what should be attached to current structure: # (id, name, cable, parent, (px, py, pz, pDiam), (dx, dy, dz, dDiam)) # rDummy = Section(region=cableIDToRegionDict[rN[2]], x=rN[4][0], y=rN[4][1], z=rN[4][2], r=rN[4][3] / 2.0) rDummy = Section(region=None, x=rN[4][0], y=rN[4][1], z=rN[4][2], r=rN[4][3] / 2.0) rActual = rDummy.create_distal_section( region=cableIDToRegionDict[rN[2]], x=rN[5][0], y=rN[5][1], z=rN[5][2], r=rN[5][3] / 2.0, idtag=rN[1] ) idToSectionMap = {rN[0]: rActual} recentlyAdded = [rN[0]] while recentlyAdded != []: curNode = recentlyAdded[0][0] curSect = idToSectionMap[curNode] recentlyAdded = recentlyAdded[1:] childNodes = Filter(segmentListInfo.values(), lambda s: s[3] == curNode) for c in childNodes: # print c newSect = curSect.create_distal_section( region=cableIDToRegionDict[c[2]], x=c[5][0], y=c[5][1], z=c[5][2], r=c[5][3] / 2.0, idtag=c[1] ) idToSectionMap[c[0]] = newSect recentlyAdded.append(c) return MorphologyTree(name="FromNeuroML", dummysection=rDummy, metadata={})
def load(cls, neuroMLFileObj, regions=None): """regions is a dictionary, which maps cable-groups to a Region name; this handles the case when there are multiple group tags in a MorphML document, since morphforge does not allow multiple regions' """ doc = XML.parse(neuroMLFileObj).documentElement assert doc.tagName in ('morphml', 'neuroml') # Do the action: cellsNode = SeqUtils.filter_expect_single( doc.childNodes, filter_func=isElementWithTag("cells")) morphs = [] for cellNode in Filter(cellsNode.childNodes, isElementWithTag("cell")): morph = cls.load_cell(cellNode, regions=regions) morphs.append(morph) assert len(morphs) == 1 return morphs[0]
def load(cls, src, regions=None): """regions is a dictionary, which maps cable-groups to a Region name; this handles the case when there are multiple group tags in a MorphML document, since morphforge does not allow multiple regions' """ doc = XML.parse(src).documentElement assert doc.tagName in ("morphml", "neuroml") # Do the action: cellsNode = SeqUtils.filter_expect_single(doc.childNodes, filter_func=isElementWithTag("cells")) morphs = [] for cellNode in Filter(cellsNode.childNodes, isElementWithTag("cell")): morph = cls.load_cell(cellNode, regions=regions) morphs.append(morph) assert len(morphs) == 1 return morphs[0]
def record_from_mechanism(sim, mechanism_name, cell_location, what, on_error_skip=False, user_tags=None, **kwargs): user_tags = user_tags or [] assert on_error_skip == False mechs = cell_location.cell.get_biophysics( ).get_all_mechanisms_applied_to_cell() chl = SeqUtils.filter_expect_single( seq=mechs, filter_func=lambda m: m.name == mechanism_name) r = sim.record(chl, what=what, cell_location=cell_location, user_tags=user_tags + [chl.name], **kwargs) return r
def FilterExpectSingleChildrenByTag(node, tag): return SeqUtils.filter_expect_single(node.childNodes, isElementWithTag(tag))
def load_cell_dictionaries(self, cellNode, regions=None): l1 = Level1NeuroMLRepresentation(cellNode) print 'CellName:', cellNode.getAttribute('name') # We are not too worried about cables, but we do need the region name out of them: cableIDToRegionName = {} cablesNode = SeqUtils.filter_expect_single(cellNode.childNodes, isElementWithTag("cables")) for cableNode in Filter(cablesNode.childNodes, isElementWithTag("cable")): id = cableNode.getAttribute("id") name = cableNode.getAttribute("name") group_nodes = Filter(cableNode.childNodes, isElementWithTag("group")) if group_nodes: if regions: metaGroupNode = SeqUtils.filter_expect_single( group_nodes, lambda e: get_text(e) in regions) region_name = regions[get_text(metaGroupNode)] else: metaGroupNode = SeqUtils.expect_single(group_nodes) region_name = get_text(metaGroupNode) assert not id in cableIDToRegionName cableIDToRegionName[id] = region_name else: cableIDToRegionName[id] = None pass print 'Loaded Cable: ', id, name # Load the segments: segmentListInfo = { } # id -> (id, name, cable, parent, (px, py, pz, pDiam), (dx, dy, dz, dDiam)) segmentsNode = SeqUtils.filter_expect_single( cellNode.childNodes, isElementWithTag("segments")) for segNode in Filter(segmentsNode.childNodes, isElementWithTag("segment")): print "Segment" id = segNode.getAttribute("id") name = segNode.getAttribute("name") cable = segNode.getAttribute("cable") parent = segNode.getAttribute("parent") # Every point should have a distal End: dNode = SeqUtils.filter_expect_single(segNode.childNodes, isElementWithTag("distal")) d_x, d_y, d_z = dNode.getAttribute("x"), dNode.getAttribute( "y"), dNode.getAttribute("z") d_diam = dNode.getAttribute("diameter") if not parent: pass pNode = SeqUtils.filter_expect_single( segNode.childNodes, isElementWithTag("proximal")) p_x, p_y, p_z = pNode.getAttribute("x"), pNode.getAttribute( "y"), pNode.getAttribute("z") p_diam = pNode.getAttribute("diameter") else: # Not every point need have a proximal end, we look at the parent in case it has # both and check the locations agree parent_Dist_Loc = segmentListInfo[parent][5] pNode = Filter(segNode.childNodes, isElementWithTag("proximal")) if len(pNode) == 0: p_x, p_y, p_z, p_diam = parent_Dist_Loc elif len(pNode) == 1: p_xR, p_yR, p_zR = pNode[0].getAttribute("x"), pNode[ 0].getAttribute("y"), pNode[0].getAttribute("z") p_diamR = pNode[0].getAttribute("diameter") # I do not understand MorphML to understand why these checks do not fail.... #print (p_xR, p_yR, p_zR) #print (parent_Dist_Loc[0], parent_Dist_Loc[1], parent_Dist_Loc[2]) #assert (p_xR, p_yR, p_zR) == (parent_Dist_Loc[0], parent_Dist_Loc[1], parent_Dist_Loc[2]) # In this case, use the diameter just read: p_x, p_y, p_z = p_xR, p_yR, p_zR p_diam = p_diamR else: assert False print id, name, cable, parent, (p_x, p_y, p_z, p_diam), (d_x, d_y, d_z, d_diam) infTuple = (id, name, cable, parent, (float(p_x), float(p_y), float(p_z), float(p_diam)), (float(d_x), float(d_y), float(d_z), float(d_diam))) assert not id in segmentListInfo segmentListInfo[id] = infTuple # Now we have read the file and created the dictionaries: return (cableIDToRegionName, segmentListInfo)
def load_cell(cls, cellNode, regions=None): cableIDToRegionName, segmentListInfo = cls.load_cell_dictionaries( cellNode, regions=regions) # Make all the regions: regionNames = list( set(Filter(cableIDToRegionName.values(), lambda e: e is not None))) print 'RegionNames:', regionNames regionNamesClean = [ _clean_name(str(region_name)) for region_name in regionNames ] rgns = [Region(name=region_name) for region_name in regionNamesClean] region_nameToRegionDict = dict([(rgn.name, rgn) for rgn in rgns]) cableIDToRegionDict = dict([ (cableId, region_nameToRegionDict[_clean_name(region_name)]) if region_name is not None else (cableId, None) for cableId, region_name in cableIDToRegionName.iteritems() ]) # Find the node without a parent: rN = SeqUtils.filter_expect_single(segmentListInfo.values(), lambda s: not s[3]) #Recursively Construct by finding what should be attached to current structure: #(id, name, cable, parent, (px, py, pz, pDiam), (dx, dy, dz, dDiam)) #rDummy = Section(region=cableIDToRegionDict[rN[2]], x=rN[4][0], y=rN[4][1], z=rN[4][2], r=rN[4][3] / 2.0) rDummy = Section(region=None, x=rN[4][0], y=rN[4][1], z=rN[4][2], r=rN[4][3] / 2.0) rActual = rDummy.create_distal_section( region=cableIDToRegionDict[rN[2]], x=rN[5][0], y=rN[5][1], z=rN[5][2], r=rN[5][3] / 2.0, idtag=rN[1]) idToSectionMap = {rN[0]: rActual} recentlyAdded = [rN[0]] while recentlyAdded != []: curNode = recentlyAdded[0][0] curSect = idToSectionMap[curNode] recentlyAdded = recentlyAdded[1:] childNodes = Filter(segmentListInfo.values(), lambda s: s[3] == curNode) for c in childNodes: #print c newSect = curSect.create_distal_section( region=cableIDToRegionDict[c[2]], x=c[5][0], y=c[5][1], z=c[5][2], r=c[5][3] / 2.0, idtag=c[1]) idToSectionMap[c[0]] = newSect recentlyAdded.append(c) return MorphologyTree(name='FromNeuroML', dummysection=rDummy, metadata={})
def __init__(self, cellNode): self.cables = SearchableSet() self.cablegroups = SearchableSet() self.segments = SearchableSet() # Read the XML: # Start with the declared Cables: cablesNode = FilterExpectSingleChildrenByTag(cellNode, 'cables') # Load the individual cable objects for cableNode in FilterChildrenByTag(cablesNode, "cable"): if cableNode.getAttribute('id') in self.cables.keys('cable_id'): continue cable = NeuroMLCable(cable_id=cableNode.getAttribute('id'), name=cablesNode.getAttribute('name')) self.cables.add(cable) # Does the cable belong to any groups? for group_node in Filter(cableNode.childNodes, isElementWithTag("group")): cablegroupname = get_text(group_node) if not cablegroupname in self.cablegroups.keys('name'): self.cablegroups.add( NeuroMLCableGroup(name=cablegroupname)) self.cablegroups.getitem(name=cablegroupname).add_cable(cable) # Load the CableGroup Objects: for cableGroupNode in Filter(cablesNode.childNodes, isElementWithTag("cablegroup")): # Get, or create, the cable node: cablegroupname = cableGroupNode.getAttribute('name') if not cablegroupname in self.cablegroups.keys('name'): self.cablegroups.add(NeuroMLCableGroup(name=cablegroupname)) grp = self.cablegroups.getitem(name=cablegroupname) # Add the cable segments: for cableNode in Filter(cableGroupNode.childNodes, isElementWithTag("cable")): cableNodeID = cableNode.getAttribute('id') if not cableNodeID in self.cables.keys('cable_id'): assert False cable = self.cables.getitem(cable_id=cableNodeID) grp.add_cable(cable) # Load the Segments Objects: segmentsNode = SeqUtils.filter_expect_single( cellNode.childNodes, isElementWithTag("segments")) for segmentNode in Filter(segmentsNode.childNodes, isElementWithTag("segment")): #Attributes: segment_id = segmentNode.getAttribute('id') parent_id = segmentNode.getAttribute('parent') cable_id = segmentNode.getAttribute('cable') cable = self.cables.getitem( cable_id=cable_id) if cable_id else None # Distal Information: distalNode = FilterExpectSingleChildrenByTag(segmentNode, 'distal') d_X, d_Y, d_Z, d_d = float(distalNode.getAttribute('x')), float( distalNode.getAttribute('y')), float( distalNode.getAttribute('z')), float( distalNode.getAttribute('diameter')) dInfo = (d_X, d_Y, d_Z, d_d) proximalNode = FilterChildrenByTag(segmentNode, 'proximal') # Root node: if not parent_id: assert proximalNode proximalNode = SeqUtils.expect_single(proximalNode) p_X, p_Y, p_Z, p_d = float( proximalNode.getAttribute('x')), float( proximalNode.getAttribute('y')), float( proximalNode.getAttribute('z')), float( proximalNode.getAttribute('diameter')) pInfo = (p_X, p_Y, p_Z, p_d) segment = NeuroMLSegment(segment_id=segment_id, distInfo=dInfo, proxInfo=pInfo, parent=None, cable=cable) #Child Node: else: # Not the root node, but a proximal node is provided: if proximalNode: pInfo = (p_X, p_Y, p_Z, p_d) # Proximal node and NOT the root: if parent_id: parentNode = self.segments.getitem( segment_id=parent_id) parentDistalInfo = parentNode.distInfo eps = 0.01 # assert fabs(parentDistalInfo[0] - pInfo[0]) < eps # assert fabs(parentDistalInfo[1] - pInfo[1]) < eps # assert fabs(parentDistalInfo[2] - pInfo[2]) < eps # assert fabs(parentDistalInfo[3] - pInfo[3]) < eps # Add the child node: segment = NeuroMLSegment(segment_id=segment_id, distInfo=dInfo, proxInfo=pInfo, parent=None, cable=cable) # Add the segment to groups? self.segments.add(segment)
def load_cell_dictionaries(self, cellNode, regions=None): l1 = Level1NeuroMLRepresentation(cellNode) print "CellName:", cellNode.getAttribute("name") # We are not too worried about cables, but we do need the region name out of them: cableIDToRegionName = {} cablesNode = SeqUtils.filter_expect_single(cellNode.childNodes, isElementWithTag("cables")) for cableNode in Filter(cablesNode.childNodes, isElementWithTag("cable")): id = cableNode.getAttribute("id") name = cableNode.getAttribute("name") group_nodes = Filter(cableNode.childNodes, isElementWithTag("group")) if group_nodes: if regions: # for n in group_nodes: # print get_text(n) metaGroupNode = SeqUtils.filter_expect_single(group_nodes, lambda e: get_text(e) in regions) region_name = regions[get_text(metaGroupNode)] else: metaGroupNode = SeqUtils.expect_single(group_nodes) region_name = get_text(metaGroupNode) assert not id in cableIDToRegionName cableIDToRegionName[id] = region_name else: cableIDToRegionName[id] = None pass print "Loaded Cable: ", id, name # Load the segments: segmentListInfo = {} # id -> (id, name, cable, parent, (px, py, pz, pDiam), (dx, dy, dz, dDiam)) segmentsNode = SeqUtils.filter_expect_single(cellNode.childNodes, isElementWithTag("segments")) for segNode in Filter(segmentsNode.childNodes, isElementWithTag("segment")): print "Segment" id = segNode.getAttribute("id") name = segNode.getAttribute("name") cable = segNode.getAttribute("cable") parent = segNode.getAttribute("parent") # Every point should have a distal End: dNode = SeqUtils.filter_expect_single(segNode.childNodes, isElementWithTag("distal")) d_x, d_y, d_z = dNode.getAttribute("x"), dNode.getAttribute("y"), dNode.getAttribute("z") d_diam = dNode.getAttribute("diameter") if not parent: pass pNode = SeqUtils.filter_expect_single(segNode.childNodes, isElementWithTag("proximal")) p_x, p_y, p_z = pNode.getAttribute("x"), pNode.getAttribute("y"), pNode.getAttribute("z") p_diam = pNode.getAttribute("diameter") else: # Not every point need have a proximal end, we look at the parent in case it has # both and check the locations agree parent_Dist_Loc = segmentListInfo[parent][5] pNode = Filter(segNode.childNodes, isElementWithTag("proximal")) if len(pNode) == 0: p_x, p_y, p_z, p_diam = parent_Dist_Loc elif len(pNode) == 1: p_xR, p_yR, p_zR = ( pNode[0].getAttribute("x"), pNode[0].getAttribute("y"), pNode[0].getAttribute("z"), ) p_diamR = pNode[0].getAttribute("diameter") # I do not understand MorphML to understand why these checks do not fail.... # print (p_xR, p_yR, p_zR) # print (parent_Dist_Loc[0], parent_Dist_Loc[1], parent_Dist_Loc[2]) # assert (p_xR, p_yR, p_zR) == (parent_Dist_Loc[0], parent_Dist_Loc[1], parent_Dist_Loc[2]) # In this case, use the diameter just read: p_x, p_y, p_z = p_xR, p_yR, p_zR p_diam = p_diamR else: assert False print id, name, cable, parent, (p_x, p_y, p_z, p_diam), (d_x, d_y, d_z, d_diam) infTuple = ( id, name, cable, parent, (float(p_x), float(p_y), float(p_z), float(p_diam)), (float(d_x), float(d_y), float(d_z), float(d_diam)), ) assert not id in segmentListInfo segmentListInfo[id] = infTuple # Now we have read the file and created the dictionaries: return (cableIDToRegionName, segmentListInfo)
def __init__(self, cellNode): self.cables = SearchableSet() self.cablegroups = SearchableSet() self.segments = SearchableSet() # Read the XML: # Start with the declared Cables: cablesNode = FilterExpectSingleChildrenByTag(cellNode, "cables") # Load the individual cable objects for cableNode in FilterChildrenByTag(cablesNode, "cable"): if cableNode.getAttribute("id") in self.cables.keys("cable_id"): continue cable = NeuroMLCable(cable_id=cableNode.getAttribute("id"), name=cablesNode.getAttribute("name")) self.cables.add(cable) # Does the cable belong to any groups? for group_node in Filter(cableNode.childNodes, isElementWithTag("group")): cablegroupname = get_text(group_node) if not cablegroupname in self.cablegroups.keys("name"): self.cablegroups.add(NeuroMLCableGroup(name=cablegroupname)) self.cablegroups.getitem(name=cablegroupname).add_cable(cable) # Load the CableGroup Objects: for cableGroupNode in Filter(cablesNode.childNodes, isElementWithTag("cablegroup")): # Get, or create, the cable node: cablegroupname = cableGroupNode.getAttribute("name") if not cablegroupname in self.cablegroups.keys("name"): self.cablegroups.add(NeuroMLCableGroup(name=cablegroupname)) grp = self.cablegroups.getitem(name=cablegroupname) # Add the cable segments: for cableNode in Filter(cableGroupNode.childNodes, isElementWithTag("cable")): cableNodeID = cableNode.getAttribute("id") if not cableNodeID in self.cables.keys("cable_id"): assert False cable = self.cables.getitem(cable_id=cableNodeID) grp.add_cable(cable) # Load the Segments Objects: segmentsNode = SeqUtils.filter_expect_single(cellNode.childNodes, isElementWithTag("segments")) for segmentNode in Filter(segmentsNode.childNodes, isElementWithTag("segment")): # Attributes: segment_id = segmentNode.getAttribute("id") parent_id = segmentNode.getAttribute("parent") cable_id = segmentNode.getAttribute("cable") cable = self.cables.getitem(cable_id=cable_id) if cable_id else None # Distal Information: distalNode = FilterExpectSingleChildrenByTag(segmentNode, "distal") d_X, d_Y, d_Z, d_d = ( float(distalNode.getAttribute("x")), float(distalNode.getAttribute("y")), float(distalNode.getAttribute("z")), float(distalNode.getAttribute("diameter")), ) dInfo = (d_X, d_Y, d_Z, d_d) proximalNode = FilterChildrenByTag(segmentNode, "proximal") # Root node: if not parent_id: assert proximalNode proximalNode = SeqUtils.expect_single(proximalNode) p_X, p_Y, p_Z, p_d = ( float(proximalNode.getAttribute("x")), float(proximalNode.getAttribute("y")), float(proximalNode.getAttribute("z")), float(proximalNode.getAttribute("diameter")), ) pInfo = (p_X, p_Y, p_Z, p_d) segment = NeuroMLSegment( segment_id=segment_id, distInfo=dInfo, proxInfo=pInfo, parent=None, cable=cable ) # Child Node: else: # Not the root node, but a proximal node is provided: if proximalNode: pInfo = (p_X, p_Y, p_Z, p_d) # Proximal node and NOT the root: if parent_id: parentNode = self.segments.getitem(segment_id=parent_id) parentDistalInfo = parentNode.distInfo eps = 0.01 # assert fabs(parentDistalInfo[0] - pInfo[0]) < eps # assert fabs(parentDistalInfo[1] - pInfo[1]) < eps # assert fabs(parentDistalInfo[2] - pInfo[2]) < eps # assert fabs(parentDistalInfo[3] - pInfo[3]) < eps # Add the child node: segment = NeuroMLSegment( segment_id=segment_id, distInfo=dInfo, proxInfo=pInfo, parent=None, cable=cable ) # Add the segment to groups? self.segments.add(segment)
def get_trace_by_tagselection(self, ts): import morphforge selector = morphforge.stdimports.TagSelector.from_string(ts) return SeqUtils.filter_expect_single(self.traces, selector)