Exemplo n.º 1
0
traces_file = "gary_decker_traces.txt"

seed = 2
npr.seed(seed)

model_number = 8  # Decker dog
original_gs, g_parameters = ms.get_original_params(model_number)
original_gs = np.array(original_gs)
print original_gs, "\n"

lower_bounds = 0.5 * original_gs
upper_bounds = 2.0 * original_gs

times = np.arange(solve_start, solve_end + solve_timestep, solve_timestep)

ap = ap_simulator.APSimulator()

ap.DefineStimulus(stimulus_magnitude, stimulus_duration, stimulus_period,
                  stimulus_start_time)
ap.DefineSolveTimes(solve_start, solve_end, solve_timestep)
ap.DefineModel(model_number)
ap.SetNumberOfSolves(num_solves)

true_params = np.loadtxt(params_file)
expt_traces = np.loadtxt(traces_file)

best_params_file = "gary_decker_optimize_best_fits.txt"
best_params = np.loadtxt(best_params_file)
#print "best_params:", best_params

num_expts = best_params.shape[0]
Exemplo n.º 2
0
    def test_ap_simulator(self):

        stim_amp = -25.5
        stim_duration = 2
        stim_period = 1000
        stim_start = 10.

        solve_start = 0.

        sampling_timestep = 0.2

        ap = ap_simulator.APSimulator()
        ap.DefineProtocol(protocol_number)
        ap.DefineModel(model_number)

        how_many_repeats = 1000

        start = time.time()
        for _ in xrange(how_many_repeats):
            test_trace = ap.SolveForVoltageTraceWithParams(params)
            #example_loglikelihood_1 = example_loglikelihood(test_trace)
            example_loglikelihood_1 = ap.ExampleLogLikelihoodFunction(
                test_trace)
        time_taken = time.time() - start
        print "\nTime taken by ap_simulator SolveForVoltageTraceWithParams: {} s\n".format(
            round(time_taken, 2))

        start = time.time()
        for _ in xrange(how_many_repeats):
            test_trace = ap.SolveForVoltageTraceWithParams(params)
            numpy_loglikelihood = example_loglikelihood(test_trace)
        time_taken = time.time() - start
        print "\nTime taken by numpy example_loglikelihood: {} s\n".format(
            round(time_taken, 2))

        print "numpy_loglikelihood =", numpy_loglikelihood

        EXE_NAME = "/home/rossj/chaste-build/projects/RossJ/apps/APApp"  # this needs to be more general for chaste build path
        process = subprocess.Popen(EXE_NAME,
                                   False,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   stdin=subprocess.PIPE)
        PROVENANCE_HEADER = "This version of Chaste was compiled on:"
        reading_values, looking_for_blank = False, False
        while (not reading_values):
            line = process.stdout.readline()
            if line.startswith(PROVENANCE_HEADER):
                looking_for_blank = True
            elif looking_for_blank and line.strip() == "":
                reading_values = True

        process.stdin.write(str(protocol_number) + '\n')
        process.stdin.write(str(model_number) + '\n')

        start = time.time()
        for _ in xrange(how_many_repeats):
            output_string_to_c = " ".join(map(str, params)) + '\n'
            #print "output_string_to_c =", output_string_to_c
            process.stdin.write(output_string_to_c)
            from_chaste = process.stdout.readline()
            #print "from_chaste =", from_chaste
            example_loglikelihood_2 = float(from_chaste)
        time_taken = time.time() - start
        print "\nTime taken by APApp: {} s\n".format(round(time_taken, 2))

        if (example_loglikelihood_1 == example_loglikelihood_2):
            print "The two outputted 'log-likelihoods' are the same, don't worry."
        else:
            print "ap_simulator:", example_loglikelihood_1
            print "app:         ", example_loglikelihood_2
            print "numpy:       ", numpy_loglikelihood
