コード例 #1
0
def convert_search_parameter(jl_dir, py_dir):
    os.makedirs(
        jl_dir, exist_ok=True
    )

    space_num = 4
    with open(py_dir+'/search_parameter.py') as f:
        lines = f.readlines()

    search_parameter = jl_source.header()

    search_parameter += jl_source.search_idx_const_header()
    search_idx_const = triming_tools.cut_out_lines(
        lines, 'search_idx_const=[', ']'
    )[1:]
    for i, line in enumerate(search_idx_const):
        search_parameter += line.replace('\\', '')
    search_parameter += jl_source.search_idx_const_footer()

    search_parameter += jl_source.search_idx_init_header()
    search_idx_init = triming_tools.cut_out_lines(
        lines, 'search_idx_init=[', ']'
    )[1:]
    for i, line in enumerate(search_idx_init):
        search_parameter += line.replace('\\', '')
    search_parameter += jl_source.search_idx_init_footer()

    search_parameter += jl_source.get_search_region_header()

    lines = triming_tools.convert_comment_out(lines)
    lines = triming_tools.lines_triming(lines, space_num)
    lines = triming_tools.insert_end(lines)
    # for i,line in enumerate(lines):
    #    print(line.replace('\n',''))

    search_region = triming_tools.cut_out_lines(
        lines, 'search_region=np.zeros', 'lin2log'
    )[1:]
    for i,line in enumerate(search_region):
        line = line.replace('for i, j', 'for (i,j)')
        line = line.replace('np.', '')
        line = line.replace('len', 'length')
        line = line.replace('(x)', '(p)')
        line = triming_tools.list_adder(line)
        search_parameter += line

    search_parameter += jl_source.get_search_region_footer()
    search_parameter += jl_source.lin2log()

    with open(jl_dir + '/search_parameter.jl', mode='w') as f:
        f.write(search_parameter)
コード例 #2
0
def make_param_const(jl_dir, py_dir, space_num=4):
    jl_dir = jl_dir + '/model/param_const.jl'
    py_dir = py_dir + '/model/param_const.py'

    with open(py_dir, mode='r') as f:
        lines = f.readlines()
    lines = triming_tools.lines_triming(lines, space_num)
    lines = triming_tools.cut_out_lines(lines, 'len_f_params', 'return x')[1:]
    param_const = jl_source.param_const_header()
    for i, line in enumerate(lines):
        param_const += line
    param_const += jl_source.param_const_footer()
    with open(jl_dir, mode='w') as f:
        f.write(param_const)
コード例 #3
0
def make_initial_condition(jl_dir, py_dir, space_num=4):
    jl_dir = jl_dir + '/model/initial_condition.jl'
    py_dir = py_dir + '/model/initial_condition.py'

    with open(py_dir, mode='r') as f:
        lines = f.readlines()
    lines = triming_tools.lines_triming(lines, space_num)
    lines = triming_tools.cut_out_lines(lines, 'len_f_vars', 'return y0')[1:]
    initial_condition = jl_source.initial_condition_header()
    for i, line in enumerate(lines):
        initial_condition += line
    initial_condition += jl_source.initial_condition_footer()

    with open(jl_dir, mode='w') as f:
        f.write(initial_condition)
コード例 #4
0
def make_variables(jl_dir, py_dir, space_num=4):
    jl_dir = jl_dir + '/model/name2idx/variables.jl'
    py_dir = py_dir + '/model/name2idx/variables.py'

    with open(py_dir, mode='r') as f:
        lines = f.readlines()
    lines = triming_tools.lines_triming(lines, space_num)
    lines = triming_tools.cut_out_lines(lines, 'var_names', 'len_f_vars')[1:]
    variables = jl_source.variables_header()
    for i, line in enumerate(lines):
        variables += line
    variables = variables + jl_source.variables_footer()

    with open(jl_dir, mode='w') as f:
        f.write(variables)
コード例 #5
0
def make_parameters(jl_dir, py_dir, space_num=4):
    jl_dir = jl_dir + '/model/name2idx/parameters.jl'
    py_dir = py_dir + '/model/name2idx/parameters.py'
    with open(py_dir, mode='r') as f:
        lines = f.readlines()
    lines = triming_tools.lines_triming(lines, space_num)
    lines = triming_tools.cut_out_lines(lines, 'param_names',
                                        'len_f_params')[1:]
    parameters = jl_source.parameters_header()
    for i, line in enumerate(lines):
        parameters += line
    parameters = parameters + jl_source.parameters_footer()

    with open(jl_dir, mode='w') as f:
        f.write(parameters)
