Example #1
0
for i in range(n_models):

    # create a model randomly from all channels
    weights_model = problem.generator(prng, None)

    for j, dt in enumerate(dts):
        print 'run '+'model: ' + str(i) + ' dt: ' +str(dt)
        
        # change dt
        data_newdt = change_dt(dt, problem.data)
        problem.simulation_params = extract_simulation_params(data_newdt)

        # run simulation
        problem.update_cell(weights_model)
        currents = [problem.cell.soma.record_from(channel_list[k], 'i'+ion_list[k], pos=.5) for k in range(len(channel_list))]
        v_newdt, t_newdt = run_simulation(problem.cell, **problem.simulation_params)

        #pl.figure()
        #pl.plot(t_newdt, v_newdt)
        #pl.show()

        # compute parameter
        dvdt_newdt = np.concatenate((np.array([(v_newdt[1]-v_newdt[0])/dt]), np.diff(v_newdt)/dt))
        i_newdt = data_newdt.i.values
        celsius = problem.simulation_params['celsius']

        # get currents
        candidate = np.ones(len(problem.path_variables))  # gbars should be 1
        problem.update_cell(candidate)
        currents_newdt = currents_given_v(v_newdt, t_newdt, problem.cell.soma, channel_list, ion_list, celsius)
    for i in range(len(merge_points)-1):
        y_plot = y[merge_points[i]:merge_points[i+1]]
        X_plot = X[merge_points[i]:merge_points[i+1], :]
        t_plot = t_exp[merge_points[i]:merge_points[i+1]]
        plot_fit(y_plot, X_plot, weights, t_plot, channel_list, save_dir=save_dir+str(i))

    # save
    np.savetxt(save_dir+'/best_candidate_'+str(trial)+'.txt', weights)
    np.savetxt(save_dir+'/error_'+str(trial)+'.txt', np.array([residual]))

    # simulate
    #cm = weights[-1]
    for i, w in enumerate(weights[:]):
        keys = ['soma', '0.5', channel_list[i], 'gbar']
        if channel_list[i] == 'pas':
            keys = ['soma', '0.5', channel_list[i], 'g']
            problem.cell.update_attr(keys, w)
        elif 'ion' in channel_list[i]:
            keys = None
        else:
            keys = ['soma', '0.5', channel_list[i], 'gbar']
            problem.cell.update_attr(keys, w)

    from optimization.simulate import run_simulation
    v, t = run_simulation(problem.cell, **problem.simulation_params)

    import matplotlib.pyplot as pl
    pl.figure()
    pl.plot(t, problem.data.v, 'k')
    pl.plot(t, v, 'r')
    pl.show()