# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #------------------------------------------------------------------------------- from neurounits import NeuroUnitParser import quantities as pq from neurounits.writers.writer_ast_to_simulatable_object import EqnSimulator es = NeuroUnitParser.EqnSet(""" EQNSET van_de_pol { x' = mu * (x-(x**3)/3 - y) * {10ms-1} y' = x/mu * {10ms-1} #mu = 6.0 <=> PARAMETER mu <=> INITIAL x:0 <=> INITIAL y:0 } """) #SimulateEquations(es) #es.to_redoc().to_pdf(filename="/home/michael/Desktop//out1.pdf") import numpy as np evaluator = EqnSimulator(es, ) one = es.library_manager.backend.Quantity(1.0, es.library_manager.backend.Unit()) six = es.library_manager.backend.Quantity(6.0,
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #------------------------------------------------------------------------------- from neurounits import NeuroUnitParser import quantities as pq from neurounits.writers.writer_ast_to_simulatable_object import EqnSimulator es = NeuroUnitParser.EqnSet(""" EQNSET lorenz { x' = (sigma*(y-x)) * {1s-1} y' = (x*( rho-z)-y) * {1s-1} z' = (x*y-beta*z) * {1s-1} sigma = 1000 beta = 8/3 rho= 28 <=> OUTPUT x:(), y:(), z:() <=> INITIAL x:1.0 <=> INITIAL y:1.0 <=> INITIAL z:1.0 } """) #SimulateEquations(es) #es.to_redoc().to_pdf(filename="/home/michael/Desktop//out1.pdf") import numpy as np evaluator = EqnSimulator(es, )
def build_eqnset(chlmlinfo, eqnsetname=None): assert type(chlmlinfo) == ChannelMLInfo if not eqnsetname: eqnsetname = chlmlinfo.name unit_mode = { 'Physiological Units': NeuroMLUnitsMode.Physiological, "SI Units": NeuroMLUnitsMode.SI }[chlmlinfo.units] # Some sanity checks on what is supported: # ------------------------------------------ # Check we are dealing with an IV law, not an IAF type mechanism: if chlmlinfo.iv_cond_law and chlmlinfo.iv_cond_law != 'ohmic': raise NeuroUnitsImportNeuroMLNotImplementedException( "Non-IV Cond law-type Channels") if chlmlinfo.iv_default_gmax is None or chlmlinfo.iv_default_erev is None: raise NeuroUnitsImportNeuroMLNotImplementedException( "Can't find default reversal potentials/conductances") if chlmlinfo.unsupported_tags is not None: raise NeuroUnitsImportNeuroMLNotImplementedException( chlmlinfo.unsupported_tags) if chlmlinfo.parameters: raise NeuroUnitsImportNeuroMLNotImplementedException( "Can't deal with parameters") neuroml_dt = { NeuroMLUnitsMode.Physiological: "{1ms}", NeuroMLUnitsMode.SI: "{1s}", }[unit_mode] neuroml_v_unit = { NeuroMLUnitsMode.Physiological: "{1mV}", NeuroMLUnitsMode.SI: "{1V}", }[unit_mode] eqns = [] # Build the Q10 Info: q10gateadjustments, q10_eqns = build_gate_q10_settings_dict(chlmlinfo) eqns.extend(q10_eqns) # Build the conductance and current equations: eqns.append('%s = %s * %s' % (Name.Conductance, Name.OpenConductance, Name.PropGatesOpen)) eqns.append('%s = %s * ( (%s) - (%s) ) ' % (Name.MembraneCurrent, Name.Conductance, Name.Voltage, Name.ReversalPotential)) gate_prop_names = dict([(gate, '%s' % gate.name) for gate in chlmlinfo.gates]) gate_prop_terms = dict([ (gate, '*'.join([gate_prop_names[gate]] * gate.instances)) for gate in chlmlinfo.gates ]) if gate_prop_terms: eqns.append('%s = %s' % (Name.PropGatesOpen, '*'.join(gate_prop_terms.values()))) else: eqns.append('%s = 1.0' % (Name.PropGatesOpen)) # Build Eqns for individual gates: for g in chlmlinfo.gates: q10tempadjustmentName = q10gateadjustments.get( g.name, q10gateadjustments[None]) # Gate with alpha/beta rate variables specified: if g.transitions: gate_eqns = _build_gate_alphabetainftau( g=g, q10tempadjustmentName=q10tempadjustmentName, neuroml_dt=neuroml_dt) eqns.extend(gate_eqns) # Gate specified as an inf-tau value: elif len(g.time_courses) == 1 and len(g.steady_states) == 1: gate_eqns = _build_gate_inftau( g=g, q10tempadjustmentName=q10tempadjustmentName, neuroml_dt=neuroml_dt) eqns.extend(gate_eqns) else: raise NeuroUnitsImportNeuroMLNotImplementedException( 'Non-Standard Gate/Transtions') #Voltage Offsets: vOffset = None if chlmlinfo.offset: vOffset = "( %f * %s )" % (chlmlinfo.offset, neuroml_v_unit) vOffsetTerm = "-%s" % vOffset if vOffset is not None else "" # OK, use regular expressions to remap variables # to the variable with the unit: remaps = [ lambda e: re.sub(r"""\bcelsius\b""", "(celsius/{1K})", e), lambda e: re.sub(r"""\b__VGate__\b""", "((V %s)/%s)" % (vOffsetTerm, neuroml_v_unit), e), lambda e: re.sub(r"""\b__V__\b""", "(V)", e), ] # Apply the remappings: for r in remaps: eqns = [r(e) for e in eqns] io_string = Template(""" <=> PARAMETER $OpenConductance : (S/m2) <=> PARAMETER $ReversalPotential : (mV) <=> OUTPUT $MembraneCurrent :(A/m2) METADATA {"mf":{"role":"TRANSMEMBRANECURRENT"} } <=> INPUT V:(V) METADATA {"mf":{"role":"MEMBRANEVOLTAGE"} } <=> INPUT $Temperature :(K) METADATA {"mf":{"role":"TEMPERATURE"} } """ ).substitute(**Name.__dict__) import_string = """ from std.math import exp from std.math import pow from std.math import fabs """ neuroEqn = """ eqnset %s{ %s %s %s }""" % (eqnsetname, import_string, "\n\t\t".join(eqns), io_string) neuroEqn = "\n".join([l for l in neuroEqn.split("\n") if l.strip()]) options = NeuroUnitParserOptions( allow_unused_parameter_declarations=True, allow_unused_suppliedvalue_declarations=True) eqnset = NeuroUnitParser.EqnSet(text=neuroEqn, options=options) default_params = { NeuroMLUnitsMode.Physiological: { "GMAX": NeuroUnitParser.QuantitySimple("%s mS/cm2" % (chlmlinfo.iv_default_gmax)), "VREV": NeuroUnitParser.QuantitySimple("%s mV" % (chlmlinfo.iv_default_erev)), }, NeuroMLUnitsMode.SI: { "GMAX": NeuroUnitParser.QuantitySimple("%s S/m2" % (chlmlinfo.iv_default_gmax)), "VREV": NeuroUnitParser.QuantitySimple("%s V" % (chlmlinfo.iv_default_erev)), }, }[unit_mode] return eqnset, default_params