Exemplo n.º 1
0
def sum_squared_diff_moments(theta, model, est_par, data):

    par = model.par
    #Update parameters
    par = updatepar(par, est_par, theta)

    # Solve the model
    model.create_grids()
    model.solve()

    # Simulate the momemnts
    moments = np.nan + np.zeros((data.moments.size, par.moments_numsim))
    for s in range(par.moments_numsim):

        # Simulate
        model.simulate()

        #Calculate moments
        moments[:, s] = calc_moments(par, model.sim)

    # Mean of moments
    moments = np.mean(moments, 1)

    # Objective function
    if hasattr(par, 'weight_mat'):
        weight_mat_inv = np.linalg.inv(par.weight_mat)
    else:
        weight_mat_inv = np.eye(moments.size)  # The identity matrix and I^-1=I

    diff = (moments - data.moments).reshape(moments.size, 1)

    return (diff.T @ weight_mat_inv @ diff).ravel()
Exemplo n.º 2
0
def parallel_init(model_copy):
    '''parallel_init(model_copy) --> None
    
    parallel initilization of a pool of processes. The function takes a
    pickle safe copy of the model and resets the script module and the compiles
    the script and creates function to set the variables.
    '''
    global model, par_funcs
    model = model_copy
    model._reset_module()
    model.simulate()
    (par_funcs, start_guess, par_min, par_max) = model.get_fit_pars()
Exemplo n.º 3
0
    def update(self, dt):
        for model in self.models:
            model.simulate()
        if self.SAVE_TO_IMAGES:

            a = [0] * (self.window.width * self.window.height * 3)
            a = (GLuint * len(a))(*a)
            glReadPixels(0, 0, self.window.width, self.window.height, GL_RGB, GL_UNSIGNED_BYTE, a)
            a = np.array(a)
            a = a.reshape((self.window.height, self.window.width, 3))
            image = Image.fromarray(a, "RGB")
            image = image.transpose(Image.FLIP_TOP_BOTTOM)
            image.save("/Users/tanatanoi/Documents/university/CGRA409/project/vid/" + str(self.models[0].count)+".jpg")
Exemplo n.º 4
0
def stimulus_ISI_calculator(cellparams, stimulus, tlength=10):
    """
    Calculate ISI for a given cell and stimulus
    
    Parameters
    ----------
    cellparams: dictionary
        The model parameters of the cells
    stimulus: 1-D array
        The stimulus of interest.
    tlength: Float
        Length of the stimulus time in seconds
        
    Returns
    -------
    spiketimes: 1D array
        The timestamp of the spike times
    spikeISI: 1D array
        The ISI values for the spikes
    meanspkfr: float
        The average spiking rate
    """

    #Get the stimulus response of the model
    cellparams['v_zero'] = np.random.rand()
    spiketimes = mod.simulate(stimulus, **cellparams)
    spikeISI = np.diff(spiketimes)
    meanspkfr = len(spiketimes) / tlength  #mean spike firing rate per second

    print('mean spike firing rate (averaged over stimulus time) is %.3f' %
          (meanspkfr))
    return spiketimes, spikeISI, meanspkfr
Exemplo n.º 5
0
def worker(weights, seed, train_mode_int=1, max_len=-1):

  train_mode = (train_mode_int == 1)
  model.set_model_params(weights)
  reward_list, t_list = simulate(model, train_mode=train_mode, render_mode=False, num_episode=num_episode, seed=seed, max_len=max_len)
  if batch_mode == 'min':
    reward = np.min(reward_list)
  else:
    reward = np.mean(reward_list)
  t = np.mean(t_list)
  return reward, t
