Package park :: Package modelling :: Module expression

Module expression

source code

Functions for manipulating expressions.
Functions
 
symbols(expr, symtab)
Given an expression string and a symbol table, return the set of symbols used in the expression.
source code
 
substitute(expr, mapping)
Replace all occurrences of symbol s with mapping[s] for s in mapping.
source code
 
find_dependencies(pars)
Returns a list of pair-wise dependencies from the parameter expressions.
source code
 
parameter_mapping(pairs)
Find the parameter substitution we need so that expressions can be evaluated without having to traverse a chain of model.layer.parameter.value
source code
 
no_constraints()
This parameter set has no constraints between the parameters.
source code
 
build_eval(pars, context={})
Build and return a function to evaluate all parameter expressions in the proper order.
source code
 
test() source code
Function Details

symbols(expr, symtab)

source code 

Given an expression string and a symbol table, return the set of symbols used in the expression. Symbols are only returned once even if they occur multiple times. The return value is a set with the elements in no particular order.

This is the first step in computing a dependency graph.

find_dependencies(pars)

source code 

Returns a list of pair-wise dependencies from the parameter expressions.

For example, if p3 = p1+p2, then find_dependencies([p1,p2,p3]) will return [(p3,p1),(p3,p2)]. For base expressions without dependencies, such as p4 = 2*pi, this should return [(p4, None)]

build_eval(pars, context={})

source code 

Build and return a function to evaluate all parameter expressions in the proper order.

Inputs:
pars is a list of parameters context is a dictionary of additional symbols for the expressions
Output:
updater function
Raises:
AssertionError - model, parameter or function is missing SyntaxError - improper expression syntax ValueError - expressions have circular dependencies

This function is not terribly sophisticated, and it would be easy to trick. However it handles the common cases cleanly and generates reasonable messages for the common errors.

This code has not been fully audited for security. While we have removed the builtins and the ability to import modules, there may be other vectors for users to perform more than simple function evaluations. Unauthenticated users should not be running this code.

Parameter names are assumed to contain only _.a-zA-Z0-9#[]

The list of parameters is probably something like:

parset.setprefix()
pars = parset.flatten()

Note that math uses acos while numpy uses arccos. To avoid confusion we allow both.

Should try running the function to identify syntax errors before running it in a fit.

Use help(fn) to see the code generated for the returned function fn. dis.dis(fn) will show the corresponding python vm instructions.