def VisitLibrary(self, o, **kwargs): removed = [] for aKey in o._eqn_assignment.keys(): a = o._eqn_assignment[aKey] alhs = a.lhs fixed_value = self.visit(a.rhs) if fixed_value: sym_suffix = '_as_symconst' sym_suffix = '' s = ast.SymbolicConstant(symbol=aKey.symbol + sym_suffix, value=fixed_value) #assert False #print 'Replacing Node:', a.lhs.symbol ReplaceNode(a.lhs, s).visit(o) #o._cache_nodes() #print 'Done replacing symbol' o._symbolicconstants[aKey.symbol] = s del o._eqn_assignment[aKey] removed.append(alhs) # Double check they have gone: for a in removed: nc = EqnsetVisitorNodeCollector() nc.visit(o) assert not a in nc.all() # Should be no more assignments: assert len(o._eqn_assignment) == 0
def _res_assignments(self, o, **kwargs): removed = [] for aKey in o._eqn_assignment.keys(): a = o._eqn_assignment[aKey] alhs = a.lhs fixed_value = self.visit(a.rhs_map) if fixed_value: sym_suffix = '_as_symconst' sym_suffix = '' s = ast.SymbolicConstant(symbol=aKey.symbol + sym_suffix, value=fixed_value) #ReplaceNode(a.lhs, s).visit(o) ReplaceNode.replace_and_check(srcObj=a.lhs, dstObj=s, root = o) o._symbolicconstants[aKey.symbol] = s from neurounits.misc import SeqUtils old_ass = SeqUtils.filter_expect_single( o._eqn_assignment, lambda o:o.symbol == aKey.symbol ) del o._eqn_assignment[ old_ass ] #o.get_terminal_obj(aKey.symbol) ] #del o._eqn_assignment[ o.get_terminal_obj(aKey.symbol) ] removed.append(alhs) # Double check they have gone: for a in removed: nc = EqnsetVisitorNodeCollector() nc.visit(o) assert not a in nc.all()
def _cache_nodes(self): t = EqnsetVisitorNodeCollector() t.visit(self) self._parameters = t.nodes[Parameter] self._supplied_values = t.nodes[SuppliedValue]
def InferFromEqnset(cls, eqnset): currents = {} supplied_values = {} for io_info in [io_info for io_info in eqnset.io_data if io_info.iotype in (IOType.Output, IOType.Input)]: if not io_info.metadata or not 'mf' in io_info.metadata: continue role = io_info.metadata['mf'].get('role', None) if role: if not eqnset.has_terminal_obj(io_info.symbol): continue obj = eqnset.get_terminal_obj(io_info.symbol) # Outputs: if role == "TRANSMEMBRANECURRENT": assert io_info.iotype== IOType.Output currents[obj] = NeuronMembraneCurrent( obj=obj, symbol=obj.symbol) # Inputs (Supplied Values): elif role == "MEMBRANEVOLTAGE": assert io_info.iotype== IOType.Input supplied_values[obj] = NeuronSuppliedValues.MembraneVoltage elif role == "TIME": assert io_info.iotype== IOType.Input supplied_values[obj] = NeuronSuppliedValues.Time elif role == "TEMPERATURE": assert io_info.iotype== IOType.Input supplied_values[obj] = NeuronSuppliedValues.Temperature else: assert False if not currents: raise ValueError('Mechanism does not expose any currents! %s'% eqnset.name) # PointProcess or Distributed Process: mech_type = currents.values()[0].getCurrentType() for c in currents.values(): assert mech_type == c.getCurrentType(), 'Mixed Current types [Distributed/Point]' # Work out the units of all the terminal symbols: symbol_units = {} objs = eqnset.terminal_symbols n = EqnsetVisitorNodeCollector() n.visit(eqnset) objs = n.all() for s in objs: if s in currents: symbol_units[s] = NEURONMappings.current_units[mech_type] elif s in supplied_values: t = supplied_values[s] if t in NeuronSuppliedValues.All: symbol_units[s] = NEURONMappings.supplied_value_units[t] #NeuroUnitParser.Unit("mV") else: print 'Unknown supplied value:', t assert False else: if isinstance(s,(EqnTimeDerivativeByRegime, EqnAssignmentByRegime, EqnSet, ConstValue)): continue if isinstance(s,(InEquality, OnEvent)): continue symbol_units[s] = s.get_dimension() # Event Handling: assert False, 'Deprecated, needs rewrite' zero_arg_events = [ev for ev in eqnset.onevents if len(ev.parameters) == 0] if len(zero_arg_events) == 0: event_function = None elif len(zero_arg_events) == 1: event_function= zero_arg_events[0] else: raise ValueError('Multiple Zero-Param Events') return MODLBuildParameters(mechanismtype=mech_type, currents=currents, supplied_values=supplied_values, suffix="nmmodl"+eqnset.name, symbol_units=symbol_units, event_function=event_function )
def InferFromEqnset(cls, eqnset): currents = {} supplied_values = {} for io_info in [ io_info for io_info in eqnset.io_data if io_info.iotype in (IOType.Output, IOType.Input) ]: if not io_info.metadata or not 'mf' in io_info.metadata: continue role = io_info.metadata['mf'].get('role', None) if role: if not eqnset.has_terminal_obj(io_info.symbol): continue obj = eqnset.get_terminal_obj(io_info.symbol) # Outputs: if role == "TRANSMEMBRANECURRENT": assert io_info.iotype == IOType.Output currents[obj] = NeuronMembraneCurrent(obj=obj, symbol=obj.symbol) # Inputs (Supplied Values): elif role == "MEMBRANEVOLTAGE": assert io_info.iotype == IOType.Input supplied_values[obj] = NeuronSuppliedValues.MembraneVoltage elif role == "TIME": assert io_info.iotype == IOType.Input supplied_values[obj] = NeuronSuppliedValues.Time elif role == "TEMPERATURE": assert io_info.iotype == IOType.Input supplied_values[obj] = NeuronSuppliedValues.Temperature else: assert False if not currents: raise ValueError('Mechanism does not expose any currents! %s' % eqnset.name) # PointProcess or Distributed Process: mech_type = currents.values()[0].getCurrentType() for c in currents.values(): assert mech_type == c.getCurrentType( ), 'Mixed Current types [Distributed/Point]' # Work out the units of all the terminal symbols: symbol_units = {} objs = eqnset.terminal_symbols n = EqnsetVisitorNodeCollector() n.visit(eqnset) objs = n.all() for s in objs: if s in currents: symbol_units[s] = NEURONMappings.current_units[mech_type] elif s in supplied_values: t = supplied_values[s] if t in NeuronSuppliedValues.All: symbol_units[s] = NEURONMappings.supplied_value_units[ t] #NeuroUnitParser.Unit("mV") else: print 'Unknown supplied value:', t assert False else: if isinstance(s, (EqnTimeDerivativeByRegime, EqnAssignmentByRegime, EqnSet, ConstValue)): continue if isinstance(s, (InEquality, OnEvent)): continue symbol_units[s] = s.get_dimension() # Event Handling: assert False, 'Deprecated, needs rewrite' zero_arg_events = [ ev for ev in eqnset.onevents if len(ev.parameters) == 0 ] if len(zero_arg_events) == 0: event_function = None elif len(zero_arg_events) == 1: event_function = zero_arg_events[0] else: raise ValueError('Multiple Zero-Param Events') return MODLBuildParameters(mechanismtype=mech_type, currents=currents, supplied_values=supplied_values, suffix="nmmodl" + eqnset.name, symbol_units=symbol_units, event_function=event_function)
def _cache_nodes(self): t = EqnsetVisitorNodeCollector() t.visit(self) self._parameters = t.nodes[Parameter] self._supplied_values = t.nodes[SuppliedValue]
def all_ast_nodes(self): from neurounits.visitors.common.terminal_node_collector import EqnsetVisitorNodeCollector c = EqnsetVisitorNodeCollector() c.visit(self) return itertools.chain(*c.nodes.values())
def all_ast_nodes(self): from neurounits.visitors.common.terminal_node_collector import EqnsetVisitorNodeCollector c = EqnsetVisitorNodeCollector() c.visit(self) return itertools.chain( *c.nodes.values() )