Exemplo n.º 6
0
 def on_key_press(self, symbol, modifiers):
     if(symbol == key.Q):
         self.r_x += 0.3
     elif(symbol == key.E):
         self.r_x -= 0.3
     elif(symbol == key.S):
         self.z -= 50
     elif(symbol == key.W):
         self.z += 50
     elif(symbol == key.A):
         self.x += 200
     elif(symbol == key.D):
         self.x -= 200
     elif(symbol == key.Z):
         self.r_x += 90
     elif(symbol == key.R):
         if(self.models[0].wind_magnitude == 0):
             for model in self.models:
                 model.wind_magnitude = 1
         for model in self.models:
             model.wind_magnitude *= 1.5
         print("Wind magnitude is ", self.models[0].wind_magnitude)
     elif(symbol == key.T):
         if(abs(self.models[0].wind_magnitude) < 1):
             for model in self.models:
                 model.wind_magnitude = 0
         for model in self.models:
             model.wind_magnitude /= 1.5
         print("Wind magnitude is ", self.models[0].wind_magnitude)
     elif(symbol == key.Y):
         for model in self.models:
             model.wind_magnitude *= -1
         print("Wind magnitude is ", self.models[0].wind_magnitude)
     elif(symbol == key.SPACE):
         for model in self.models:
             model.simulate()
     elif(symbol == key.F):
         self.DRAW_LINES = not self.DRAW_LINES
     elif(symbol == key.ESCAPE): # escape
         exit()
def worker(weights, seed, max_len, new_model, train_mode_int = 1):
    # print('WORKER working on environment {}'.format(_new_model.env_name))
    
    train_mode = (train_mode_int == 1)
    new_model.set_model_params(weights)
    reward_list, t_list = simulate(new_model,
                                   train_mode = train_mode, render_mode = False, num_episode = num_episode, seed = seed, max_len = max_len)
    if batch_mode == 'min':
        reward = np.min(reward_list)
    else:
        reward = np.mean(reward_list)
    t = np.mean(t_list)
    return reward, t
Exemplo n.º 8
0
def worker(weights, seed, max_len, new_model, train_mode_int=1):
    train_mode = (train_mode_int == 1)
    new_model.set_model_params(weights)

    reward_list, t_list = simulate(new_model,
                                   train_mode=train_mode, render_mode=True, num_episode=config.NUM_EPISODE, seed=seed,
                                   max_len=max_len)

    if config.BATCH_MODE == 'min':
        reward = np.min(reward_list)
    else:
        reward = np.mean(reward_list)
    t = np.mean(t_list)
    return reward, t
Exemplo n.º 9
0
def worker(weights, seed, max_len, new_model, train_mode_int=1):

  #print('WORKER working on environment {}'.format(_new_model.env_name))

  train_mode = (train_mode_int == 1)
  new_model.set_model_params(weights)
  reward_list, t_list = simulate(new_model,
    train_mode=train_mode, render_mode=False, num_episode=num_episode, seed=seed, max_len=max_len)
  if batch_mode == 'min':
    reward = np.min(reward_list)
  else:
    reward = np.mean(reward_list)
  t = np.mean(t_list)
  return reward, t
Exemplo n.º 10
0
def main():
    import pandas as pd

    df = pd.read_csv("customer-data.csv")

    summary, holdout = reformat.reformat(df)
    print(summary.head())

    pareto, mbg = model.model(holdout)
    print(pareto.params_)
    print(mbg.summary['coef'])

    pred_purchases_pareto, pred_purchases_mbg = model.predictions(
        pareto, mbg, summary)

    pred_clv_pareto, pred_clv_mbg = model.clv(pareto, mbg, summary)

    model.simulate(pareto, mbg)

    visuals.future_prediction_plot(pareto, holdout, n=50)
    visuals.future_prediction_plot(mbg, holdout, n=50)

    visuals.transaction_plot(pareto)
    visuals.transaction_plot(mbg)
Exemplo n.º 11
0
def worker(weights, seed, max_len, new_model, train_mode_int=1):

  #print('WORKER working on environment {}'.format(_new_model.env_name))

  train_mode = (train_mode_int == 1)
  new_model.set_model_params(weights)
  #KOE: Render_mode false below results in a static image of each individual. True shows actual behavior.
  #What I really want is to turn rendering completely off. Haven't figured that out yet.
  reward_list, t_list = simulate(new_model,
    train_mode=train_mode, render_mode=True, num_episode=num_episode, seed=seed, max_len=max_len) #KOE: Debugging. changed rendermode from false to true
  if batch_mode == 'min':
    reward = np.min(reward_list)
  else:
    reward = np.mean(reward_list)
  t = np.mean(t_list)
  return reward, t