Exemplo n.º 3
0
def do_for_trace(trace_number):
    def sum_of_square_diffs(params,
                            expt_trace):  #,expt_trace,upper_bounds,ap):
        if np.any(params < 0) or np.any(params > upper_bounds):
            #print test_gs
            return np.inf
        #ap.LoadStateVariables()
        ap.SetToModelInitialConditions()
        test_trace = ap.SolveForVoltageTraceWithParams(params)
        return np.sum((test_trace - expt_trace)**2)

    try:
        trace_label = "Trace_2_2_{}_1".format(trace_number)
    except:
        print "No trace", trace_number
        return None
    expt_times = 1000. * all_APs[trace_label][:, 0]
    expt_trace = 1000. * all_APs[trace_label][:, 1]

    solve_start = expt_times[0]
    solve_end = expt_times[-1]
    solve_timestep = expt_times[1] - expt_times[0]

    times = np.arange(solve_start, solve_end + solve_timestep, solve_timestep)

    ap = ap_simulator.APSimulator()
    ap.SetNumberOfSolves(num_solves)
    ap.DefineSolveTimes(solve_start, solve_end, solve_timestep)
    ap.DefineStimulus(stimulus_magnitude, stimulus_duration, stimulus_period,
                      stimulus_start_time)
    ap.DefineModel(model_number)
    ap.UseDataClamp(data_clamp_on, data_clamp_off)
    ap.SetExperimentalTraceAndTimesForDataClamp(expt_times, expt_trace)
    ap.SetExtracellularPotassiumConc(extra_K_conc)

    for python_seed in xrange(1, 4):
        x0 = original_gs * (1. + npr.randn(len(original_gs)))

        x0[np.where(x0 < 0)] = 1e-5

        print "x0 =", x0

        original_obj_fun = sum_of_square_diffs(
            x0, expt_trace)  #,expt_trace,upper_bounds,ap)
        print "original_obj_fun =", original_obj_fun

        start = time.time()

        res = so.minimize(sum_of_square_diffs,
                          x0,
                          args=(expt_trace, ),
                          method='Nelder-Mead')
        time_taken = time.time() - start
        best_gs = res.x
        best_f = res.fun

        print "\nTime taken: {} s\n".format(round(time_taken, 1))

        #ap.LoadStateVariables()
        ap.SetToModelInitialConditions()
        best_fit_trace = ap.SolveForVoltageTraceWithParams(best_gs)

        print "original_gs:", original_gs
        print "best_gs:", best_gs

        #ap.LoadStateVariables()
        ap.SetToModelInitialConditions()
        true_trace = ap.SolveForVoltageTraceWithParams(original_gs)

        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.grid()
        ax.plot(expt_times, expt_trace, color='red', label='Expt')
        ax.plot(times, true_trace, color='blue', label='Original')
        ax.plot(times, best_fit_trace, color='green', label='Best fit')
        ax.legend()
        fig.tight_layout()
        fig.savefig(output_dir +
                    "trace_{}_fit_to_model_{}_python_seed_{}.png".format(
                        trace_number, model_number, python_seed))
        #plt.show()
        plt.close()
    return None
Exemplo n.º 4
0
solve_timestep = expt_times[1] - expt_times[0]

data_clamp_on = 50
data_clamp_off = 52

num_solves = 100

stimulus_magnitude, stimulus_duration, stimulus_period, stimulus_start_time = 0, 1, 1000, 0

# need to make sure these are always same as in C++ protocol
# might have to change it to have protocols hard-coded into Python instead of C++

expt_times = np.arange(solve_start, solve_end + solve_timestep, solve_timestep)
num_pts = len(expt_times)

cell = ap.APSimulator()
cell.SetNumberOfSolves(num_solves)
cell.DefineStimulus(stimulus_magnitude, stimulus_duration, stimulus_period,
                    stimulus_start_time)
cell.DefineSolveTimes(solve_start, solve_end, solve_timestep)
cell.DefineModel(model)
cell.UseDataClamp(data_clamp_on, data_clamp_off)
cell.SetExperimentalTraceAndTimesForDataClamp(expt_times, expt_trace)
test_trace = cell.SolveForVoltageTraceWithParams(original_gs)
print "len(test_trace) =", len(test_trace)
print "len(expt_times) =", len(expt_times)
ax.plot(expt_times, test_trace, color='blue')
ax.axvline(data_clamp_on, color='green')
ax.axvline(data_clamp_off, color='green')

plt.show(block=True)