Beispiel #1
0
def print_matlab(sexpr):
    from dolo.compiler.codegen import to_source
    ss = (to_source(sexpr))
    ss = ss.replace(' ** ', '.^')
    ss = ss.replace(' * ', '.*')
    ss = ss.replace(' / ', './')
    return ss
def timeshift(expr, variables, shift):
    if isinstance(expr, str):
        aexpr = ast.parse(expr).body[0].value
    else:
        aexpr = copy.deepcopy(expr)
    resp = TimeShiftTransformer(variables, shift).visit(aexpr)
    if isinstance(expr, str):
        return to_source(resp)
    else:
        return resp
Beispiel #3
0
def compile_function_ast(equations, symbols, arg_names, output_names=None, funname='anonymous', rhs_only=False,
            return_ast=False, print_code=False, definitions=None, vectorize=True, use_file=False):

    arguments = OrderedDict()
    for an in arg_names:
        if an[0] != 'parameters':
            t = an[1]
            arguments[an[2]] = [(s,t) for s in symbols[an[0]]]
    # arguments = [ [ (s,t) for s in symbols[sg]] for sg,t in arg_names if sg != 'parameters']
    parameters = [(s,0) for s in symbols['parameters']]
    targets = output_names
    if targets is not None:
        targets = [(s,targets[1]) for s in symbols[targets[0]]]

    mod = make_function(equations, arguments, parameters, definitions=definitions, targets=targets, rhs_only=rhs_only, funname=funname)

    from dolo.compiler.codegen import to_source
    import dolo.config
    if dolo.config.debug:
        print(to_source(mod))

    if vectorize:
        from numba import float64, void
        coredims = [len(symbols[an[0]]) for an in arg_names]
        signature = str.join(',', ['(n_{})'.format(d) for d in coredims])
        n_out = len(equations)
        if n_out in coredims:
            signature += '->(n_{})'.format(n_out)
            # ftylist = float64[:](*([float64[:]] * len(coredims)))
            fty = "void(*[float64[:]]*{})".format(len(coredims)+1)
        else:
            signature += ',(n_{})'.format(n_out)
            fty = "void(*[float64[:]]*{})".format(len(coredims)+1)
        ftylist = [fty]
    else:
        signature=None
        ftylist=None

    if use_file:
        fun = eval_ast_with_file(mod, print_code=True)
    else:
        fun = eval_ast(mod)

    from numba import jit, guvectorize

    jitted = jit(fun, nopython=True)
    if vectorize:
        gufun = guvectorize([fty], signature, target='parallel', nopython=True)(fun)
        return jitted, gufun
    else:
        return jitted
    return [f,None]
Beispiel #4
0
def eval_ast_with_file(mod, print_code=False, signature=None, ftylist=None):

    name = mod.body[0].name

    code = """\
from __future__ import division

from numpy import exp, log, sin, cos, abs
from numpy import inf, maximum, minimum
"""

    #     if signature is not None:
    #         print(signature)
    #
    #         decorator = """
    # from numba import float64, void, guvectorize
    # @guvectorize(signature='{signature}', ftylist={ftylist}, target='parallel', nopython=True)
    # """.format(signature=signature, ftylist=ftylist)
    #         code += decorator

    code += to_source(mod)

    if print_code:
        print(code)

    import sys
    # try to create a new file
    import time
    import tempfile
    import os, importlib
    from dolo.config import temp_dir
    temp_file = tempfile.NamedTemporaryFile(mode='w+t',
                                            prefix='fun',
                                            suffix='.py',
                                            dir=temp_dir,
                                            delete=False)
    with temp_file:
        temp_file.write(code)
    modname = os.path.basename(temp_file.name).strip('.py')

    full_name = os.path.basename(temp_file.name)
    modname, extension = os.path.splitext(full_name)

    module = importlib.import_module(modname)

    fun = module.__dict__[name]

    return fun
def eval_ast_with_file(mod, print_code=False, signature=None, ftylist=None):

    name = mod.body[0].name

    code = """\
from __future__ import division

from numpy import exp, log, sin, cos, abs
from numpy import inf, maximum, minimum
"""