Exemplo n.º 12
0
def power_spectrum_transfer_function(frequency,
                                     t,
                                     contrast,
                                     fAMs,
                                     kernel,
                                     nperseg,
                                     amp=1,
                                     **cellparams):
    """
    Calculate the transfer function for a given set of AM (amplitude modulation) frequencies and cell model
    
    Parameters
    ----------
    frequency: float
        The stimulus sine frequency
    t: 1-D array
        The time aray
    contrast: float
        The AM strength (can be between 0 and inf)
    fAMs: 1-D array
        The array containing the AM frequencies 
    kernel: 1-D array
        The kernel array for convolution
    nperseg: float
        The power spectrum nperseg variable
    amp: float
        The amplitude of the sinus wave, set to 1 by default.
    *stimulusparams: list
        List of stimulus parameters. 
    **cellparams: dictionary
        Dictionary containing parameters of the cell model
    
    Returns
    -------
    tfAMs: 1-D array
        The array containing the transfer function values for the given array of fAMs.
    """
    pfAMs = np.zeros(len(fAMs))
    for idx, fAM in enumerate(fAMs):
        stimulus = amp * np.sin(2 * np.pi * frequency * t) * (
            1 + contrast * np.sin(2 * np.pi * fAM * t))
        spiketimes = mod.simulate(stimulus, **cellparams)
        f, p, __ = power_spectrum(stimulus, spiketimes, t, kernel, nperseg)
        power_interpolator = interpolate(f, p)
        pfAMs[idx] = power_interpolator(fAM)
    tfAMs = np.sqrt(pfAMs) / contrast  #transfer function value
    return tfAMs
Exemplo n.º 13
0
def worker(weights, seed, train_mode_int=1, max_len=-1):
  behaviour_char = np.zeros(BC_SIZE)

  train_mode = (train_mode_int == 1)
  model.set_model_params(weights)
  reward_list, bc_list, t_list = simulate(model, train_mode=train_mode, render_mode=False, num_episode=num_episode,
                                          novelty_search=novelty_search, novelty_mode=novelty_mode, seq_len=BC_SEQ_LENGTH,
                                          seed=seed, max_len=max_len)

  if novelty_search:
    # bc_list shape (n_episodes, bc_size)
    behaviour_char = np.array(bc_list).mean(axis=0)
  if batch_mode == 'min':
    reward = np.min(reward_list)
  else:
    reward = np.mean(reward_list)
  t = np.mean(t_list)
  return reward, behaviour_char, t  # bc_list shape (bc_size,)
