Пример #1
0
def save_problem_specification(save_dir, n_trials, pop_size, max_iterations, problem_dicts, variables_all, generator=None):
    save_dir += 'specification/'

    # create save directory
    if os.path.exists(save_dir):
        print 'Saving directory already exists!'
        return False
    os.makedirs(save_dir)

    # save number of trials, population size, maximal number of iterations
    np.savetxt(save_dir+'/n_trials.txt', np.array([n_trials]))
    np.savetxt(save_dir+'/pop_size.txt', np.array([pop_size]))
    np.savetxt(save_dir+'/max_iterations.txt', np.array([max_iterations]))
    with open(save_dir+'/variables_all.json', 'w') as f:
        json.dump(variables_all, f)

    # load all different mechanism directories (needed for saving cell)
    mechanism_dirs = set([p['mechanism_dir'] for p in problem_dicts])
    for m_dir in mechanism_dirs:
        load_mechanism_dir(m_dir)

    for trial in range(n_trials):
        # create directory for each trial
        save_dir_trial = save_dir + 'trial'+str(trial)+'/'
        os.makedirs(save_dir_trial)

        # save seeds
        seed = time()
        np.savetxt(save_dir_trial+'seed.txt', np.array([seed]))

        # save problem dict
        with open(save_dir_trial+'problem.json', 'w') as f:
            json.dump(problem_dicts[trial], f, indent=4)

        # save cell
        with open(save_dir_trial+'cell.json', 'w') as f:
            json.dump(Cell.from_modeldir(problem_dicts[trial]['model_dir']).get_dict(), f, indent=4)

        # save initial population
        if generator is not None:
            random = Random()
            random.seed(seed)
            initial_pop = list()
            lower_bound, upper_bound, path_variables = get_lowerbound_upperbound_path(problem_dicts[trial]['variables'])
            for i in range(pop_size):
                initial_pop.append(generator(random, lower_bound, upper_bound))
            with open(save_dir_trial+'initial_pop.json', 'w') as f:
                json.dump(initial_pop, f, indent=4)

    return True
Пример #2
0
import matplotlib.pyplot as pl

from nrn_wrapper import Cell, complete_mechanismdir
from optimization.simulate import iclamp, extract_simulation_params

__author__ = 'caro'

data_real_dir = '../2015_08_11d/ramp/ramp.csv'
data_new_dir = './ramp.csv'
model_dir = '../../model/cells/dapmodel0.json'
mechanism_dir = '../../model/channels/schmidthieber'

data_real = pd.read_csv(data_real_dir)

# create cell and run simulation
cell = Cell.from_modeldir(model_dir, complete_mechanismdir(mechanism_dir))
simulation_params = extract_simulation_params(data_real)
v, t = iclamp(cell, **simulation_params)
i = data_real.i.values
sec = data_real.sec.values

data = pd.DataFrame({'v': v, 't': t, 'i': i, 'sec': sec})
data.to_csv(data_new_dir, index=None)

pl.figure()
pl.plot(t, v)
pl.show()

pl.figure()
pl.plot(t, i)
pl.show()
Пример #3
0
"""
dt = 0.01
tstop = 100
t = np.arange(0, tstop+dt, dt)
i_inj = np.zeros(len(t))
i_inj[np.logical_and(20 <= t, t < 60)] = 1
v_init = -60
sec = ['soma', None]
start_idx = int(np.round(20.0/dt))  #np.where(np.abs(np.diff(20 <= t)))[0] + 1
end_idx = int(np.round(60.0/dt))  #np.where(np.abs(np.diff(t <= 60)))[0]
discontinuities=[start_idx, end_idx]
"""

# create cell
cell = Cell.from_modeldir('../demo/test_cell.json', '../demo/channels')


v_rec, t_rec, i_rec = iclamp_adaptive(cell, sec, i_inj, v_init, tstop, dt, celsius=35, pos_i=0.5, pos_v=0.5, atol=1e-8,
                                      continuous=True, discontinuities=discontinuities, interpolate=True)

print tstop
print t_rec[-1]
print np.diff(t_rec)[1:10]

pl.figure()
pl.plot(t_rec, v_rec)
pl.show()

pl.figure()
pl.plot(t, i_inj, 'x')
Пример #4
0
 def get_cell(self):
     cell = Cell.from_modeldir(self.model_dir)
     insert_mechanisms(cell, self.variable_keys)
     return cell
Пример #5
0
 def get_optimal_candidate(self, model_dir, variables):
     cell = Cell.from_modeldir(model_dir)
     optimal_candidate = [cell.get_attr(var[2][0]) for var in variables]
     return optimal_candidate
Пример #6
0
prng.seed(seed)

# initialize data
t = np.arange(0, problem.simulation_params['tstop']+dt, dt)
i_inj = problem.simulation_params['i_amp']
data = np.zeros((n_data, len(t)/subsample, 2))  # inputs are the trace of v and i_inj
labels = np.zeros(n_data)

for i in range(n_data):
    # modify parameter
    candidate = problem.generator(prng, None)

    # run simulation
    problem.update_cell(candidate)
    v, t = iclamp(problem.cell, **problem.simulation_params)
    data[i, :, 0] = v[:-1:subsample]
    data[i, :, 1] = i_inj[:-1:subsample]
    labels[i] = candidate[0]

# store data
if not os.path.exists(save_dir):
    os.makedirs(save_dir)
with open(save_dir+'/problem.json', 'w') as f:
    json.dump(params, f, indent=4)
with open(save_dir+'/cell.json', 'w') as f:
    json.dump(Cell.from_modeldir(params['model_dir']).get_dict(), f, indent=4)
np.savetxt(save_dir+'/seed.txt', np.array([seed]))
with open(save_dir+'data.npy', 'w') as f:
    np.save(f, data)
with open(save_dir+'labels.npy', 'w') as f:
    np.save(f, labels)
Пример #7
0
print dt
"""
dt = 0.01
tstop = 100
t = np.arange(0, tstop+dt, dt)
i_inj = np.zeros(len(t))
i_inj[np.logical_and(20 <= t, t < 60)] = 1
v_init = -60
sec = ['soma', None]
start_idx = int(np.round(20.0/dt))  #np.where(np.abs(np.diff(20 <= t)))[0] + 1
end_idx = int(np.round(60.0/dt))  #np.where(np.abs(np.diff(t <= 60)))[0]
discontinuities=[start_idx, end_idx]
"""

# create cell
cell = Cell.from_modeldir('../demo/test_cell.json', '../demo/channels')

v_rec, t_rec, i_rec = iclamp_adaptive(cell,
                                      sec,
                                      i_inj,
                                      v_init,
                                      tstop,
                                      dt,
                                      celsius=35,
                                      pos_i=0.5,
                                      pos_v=0.5,
                                      atol=1e-8,
                                      continuous=True,
                                      discontinuities=discontinuities,
                                      interpolate=True)