#     if signature is not None:
#         print(signature)
#
#         decorator = """
# from numba import float64, void, guvectorize
# @guvectorize(signature='{signature}', ftylist={ftylist}, target='parallel', nopython=True)
# """.format(signature=signature, ftylist=ftylist)
#         code += decorator

    code += to_source(mod)

    if print_code:
        print(code)

    import sys
    # try to create a new file
    import time
    import tempfile
    import os, importlib
    from dolo.config import temp_dir
    temp_file = tempfile.NamedTemporaryFile(mode='w+t', prefix='fun', suffix='.py', dir=temp_dir, delete=False)
    with temp_file:
        temp_file.write(code)
    modname = os.path.basename(temp_file.name).strip('.py')


    full_name = os.path.basename(temp_file.name)
    modname, extension = os.path.splitext(full_name)

    module = importlib.import_module(modname)

    fun =  module.__dict__[name]

    return fun
def compile_function_ast(expressions, symbols, arg_names, output_names=None, funname='anonymous', return_ast=False, print_code=False, definitions=None, vectorize=True, use_file=False):
    '''
    expressions: list of equations as string
    '''

    from collections import OrderedDict
    table = OrderedDict()

    aa = arg_names

    if output_names is not None:
        aa = arg_names + [output_names]

    for a in aa:
        symbol_group = a[0]
        date = a[1]
        an = a[2]

        for b in symbols[symbol_group]:
            index = symbols[symbol_group].index(b)
            table[(b, date)] = (an, index)

    table_symbols = {k: (std_date_symbol(*k)) for k in table.keys()}

    # standard assignment: i.e. k = s[0]
    index = lambda x: Index(Num(x))

    # declare symbols

    aux_short_names = [e[2] for e in arg_names if e[0]=='auxiliaries']



    preamble = []

    for k in table:  # order it
        # k : var, date
        arg, pos = table[k]
        if not (arg in aux_short_names):
            std_name = table_symbols[k]
            val = Subscript(value=Name(id=arg, ctx=Load()), slice=index(pos), ctx=Load())
            line = Assign(targets=[Name(id=std_name, ctx=Store())], value=val)
            if arg != 'out':
                preamble.append(line)

    body = []
    std_dates = StandardizeDates(symbols, aa)


    if definitions is not None:
        for k,v in definitions.items():
            if isinstance(k, str):
                lhs = ast.parse(k).body[0].value
            if isinstance(v, str):
                rhs = ast.parse(v).body[0].value
            else:
                rhs = v
            lhs = std_dates.visit(lhs)
            rhs = std_dates.visit(rhs)
            vname = lhs.id
            line = Assign(targets=[Name(id=vname, ctx=Store())], value=rhs)
            preamble.append(line)


    outs = []
    for i, expr in enumerate(expressions):

        expr = ast.parse(expr).body[0].value
        # if definitions is not None:
        #     expr = ReplaceName(defs).visit(expr)

        rexpr = std_dates.visit(expr)

        rhs = rexpr

        if output_names is not None:
            varname = symbols[output_names[0]][i]
            date = output_names[1]
            out_name = table_symbols[(varname, date)]
        else:
            out_name = 'out_{}'.format(i)

        line = Assign(targets=[Name(id=out_name, ctx=Store())], value=rhs)
        body.append(line)

        line = Assign(targets=[Subscript(value=Name(id='out', ctx=Load()),
                                         slice=index(i), ctx=Store())], value=Name(id=out_name, ctx=Load()))
        body.append(line)

    arg_names = [e for e in arg_names if e[0]!="auxiliaries"]

    args = [e[2] for e in arg_names] + ['out']

    if is_python_3:
        from ast import arg
        f = FunctionDef(name=funname, args=arguments(args=[arg(arg=a) for a in args], vararg=None, kwarg=None, kwonlyargs=[], kw_defaults=[], defaults=[]),
                        body=preamble + body, decorator_list=[])
    else:
        f = FunctionDef(name=funname, args=arguments(args=[Name(id=a, ctx=Param()) for a in args], vararg=None, kwarg=None, kwonlyargs=[], kw_defaults=[], defaults=[]),
                        body=preamble + body, decorator_list=[])

    mod = Module(body=[f])
    mod = ast.fix_missing_locations(mod)

    if print_code:
        s = "Function {}".format(mod.body[0].name)
        print("-" * len(s))
        print(s)
        print("-" * len(s))
        print(to_source(mod))

    if vectorize:
        from numba import float64, void
        coredims = [len(symbols[an[0]]) for an in arg_names]
        signature = str.join(',', ['(n_{})'.format(d) for d in coredims])
        n_out = len(expressions)
        if n_out in coredims:
            signature += '->(n_{})'.format(n_out)
            # ftylist = float64[:](*([float64[:]] * len(coredims)))
            fty = "void(*[float64[:]]*{})".format(len(coredims)+1)
        else:
            signature += ',(n_{})'.format(n_out)
            fty = "void(*[float64[:]]*{})".format(len(coredims)+1)
        ftylist = [fty]
    else:
        signature=None
        ftylist=None

    if use_file:
        fun = eval_ast_with_file(mod, print_code=True)
    else:
        fun = eval_ast(mod)

    jitted = njit(fun)
    if vectorize:
        gufun = guvectorize([fty], signature, target='parallel', nopython=True)(fun)
        return jitted, gufun
    else:
        return jitted