Exemplo n.º 14
0
def main():
    # tiny example program:

    example_cell_idx = 20

    # load model parameter:
    parameters = load_models("models.csv")

    model_params = parameters[example_cell_idx]
    cell = model_params.pop('cell')
    EODf = model_params.pop('EODf')
    print("Example with cell:", cell)

    # generate EOD-like stimulus with an amplitude step:
    deltat = model_params["deltat"]
    stimulus_length = 2.0  # in seconds
    time = np.arange(0, stimulus_length, deltat)
    # baseline EOD with amplitude 1:
    stimulus = np.sin(2 * np.pi * EODf * time)
    # amplitude step with given contrast:
    t0 = 0.5
    t1 = 1.5
    contrast = 0.3
    stimulus[int(t0 // deltat):int(t1 // deltat)] *= (1.0 + contrast)

    # integrate the model:
    spikes = simulate(stimulus, **model_params)

    # some analysis an dplotting:
    freq = calculate_isi_frequency(spikes, deltat)
    freq_time = np.arange(spikes[0], spikes[-1], deltat)

    fig, axs = plt.subplots(2, 1, sharex="col")

    axs[0].plot(time, stimulus)
    axs[0].set_title("Stimulus")
    axs[0].set_ylabel("Amplitude in mV")

    axs[1].plot(freq_time, freq)
    axs[1].set_title("Model Frequency")
    axs[1].set_ylabel("Frequency in Hz")
    axs[1].set_xlabel("Time in s")
    plt.show()
    plt.close()
Exemplo n.º 15
0
def run(Bnum, indexing):
    prefix = "BUOY_"
    bname = prefix + Bnum
    path = "../../generated_data/" + bname
    PD = model.readposveldata(path)
    s = settings.settings()
    h = s['h']
    trate = s['trate']
    forcevec = settings.forcevec
    (forcenam, folname) = gf.forcedetail(forcevec, trate, h)
    #creation of the folder for storing the simulated data.
    path = path + '/' + folname
    gf.mkdir_p(path)
    ## description of consant ice thickness or not in the simulation.
    [forcevecn, trate] = gf.icethicktyp(forcevec, trate)
    s['trate'] = trate
    logging.info("Model simulation started.")
    PD = model.simulate(s, Bnum, indexing, forcevecn, PD)
    logging.info("Model Simulations done.")
    Xib = PD['Xib']
    Yib = PD['Yib']
    Uibvec = PD['Uibvec']
    hvec = PD['hvec']
    Tib = PD['Tib']
    Xis = PD['Xis']
    Yis = PD['Yis']
    Uisvec = PD['Uisvec']
    ## We will do elaborate plotting part later in postprocessing.
    logging.info("Plotting started")
    plots.plticepos(Xib, Yib, Xis, Yis, path)
    logging.info("Plotting completed for the buoy path.")
    ## Statistic computation.
    (XD, YD) = statscompute(s, Xib, Yib, Xis, Yis)
    (UD, VD) = statscompute(s, Uibvec[:, 0], Uibvec[:, 1], Uisvec[:, 0],
                            Uisvec[:, 1])
    logging.info("Statistics computation done.")
    # simdata2excel(path,Bnum,forcenam,XD,YD,UD,VD,PD)
    simdata2excl(s, path, Bnum, forcenam, XD, YD, UD, VD, PD)
    logging.info("Excel data file created for simulated data.")
    logging.info("Processing completed for buoy: " + bname)
    gf.logcopy(path)
    logging.shutdown()
Exemplo n.º 16
0
def worker(weights, seed, train_mode, max_len=-1):
    model.set_model_params(weights)

    if train_mode == 2:
        model.env.start_accumulate_trajs()

    reward_list, t_list = simulate(model,
                                   train_mode=train_mode == 1,
                                   render_mode=False,
                                   num_episode=num_episode,
                                   seed=seed,
                                   max_len=max_len)

    if train_mode == 2:
        model.env.end_accumulate_trajs()

    if batch_mode == 'min':
        reward = np.min(reward_list)
    else:
        reward = np.mean(reward_list)
    t = np.mean(t_list)
    return reward, t
Exemplo n.º 17
0
                      t) * (1 + contrast * np.sin(2 * np.pi * contrastf * t))

    #kernel parameters
    kernelparams = {
        'sigma': 0.0001,
        'lenfactor': 5,
        'resolution': t_delta
    }  #kernel is muhc shorter for power spectrum

    #create kernel
    kernel, kerneltime = helpers.spike_gauss_kernel(**kernelparams)

    #power spectrum parameters:
    nperseg = 2**15

    spiketimes = mod.simulate(stimulus, **cellparams)

    fexample, pexample, meanspkfr = helpers.power_spectrum(
        stimulus, spiketimes, t, kernel, nperseg)

    pdB = helpers.decibel_transformer(pexample)
    power_interpolator_decibel = interpolate(fexample, pdB)

    #stimulus power spectrum
    fexamplestim, pexamplestim = welch(
        np.abs(stimulus - np.mean(stimulus)), nperseg=nperseg,
        fs=1 / t_delta)  #zero peak of power spectrum is part of the stimulus,
    #which stays even when stimulus mean is substracted.
    #take absolute value to get the envelope
    pdBstim = helpers.decibel_transformer(pexamplestim)
Exemplo n.º 18
0
def plot_selected():
    import model
    cd = pathlib.Path(__file__).absolute().parent
    path_weight_list = [(cd / "param_q1_opt_1E-6.npy", "1E-6"),
                        (cd / "param_q2_opt_1E-6.npy", "1E-6"),
                        (cd / "param_q1_opt_1E-5.npy", "1E-5"),
                        (cd / "param_q2_opt_1E-5.npy", "1E-5"),
                        (cd / "param_q1_opt_1E-4.npy", "1E-4"),
                        (cd / "param_q2_opt_1E-4.npy", "1E-4"),
                        (cd / "param_q1_opt_1E-3.npy", "1E-3"),
                        (cd / "param_q2_opt_1E-3.npy", "1E-3")]
    param_pair_weight_list = [
        (numpy.load(p1), numpy.load(p2), w)
        for (p1, w), (p2, _) in make_pair_list(path_weight_list)
    ]
    x_ticks = [w for _, _, w in param_pair_weight_list]
    pc_vars = numpy.array(
        [get_var(p1, p2) for p1, p2, _ in param_pair_weight_list])
    pc_q1_std = numpy.sqrt(pc_vars[:, 0])
    pc_q2_std = numpy.sqrt(pc_vars[:, 1])
    tau_ises = numpy.array([
        get_tau_ises(*get_taus(p1, p2)) for p1, p2, _ in param_pair_weight_list
    ])
    tau_ises_npy_path = cd / "tau_ises.npy"
    if not tau_ises_npy_path.exists():
        numpy.save(tau_ises_npy_path, tau_ises)
    tau1_ises = tau_ises[:, 0]
    tau2_ises = tau_ises[:, 1]

    height = 55 / 25.4
    width = 84 / 25.4

    f = plt.figure(figsize=(width, height))
    plt.subplots_adjust(top=0.784,
                        bottom=0.207,
                        left=0.202,
                        right=0.792,
                        hspace=0.2,
                        wspace=0.2)
    plt.plot(x_ticks,
             pc_q1_std,
             linestyle="none",
             marker="o",
             markerfacecolor="#00000000",
             markeredgecolor="#000000ff",
             label=r"$\theta_1$")
    plt.plot(x_ticks,
             pc_q2_std,
             linestyle="none",
             marker="x",
             markerfacecolor="#00000000",
             markeredgecolor="#000000ff",
             label=r"$\theta_2$")
    plt.xlabel(r"$w_{\tau}$")
    plt.ylabel(r"Std. dev. of $\theta_1, \theta_2$ [deg]")
    plt.legend()
    plt.gca().twinx()
    plt.plot(x_ticks,
             tau1_ises,
             linestyle="none",
             marker="^",
             markerfacecolor="#00000000",
             markeredgecolor="#000000ff",
             label=r"$\tau_1$")
    plt.plot(x_ticks,
             tau2_ises,
             linestyle="none",
             marker="^",
             markerfacecolor="#000000ff",
             markeredgecolor="#000000ff",
             label=r"$\tau_2$")
    plt.ylabel(r"Int. Sq. of torque [N${}^2$m${}^2$]")
    plt.legend()
    f.savefig("angle_and_torque.svg")

    f = plt.figure(figsize=(width, height))
    plt.subplots_adjust(top=0.975,
                        bottom=0.21,
                        left=0.185,
                        right=0.975,
                        hspace=0.2,
                        wspace=0.2)
    tlist = numpy.linspace(0, 1, 1001)
    p1, p2, w = param_pair_weight_list[3]
    tau1, tau2 = get_tau(p1, p2)
    plt.plot(tlist,
             tau1,
             linestyle="-",
             color="black",
             label=r"$\tau_{{1}}$ ({})".format(w))
    plt.plot(tlist,
             tau2,
             linestyle="-.",
             color="black",
             label=r"$\tau_{{2}}$ ({})".format(w))
    p1, p2, w = param_pair_weight_list[1]
    tau1, tau2 = get_tau(p1, p2)
    plt.plot(tlist,
             tau1,
             linestyle="--",
             color="black",
             label=r"$\tau_{{1}}$ ({})".format(w))
    plt.plot(tlist,
             tau2,
             linestyle=":",
             color="black",
             label=r"$\tau_{{2}}$ ({})".format(w))
    plt.xlabel(r"Time [s]")
    plt.ylabel(r"Torque [Nm]")
    plt.yticks([-25.0, 0.0, 25.0, 50.0])
    plt.grid()
    f.savefig("torque_series.svg")

    f = plt.figure(figsize=(width, height))
    plt.subplots_adjust(top=0.975,
                        bottom=0.21,
                        left=0.185,
                        right=0.975,
                        hspace=0.2,
                        wspace=0.2)
    tlist = numpy.linspace(0, 1, 1001)
    p1, p2, w = param_pair_weight_list[3]
    tau1, tau2 = get_tau(p1, p2)
    q1, q2, _, _ = model.simulate(tau1, tau2, 0)
    plt.plot(tlist,
             q1 * 180 / numpy.pi,
             linestyle="-",
             color="black",
             label=r"$\theta_{{1}}$ ({})".format(w))
    plt.plot(tlist,
             q2 * 180 / numpy.pi,
             linestyle="-.",
             color="black",
             label=r"$\theta_{{2}}$ ({})".format(w))
    p1, p2, w = param_pair_weight_list[1]
    tau1, tau2 = get_tau(p1, p2)
    q1, q2, _, _ = model.simulate(tau1, tau2, 0)
    plt.plot(tlist,
             q1 * 180 / numpy.pi,
             linestyle="--",
             color="black",
             label=r"$\theta_{{1}}$ ({})".format(w))
    plt.plot(tlist,
             q2 * 180 / numpy.pi,
             linestyle=":",
             color="black",
             label=r"$\theta_{{2}}$ ({})".format(w))
    plt.xlabel(r"Time [s]")
    plt.ylabel(r"Posture angle [deg]")
    plt.yticks([-60.0, 0.0, 60.0, 120.0])
    plt.grid()
    f.savefig("angle_series.svg")

    plt.show()
Exemplo n.º 19
0
from model import simulate
import random
from csv import writer

random.seed(97)

size = 50
unblock_input = list(range(1, size**2 + 1))
number_of_observation = 10000
header = ["edge_size", "total_sites", "unblocked_sites"]

with open("data.csv", "w") as data_file:
    file_writer = writer(data_file)
    file_writer.writerow(header)

with open("data.csv", "a") as data_file:
    file_writer = writer(data_file)

    for i in range(number_of_observation):
        new_row = [size, size**2]
        random.shuffle(unblock_input)
        new_row.append(simulate(size, unblock_input))

        file_writer.writerow(new_row)
Exemplo n.º 20
0
 for key, val in solver_settings.items():
     getattr(solver.optimizer, key)(val)
 #update mask info
 model.data = copy.deepcopy(model.data_original)
 [each.apply_mask() for each in model.data]
 if RAXS_FIT:
     raxs_tag, all_tag = get_data_type_tag(model)
     for i in range(len(raxs_tag)):
         model = set_model(model, int(raxs_tag[i] - 100),
                           all_tag.index(raxs_tag[i]))
         #simulate the model first
         print(
             'Starting the RAXS fit, trial {} of {} trials in total'.format(
                 i, len(raxs_tag)))
         print("Simulating the model now ...")
         model.simulate()
         print("Start the fit...")
         solver.StartFit()
         model.save(each_file)
 else:
     #simulate the model first
     print("Simulating the model now ...")
     model.simulate()
     print("Start the fit...")
     solver.StartFit()
     model.save(each_file)
     cov = solver.optimizer.return_covariance_matrix(fom_level=0.2)
     model.save_addition('covariance_matrix', cov)
     sensitivity = solver.optimizer.return_sensitivity(max_epoch=200,
                                                       epoch_step=0.1)
     model.save_addition('sensitivity', str(list(sensitivity)))
Exemplo n.º 21
0
Test
----

Main testing file for model.

"""

import numpy as np
import numpy.testing as npt
import model

p = model.parameter_set(beta=1.0, gamma=0.4)
x0 = np.array([0.99, 0.01])
T = 20

ts, result = model.simulate(p, x0, T=T, dt=0.01)


def test_parameters_set():
    """
    Test that parameter set works correctly.

    """
    beta = 0.1
    gamma = 0.2
    p = model.parameter_set(beta=beta, gamma=gamma)

    npt.assert_equal(beta, p['beta'])
    npt.assert_equal(gamma, p['gamma'])