Module contents

class nmodl.NmodlDriver

Bases: nmodl::parser::NmodlDriver

This is the NmodlDriver class documentation

get_ast(self: nmodl._nmodl.nmodl::parser::NmodlDriver) nmodl::ast::Program

Get ast

Returns:

Instance of Program

parse_file(self: nmodl._nmodl.NmodlDriver, filename: str) nmodl::ast::Program

Parse NMODL provided as a file

Parameters:

filename (str) – name of the C file

Returns:

ast root node if success, throws an exception otherwise

Return type:

AST

parse_stream(self: nmodl._nmodl.NmodlDriver, in: object) nmodl::ast::Program

Parse NMODL file provided as istream

Parameters:

in (file) – ifstream object

Returns:

ast root node if success, throws an exception otherwise

Return type:

AST

parse_string(self: nmodl._nmodl.NmodlDriver, input: str) nmodl::ast::Program

Parse NMODL provided as a string

Parameters:

input (str) – C code as string

Returns:

ast root node if success, throws an exception otherwise

Return type:

AST

>>> ast = driver.parse_string("DEFINE NSTEP 6")
nmodl.to_json(node: nmodl::ast::Ast, compact: bool = False, expand: bool = False, add_nmodl: bool = False) str

Given AST node, return the JSON string representation

Parameters:
  • node (AST) – AST node

  • compact (bool) – Compact node

  • expand (bool) – Expand node

Returns:

JSON string representation

Return type:

str

>>> ast = driver.parse_string("NEURON{}")
>>> nmodl.to_json(ast, True)
'{"Program":[{"NeuronBlock":[{"StatementBlock":[]}]}]}'
nmodl.to_nmodl(node: nmodl::ast::Ast, exclude_types: set[nmodl::ast::AstNodeType] = set()) str

Given AST node, return the NMODL string representation

Parameters:
  • node (AST) – AST node

  • excludeTypes (set of AstNodeType) – Excluded node types

Returns:

NMODL string representation

Return type:

str

>>> ast = driver.parse_string("NEURON{}")
>>> nmodl.to_nmodl(ast)
'NEURON {\n}\n'

Submodules

nmodl.ast module

Module for vizualization of NMODL abstract syntax trees (ASTs).

nmodl.ast.view(nmodl_ast)[source]

Visualize given NMODL AST in web browser

In memory representation of AST can be converted to JSON form and it can be visualized using AST viewer implemented using d3.js.

Parameters:

nmodl_ast – AST object of nmodl file or string

Returns:

None

nmodl.dsl module

nmodl.dsl.list_examples()[source]

Returns a list of examples available

The NMODL Framework provides a few examples for testing

Returns:

List of available examples

nmodl.dsl.load_example(example)[source]

Load an example from the NMODL examples

The NMODL Framework provides a few examples for testing. The list of examples can be requested using list_examples(). This function then returns the NMODL code of the requested example file.

Parameters:

example – Filename of an example as provided by list_examples()

Returns:

An path to the example as a string

nmodl.ode module

nmodl.ode.differentiate2c(expression, dependent_var, vars, prev_expressions=None, stepsize=0.001)[source]

Analytically differentiate supplied expression, return solution as C code.

Expression should be of the form “f(x)”, where “x” is the dependent variable, and the function returns df(x)/dx

The set vars must contain all variables used in the expression.

Furthermore, if any of these variables are themselves functions that should be substituted before differentiating, they can be supplied in the prev_expressions list. Before differentiating each of these expressions will be substituted into expressions, where possible, in reverse order - i.e. starting from the end of the list.

If the result coincides with one of the vars, or the LHS of one of the prev_expressions, then it is simplified to this expression.

Note that, in order to differentiate against indexed variables (such as x[0]), you must pass an instance of sympy.Indexed to dependent_var (_not_ an instance of sympy.IndexedBase), as well as an instance of sympy.IndexedBase to vars.

Some simple examples of use:

  • nmodl.ode.differentiate2c ("a*x", "x", {"a"}) == "a"

  • differentiate2c ("cos(y) + b*y**2", "y", {"a","b"}) == "Dy = 2*b*y - sin(y)"

  • differentiate2c("a * x[0]", sympy.IndexedBase("x", shape=[1])[0], {"a", sympy.IndexedBase("x", shape=[1])}) == "a"

Parameters:
  • expression – expression to be differentiated e.g. “a*x + b”

  • dependent_var – dependent variable, e.g. “x”

  • vars – set of all other variables used in expression, e.g. {“a”, “b”, “c”}

  • prev_expressions – time-ordered list of preceeding expressions to evaluate & substitute, e.g. [“b = x + c”, “a = 12*b”]

  • stepsize – in case an analytic expression is not possible, finite differences are used; this argument sets the step size

Returns:

string containing analytic derivative of expression (including any substitutions of variables from supplied prev_expressions) w.r.t. dependent_var as C code.

nmodl.ode.discretize_derivative(expr)[source]
nmodl.ode.finite_difference_step_variable(sym)[source]
nmodl.ode.forwards_euler2c(diff_string, dt_var, vars, function_calls)[source]

Return forwards euler solution of diff_string as C code.

Derivative should be of the form “x’ = f(x)”, and vars should contain the set of all the variables referenced by f(x), for example: -forwards_euler2c(“x’ = a*x”, [“a”,”x”]) -forwards_euler2c(“x’ = a + b*x - sin(3.2)”, {“x”,”a”,”b”})

Parameters:
  • diff_string – Derivative to be integrated e.g. “x’ = a*x”

  • dt_var – name of timestep dt variable in NEURON

  • vars – set of variables used in expression, e.g. {“x”, “a”}

  • function_calls – set of function calls used in the ODE

Returns:

String containing forwards Euler timestep as C code

nmodl.ode.integrate2c(diff_string, dt_var, vars, use_pade_approx=False)[source]

Analytically integrate supplied derivative, return solution as C code.

Given a differential equation of the form x’ = f(x), the value of x at time t+dt is found in terms of the value of x at time t: x(t + dt) = g( x(t), dt ) and this equation is returned in the format NEURON expects: x = g( x, dt ), where the x on the right is the current value of x at time t, and the x on the left is the new value of x at time t+dt

The derivative should be of the form “x’ = f(x)”, and vars should contain the set of all the variables referenced by f(x), for example:

-integrate2c("x' = a*x", "dt", {"a"}) -integrate2c("x' = a + b*x - sin(3.2)", "dt", {"a","b"})

Optionally, the analytic result can be expanded in powers of dt, and the (1,1) Pade approximant to the solution returned. This approximate solution is correct to second order in dt.

Parameters:
  • diff_string – Derivative to be integrated e.g. “x’ = a*x + b”

  • t_var – name of time variable t in NEURON

  • dt_var – name of timestep variable dt in NEURON

  • vars – set of variables used in expression, e.g. {“a”, “b”}

  • use_pade_approx – if False, return exact solution if True, return (1,1) Pade approx to solution correct to second order in dt_var

Returns:

string containing analytic integral of derivative as C code

Raises:

NotImplementedError – if the ODE is too hard, or if it fails to solve it.

nmodl.ode.make_symbol(var, /)[source]

Create SymPy symbol from a variable.

nmodl.ode.needs_finite_differences(mat)[source]
nmodl.ode.search_and_replace_protected_identifiers_from_sympy(eqs, function_calls)[source]
nmodl.ode.search_and_replace_protected_identifiers_to_sympy(eqs, vars, function_calls)[source]
nmodl.ode.solve_lin_system(eq_strings, vars, constants, function_calls, tmp_unique_prefix, small_system=False, do_cse=False)[source]

Solve linear system of equations, return solution as C code.

If system is small (small_system=True, typically N<=3):
  • solve analytically by gaussian elimination

  • optionally do Common Subexpression Elimination if do_cse is true

If system is large (default):
  • gaussian elimination may not be numerically stable at runtime

  • instead return a matrix J and vector F, where J X = F

  • this linear system can then be solved for X by e.g. LU factorization

Parameters:
  • eqs – list of equations e.g. [“x + y = a”, “y = 3 + b”]

  • vars – list of variables to solve for, e.g. [“x”, “y”]

  • constants – set of any other symbolic expressions used, e.g. {“a”, “b”}

  • function_calls – set of function calls used in the ODE

  • tmp_unique_prefix – is a unique prefix on which new variables can be easily created by appending strings. It is usually of the form “tmp”

  • small_system – if True, solve analytically by gaussian elimination otherwise return matrix system to be solved

  • do_cse – if True, do Common Subexpression Elimination

Returns:

list of strings containing assignment statements vars: list of strings containing new local variables

Return type:

code

nmodl.ode.solve_non_lin_system(eq_strings, vars, constants, function_calls)[source]

Solve non-linear system of equations, return solution as C code.

  • returns a vector F, and its Jacobian J, both in terms of X

  • where F(X) = 0 is the implicit equation to solve for X

  • this non-linear system can then be solved with the newton solver

Parameters:
  • eqs – list of equations e.g. [“x + y = a”, “y = 3 + b”]

  • vars – list of variables to solve for, e.g. [“x”, “y”]

  • constants – set of any other symbolic expressions used, e.g. {“a”, “b”}

  • function_calls – set of function calls used in the ODE

Returns:

List of strings containing assignment statements

nmodl.ode.transform_expression(expr, transform)[source]
nmodl.ode.transform_matrix_elements(mat, transform)[source]

nmodl.symtab module

nmodl.visitor module