Beispiel #7
0
def compile_function_ast(equations,
                         symbols,
                         arg_names,
                         output_names=None,
                         funname='anonymous',
                         rhs_only=False,
                         return_ast=False,
                         print_code=False,
                         definitions=None,
                         vectorize=True,
                         use_file=False):

    arguments = OrderedDict()
    for an in arg_names:
        if an[0] != 'parameters':
            t = an[1]
            arguments[an[2]] = [(s, t) for s in symbols[an[0]]]
    # arguments = [ [ (s,t) for s in symbols[sg]] for sg,t in arg_names if sg != 'parameters']
    parameters = [(s, 0) for s in symbols['parameters']]
    targets = output_names
    if targets is not None:
        targets = [(s, targets[1]) for s in symbols[targets[0]]]

    mod = make_function(equations,
                        arguments,
                        parameters,
                        definitions=definitions,
                        targets=targets,
                        rhs_only=rhs_only,
                        funname=funname)

    from dolo.compiler.codegen import to_source
    import dolo.config
    if dolo.config.debug:
        print(to_source(mod))

    if vectorize:
        from numba import float64, void
        coredims = [len(symbols[an[0]]) for an in arg_names]
        signature = str.join(',', ['(n_{})'.format(d) for d in coredims])
        n_out = len(equations)
        if n_out in coredims:
            signature += '->(n_{})'.format(n_out)
            # ftylist = float64[:](*([float64[:]] * len(coredims)))
            fty = "void(*[float64[:]]*{})".format(len(coredims) + 1)
        else:
            signature += ',(n_{})'.format(n_out)
            fty = "void(*[float64[:]]*{})".format(len(coredims) + 1)
        ftylist = [fty]
    else:
        signature = None
        ftylist = None

    if use_file:
        fun = eval_ast_with_file(mod, print_code=True)
    else:
        fun = eval_ast(mod)

    from numba import jit, guvectorize

    jitted = jit(fun, nopython=True)
    if vectorize:
        gufun = guvectorize([fty], signature, target='parallel',
                            nopython=True)(fun)
        return jitted, gufun
    else:
        return jitted
    return [f, None]
def expr_to_sympy(sexpr):
    from dolo.compiler.codegen import to_source
    ss = (to_source(sexpr))
    import sympy
    return sympy.sympify(ss)
def expr_to_sympy(sexpr):
    from dolo.compiler.codegen import to_source
    ss = (to_source(sexpr))
    import sympy
    return sympy.sympify(ss)
Beispiel #10
0
    sol = solve_triangular_system(calibration)


with timeit("Parse and normalize equations"):
    equations = model['equations']

    compat = lambda s: s.replace("^", "**").replace('==','=').replace('=','==')

    equations = [compat(eq) for eq in equations]
    equations = ['{} - ({})'.format(*str.split(eq,'==')) for eq in equations]
    equations = [to_expr(e) for e in equations]

    all_symbols = sum(model['symbols'].values(), [])
    sds = StandardizeDatesSimple(all_symbols)
    equations_normalized = [sds.visit(e) for e in equations]
    equations_normalized_strings = [to_source(e) for e in equations_normalized]
    symbols_normalized_strings = [to_source(sds.visit(to_expr(e))) for e in all_symbols]



with timeit("Sympify equations"):
    equations_normalized_sympy = [sympy.sympify(e) for e in equations_normalized_strings]


with timeit("Compute jacobian (sympy)"):
    jac = []
    for eq in equations_normalized_strings:
        line = []
        eqs = sympy.sympify(eq)
        atoms = [v for v in eqs.atoms() if str(v) in symbols_normalized_strings]
        for v in atoms: