Esempio n. 1
0
def do_simulations(params):

    rt = []
    response = []
    stimulus = []
    for stim in [1, 0]:

        # get traces:
        x = get_OU_traces(
            v=params['v'],
            ll=params['ll'],
            dc=params['dc'],
            z=params['z'],
            pre_generated=False,
            stim=stim,
            nr_trials=params['nr_trials'],
            tmax=tmax,
            dt=dt,
        )

        # get bounds:
        if params['bound'] == 'default':
            b1, b0 = _bounds(a=params['a'], lower_is_0=False, tmax=tmax, dt=dt)
        elif params['bound'] == 'collapse_linear':
            b1, b0 = _bounds_collapse_linear(a=params['a'],
                                             c1=params['c1'],
                                             c0=params['c0'],
                                             lower_is_0=False,
                                             tmax=tmax,
                                             dt=dt)
        elif params['bound'] == 'collapse_hyperbolic':
            b1, b0 = _bounds_collapse_hyperbolic(a=params['a'],
                                                 c=params['c'],
                                                 lower_is_0=False,
                                                 tmax=tmax,
                                                 dt=dt)

        # apply bounds:
        rt_dum, response_dum = apply_bounds_diff_trace(x=x, b1=b1, b0=b0)

        # store results:
        rt.append((rt_dum * dt) + ndt)
        response.append(response_dum)
        stimulus.append(np.ones(params['nr_trials']) * stim)

    df = pd.DataFrame()
    df.loc[:, 'rt'] = np.concatenate(rt)
    df.loc[:, 'response'] = np.concatenate(response)
    df.loc[:, 'stimulus'] = np.concatenate(stimulus)
    df.loc[:, 'correct'] = np.array(
        np.concatenate(stimulus) == np.concatenate(response), dtype=int)
    df.loc[:, 'subj_idx'] = params['subj_idx']
    df.to_csv(os.path.join(data_folder,
                           'df_{}.csv'.format(params['subj_idx'])))
def do_simulations(params):

    # SIMULATE A SLOWLY AUTOCORRELATED DRIFT BIAS
    v_autocorr = sample_signal(params['nr_trials'],
                               params['v_autocorr'],
                               mu=params['v'],
                               sigma=1)
    dc_autocorr = sample_signal(params['nr_trials'],
                                params['dc_autocorr'],
                                mu=params['dc'],
                                sigma=1)

    # SIMULATE A SEQUENCE OF RANDOM STIMULI
    stims = np.repeat([0, 1], int(params['nr_trials'] / 2))
    np.random.shuffle(stims)

    ## NOW DRAW RESPONSES FROM THAT PROCESS!
    rt = []
    response = []
    stimulus = []
    for t in tqdm(range(params['nr_trials'])):

        # get traces:
        x = get_DDM_traces(
            v=v_autocorr[t],
            z=params['z'],
            dc=dc_autocorr[t],
            dc_slope=0,
            sv=params['sv'],
            stim=stims[t],
            nr_trials=1,
            tmax=tmax,
            dt=dt,
        )

        # get bounds:
        if params['bound'] == 'default':
            b1, b0 = _bounds(a=params['a'], tmax=tmax, dt=dt)
        elif params['bound'] == 'collapse_linear':
            b1, b0 = _bounds_collapse_linear(a=params['a'],
                                             c1=params['c1'],
                                             c0=params['c0'],
                                             tmax=tmax,
                                             dt=dt)
        elif params['bound'] == 'collapse_hyperbolic':
            b1, b0 = _bounds_collapse_hyperbolic(a=params['a'],
                                                 c=params['c'],
                                                 tmax=tmax,
                                                 dt=dt)

        # apply bounds:
        rt_dum, response_dum = apply_bounds_diff_trace(x=x, b1=b1, b0=b0)

        # store results for single rtial
        rt.append((rt_dum * dt) + ndt)
        response.append(response_dum)
        stimulus.append(stims[t])

    df = pd.DataFrame()
    df.loc[:, 'rt'] = np.concatenate(rt)
    df.loc[:, 'response'] = np.concatenate(response)
    df.loc[:, 'stimulus'] = stimulus
    df.loc[:, 'correct'] = np.array(stimulus == np.concatenate(response),
                                    dtype=int)
    df.loc[:, 'subj_idx'] = params['subj_idx']

    # NOW RECODE REPEATS/ALTERNATES INTO RESPONSES
    df['prevresp'] = np.roll(df['response'], 1)
    df['repeat'] = 1 * (df['prevresp'] == df['response'])
    df['choice'] = df['response'].copy()
    df['response'] = df['repeat']

    df.to_csv(os.path.join(data_folder,
                           'df_{}.csv'.format(params['subj_idx'])))
Esempio n. 3
0
    list(np.arange(23, 34)),
]
for i, group in enumerate(groups):
    df = pd.concat([
        pd.read_csv(os.path.join(data_folder, 'df_{}.csv'.format(g)))
        for g in group
    ],
                   axis=0)
    df.to_csv(os.path.join(fits_folder, '2018_ou_data_{}.csv'.format(i + 1)))

# hyperbolic collapse:
fig = plt.figure()
t = np.arange(0, tmax, dt)
b1, b0 = _bounds_collapse_hyperbolic(a=a,
                                     c=c,
                                     lower_is_0=False,
                                     tmax=tmax,
                                     dt=dt)
plt.plot(t, b1)
plt.plot(t, b0)
fig.savefig(os.path.join(fig_folder, 'collapse_hyperbolic.pdf'))

# load ddm results:
for i, group in enumerate(groups):

    # simulated data:
    df = pd.read_csv(
        os.path.join(fits_folder, '2018_ou_data_{}.csv'.format(i + 1)))

    # model params:
    params = []
Esempio n. 4
0
def do_simulations(params):
    rt = []
    response = []
    stimulus = []
    traces = []
    for stim in [1, 0]:

        # get traces:
        x = get_DDM_traces(
            v=params['v'],
            z=params['z'],
            dc=params['dc'],
            dc_slope=params['dc_slope'],
            sv=params['sv'],
            stim=stim,
            nr_trials=params['nr_trials'],
            tmax=tmax,
            dt=dt,
        )

        # get bounds:
        if params['bound'] == 'default':
            b1, b0 = _bounds(a=params['a'], tmax=tmax, dt=dt)
        elif params['bound'] == 'collapse_linear':
            b1, b0 = _bounds_collapse_linear(a=params['a'],
                                             c1=params['c1'],
                                             c0=params['c0'],
                                             tmax=tmax,
                                             dt=dt)
        elif params['bound'] == 'collapse_hyperbolic':
            b1, b0 = _bounds_collapse_hyperbolic(a=params['a'],
                                                 c=params['c'],
                                                 tmax=tmax,
                                                 dt=dt)

        # apply bounds:
        rt_dum, response_dum = apply_bounds_diff_trace(x=x, b1=b1, b0=b0)

        # store results:
        rt.append((rt_dum * dt) + ndt)
        response.append(response_dum)
        stimulus.append(np.ones(params['nr_trials']) * stim)
        traces.append(x)

    df = pd.DataFrame()
    df.loc[:, 'rt'] = np.concatenate(rt)
    df.loc[:, 'response'] = np.concatenate(response)
    df.loc[:, 'stimulus'] = np.concatenate(stimulus)
    df.loc[:, 'correct'] = np.array(
        np.concatenate(stimulus) == np.concatenate(response), dtype=int)
    df.loc[:, 'subj_idx'] = params['subj_idx']
    df.to_csv(os.path.join(data_folder,
                           'df_{}.csv'.format(params['subj_idx'])))

    traces = np.vstack(traces)
    for i in range(traces.shape[0]):
        if sum(traces[i, :] > params['a']) > 0:
            traces[i,
                   np.where(traces[i, :] > params['a'])[0][0]:] = params['a']
        if sum(traces[i, :] < 0) > 0:
            traces[i, np.where(traces[i, :] < 0)[0][0]:] = 0

    hit = np.array((df['stimulus'] == 1) & (df['response'] == 1))
    fa = np.array((df['stimulus'] == 0) & (df['response'] == 1))
    miss = np.array((df['stimulus'] == 1) & (df['response'] == 0))
    cr = np.array((df['stimulus'] == 0) & (df['response'] == 0))

    shell()

    fig = plt.figure(figsize=(2, 2))

    for t in traces[hit, :][0:5000]:
        plt.plot(t, alpha=0.005, lw=1, color='black')
    # for t in traces[fa,:][0:500]:
    #     plt.plot(t, alpha=0.02, color='orange')
    # for t in traces[miss,:][0:500]:
    #     plt.plot(t, alpha=0.02, color='green')
    # for t in traces[cr,:][0:500]:
    #     plt.plot(t, alpha=0.02, color='green')

    for trial, color, alpha in zip([hit, fa, miss, cr],
                                   ['orange', 'orange', 'green', 'green'],
                                   [1, 0.5, 0.5, 1]):
        y = np.nanmean(traces[trial, :], axis=0)
        x = np.arange(y.shape[0])
        ind = np.zeros(y.shape[0], dtype=bool)
        ind[0:20] = True
        (m, b) = sp.polyfit(x[ind], y[ind], 1)
        regression_line = sp.polyval([m, b], x)
        plt.plot(y, color=color, lw=2, alpha=alpha)
        plt.plot(x[ind], regression_line[ind], color='black', lw=1, ls='--')
        print(m)
    plt.axhline(0, color='k', lw=0.5)
    plt.axhline(params['a'], color='k', lw=0.5)
    plt.axhline(params['z'], color='k', lw=0.5)
    plt.xlim(0, 30)
    plt.ylim(-0.1, params['a'] + 0.1)
    plt.xlabel('Timesteps')
    plt.ylabel('Decision variable')
    sns.despine()
    plt.tight_layout()
    fig.savefig(
        os.path.join(fig_folder,
                     'simulate_slopes_{}.pdf'.format(params['subj_idx'])))