コード例 #6
0
def make_differential_equation(jl_dir, py_dir, space_num=4):
    jl_dir = jl_dir + '/model/differential_equation.jl'
    py_dir = py_dir + '/model/differential_equation.py'

    with open(py_dir, mode='r') as f:
        lines = f.readlines()
    lines = triming_tools.lines_triming(lines, space_num)
    lines = triming_tools.insert_end(lines)
    differential_equation = jl_source.differential_equation_header1()

    for i, line in enumerate(lines):
        rep_line = line.replace(' ', '')
        if rep_line.find('[0]*') != -1:
            if rep_line.find('dydt') == -1:
                rep_line_trim = rep_line.replace(' ', '').strip()
                vector_name = \
                    rep_line_trim[:rep_line_trim.find('=')]
                elements_num = \
                    rep_line_trim[rep_line_trim.find('[0]*')+4:rep_line_trim.find('#')]
                lines[i] = jl_source.differential_equation_header2(
                    vector_name, elements_num)
                if rep_line_trim.find('#') != -1:
                    lines[i] = lines[i].rstrip() + ' ' + \
                        rep_line[rep_line.find('#'):]
            else:
                lines[i] = ''

    is_keyword = False
    for i, line in enumerate(lines):
        if (lines[i - 1].replace(' ', '').find('diffeq(t,y,x):') != -1 or
                lines[i - 1].replace(' ', '').find('diffeq(y,t,*x):') != -1):
            is_keyword = True
            #print('\n\n\ntest:start of values is found')
        elif is_keyword and line.find('return dydt') != -1:
            break
            #print('test:end of values is found\n\n\n\n')
        if is_keyword:
            differential_equation += line

    with open(jl_dir, mode='w') as f:
        f.write(differential_equation)
コード例 #7
0
def make_simulation(jl_dir, py_dir):
    with open(py_dir + '/observable.py') as f:
        lines = f.readlines()

    lines = triming_tools.cut_out_lines(lines, 'class NumericalSimulation',
                                        'class ExperimentalData')
    lines = triming_tools.lines_triming(lines)
    for i, line in enumerate(lines):
        lines[i] = triming_tools.copy_list(line)

    simulation = jl_source.simulation_header()

    line_tspan = triming_tools.cut_out_line(lines, 'tspan=')
    line_tspan = line_tspan.replace('[', '(').replace(']', ')')
    line_tspan = triming_tools.indent_remover(line_tspan, 1)

    line_t = triming_tools.cut_out_line(lines, 't=')

    simulation += triming_tools.insert_after_indent(line_tspan, 'const ')

    if line_t.find('/') != -1:
        line_t = line_t[line_t.find('/') + 1:]
        simulation += 'const t = collect(tspan[1]:1.0:tspan[end])./' + \
            line_t + '\n'
    else:
        line_t = line_t[line_t.find(')') + 1:]
        simulation += 'const t = collect(tspan[1]:1.0:tspan[end])' + \
            line_t + '\n'

    line_conditions = triming_tools.cut_out_line(lines, 'conditions=')

    simulation += triming_tools.indent_remover(
        triming_tools.insert_after_indent(line_conditions, 'const ') + '\n', 1)

    simulation += jl_source.simulation_body1()

    conditions1 = triming_tools.cut_out_lines(lines, 'def simulate(',
                                              'steady_state')[1:]
    conditions2 = triming_tools.cut_out_lines(lines,
                                              '=copy',
                                              'solveode',
                                              mode=1)[1:]

    conditions2 = triming_tools.insert_end(conditions2)[:-2]

    for i, line in enumerate(conditions1):
        simulation += line

    simulation += jl_source.simulation_body2()

    for i, line in enumerate(conditions2):
        simulation += line

    simulation += jl_source.simulation_body3()

    get_timepoint = triming_tools.cut_out_lines(lines,
                                                'simulations[',
                                                'class ExperimentalData',
                                                mode=1)
    for i, line in enumerate(get_timepoint):
        rep_line = line
        if rep_line.find('class ExperimentalData') != -1:
            break
        rep_line = rep_line.replace('self.', '')
        rep_line = rep_line.replace('observables.index', 'observables_index')
        if rep_line.find('Y[:, ') != -1:
            rep_line = rep_line.replace('Y[:, ', 'sol.u[j][')
        else:
            rep_line = rep_line.replace('Y[:,', 'sol.u[j][')
        if rep_line.find('simulations[') != -1:
            rep_line = rep_line.replace(':,', 'j,')
        simulation += rep_line

    simulation += jl_source.simulation_footer()

    with open(jl_dir + '/simulation.jl', mode='w') as f:
        f.write(simulation)