Example #1
0
def single_trial(trialsfile, savefile):
    trials, A, R, M, perf = utils.load(trialsfile)
    randomTrial = np.random.randint(0, 200)
    condition = trials[randomTrial]['offer']
    len_go = 200
    len_decision = 2000
    len_ITI = trials[randomTrial]['durations']['ITI'][1] - trials[randomTrial][
        'durations']['ITI'][0]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    rect_go = plt.Rectangle((0, 0), len_go, 1)
    rect_decision = plt.Rectangle((len_go, 0), len_decision, 1)
    rect_ITI = plt.Rectangle((len_go + len_decision, 0), len_ITI, 1)
    ax.add_patch(rect_go)
    ax.add_patch(rect_decision)
    ax.add_patch(rect_ITI)
    for action in A:
        plt.vlines(x, ymin, ymax)
    filename = savefile + '/trial' + str(randomTrial) + '.png'
    print filename
    plt.savefig(filename, format='png')
    plt.show()

    plt.close()
Example #2
0
def sure_stimulus_duration(trialsfile, plot, saved=None, **kwargs):
    if saved is not None:
        psure_by_duration_by_coh = saved
    else:
        trials, A, R, M, perf = utils.load(trialsfile)

        # Sort
        trials_by_cond = {}
        for n, trial in enumerate(trials):
            if not trial['wager']:
                continue

            cond = trial['coh']
            if perf.choices[n] is not None:
                trials_by_cond.setdefault(cond, {'durations': [], 'sures': []})

                duration = np.ptp(trial['durations']['stimulus'])
                trials_by_cond[cond]['durations'].append(duration)
                trials_by_cond[cond]['sures'].append(perf.choices[n] == 'S')

        # Number of bins
        nbins = kwargs.get('nbins', 10)

        # Average
        psure_by_duration_by_coh = {}
        for coh, v in trials_by_cond.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['sures'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            psure = [utils.divide(np.sum(ybin > 0), len(ybin)) for ybin in ybins]
            psure_by_duration_by_coh[coh] = (duration, psure)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lineprop = {'lw':  kwargs.get('lw', 1)}
    dataprop = {'ms':  kwargs.get('ms', 7),
                'mew': kwargs.get('mew', 0)}
    colors = kwargs.get('colors', kiani2009_colors)

    cohs = sorted(psure_by_duration_by_coh)
    for coh in cohs:
        duration, psure = psure_by_duration_by_coh[coh]

        plot.plot(duration, psure, color=colors[coh], label='{}\%'.format(coh), **lineprop)
        plot.plot(duration, psure, 'o', mfc=colors[coh], **dataprop)

    plot.xlim(100, 800)
    plot.ylim(0, 1)

    #=====================================================================================

    return psure_by_duration_by_coh
Example #3
0
def sure_stimulus_duration(trialsfile, plot, saved=None, **kwargs):
    if saved is not None:
        psure_by_duration_by_coh = saved
    else:
        trials, A, R, M, perf = utils.load(trialsfile)

        # Sort
        trials_by_cond = {}
        for n, trial in enumerate(trials):
            if not trial['wager']:
                continue

            cond = trial['coh']
            if perf.choices[n] is not None:
                trials_by_cond.setdefault(cond, {'durations': [], 'sures': []})

                duration = np.ptp(trial['durations']['stimulus'])
                trials_by_cond[cond]['durations'].append(duration)
                trials_by_cond[cond]['sures'].append(perf.choices[n] == 'S')

        # Number of bins
        nbins = kwargs.get('nbins', 10)

        # Average
        psure_by_duration_by_coh = {}
        for coh, v in trials_by_cond.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['sures'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            psure = [utils.divide(np.sum(ybin > 0), len(ybin)) for ybin in ybins]
            psure_by_duration_by_coh[coh] = (duration, psure)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lineprop = {'lw':  kwargs.get('lw', 1)}
    dataprop = {'ms':  kwargs.get('ms', 7),
                'mew': kwargs.get('mew', 0)}
    colors = kwargs.get('colors', kiani2009_colors)

    cohs = sorted(psure_by_duration_by_coh)
    for coh in cohs:
        duration, psure = psure_by_duration_by_coh[coh]

        plot.plot(duration, psure, color=colors[coh], label='{}\%'.format(coh), **lineprop)
        plot.plot(duration, psure, 'o', mfc=colors[coh], **dataprop)

    plot.xlim(100, 800)
    plot.ylim(0, 1)

    #=====================================================================================

    return psure_by_duration_by_coh
Example #4
0
def choice_pattern(trialsfile, offers, plot, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    B_by_offer = {}
    n_nondecision = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        juice_L, juice_R = trial['juice']
        offer = trial['offer']

        if perf.choices[n] == 'B':
            B = 1
        elif perf.choices[n] == 'A':
            B = 0
        else:
            raise ValueError("invalid choice")

        B_by_offer.setdefault(offer, []).append(B)
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    pB_by_offer = {}
    for offer in B_by_offer:
        Bs = B_by_offer[offer]
        pB_by_offer[offer] = utils.divide(sum(Bs), len(Bs))
        #print(offer, pB_by_offer[offer])

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    if plot is None:
        return

    ms = kwargs.get('ms', 7)
    rotation = kwargs.get('rotation', 60)

    for i, offer in enumerate(offers):
        plot.plot(i, 100 * pB_by_offer[offer], 'o', color='k', ms=ms)

    plot.xticks(range(len(offers)))
    plot.xticklabels(['{}B:{}A'.format(*offer) for offer in offers],
                     rotation=rotation)

    plot.xlim(0, len(offers) - 1)
    plot.ylim(0, 100)
Example #5
0
def performance(trialsfile, plot, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    correct_by_cond = {}
    for n, trial in enumerate(trials):
        if not perf.decisions[n]:
            continue

        gt_lt = trial['gt_lt']
        fpair = trial['fpair']
        if gt_lt == '>':
            f1, f2 = fpair
        else:
            f2, f1 = fpair
        cond = (f1, f2)

        correct_by_cond.setdefault(cond, []).append(perf.corrects[n])

    pcorrect_by_cond = {}
    for c in correct_by_cond:
        corrects = correct_by_cond[c]
        pcorrect_by_cond[c] = utils.divide(sum(corrects), len(corrects))

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    plot.equal()

    lw = kwargs.get('lw', 1)
    fontsize = kwargs.get('fontsize', 10)
    _min, _max = kwargs.get('lims', (10 - 4, 34 + 4))
    r = kwargs.get('r', 1.5)

    for (f1, f2), pcorrect in pcorrect_by_cond.items():
        plot.circle((f1, f2), r, ec='none', fc=smap.to_rgba(f1))
        plot.text(f1,
                  f2,
                  '{}'.format(int(100 * pcorrect)),
                  color='w',
                  fontsize=fontsize,
                  ha='center',
                  va='center')

    plot.xlim(_min, _max)
    plot.ylim(_min, _max)
    plot.plot([_min, _max], [_min, _max], color='k', lw=lw)
Example #6
0
def choice_pattern(trialsfile, offers, plot, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    B_by_offer    = {}
    n_nondecision = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        juice_L, juice_R = trial['juice']
        offer = trial['offer']

        if perf.choices[n] == 'B':
            B = 1
        elif perf.choices[n] == 'A':
            B = 0
        else:
            raise ValueError("invalid choice")

        B_by_offer.setdefault(offer, []).append(B)
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    pB_by_offer = {}
    for offer in B_by_offer:
        Bs = B_by_offer[offer]
        pB_by_offer[offer] = utils.divide(sum(Bs), len(Bs))
        #print(offer, pB_by_offer[offer])

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    if plot is None:
        return

    ms       = kwargs.get('ms', 7)
    rotation = kwargs.get('rotation', 60)

    for i, offer in enumerate(offers):
        plot.plot(i, 100*pB_by_offer[offer], 'o', color='k', ms=ms)

    plot.xticks(range(len(offers)))
    plot.xticklabels(['{}B:{}A'.format(*offer) for offer in offers], rotation=rotation)

    plot.xlim(0, len(offers)-1)
    plot.ylim(0, 100)
Example #7
0
def sort(trialsfile, plots, units=None, network='p', **kwargs):
    """
    Sort trials.

    """
    # Load trials
    data = utils.load(trialsfile)
    if len(data) == 9:
        trials, U, Z, A, P, M, perf, r_p, r_v = data
    else:
        trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    # Which network?
    if network == 'p':
        r = r_p
    else:
        r = r_v

    # Data shape
    Ntime = r.shape[0]
    N = r.shape[-1]

    # Same for every trial
    time = trials[0]['time']

    # Aligned time
    time_a = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Sort trials
    #=====================================================================================

    # Sort
    trials_by_cond = {}
    for n, trial in enumerate(trials):
        if perf.choices[n] is None or not perf.corrects[n]:
            continue

        # Condition
        gt_lt = trial['gt_lt']
        fpair = trial['fpair']
        if gt_lt == '>':
            f1, f2 = fpair
        else:
            f2, f1 = fpair
        cond = (f1, f2)

        # Firing rates
        Mn = np.tile(M[:, n], (N, 1)).T
        Rn = r[:, n] * Mn

        # Align point
        t0 = trial['epochs']['f1'][0] - 1

        # Storage
        trials_by_cond.setdefault(cond, {
            'r': np.zeros((Ntime_a, N)),
            'n': np.zeros((Ntime_a, N))
        })

        # Before
        n_b = Rn[:t0].shape[0]
        trials_by_cond[cond]['r'][Ntime - 1 - n_b:Ntime - 1] += Rn[:t0]
        trials_by_cond[cond]['n'][Ntime - 1 - n_b:Ntime - 1] += Mn[:t0]

        # After
        n_a = Rn[t0:].shape[0]
        trials_by_cond[cond]['r'][Ntime - 1:Ntime - 1 + n_a] += Rn[t0:]
        trials_by_cond[cond]['n'][Ntime - 1:Ntime - 1 + n_a] += Mn[t0:]

    # Average
    for cond in trials_by_cond:
        trials_by_cond[cond] = utils.div(trials_by_cond[cond]['r'],
                                         trials_by_cond[cond]['n'])

    #=====================================================================================
    # Plot
    #=====================================================================================

    lw = kwargs.get('lw', 1.5)

    w, = np.where((time_a >= -500) & (time_a <= 4000))

    def plot_sorted(plot, unit):
        t = 1e-3 * time_a[w]
        yall = [[1]]
        for (f1, f2), r in trials_by_cond.items():
            plot.plot(t, r[w, unit], color=smap.to_rgba(f1), lw=lw)
            yall.append(r[w, unit])

        return t, yall

    if units is not None:
        for plot, unit in zip(plots, units):
            plot_sorted(plot, unit)
    else:
        figspath, name = plots
        for unit in xrange(N):
            fig = Figure()
            plot = fig.add()

            #-----------------------------------------------------------------------------

            t, yall = plot_sorted(plot, unit)

            plot.xlim(t[0], t[-1])
            plot.lim('y', yall, lower=0)

            plot.highlight(0, 0.5)
            plot.highlight(3.5, 4)

            #-----------------------------------------------------------------------------

            fig.save(path=figspath,
                     name=name + '_{}{:03d}'.format(network, unit))
            fig.close()
Example #8
0
def choice_pattern(trialsfile, offers, savefile, action, **kwargs):
    # Load trials
    #this is up to date

    trials, A, R, M, perf = utils.load(trialsfile)

    # ComChoice = []
    Choice = []
    Reward = []
    n_nondecision = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        juice = trial['juice']
        offer = trial['offer']

        if perf.choices[n] == 'LEFT':
            C = -1
        elif perf.choices[n] == 'RIGHT':
            C = 1
        else:
            raise ValueError("invalid choice")
        if perf.corrects[n]:
            R = 1
        else:
            R = 0

        Choice.append(C)
        Reward.append(R)
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    # pL_by_offer = {}
    # for offer in B_by_offer:
    #     Bs = B_by_offer[offer]
    #     pB_by_offer[offer] = utils.divide(sum(Bs), len(Bs))
    # print(offer, pB_by_offer[offer])

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    #if plot is None:
    #    return

    # ms       = kwargs.get('ms', 7)
    # rotation = kwargs.get('rotation', 60)

    #for i, offer in enumerate(offers):
    #    plot.plot(i, 100*pB_by_offer[offer], 'o', color='k', ms=ms)
    plt.figure()
    plt.subplot(2, 1, 1)
    plt.bar([x for x in range(1,
                              len(Choice) + 1)],
            [-1 if x == -1 else 0 for x in Choice],
            color='r',
            edgeColor='none')
    plt.bar([x for x in range(1,
                              len(Choice) + 1)],
            [1 if x == 1 else 0 for x in Choice],
            1,
            color='b',
            edgeColor='none')
    plt.ylabel("Animal's choice")
    plt.yticks([-1, 0, 1], ('Left', ' ', 'Right'))
    # plt.yticklabels({'Left', 'Right'});
    plt.xlim(0, len(Choice))

    plt.subplot(2, 1, 2)
    average_reward = sum(Reward) / len(Choice)
    smooth = savgol_filter(Reward, 11, 1)
    plt.bar([x for x in range(1,
                              len(Choice) + 1)],
            Reward,
            color='black',
            edgeColor='none')
    plt.plot([x for x in range(1,
                               len(Choice) + 1)],
             smooth,
             color='red',
             linewidth=2)
    plt.ylabel("Reward")
    plt.yticks([0, 1], ('No reward', 'Reward'))
    plt.text((len(Choice) - 50), 1.05,
             "Average reward: " + str(average_reward))
    # plt.yticklabels({'Left', 'Right'});
    plt.xlim(0, len(Choice))

    filename = savefile + '/' + action + '.png'
    print filename
    plt.savefig(filename, format='png')
    plt.show()

    plt.close()
Example #9
0
y0 = 0.1

dy = 0.05

w = 0.82
h = 0.13

fig.add('trial-5', [x0, y0, w, h])
fig.add('trial-4', [x0, fig[-1].top + dy, w, h])
fig.add('trial-3', [x0, fig[-1].top + dy, w, h])
fig.add('trial-2', [x0, fig[-1].top + dy, w, h])
fig.add('trial-1', [x0, fig[-1].top + dy, w, h])

#=========================================================================================

trials, U, Z, A, rho, M, perf, r_policy, r_value = utils.load(
    rdm_fixed_activity)

inputs = rdm_fixed_model.inputs


def process_trial(plot, n):
    if perf.choices[n] is None:
        print("Trial {}: No decision.".format(n))
        return

    trial = trials[n]
    time = trial['time']
    u = U[:, n]
    z = Z[:, n]

    stimulus = np.asarray(trial['epochs']['stimulus'])
Example #10
0
def value_stimulus_duration(trialsfile, plot, saved=None, **kwargs):
    if saved is not None:
        value_by_duration_by_coh, value_by_duration_by_coh_wager = saved
    else:
        # Load trials
        trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = utils.load(trialsfile)

        # Time
        time = trials[0]['time']

        # Sort
        trials_by_cond       = {}
        trials_by_cond_wager = {}
        for n, trial in enumerate(trials):
            coh = trial['coh']
            if coh == 0 or perf.choices[n] not in ['L', 'R']:
                continue

            cond     = coh
            duration = np.ptp(trial['durations']['stimulus'])

            delay_start, _ = trial['durations']['delay']
            before_sure,   = np.where((delay_start <= time) & (time < delay_start+500))
            value          = np.mean(Z_b[before_sure,n])
            if trial['wager']:
                trials_by_cond_wager.setdefault(cond, {'durations': [], 'values': []})
                trials_by_cond_wager[cond]['durations'].append(duration)
                trials_by_cond_wager[cond]['values'].append(value)
            else:
                trials_by_cond.setdefault(cond, {'durations': [], 'values': []})
                trials_by_cond[cond]['durations'].append(duration)
                trials_by_cond[cond]['values'].append(value)

        # Number of bins
        nbins = kwargs.get('nbins', 10)

        # Average no-wager trials
        value_by_duration_by_coh = {}
        for coh, v in trials_by_cond.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['values'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            value    = [np.mean(ybin) for ybin in ybins]
            value_by_duration_by_coh[coh] = (duration, value)

        # Average wager trials
        value_by_duration_by_coh_wager = {}
        for coh, v in trials_by_cond_wager.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['values'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            value    = [np.mean(ybin) for ybin in ybins]
            value_by_duration_by_coh_wager[coh] = (duration, value)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lineprop = {'ls':     '--',
                'lw':     kwargs.get('lw', 1),
                'dashes': kwargs.get('dashes', [9, 4])}
    dataprop = {'mew': kwargs.get('mew', 1)}
    dataprop['ms'] = kwargs.get('ms', 6) + dataprop['mew']/2

    lineprop_wager = {'lw':  kwargs.get('lw', 1)}
    dataprop_wager = {'ms':  kwargs.get('ms', 7),
                      'mew': kwargs.get('mew', 0)}

    colors = kwargs.get('colors', kiani2009_colors)

    # No-wager trials
    cohs = sorted(value_by_duration_by_coh)
    for coh in cohs:
        duration, value = value_by_duration_by_coh[coh]

        plot.plot(duration, value, color=colors[coh], zorder=10, **lineprop)
        plot.plot(duration, value, 'o', mfc='w', mec=colors[coh],
                  zorder=10, **dataprop)

    # Wager trials
    cohs = sorted(value_by_duration_by_coh_wager)
    for coh in cohs:
        duration, value = value_by_duration_by_coh_wager[coh]

        plot.plot(duration, value, color=colors[coh], zorder=5, **lineprop_wager)
        plot.plot(duration, value, 'o', mfc=colors[coh], mec=colors[coh],
                  zorder=5, **dataprop_wager)

    plot.xlim(100, 800)
    #plot.ylim(0.5, 1)

    #=====================================================================================

    return value_by_duration_by_coh, value_by_duration_by_coh_wager
Example #11
0
y0 = 0.18

w = 0.24
h = 0.71

DX = 0.07

fig.add('Wrec', [x0, y0, w, h])
fig.add('Wrec_lambda', [fig[-1].right + DX, y0, w, h])
fig.add('Wrec_gamma', [fig[-1].right + DX, y0, w, h])

#=========================================================================================

datapath = os.path.join(parent, 'examples', 'work', 'data', modelname)
savefile = os.path.join(datapath, modelname + '.pkl')
save = utils.load(savefile)

masks = save['policy_masks']
params = save['best_policy_params']
N = params['Wrec'].shape[0]

Win = save['best_policy_params']['Win']
if 'Win' in save['policy_masks']:
    Win *= save['policy_masks']['Win']
print(np.mean(Win))
print(np.std(Win))

Win = save['best_baseline_params']['Win']
if 'Win' in save['baseline_masks']:
    Win *= save['baseline_masks']['Win']
print(np.mean(Win))
Example #12
0
def sort(trialsfile, plots, unit=None, network='p', **kwargs):
    # Load trials
    data = utils.load(trialsfile)
    if len(data) == 9:
        trials, U, Z, A, P, M, perf, r_p, r_v = data
    else:
        trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    if network == 'p':
        print("Sorting policy network activity.")
        r = r_p
    else:
        print("Sorting value network activity.")
        r = r_v

    # Number of units
    N = r.shape[-1]

    # Time
    time = trials[0]['time']
    Ntime = len(time)

    # Aligned time
    time_a  = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Preferred targets
    #=====================================================================================

    preferred_targets = get_preferred_targets(trials, perf, r)

    #=====================================================================================
    # No-wager trials
    #=====================================================================================

    def get_no_wager(func_t0):
        trials_by_cond = {}
        for n, trial in enumerate(trials):
            if trial['wager']:
                continue

            if trial['coh'] == 0:
                continue

            if perf.choices[n] is None:
                continue

            cond = trial['left_right']

            m_n = np.tile(M[:,n], (N, 1)).T
            r_n = r[:,n]*m_n

            t0 = func_t0(trial['epochs'], perf.t_choices[n])

            # Storage
            trials_by_cond.setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                             'n': np.zeros((Ntime_a, N))})

            # Before
            n_b = r_n[:t0].shape[0]
            trials_by_cond[cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
            trials_by_cond[cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

            # After
            n_a = r_n[t0:].shape[0]
            trials_by_cond[cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
            trials_by_cond[cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]

        # Average
        for cond in trials_by_cond:
            trials_by_cond[cond] = utils.div(trials_by_cond[cond]['r'],
                                             trials_by_cond[cond]['n'])

        return trials_by_cond

    noTs_stimulus = get_no_wager(lambda epochs, t_choice: epochs['stimulus'][0] - 1)
    noTs_choice   = get_no_wager(lambda epochs, t_choice: t_choice)

    #=====================================================================================
    # Wager trials, aligned to stimulus onset
    #=====================================================================================

    def get_wager(func_t0):
        trials_by_cond      = {}
        trials_by_cond_sure = {}
        for n, trial in enumerate(trials):
            if not trial['wager']:
                continue

            if perf.choices[n] is None:
                continue

            if trial['coh'] == 0:
                continue

            cond = trial['left_right']

            m_n = np.tile(M[:,n], (N, 1)).T
            r_n = r[:,n]*m_n

            t0 = func_t0(trial['epochs'], perf.t_choices[n])

            if perf.choices[n] == 'S':
                # Storage
                trials_by_cond_sure.setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                                      'n': np.zeros((Ntime_a, N))})

                # Before
                n_b = r_n[:t0].shape[0]
                trials_by_cond_sure[cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
                trials_by_cond_sure[cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

                # After
                n_a = r_n[t0:].shape[0]
                trials_by_cond_sure[cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
                trials_by_cond_sure[cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]
            else:
                # Storage
                trials_by_cond.setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                                 'n': np.zeros((Ntime_a, N))})

                # Before
                n_b = r_n[:t0].shape[0]
                trials_by_cond[cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
                trials_by_cond[cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

                # After
                n_a = r_n[t0:].shape[0]
                trials_by_cond[cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
                trials_by_cond[cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]

        # Average
        for cond in trials_by_cond:
            trials_by_cond[cond] = utils.div(trials_by_cond[cond]['r'],
                                             trials_by_cond[cond]['n'])

        # Average
        for cond in trials_by_cond_sure:
            trials_by_cond_sure[cond] = utils.div(trials_by_cond_sure[cond]['r'],
                                                  trials_by_cond_sure[cond]['n'])

        return trials_by_cond, trials_by_cond_sure

    Ts_stimulus, Ts_stimulus_sure = get_wager(lambda epochs, t_choice: epochs['stimulus'][0] - 1)
    Ts_sure, Ts_sure_sure         = get_wager(lambda epochs, t_choice: epochs['sure'][0] - 1)
    Ts_choice, Ts_choice_sure     = get_wager(lambda epochs, t_choice: t_choice)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lw     = kwargs.get('lw', 1.25)
    dashes = kwargs.get('dashes', [3, 1.5])

    in_opp_colors = {-1: '0.6', +1: 'k'}

    def plot_noTs(noTs, plot, unit, tmin, tmax):
        w,   = np.where((tmin <= time_a) & (time_a <= tmax))
        t    = time_a[w]
        yall = [[1]]

        for lr in noTs:
            color = in_opp_colors[lr*preferred_targets[unit]]
            y = noTs[lr][w,unit]
            plot.plot(t, y, color=color, lw=lw)
            yall.append(y)

        plot.xlim(tmin, tmax)
        plot.xticks([0, tmax])
        plot.lim('y', yall, lower=0)

        return yall

    def plot_Ts(Ts, Ts_sure, plot, unit, tmin, tmax):
        w,   = np.where((tmin <= time_a) & (time_a <= tmax))
        t    = time_a[w]
        yall = [[1]]

        for lr in Ts:
            color = in_opp_colors[lr*preferred_targets[unit]]
            y = Ts[lr][w,unit]
            plot.plot(t, y, color=color, lw=lw)
            yall.append(y)
        for lr in Ts_sure:
            color = in_opp_colors[lr*preferred_targets[unit]]
            y = Ts_sure[lr][w,unit]
            plot.plot(t, y, color=color, lw=lw, linestyle='--', dashes=dashes)
            yall.append(y)

        plot.xlim(tmin, tmax)
        plot.xticks([0, tmax])
        plot.lim('y', yall, lower=0)

        return yall

    if unit is not None:
        y = []

        tmin = kwargs.get('noTs-stimulus-tmin', -100)
        tmax = kwargs.get('noTs-stimulus-tmax', 700)
        y += plot_noTs(noTs_stimulus, plots['noTs-stimulus'], unit, tmin, tmax)

        tmin = kwargs.get('noTs-choice-tmin', -500)
        tmax = kwargs.get('noTs-choice-tmax', 0)
        y += plot_noTs(noTs_choice, plots['noTs-choice'], unit, tmin, tmax)

        tmin = kwargs.get('Ts-stimulus-tmin', -100)
        tmax = kwargs.get('Ts-stimulus-tmax', 700)
        y += plot_Ts(Ts_stimulus, Ts_stimulus_sure, plots['Ts-stimulus'], unit, tmin, tmax)

        tmin = kwargs.get('Ts-sure-tmin', -200)
        tmax = kwargs.get('Ts-sure-tmax', 700)
        y += plot_Ts(Ts_sure, Ts_sure_sure, plots['Ts-sure'], unit, tmin, tmax)

        tmin = kwargs.get('Ts-choice-tmin', -500)
        tmax = kwargs.get('Ts-choice-tmax', 0)
        y += plot_Ts(Ts_choice, Ts_choice_sure, plots['Ts-choice'], unit, tmin, tmax)

        return y
    else:
        name = plots
        for unit in xrange(N):
            w   = utils.mm_to_inch(174)
            r   = 0.35
            fig = Figure(w=w, r=r)

            x0 = 0.09
            y0 = 0.15

            w = 0.13
            h = 0.75

            dx = 0.05
            DX = 0.08

            fig.add('noTs-stimulus', [x0, y0, w, h])
            fig.add('noTs-choice',   [fig[-1].right+dx, y0, w, h])
            fig.add('Ts-stimulus',   [fig[-1].right+DX, y0, w, h])
            fig.add('Ts-sure',       [fig[-1].right+dx, y0, w, h])
            fig.add('Ts-choice',     [fig[-1].right+dx, y0, w, h])

            #-----------------------------------------------------------------------------

            y = []

            plot = fig['noTs-stimulus']
            y += plot_noTs(noTs_stimulus, plot, unit, -100, 700)
            plot.vline(0)

            plot = fig['noTs-choice']
            y += plot_noTs(noTs_choice, plot, unit, -500, 200)
            plot.vline(0)

            plot = fig['Ts-stimulus']
            y += plot_Ts(Ts_stimulus, Ts_stimulus_sure, plot, unit, -100, 700)
            plot.vline(0)

            plot = fig['Ts-sure']
            y += plot_Ts(Ts_sure, Ts_sure_sure, plot, unit, -200, 700)
            plot.vline(0)

            plot = fig['Ts-choice']
            y += plot_Ts(Ts_choice, Ts_choice_sure, plot, unit, -500, 200)
            plot.vline(0)

            for plot in fig.plots.values():
                plot.lim('y', y, lower=0)

            #-----------------------------------------------------------------------------

            fig.save(name+'_{}{:03d}'.format(network, unit))
            fig.close()
Example #13
0
original = [modelname]
if modelname == 'postdecisionwager':
    additional = [
        modelname + '_s' + str(i) for i in [101, 102, 103, 104, 1000]
    ]
else:
    additional = [modelname + '_s' + str(i) for i in [101, 102, 103, 104, 105]]
num_trials = []
for name in additional + original:
    # Training history
    datapath = os.path.join(parent, 'examples', 'work', 'data', name)
    savefile = os.path.join(datapath, name + '.pkl')
    if not os.path.isfile(savefile):
        continue

    training_history = utils.load(savefile)['training_history']

    # Time
    timefile = os.path.join(timespath, name + '.txt')
    if os.path.isfile(timefile):
        times.append(np.loadtxt(timefile))
    #-------------------------------------------------------------------------------------

    if name in original:
        color = 'k'
        lw = 1.75
    else:
        color = '0.8'
        lw = 1.25

    #-------------------------------------------------------------------------------------
Example #14
0
y0 = 0.18

w = 0.24
h = 0.71

DX = 0.07

fig.add('Wrec',        [x0, y0, w, h])
fig.add('Wrec_lambda', [fig[-1].right+DX, y0, w, h])
fig.add('Wrec_gamma',  [fig[-1].right+DX, y0, w, h])

#=========================================================================================

datapath = os.path.join(parent, 'examples', 'work', 'data', modelname)
savefile = os.path.join(datapath, modelname+'.pkl')
save     = utils.load(savefile)

masks  = save['policy_masks']
params = save['best_policy_params']
N      = params['Wrec'].shape[0]

Win = save['best_policy_params']['Win']
if 'Win' in save['policy_masks']:
    Win *= save['policy_masks']['Win']
print(np.mean(Win))
print(np.std(Win))

Win = save['best_baseline_params']['Win']
if 'Win' in save['baseline_masks']:
    Win *= save['baseline_masks']['Win']
print(np.mean(Win))
Example #15
0
times = []
xall  = []
original   = [modelname]
if modelname == 'postdecisionwager':
    additional = [modelname+'_s'+str(i) for i in [101, 102, 103, 104, 1000]]
else:
    additional = [modelname+'_s'+str(i) for i in [101, 102, 103, 104, 105]]
num_trials = []
for name in additional + original:
    # Training history
    datapath = os.path.join(parent, 'examples', 'work', 'data', name)
    savefile = os.path.join(datapath, name+'.pkl')
    if not os.path.isfile(savefile):
        continue

    training_history = utils.load(savefile)['training_history']

    # Time
    timefile = os.path.join(timespath, name+'.txt')
    if os.path.isfile(timefile):
        times.append(np.loadtxt(timefile))
    #-------------------------------------------------------------------------------------

    if name in original:
        color = 'k'
        lw    = 1.75
    else:
        color = '0.8'
        lw    = 1.25

    #-------------------------------------------------------------------------------------
Example #16
0
def indifference_point(trialsfile, offers, plot=None, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    B_by_offer    = {}
    n_nondecision = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        juice_L, juice_R = trial['juice']
        offer = trial['offer']

        if perf.choices[n] == 'B':
            B = 1
        elif perf.choices[n] == 'A':
            B = 0
        else:
            raise ValueError("invalid choice")

        B_by_offer.setdefault(offer, []).append(B)
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    pB_by_offer = {}
    for offer in B_by_offer:
        Bs = B_by_offer[offer]
        pB_by_offer[offer] = utils.divide(sum(Bs), len(Bs))
        #print(offer, pB_by_offer[offer])

    X = []
    Y = []
    for i, offer in enumerate(offers):
        B, A = offer
        X.append((B - A)/(B + A))
        Y.append(pB_by_offer[offer])
    X = np.asarray(X)
    Y = np.asarray(Y)

    idx = np.argsort(X)
    X   = X[idx]
    Y   = Y[idx]

    #-------------------------------------------------------------------------------------
    # Fit
    #-------------------------------------------------------------------------------------

    try:
        popt, func = fittools.fit_psychometric(X, Y)

        mu   = popt['mu']
        idpt = (1+mu)/(1-mu)
        print("Indifference point = {}".format(idpt))

        fit_x = np.linspace(min(X), max(X), 201)
        fit_y = func(fit_x, **popt)
        fit   = fit_x, fit_y
    except RuntimeError:
        print("Unable to fit, drawing a line through the points.")
        mu   = None
        idpt = None
        fit  = X, Y

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    if plot is None:
        return idpt

    lw       = kwargs.get('lw', 1.5)
    ms       = kwargs.get('ms', 7)
    rotation = kwargs.get('rotation', 60)

    plot.plot(fit_x, 100*fit_y, '-', color='k', lw=lw)
    plot.plot(X, 100*Y, 'o', color='k', ms=ms)

    plot.hline(50, color='0.5', zorder=2)
    if mu is not None:
        plot.text_upper_left('1A = {:.1f}B'.format(idpt), fontsize=10)
        plot.vline(mu, color='0.5', zorder=2)

    #plot.xticks(range(len(offers)))
    #plot.xticklabels(['{}B:{}A'.format(*offer) for offer in offers], rotation=rotation)

    #plot.xlim(0, len(offers)-1)
    plot.ylim(0, 100)

    return idpt
Example #17
0
def sort_epoch(behaviorfile, activityfile, epoch, offers, plots, units=None, network='p',
               separate_by_choice=False, **kwargs):
    """
    Sort trials.

    """
    # Load trials
    data = utils.load(activityfile)
    trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    if network == 'p':
        print("POLICY NETWORK")
        r = r_p
    else:
        print("VALUE NETWORK")
        r = r_v

    # Number of units
    N = r.shape[-1]

    # Same for every trial
    time  = trials[0]['time']
    Ntime = len(time)

    # Aligned time
    time_a  = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Sort trials
    #=====================================================================================

    # Epochs
    events = ['offer', 'choice']

    # Sort
    events_by_cond = {e: {} for e in events}
    n_by_cond      = {}
    n_nondecision  = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        # Condition
        offer  = trial['offer']
        choice = perf.choices[n]

        if separate_by_choice:
            cond = (offer, choice)
        else:
            cond = offer

        n_by_cond.setdefault(cond, 0)
        n_by_cond[cond] += 1

        # Storage
        for e in events_by_cond:
            events_by_cond[e].setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                                'n': np.zeros((Ntime_a, N))})

        # Firing rates
        m_n = np.tile(M[:,n], (N,1)).T
        r_n = r[:,n]*m_n

        for e in events_by_cond:
            # Align point
            if e == 'offer':
                t0 = trial['epochs']['offer-on'][0]
            elif e == 'choice':
                t0 = perf.t_choices[n]
            else:
                raise ValueError(e)

            # Before
            n_b = r_n[:t0].shape[0]
            events_by_cond[e][cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
            events_by_cond[e][cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

            # After
            n_a = r_n[t0:].shape[0]
            events_by_cond[e][cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
            events_by_cond[e][cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    # Average trials
    for e in events_by_cond:
        for cond in events_by_cond[e]:
            events_by_cond[e][cond] = utils.div(events_by_cond[e][cond]['r'],
                                                events_by_cond[e][cond]['n'])

    # Epochs
    epochs = ['preoffer', 'postoffer', 'latedelay', 'prechoice']

    # Average epochs
    epochs_by_cond = {e: {} for e in epochs}
    for e in epochs_by_cond:
        if e == 'preoffer':
            ev = 'offer'
            w, = np.where((-500 <= time_a) & (time_a < 0))
        elif e == 'postoffer':
            ev = 'offer'
            w, = np.where((0 <= time_a) & (time_a < 500))
        elif e == 'latedelay':
            ev = 'offer'
            w, = np.where((500 <= time_a) & (time_a < 1000))
        elif e == 'prechoice':
            ev = 'choice'
            w, = np.where((-500 <= time_a) & (time_a < 0))
        else:
            raise ValueError(e)

        for cond in events_by_cond[ev]:
            epochs_by_cond[e][cond] = np.mean(events_by_cond[ev][cond][w], axis=0)

    #=====================================================================================
    # Classify units
    #=====================================================================================

    idpt = indifference_point(behaviorfile, offers)
    unit_types = classify_units(trials, perf, r, idpt)
    #unit_types = {}

    numbers = {}
    for v in unit_types.values():
        numbers[v] = 0
    for k, v in unit_types.items():
        numbers[v] += 1

    n_tot = np.sum(numbers.values())
    for k, v in numbers.items():
        print("{}: {}/{} = {}%".format(k, v, n_tot, 100*v/n_tot))

    #=====================================================================================
    # Plot
    #=====================================================================================

    lw  = kwargs.get('lw',  1.5)
    ms  = kwargs.get('ms',  6)
    mew = kwargs.get('mew', 0.5)
    rotation = kwargs.get('rotation', 60)
    #min_trials = kwargs.get('min_trials', 100)

    def plot_activity(plot, unit):
        yall = [1]

        min_trials = 20

        # Pre-offer
        epoch_by_cond = epochs_by_cond['preoffer']
        color = '0.7'
        if separate_by_choice:
            for choice, marker in zip(['A', 'B'], ['d', 'o']):
                x = []
                y = []
                for i, offer in enumerate(offers):
                    cond = (offer, choice)
                    if cond in n_by_cond and n_by_cond[cond] >= min_trials:
                        y_i = epoch_by_cond[cond][unit]
                        plot.plot(i, y_i, marker, mfc=color, mec=color, ms=0.8*ms,
                                  mew=0.8*mew, zorder=10)
                        yall.append(y_i)
                        if i != 0 and i != len(offers)-1:
                            x.append(i)
                            y.append(y_i)
                plot.plot(x, y, '-', color=color, lw=0.8*lw, zorder=5)
        else:
            x = []
            y = []
            for i, offer in enumerate(offers):
                y_i = epoch_by_cond[offer][unit]
                plot.plot(i, y_i, 'o', mfc=color, mec=color, ms=0.8*ms,
                          mew=0.8*mew, zorder=10)
                yall.append(y_i)
                if i != 0 and i != len(offers)-1:
                    x.append(i)
                    y.append(y_i)
            plot.plot(x, y, '-', color=color, lw=0.8*lw, zorder=5)

        # Epoch
        epoch_by_cond = epochs_by_cond[epoch]
        if epoch == 'postoffer':
            color = Figure.colors('darkblue')
        elif epoch == 'latedelay':
            color = Figure.colors('darkblue')
        elif epoch == 'prechoice':
            color = Figure.colors('darkblue')
        else:
            raise ValueError(epoch)
        if separate_by_choice:
            for choice, marker, color in zip(['A', 'B'], ['d', 'o'], [Figure.colors('red'), Figure.colors('blue')]):
                x = []
                y = []
                for i, offer in enumerate(offers):
                    cond = (offer, choice)
                    if cond in n_by_cond and n_by_cond[cond] >= min_trials:
                        y_i = epoch_by_cond[cond][unit]
                        yall.append(y_i)
                        plot.plot(i, y_i, marker, mfc=color, mec=color, ms=ms, mew=mew, zorder=10)
                        if i != 0 and i != len(offers)-1:
                            x.append(i)
                            y.append(y_i)
                plot.plot(x, y, '-', color=color, lw=lw, zorder=5)
        else:
            x = []
            y = []
            for i, offer in enumerate(offers):
                y_i = epoch_by_cond[offer][unit]
                plot.plot(i, y_i, 'o', mfc=color, mec=color, ms=ms, mew=mew, zorder=10)
                yall.append(y_i)
                if i != 0 and i != len(offers)-1:
                    x.append(i)
                    y.append(y_i)
            plot.plot(x, y, '-', color=color, lw=lw, zorder=5)

        plot.xticks(range(len(offers)))
        plot.xticklabels(['{}B:{}A'.format(*offer) for offer in offers],
                         rotation=rotation)

        plot.xlim(0, len(offers)-1)
        plot.lim('y', yall, lower=0)

        return yall

    #-------------------------------------------------------------------------------------

    if units is not None:
        for plot, unit in zip(plots, units):
            plot_activity(plot, unit)
    else:
        name = plots
        for unit in xrange(N):
            fig  = Figure()
            plot = fig.add()

            plot_activity(plot, unit)

            if separate_by_choice:
                suffix = '_sbc'
            else:
                suffix = ''

            if unit in unit_types:
                plot.text_upper_right(unit_types[unit], fontsize=9)

            fig.save(name+'_{}{}_{}{:03d}'.format(epoch, suffix, network, unit))
            fig.close()
Example #18
0
def value_stimulus_duration(trialsfile, plot, saved=None, **kwargs):
    if saved is not None:
        value_by_duration_by_coh, value_by_duration_by_coh_wager = saved
    else:
        # Load trials
        trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = utils.load(trialsfile)

        # Time
        time = trials[0]['time']

        # Sort
        trials_by_cond       = {}
        trials_by_cond_wager = {}
        for n, trial in enumerate(trials):
            coh = trial['coh']
            if coh == 0 or perf.choices[n] not in ['L', 'R']:
                continue

            cond     = coh
            duration = np.ptp(trial['durations']['stimulus'])

            delay_start, _ = trial['durations']['delay']
            before_sure,   = np.where((delay_start <= time) & (time < delay_start+500))
            value          = np.mean(Z_b[before_sure,n])
            if trial['wager']:
                trials_by_cond_wager.setdefault(cond, {'durations': [], 'values': []})
                trials_by_cond_wager[cond]['durations'].append(duration)
                trials_by_cond_wager[cond]['values'].append(value)
            else:
                trials_by_cond.setdefault(cond, {'durations': [], 'values': []})
                trials_by_cond[cond]['durations'].append(duration)
                trials_by_cond[cond]['values'].append(value)

        # Number of bins
        nbins = kwargs.get('nbins', 10)

        # Average no-wager trials
        value_by_duration_by_coh = {}
        for coh, v in trials_by_cond.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['values'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            value    = [np.mean(ybin) for ybin in ybins]
            value_by_duration_by_coh[coh] = (duration, value)

        # Average wager trials
        value_by_duration_by_coh_wager = {}
        for coh, v in trials_by_cond_wager.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['values'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            value    = [np.mean(ybin) for ybin in ybins]
            value_by_duration_by_coh_wager[coh] = (duration, value)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lineprop = {'ls':     '--',
                'lw':     kwargs.get('lw', 1),
                'dashes': kwargs.get('dashes', [9, 4])}
    dataprop = {'mew': kwargs.get('mew', 1)}
    dataprop['ms'] = kwargs.get('ms', 6) + dataprop['mew']/2

    lineprop_wager = {'lw':  kwargs.get('lw', 1)}
    dataprop_wager = {'ms':  kwargs.get('ms', 7),
                      'mew': kwargs.get('mew', 0)}

    colors = kwargs.get('colors', kiani2009_colors)

    # No-wager trials
    cohs = sorted(value_by_duration_by_coh)
    for coh in cohs:
        duration, value = value_by_duration_by_coh[coh]

        plot.plot(duration, value, color=colors[coh], zorder=10, **lineprop)
        plot.plot(duration, value, 'o', mfc='w', mec=colors[coh],
                  zorder=10, **dataprop)

    # Wager trials
    cohs = sorted(value_by_duration_by_coh_wager)
    for coh in cohs:
        duration, value = value_by_duration_by_coh_wager[coh]

        plot.plot(duration, value, color=colors[coh], zorder=5, **lineprop_wager)
        plot.plot(duration, value, 'o', mfc=colors[coh], mec=colors[coh],
                  zorder=5, **dataprop_wager)

    plot.xlim(100, 800)
    #plot.ylim(0.5, 1)

    #=====================================================================================

    return value_by_duration_by_coh, value_by_duration_by_coh_wager
Example #19
0
def sort(trialsfile, plots, units=None, network='p', **kwargs):
    """
    Sort trials.

    """
    # Load trials
    data = utils.load(trialsfile)
    trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    # Which network?
    if network == 'p':
        r = r_p
    else:
        r = r_v

    # Number of units
    N = r.shape[-1]

    # Same for every trial
    time = trials[0]['time']
    Ntime = len(time)

    # Aligned time
    time_a = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Aligned to stimulus onset
    #=====================================================================================

    r_by_cond_stimulus = {}
    n_r_by_cond_stimulus = {}
    for n, trial in enumerate(trials):
        if not perf.decisions[n]:
            continue

        if trial['mod'] == 'va':
            continue
        assert trial['mod'] == 'v' or trial['mod'] == 'a'

        if not perf.corrects[n]:
            continue

        # Condition
        mod = trial['mod']
        choice = perf.choices[n]
        cond = (mod, choice)

        # Storage
        r_by_cond_stimulus.setdefault(cond, np.zeros((Ntime_a, N)))
        n_r_by_cond_stimulus.setdefault(cond, np.zeros((Ntime_a, N)))

        # Firing rates
        Mn = np.tile(M[:, n], (N, 1)).T
        Rn = r[:, n] * Mn

        # Align point
        t0 = trial['epochs']['stimulus'][0] - 1

        # Before
        n_b = Rn[:t0].shape[0]
        r_by_cond_stimulus[cond][Ntime - 1 - n_b:Ntime - 1] += Rn[:t0]
        n_r_by_cond_stimulus[cond][Ntime - 1 - n_b:Ntime - 1] += Mn[:t0]

        # After
        n_a = Rn[t0:].shape[0]
        r_by_cond_stimulus[cond][Ntime - 1:Ntime - 1 + n_a] += Rn[t0:]
        n_r_by_cond_stimulus[cond][Ntime - 1:Ntime - 1 + n_a] += Mn[t0:]

    for cond in r_by_cond_stimulus:
        r_by_cond_stimulus[cond] = utils.div(r_by_cond_stimulus[cond],
                                             n_r_by_cond_stimulus[cond])

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    lw = kwargs.get('lw', 1.5)
    dashes = kwargs.get('dashes', [3, 2])

    vline_props = {'lw': kwargs.get('lw_vline', 0.5)}
    if 'dashes_vline' in kwargs:
        vline_props['linestyle'] = '--'
        vline_props['dashes'] = dashes

    colors_by_mod = {'v': Figure.colors('blue'), 'a': Figure.colors('green')}
    linestyle_by_choice = {'L': '-', 'H': '--'}
    lineprops = dict(lw=lw)

    def plot_sorted(plot, unit, w, r_sorted):
        t = time_a[w]
        yall = [[1]]
        for cond in [('v', 'H'), ('v', 'L'), ('a', 'H'), ('a', 'L')]:
            mod, choice = cond

            if mod == 'v':
                label = 'Vis, '
            elif mod == 'a':
                label = 'Aud, '
            else:
                raise ValueError(mod)

            if choice == 'H':
                label += 'high'
            elif choice == 'L':
                label += 'low'
            else:
                raise ValueError(choice)

            linestyle = linestyle_by_choice[choice]
            if linestyle == '-':
                lineprops = dict(linestyle=linestyle, lw=lw)
            else:
                lineprops = dict(linestyle=linestyle, lw=lw, dashes=dashes)
            plot.plot(t,
                      r_sorted[cond][w, unit],
                      color=colors_by_mod[mod],
                      label=label,
                      **lineprops)
            yall.append(r_sorted[cond][w, unit])

        return t, yall

    def on_stimulus(plot, unit):
        w, = np.where((time_a >= -300) & (time_a <= 1000))
        t, yall = plot_sorted(plot, unit, w, r_by_cond_stimulus)

        plot.xlim(t[0], t[-1])

        return yall

    if units is not None:
        for plot, unit in zip(plots, units):
            on_stimulus(plot, unit)
    else:
        figspath, name = plots
        for unit in xrange(N):
            fig = Figure()
            plot = fig.add()

            #-----------------------------------------------------------------------------

            yall = []
            yall += on_stimulus(plot, unit)

            plot.lim('y', yall, lower=0)
            plot.vline(0)

            plot.xlabel('Time (ms)')
            plot.ylabel('Firing rate (a.u.)')

            #-----------------------------------------------------------------------------

            fig.save(path=figspath,
                     name=name + '_{}{:03d}'.format(network, unit))
            fig.close()
Example #20
0
def sort_epoch(behaviorfile,
               activityfile,
               epoch,
               offers,
               plots,
               units=None,
               network='p',
               separate_by_choice=False,
               **kwargs):
    """
    Sort trials.

    """
    # Load trials
    data = utils.load(activityfile)
    trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    if network == 'p':
        print("POLICY NETWORK")
        r = r_p
    else:
        print("VALUE NETWORK")
        r = r_v

    # Number of units
    N = r.shape[-1]

    # Same for every trial
    time = trials[0]['time']
    Ntime = len(time)

    # Aligned time
    time_a = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Sort trials
    #=====================================================================================

    # Epochs
    events = ['offer', 'choice']

    # Sort
    events_by_cond = {e: {} for e in events}
    n_by_cond = {}
    n_nondecision = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        # Condition
        offer = trial['offer']
        choice = perf.choices[n]

        if separate_by_choice:
            cond = (offer, choice)
        else:
            cond = offer

        n_by_cond.setdefault(cond, 0)
        n_by_cond[cond] += 1

        # Storage
        for e in events_by_cond:
            events_by_cond[e].setdefault(cond, {
                'r': np.zeros((Ntime_a, N)),
                'n': np.zeros((Ntime_a, N))
            })

        # Firing rates
        m_n = np.tile(M[:, n], (N, 1)).T
        r_n = r[:, n] * m_n

        for e in events_by_cond:
            # Align point
            if e == 'offer':
                t0 = trial['epochs']['offer-on'][0]
            elif e == 'choice':
                t0 = perf.t_choices[n]
            else:
                raise ValueError(e)

            # Before
            n_b = r_n[:t0].shape[0]
            events_by_cond[e][cond]['r'][Ntime - 1 - n_b:Ntime - 1] += r_n[:t0]
            events_by_cond[e][cond]['n'][Ntime - 1 - n_b:Ntime - 1] += m_n[:t0]

            # After
            n_a = r_n[t0:].shape[0]
            events_by_cond[e][cond]['r'][Ntime - 1:Ntime - 1 + n_a] += r_n[t0:]
            events_by_cond[e][cond]['n'][Ntime - 1:Ntime - 1 + n_a] += m_n[t0:]
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    # Average trials
    for e in events_by_cond:
        for cond in events_by_cond[e]:
            events_by_cond[e][cond] = utils.div(events_by_cond[e][cond]['r'],
                                                events_by_cond[e][cond]['n'])

    # Epochs
    epochs = ['preoffer', 'postoffer', 'latedelay', 'prechoice']

    # Average epochs
    epochs_by_cond = {e: {} for e in epochs}
    for e in epochs_by_cond:
        if e == 'preoffer':
            ev = 'offer'
            w, = np.where((-500 <= time_a) & (time_a < 0))
        elif e == 'postoffer':
            ev = 'offer'
            w, = np.where((0 <= time_a) & (time_a < 500))
        elif e == 'latedelay':
            ev = 'offer'
            w, = np.where((500 <= time_a) & (time_a < 1000))
        elif e == 'prechoice':
            ev = 'choice'
            w, = np.where((-500 <= time_a) & (time_a < 0))
        else:
            raise ValueError(e)

        for cond in events_by_cond[ev]:
            epochs_by_cond[e][cond] = np.mean(events_by_cond[ev][cond][w],
                                              axis=0)

    #=====================================================================================
    # Classify units
    #=====================================================================================

    idpt = indifference_point(behaviorfile, offers)
    unit_types = classify_units(trials, perf, r, idpt)
    #unit_types = {}

    numbers = {}
    for v in unit_types.values():
        numbers[v] = 0
    for k, v in unit_types.items():
        numbers[v] += 1

    n_tot = np.sum(numbers.values())
    for k, v in numbers.items():
        print("{}: {}/{} = {}%".format(k, v, n_tot, 100 * v / n_tot))

    #=====================================================================================
    # Plot
    #=====================================================================================

    lw = kwargs.get('lw', 1.5)
    ms = kwargs.get('ms', 6)
    mew = kwargs.get('mew', 0.5)
    rotation = kwargs.get('rotation', 60)

    #min_trials = kwargs.get('min_trials', 100)

    def plot_activity(plot, unit):
        yall = [1]

        min_trials = 20

        # Pre-offer
        epoch_by_cond = epochs_by_cond['preoffer']
        color = '0.7'
        if separate_by_choice:
            for choice, marker in zip(['A', 'B'], ['d', 'o']):
                x = []
                y = []
                for i, offer in enumerate(offers):
                    cond = (offer, choice)
                    if cond in n_by_cond and n_by_cond[cond] >= min_trials:
                        y_i = epoch_by_cond[cond][unit]
                        plot.plot(i,
                                  y_i,
                                  marker,
                                  mfc=color,
                                  mec=color,
                                  ms=0.8 * ms,
                                  mew=0.8 * mew,
                                  zorder=10)
                        yall.append(y_i)
                        if i != 0 and i != len(offers) - 1:
                            x.append(i)
                            y.append(y_i)
                plot.plot(x, y, '-', color=color, lw=0.8 * lw, zorder=5)
        else:
            x = []
            y = []
            for i, offer in enumerate(offers):
                y_i = epoch_by_cond[offer][unit]
                plot.plot(i,
                          y_i,
                          'o',
                          mfc=color,
                          mec=color,
                          ms=0.8 * ms,
                          mew=0.8 * mew,
                          zorder=10)
                yall.append(y_i)
                if i != 0 and i != len(offers) - 1:
                    x.append(i)
                    y.append(y_i)
            plot.plot(x, y, '-', color=color, lw=0.8 * lw, zorder=5)

        # Epoch
        epoch_by_cond = epochs_by_cond[epoch]
        if epoch == 'postoffer':
            color = Figure.colors('darkblue')
        elif epoch == 'latedelay':
            color = Figure.colors('darkblue')
        elif epoch == 'prechoice':
            color = Figure.colors('darkblue')
        else:
            raise ValueError(epoch)
        if separate_by_choice:
            for choice, marker, color in zip(
                ['A', 'B'], ['d', 'o'],
                [Figure.colors('red'),
                 Figure.colors('blue')]):
                x = []
                y = []
                for i, offer in enumerate(offers):
                    cond = (offer, choice)
                    if cond in n_by_cond and n_by_cond[cond] >= min_trials:
                        y_i = epoch_by_cond[cond][unit]
                        yall.append(y_i)
                        plot.plot(i,
                                  y_i,
                                  marker,
                                  mfc=color,
                                  mec=color,
                                  ms=ms,
                                  mew=mew,
                                  zorder=10)
                        if i != 0 and i != len(offers) - 1:
                            x.append(i)
                            y.append(y_i)
                plot.plot(x, y, '-', color=color, lw=lw, zorder=5)
        else:
            x = []
            y = []
            for i, offer in enumerate(offers):
                y_i = epoch_by_cond[offer][unit]
                plot.plot(i,
                          y_i,
                          'o',
                          mfc=color,
                          mec=color,
                          ms=ms,
                          mew=mew,
                          zorder=10)
                yall.append(y_i)
                if i != 0 and i != len(offers) - 1:
                    x.append(i)
                    y.append(y_i)
            plot.plot(x, y, '-', color=color, lw=lw, zorder=5)

        plot.xticks(range(len(offers)))
        plot.xticklabels(['{}B:{}A'.format(*offer) for offer in offers],
                         rotation=rotation)

        plot.xlim(0, len(offers) - 1)
        plot.lim('y', yall, lower=0)

        return yall

    #-------------------------------------------------------------------------------------

    if units is not None:
        for plot, unit in zip(plots, units):
            plot_activity(plot, unit)
    else:
        name = plots
        for unit in xrange(N):
            fig = Figure()
            plot = fig.add()

            plot_activity(plot, unit)

            if separate_by_choice:
                suffix = '_sbc'
            else:
                suffix = ''

            if unit in unit_types:
                plot.text_upper_right(unit_types[unit], fontsize=9)

            fig.save(name +
                     '_{}{}_{}{:03d}'.format(epoch, suffix, network, unit))
            fig.close()
              va='top',
              fontsize=fontsize + 0.5)

# Limits
plot.xlim(0, durations['decision'][1])
plot.ylim(y_timeline, y_sure_Ts + 0.2 + 0.35)

#=========================================================================================

plot = fig['sure-stimulus-duration']

savefile = os.path.join(here, 'work', 'data', 'sure_stimulus_duration.pkl')

if 'fast' in sys.argv and os.path.isfile(savefile):
    print("Plotting data in {}".format(savefile))
    saved = utils.load(savefile)
else:
    saved = None

kwargs = dict(ms=3, lw=0.7)
saved = analysis.sure_stimulus_duration(trialsfile_b,
                                        plot,
                                        saved=saved,
                                        nbins=10,
                                        **kwargs)
utils.save(savefile, saved)

plot.xticks([100, 300, 500, 700])
plot.yticks([0, 0.2, 0.4, 0.6, 0.8])

plot.xlim(100, 700)
Example #22
0
def correct_stimulus_duration(trialsfile, plot, saved=None, **kwargs):
    if saved is not None:
        pcorrect_by_duration_by_coh, pcorrect_by_duration_by_coh_wager = saved
    else:
        trials, A, R, M, perf = utils.load(trialsfile)

        # Sort
        trials_by_cond       = {}
        trials_by_cond_wager = {}
        for n, trial in enumerate(trials):
            coh = trial['coh']
            if coh == 0 or perf.choices[n] not in ['L', 'R']:
                continue

            cond     = coh
            duration = np.ptp(trial['durations']['stimulus'])
            if trial['wager']:
                trials_by_cond_wager.setdefault(cond, {'durations': [], 'corrects': []})
                trials_by_cond_wager[cond]['durations'].append(duration)
                trials_by_cond_wager[cond]['corrects'].append(perf.corrects[n])
            else:
                trials_by_cond.setdefault(cond, {'durations': [], 'corrects': []})
                trials_by_cond[cond]['durations'].append(duration)
                trials_by_cond[cond]['corrects'].append(perf.corrects[n])

        # Number of bins
        nbins = kwargs.get('nbins', 10)

        # Average no-wager trials
        pcorrect_by_duration_by_coh = {}
        for coh, v in trials_by_cond.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['corrects'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            pcorrect = [utils.divide(np.sum(ybin > 0), len(ybin)) for ybin in ybins]
            pcorrect_by_duration_by_coh[coh] = (duration, pcorrect)

        # Average wager trials
        pcorrect_by_duration_by_coh_wager = {}
        for coh, v in trials_by_cond_wager.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['corrects'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            pcorrect = [utils.divide(np.sum(ybin > 0), len(ybin)) for ybin in ybins]
            pcorrect_by_duration_by_coh_wager[coh] = (duration, pcorrect)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lineprop = {'ls':     '--',
                'lw':     kwargs.get('lw', 1),
                'dashes': kwargs.get('dashes', [9, 4])}
    dataprop = {'mew': kwargs.get('mew', 1)}
    dataprop['ms'] = kwargs.get('ms', 6) + dataprop['mew']/2

    lineprop_wager = {'lw':  kwargs.get('lw', 1)}
    dataprop_wager = {'ms':  kwargs.get('ms', 7),
                      'mew': kwargs.get('mew', 0)}

    colors = kwargs.get('colors', kiani2009_colors)

    # No-wager trials
    cohs = sorted(pcorrect_by_duration_by_coh)
    for coh in cohs:
        duration, pcorrect = pcorrect_by_duration_by_coh[coh]

        plot.plot(duration, pcorrect, color=colors[coh], zorder=10, **lineprop)
        plot.plot(duration, pcorrect, 'o', mfc='w', mec=colors[coh],
                  zorder=10, **dataprop)

    # Wager trials
    cohs = sorted(pcorrect_by_duration_by_coh_wager)
    for coh in cohs:
        duration, pcorrect = pcorrect_by_duration_by_coh_wager[coh]

        plot.plot(duration, pcorrect, color=colors[coh], zorder=5, **lineprop_wager)
        plot.plot(duration, pcorrect, 'o', mfc=colors[coh], mec=colors[coh],
                  zorder=5, **dataprop_wager)

    plot.xlim(100, 800)
    plot.ylim(0.5, 1)

    #=====================================================================================

    return pcorrect_by_duration_by_coh, pcorrect_by_duration_by_coh_wager
Example #23
0
def indifference_point(trialsfile, offers, plot=None, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    B_by_offer = {}
    n_nondecision = 0
    for n, trial in enumerate(trials):
        if perf.choices[n] is None:
            n_nondecision += 1
            continue

        juice_L, juice_R = trial['juice']
        offer = trial['offer']

        if perf.choices[n] == 'B':
            B = 1
        elif perf.choices[n] == 'A':
            B = 0
        else:
            raise ValueError("invalid choice")

        B_by_offer.setdefault(offer, []).append(B)
    print("Non-decision trials: {}/{}".format(n_nondecision, len(trials)))

    pB_by_offer = {}
    for offer in B_by_offer:
        Bs = B_by_offer[offer]
        pB_by_offer[offer] = utils.divide(sum(Bs), len(Bs))
        #print(offer, pB_by_offer[offer])

    X = []
    Y = []
    for i, offer in enumerate(offers):
        B, A = offer
        X.append((B - A) / (B + A))
        Y.append(pB_by_offer[offer])
    X = np.asarray(X)
    Y = np.asarray(Y)

    idx = np.argsort(X)
    X = X[idx]
    Y = Y[idx]

    #-------------------------------------------------------------------------------------
    # Fit
    #-------------------------------------------------------------------------------------

    try:
        popt, func = fittools.fit_psychometric(X, Y)

        mu = popt['mu']
        idpt = (1 + mu) / (1 - mu)
        print("Indifference point = {}".format(idpt))

        fit_x = np.linspace(min(X), max(X), 201)
        fit_y = func(fit_x, **popt)
        fit = fit_x, fit_y
    except RuntimeError:
        print("Unable to fit, drawing a line through the points.")
        mu = None
        idpt = None
        fit = X, Y

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    if plot is None:
        return idpt

    lw = kwargs.get('lw', 1.5)
    ms = kwargs.get('ms', 7)
    rotation = kwargs.get('rotation', 60)

    plot.plot(fit_x, 100 * fit_y, '-', color='k', lw=lw)
    plot.plot(X, 100 * Y, 'o', color='k', ms=ms)

    plot.hline(50, color='0.5', zorder=2)
    if mu is not None:
        plot.text_upper_left('1A = {:.1f}B'.format(idpt), fontsize=10)
        plot.vline(mu, color='0.5', zorder=2)

    #plot.xticks(range(len(offers)))
    #plot.xticklabels(['{}B:{}A'.format(*offer) for offer in offers], rotation=rotation)

    #plot.xlim(0, len(offers)-1)
    plot.ylim(0, 100)

    return idpt
Example #24
0
def psychometric(trialsfile, plot, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    decision_by_freq = {}
    high_by_freq = {}
    for n, trial in enumerate(trials):
        mod = trial['mod']
        freq = trial['freq']
        decision_by_freq.setdefault(mod, {})
        high_by_freq.setdefault(mod, {})
        decision_by_freq[mod].setdefault(freq, [])
        high_by_freq[mod].setdefault(freq, [])
        if perf.decisions[n]:
            decision_by_freq[mod][freq].append(True)

            if perf.choices[n] == 'H':
                high = 1
            else:
                high = 0

            high_by_freq[mod][freq].append(high)
        else:
            decision_by_freq[mod][freq].append(False)

    freqs = {}
    p_decision = {}
    p_high = {}
    for mod in decision_by_freq:
        freqs[mod] = np.sort(high_by_freq[mod].keys())
        p_decision[mod] = np.zeros(len(freqs[mod]))
        p_high[mod] = np.zeros(len(freqs[mod]))
        for i, freq in enumerate(freqs[mod]):
            p_decision[mod][i] = sum(decision_by_freq[mod][freq]) / len(
                decision_by_freq[mod][freq])
            p_high[mod][i] = utils.divide(sum(high_by_freq[mod][freq]),
                                          len(high_by_freq[mod][freq]))

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    lw = kwargs.get('lw', 1.5)
    ms = kwargs.get('ms', 6)

    all_x = []
    all_y = []
    sigmas = {}
    for mod in ['v', 'a', 'va']:
        if mod == 'v':
            label = 'Visual'
        elif mod == 'a':
            label = 'Auditory'
        elif mod == 'va':
            label = 'Multisensory'
        else:
            raise ValueError

        x = freqs[mod]
        y = p_high[mod]

        # Fit psychometric curve
        props = dict(lw=lw, color=colors[mod], label=label)
        try:
            popt, func = fittools.fit_psychometric(x, y)
            sigmas[mod] = popt['sigma']

            fit_x = np.linspace(min(x), max(x), 201)
            fit_y = func(fit_x, **popt)
            plot.plot(fit_x, 100 * fit_y, **props)
        except RuntimeError:
            print("Unable to fit, drawing a line through the points.")
            plot.plot(x, 100 * y, **props)
        plot.plot(x, 100 * y, 'o', ms=ms, mew=0, mfc=props['color'])
        all_x.append(x)

    # Is it optimal?
    print("")
    print("  Optimality test")
    print("  ---------------")
    print("")
    for mod in ['v', 'a', 'va']:
        print("  sigma_{:<2} = {:.6f}".format(mod, sigmas[mod]))
    print("  1/sigma_v**2 + 1/sigma_a**2 = {:.6f}".format(1 / sigmas['v']**2 +
                                                          1 / sigmas['a']**2))
    print("  1/sigma_va**2               = {:.6f}".format(1 / sigmas['va']**2))

    plot.xlim(np.min(all_x), np.max(all_x))
    plot.ylim(0, 100)
    plot.yticks([0, 50, 100])

    plot.xlabel('Frequency (events/sec)')
    plot.ylabel('Percent high')

    return [sigmas[k] for k in ['v', 'a', 'va']]
Example #25
0
def sort(trialsfile, plots, unit=None, network='p', **kwargs):
    # Load trials
    data = utils.load(trialsfile)
    if len(data) == 9:
        trials, U, Z, A, P, M, perf, r_p, r_v = data
    else:
        trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    if network == 'p':
        print("Sorting policy network activity.")
        r = r_p
    else:
        print("Sorting value network activity.")
        r = r_v

    # Number of units
    N = r.shape[-1]

    # Time
    time = trials[0]['time']
    Ntime = len(time)

    # Aligned time
    time_a  = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Preferred targets
    #=====================================================================================

    preferred_targets = get_preferred_targets(trials, perf, r)

    #=====================================================================================
    # No-wager trials
    #=====================================================================================

    def get_no_wager(func_t0):
        trials_by_cond = {}
        for n, trial in enumerate(trials):
            if trial['wager']:
                continue

            if trial['coh'] == 0:
                continue

            if perf.choices[n] is None:
                continue

            cond = trial['left_right']

            m_n = np.tile(M[:,n], (N, 1)).T
            r_n = r[:,n]*m_n

            t0 = func_t0(trial['epochs'], perf.t_choices[n])

            # Storage
            trials_by_cond.setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                             'n': np.zeros((Ntime_a, N))})

            # Before
            n_b = r_n[:t0].shape[0]
            trials_by_cond[cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
            trials_by_cond[cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

            # After
            n_a = r_n[t0:].shape[0]
            trials_by_cond[cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
            trials_by_cond[cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]

        # Average
        for cond in trials_by_cond:
            trials_by_cond[cond] = utils.div(trials_by_cond[cond]['r'],
                                             trials_by_cond[cond]['n'])

        return trials_by_cond

    noTs_stimulus = get_no_wager(lambda epochs, t_choice: epochs['stimulus'][0] - 1)
    noTs_choice   = get_no_wager(lambda epochs, t_choice: t_choice)

    #=====================================================================================
    # Wager trials, aligned to stimulus onset
    #=====================================================================================

    def get_wager(func_t0):
        trials_by_cond      = {}
        trials_by_cond_sure = {}
        for n, trial in enumerate(trials):
            if not trial['wager']:
                continue

            if perf.choices[n] is None:
                continue

            if trial['coh'] == 0:
                continue

            cond = trial['left_right']

            m_n = np.tile(M[:,n], (N, 1)).T
            r_n = r[:,n]*m_n

            t0 = func_t0(trial['epochs'], perf.t_choices[n])

            if perf.choices[n] == 'S':
                # Storage
                trials_by_cond_sure.setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                                      'n': np.zeros((Ntime_a, N))})

                # Before
                n_b = r_n[:t0].shape[0]
                trials_by_cond_sure[cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
                trials_by_cond_sure[cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

                # After
                n_a = r_n[t0:].shape[0]
                trials_by_cond_sure[cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
                trials_by_cond_sure[cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]
            else:
                # Storage
                trials_by_cond.setdefault(cond, {'r': np.zeros((Ntime_a, N)),
                                                 'n': np.zeros((Ntime_a, N))})

                # Before
                n_b = r_n[:t0].shape[0]
                trials_by_cond[cond]['r'][Ntime-1-n_b:Ntime-1] += r_n[:t0]
                trials_by_cond[cond]['n'][Ntime-1-n_b:Ntime-1] += m_n[:t0]

                # After
                n_a = r_n[t0:].shape[0]
                trials_by_cond[cond]['r'][Ntime-1:Ntime-1+n_a] += r_n[t0:]
                trials_by_cond[cond]['n'][Ntime-1:Ntime-1+n_a] += m_n[t0:]

        # Average
        for cond in trials_by_cond:
            trials_by_cond[cond] = utils.div(trials_by_cond[cond]['r'],
                                             trials_by_cond[cond]['n'])

        # Average
        for cond in trials_by_cond_sure:
            trials_by_cond_sure[cond] = utils.div(trials_by_cond_sure[cond]['r'],
                                                  trials_by_cond_sure[cond]['n'])

        return trials_by_cond, trials_by_cond_sure

    Ts_stimulus, Ts_stimulus_sure = get_wager(lambda epochs, t_choice: epochs['stimulus'][0] - 1)
    Ts_sure, Ts_sure_sure         = get_wager(lambda epochs, t_choice: epochs['sure'][0] - 1)
    Ts_choice, Ts_choice_sure     = get_wager(lambda epochs, t_choice: t_choice)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lw     = kwargs.get('lw', 1.25)
    dashes = kwargs.get('dashes', [3, 1.5])

    in_opp_colors = {-1: '0.6', +1: 'k'}

    def plot_noTs(noTs, plot, unit, tmin, tmax):
        w,   = np.where((tmin <= time_a) & (time_a <= tmax))
        t    = time_a[w]
        yall = [[1]]

        for lr in noTs:
            color = in_opp_colors[lr*preferred_targets[unit]]
            y = noTs[lr][w,unit]
            plot.plot(t, y, color=color, lw=lw)
            yall.append(y)

        plot.xlim(tmin, tmax)
        plot.xticks([0, tmax])
        plot.lim('y', yall, lower=0)

        return yall

    def plot_Ts(Ts, Ts_sure, plot, unit, tmin, tmax):
        w,   = np.where((tmin <= time_a) & (time_a <= tmax))
        t    = time_a[w]
        yall = [[1]]

        for lr in Ts:
            color = in_opp_colors[lr*preferred_targets[unit]]
            y = Ts[lr][w,unit]
            plot.plot(t, y, color=color, lw=lw)
            yall.append(y)
        for lr in Ts_sure:
            color = in_opp_colors[lr*preferred_targets[unit]]
            y = Ts_sure[lr][w,unit]
            plot.plot(t, y, color=color, lw=lw, linestyle='--', dashes=dashes)
            yall.append(y)

        plot.xlim(tmin, tmax)
        plot.xticks([0, tmax])
        plot.lim('y', yall, lower=0)

        return yall

    if unit is not None:
        y = []

        tmin = kwargs.get('noTs-stimulus-tmin', -100)
        tmax = kwargs.get('noTs-stimulus-tmax', 700)
        y += plot_noTs(noTs_stimulus, plots['noTs-stimulus'], unit, tmin, tmax)

        tmin = kwargs.get('noTs-choice-tmin', -500)
        tmax = kwargs.get('noTs-choice-tmax', 0)
        y += plot_noTs(noTs_choice, plots['noTs-choice'], unit, tmin, tmax)

        tmin = kwargs.get('Ts-stimulus-tmin', -100)
        tmax = kwargs.get('Ts-stimulus-tmax', 700)
        y += plot_Ts(Ts_stimulus, Ts_stimulus_sure, plots['Ts-stimulus'], unit, tmin, tmax)

        tmin = kwargs.get('Ts-sure-tmin', -200)
        tmax = kwargs.get('Ts-sure-tmax', 700)
        y += plot_Ts(Ts_sure, Ts_sure_sure, plots['Ts-sure'], unit, tmin, tmax)

        tmin = kwargs.get('Ts-choice-tmin', -500)
        tmax = kwargs.get('Ts-choice-tmax', 0)
        y += plot_Ts(Ts_choice, Ts_choice_sure, plots['Ts-choice'], unit, tmin, tmax)

        return y
    else:
        name = plots
        for unit in xrange(N):
            w   = utils.mm_to_inch(174)
            r   = 0.35
            fig = Figure(w=w, r=r)

            x0 = 0.09
            y0 = 0.15

            w = 0.13
            h = 0.75

            dx = 0.05
            DX = 0.08

            fig.add('noTs-stimulus', [x0, y0, w, h])
            fig.add('noTs-choice',   [fig[-1].right+dx, y0, w, h])
            fig.add('Ts-stimulus',   [fig[-1].right+DX, y0, w, h])
            fig.add('Ts-sure',       [fig[-1].right+dx, y0, w, h])
            fig.add('Ts-choice',     [fig[-1].right+dx, y0, w, h])

            #-----------------------------------------------------------------------------

            y = []

            plot = fig['noTs-stimulus']
            y += plot_noTs(noTs_stimulus, plot, unit, -100, 700)
            plot.vline(0)

            plot = fig['noTs-choice']
            y += plot_noTs(noTs_choice, plot, unit, -500, 200)
            plot.vline(0)

            plot = fig['Ts-stimulus']
            y += plot_Ts(Ts_stimulus, Ts_stimulus_sure, plot, unit, -100, 700)
            plot.vline(0)

            plot = fig['Ts-sure']
            y += plot_Ts(Ts_sure, Ts_sure_sure, plot, unit, -200, 700)
            plot.vline(0)

            plot = fig['Ts-choice']
            y += plot_Ts(Ts_choice, Ts_choice_sure, plot, unit, -500, 200)
            plot.vline(0)

            for plot in fig.plots.values():
                plot.lim('y', y, lower=0)

            #-----------------------------------------------------------------------------

            fig.save(name+'_{}{:03d}'.format(network, unit))
            fig.close()
Example #26
0
    args = args[1:]
    if len(args) > 0:
        action = args[0]
        args = args[1:]
    else:
        action = None
        args = []

    # Copy the savefile for safe access
    if os.path.isfile(savefile):
        base, ext = os.path.splitext(savefile)
        savefile_copy = base + '_copy.pkl'
        while True:
            shutil.copy(savefile, savefile_copy)
            try:
                utils.load(savefile_copy)
                break
            except EOFError:
                continue
    else:
        print("File {} doesn't exist.".format(savefile))

    # Pass everything on
    config = {
        'seed': 1,
        'suffix': suffix,
        'model': model,
        'savefile': savefile_copy,
        'datapath': datapath,
        'figspath': figspath,
        'trialspath': trialspath
Example #27
0
def psychometric(trialsfile, plot, **kwargs):
    # Load trials
    trials, A, R, M, perf = utils.load(trialsfile)

    decision_by_freq = {}
    high_by_freq     = {}
    for n, trial in enumerate(trials):
        mod  = trial['mod']
        freq = trial['freq']
        decision_by_freq.setdefault(mod, {})
        high_by_freq.setdefault(mod, {})
        decision_by_freq[mod].setdefault(freq, [])
        high_by_freq[mod].setdefault(freq, [])
        if perf.decisions[n]:
            decision_by_freq[mod][freq].append(True)

            if perf.choices[n] == 'H':
                high = 1
            else:
                high = 0

            high_by_freq[mod][freq].append(high)
        else:
            decision_by_freq[mod][freq].append(False)

    freqs      = {}
    p_decision = {}
    p_high     = {}
    for mod in decision_by_freq:
        freqs[mod]      = np.sort(high_by_freq[mod].keys())
        p_decision[mod] = np.zeros(len(freqs[mod]))
        p_high[mod]     = np.zeros(len(freqs[mod]))
        for i, freq in enumerate(freqs[mod]):
            p_decision[mod][i] = sum(decision_by_freq[mod][freq])/len(decision_by_freq[mod][freq])
            p_high[mod][i]     = utils.divide(sum(high_by_freq[mod][freq]), len(high_by_freq[mod][freq]))

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    lw = kwargs.get('lw', 1.5)
    ms = kwargs.get('ms', 6)

    all_x  = []
    all_y  = []
    sigmas = {}
    for mod in ['v', 'a', 'va']:
        if mod == 'v':
            label = 'Visual'
        elif mod == 'a':
            label = 'Auditory'
        elif mod == 'va':
            label = 'Multisensory'
        else:
            raise ValueError

        x = freqs[mod]
        y = p_high[mod]

        # Fit psychometric curve
        props = dict(lw=lw, color=colors[mod], label=label)
        try:
            popt, func = fittools.fit_psychometric(x, y)
            sigmas[mod] = popt['sigma']

            fit_x = np.linspace(min(x), max(x), 201)
            fit_y = func(fit_x, **popt)
            plot.plot(fit_x, 100*fit_y, **props)
        except RuntimeError:
            print("Unable to fit, drawing a line through the points.")
            plot.plot(x, 100*y, **props)
        plot.plot(x, 100*y, 'o', ms=ms, mew=0, mfc=props['color'])
        all_x.append(x)

    # Is it optimal?
    print("")
    print("  Optimality test")
    print("  ---------------")
    print("")
    for mod in ['v', 'a', 'va']:
        print("  sigma_{:<2} = {:.6f}".format(mod, sigmas[mod]))
    print("  1/sigma_v**2 + 1/sigma_a**2 = {:.6f}"
          .format(1/sigmas['v']**2 + 1/sigmas['a']**2))
    print("  1/sigma_va**2               = {:.6f}".format(1/sigmas['va']**2))

    plot.xlim(np.min(all_x), np.max(all_x))
    plot.ylim(0, 100)
    plot.yticks([0, 50, 100])

    plot.xlabel('Frequency (events/sec)')
    plot.ylabel('Percent high')

    return [sigmas[k] for k in ['v', 'a', 'va']]
Example #28
0
File: do.py Project: frsong/pyrl
    args = args[1:]
    if len(args) > 0:
        action = args[0]
        args   = args[1:]
    else:
        action = None
        args   = []

    # Copy the savefile for safe access
    if os.path.isfile(savefile):
        base, ext = os.path.splitext(savefile)
        savefile_copy = base + '_copy.pkl'
        while True:
            shutil.copy(savefile, savefile_copy)
            try:
                utils.load(savefile_copy)
                break
            except EOFError:
                continue
    else:
        print("File {} doesn't exist.".format(savefile))

    # Pass everything on
    config = {
        'seed':       1,
        'suffix':     suffix,
        'model':      model,
        'savefile':   savefile_copy,
        'datapath':   datapath,
        'figspath':   figspath,
        'trialspath': trialspath
Example #29
0
    plot.text(np.mean(durations[e]), y_timeline-0.11, label, ha='center', va='top',
              fontsize=fontsize+0.5)

# Limits
plot.xlim(0, durations['decision'][1])
plot.ylim(y_timeline, y_sure_Ts+0.2+0.35)

#=========================================================================================

plot = fig['sure-stimulus-duration']

savefile = os.path.join(here, 'work', 'data', 'sure_stimulus_duration.pkl')

if 'fast' in sys.argv and os.path.isfile(savefile):
    print("Plotting data in {}".format(savefile))
    saved = utils.load(savefile)
else:
    saved = None

kwargs = dict(ms=3, lw=0.7)
saved  = analysis.sure_stimulus_duration(trialsfile_b, plot, saved=saved,
                                         nbins=10, **kwargs)
utils.save(savefile, saved)

plot.xticks([100, 300, 500, 700])
plot.yticks([0, 0.2, 0.4, 0.6, 0.8])

plot.xlim(100, 700)
plot.ylim(0, 0.8)

plot.xlabel('Stimulus duration (ms)')
Example #30
0
def correct_stimulus_duration(trialsfile, plot, saved=None, **kwargs):
    if saved is not None:
        pcorrect_by_duration_by_coh, pcorrect_by_duration_by_coh_wager = saved
    else:
        trials, A, R, M, perf = utils.load(trialsfile)

        # Sort
        trials_by_cond       = {}
        trials_by_cond_wager = {}
        for n, trial in enumerate(trials):
            coh = trial['coh']
            if coh == 0 or perf.choices[n] not in ['L', 'R']:
                continue

            cond     = coh
            duration = np.ptp(trial['durations']['stimulus'])
            if trial['wager']:
                trials_by_cond_wager.setdefault(cond, {'durations': [], 'corrects': []})
                trials_by_cond_wager[cond]['durations'].append(duration)
                trials_by_cond_wager[cond]['corrects'].append(perf.corrects[n])
            else:
                trials_by_cond.setdefault(cond, {'durations': [], 'corrects': []})
                trials_by_cond[cond]['durations'].append(duration)
                trials_by_cond[cond]['corrects'].append(perf.corrects[n])

        # Number of bins
        nbins = kwargs.get('nbins', 10)

        # Average no-wager trials
        pcorrect_by_duration_by_coh = {}
        for coh, v in trials_by_cond.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['corrects'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            pcorrect = [utils.divide(np.sum(ybin > 0), len(ybin)) for ybin in ybins]
            pcorrect_by_duration_by_coh[coh] = (duration, pcorrect)

        # Average wager trials
        pcorrect_by_duration_by_coh_wager = {}
        for coh, v in trials_by_cond_wager.items():
            (xbins, ybins, xedges,
             binsizes) = datatools.partition(v['durations'], v['corrects'], nbins=nbins)
            duration = [np.mean(xbin) for xbin in xbins]
            pcorrect = [utils.divide(np.sum(ybin > 0), len(ybin)) for ybin in ybins]
            pcorrect_by_duration_by_coh_wager[coh] = (duration, pcorrect)

    #=====================================================================================
    # Plot
    #=====================================================================================

    lineprop = {'ls':     '--',
                'lw':     kwargs.get('lw', 1),
                'dashes': kwargs.get('dashes', [9, 4])}
    dataprop = {'mew': kwargs.get('mew', 1)}
    dataprop['ms'] = kwargs.get('ms', 6) + dataprop['mew']/2

    lineprop_wager = {'lw':  kwargs.get('lw', 1)}
    dataprop_wager = {'ms':  kwargs.get('ms', 7),
                      'mew': kwargs.get('mew', 0)}

    colors = kwargs.get('colors', kiani2009_colors)

    # No-wager trials
    cohs = sorted(pcorrect_by_duration_by_coh)
    for coh in cohs:
        duration, pcorrect = pcorrect_by_duration_by_coh[coh]

        plot.plot(duration, pcorrect, color=colors[coh], zorder=10, **lineprop)
        plot.plot(duration, pcorrect, 'o', mfc='w', mec=colors[coh],
                  zorder=10, **dataprop)

    # Wager trials
    cohs = sorted(pcorrect_by_duration_by_coh_wager)
    for coh in cohs:
        duration, pcorrect = pcorrect_by_duration_by_coh_wager[coh]

        plot.plot(duration, pcorrect, color=colors[coh], zorder=5, **lineprop_wager)
        plot.plot(duration, pcorrect, 'o', mfc=colors[coh], mec=colors[coh],
                  zorder=5, **dataprop_wager)

    plot.xlim(100, 800)
    plot.ylim(0.5, 1)

    #=====================================================================================

    return pcorrect_by_duration_by_coh, pcorrect_by_duration_by_coh_wager
Example #31
0
def sort(trialsfile, plots, units=None, network='p', **kwargs):
    """
    Sort trials.

    """
    # Load trials
    data = utils.load(trialsfile)
    trials, U, Z, Z_b, A, P, M, perf, r_p, r_v = data

    # Which network?
    if network == 'p':
        r = r_p
    else:
        r = r_v

    # Number of units
    N = r.shape[-1]

    # Same for every trial
    time  = trials[0]['time']
    Ntime = len(time)

    # Aligned time
    time_a  = np.concatenate((-time[1:][::-1], time))
    Ntime_a = len(time_a)

    #=====================================================================================
    # Aligned to stimulus onset
    #=====================================================================================

    r_by_cond_stimulus   = {}
    n_r_by_cond_stimulus = {}
    for n, trial in enumerate(trials):
        if not perf.decisions[n]:
            continue

        if trial['mod'] == 'va':
            continue
        assert trial['mod'] == 'v' or trial['mod'] == 'a'

        if not perf.corrects[n]:
            continue

        # Condition
        mod    = trial['mod']
        choice = perf.choices[n]
        cond   = (mod, choice)

        # Storage
        r_by_cond_stimulus.setdefault(cond, np.zeros((Ntime_a, N)))
        n_r_by_cond_stimulus.setdefault(cond, np.zeros((Ntime_a, N)))

        # Firing rates
        Mn = np.tile(M[:,n], (N,1)).T
        Rn = r[:,n]*Mn

        # Align point
        t0 = trial['epochs']['stimulus'][0] - 1

        # Before
        n_b = Rn[:t0].shape[0]
        r_by_cond_stimulus[cond][Ntime-1-n_b:Ntime-1]   += Rn[:t0]
        n_r_by_cond_stimulus[cond][Ntime-1-n_b:Ntime-1] += Mn[:t0]

        # After
        n_a = Rn[t0:].shape[0]
        r_by_cond_stimulus[cond][Ntime-1:Ntime-1+n_a]   += Rn[t0:]
        n_r_by_cond_stimulus[cond][Ntime-1:Ntime-1+n_a] += Mn[t0:]

    for cond in r_by_cond_stimulus:
        r_by_cond_stimulus[cond] = utils.div(r_by_cond_stimulus[cond],
                                             n_r_by_cond_stimulus[cond])

    #-------------------------------------------------------------------------------------
    # Plot
    #-------------------------------------------------------------------------------------

    lw     = kwargs.get('lw', 1.5)
    dashes = kwargs.get('dashes', [3, 2])

    vline_props = {'lw': kwargs.get('lw_vline', 0.5)}
    if 'dashes_vline' in kwargs:
        vline_props['linestyle'] = '--'
        vline_props['dashes']    = dashes

    colors_by_mod = {
        'v': Figure.colors('blue'),
        'a': Figure.colors('green')
        }
    linestyle_by_choice = {
        'L': '-',
        'H': '--'
        }
    lineprops = dict(lw=lw)

    def plot_sorted(plot, unit, w, r_sorted):
        t = time_a[w]
        yall = [[1]]
        for cond in [('v', 'H'), ('v', 'L'), ('a', 'H'), ('a', 'L')]:
            mod, choice = cond

            if mod == 'v':
                label = 'Vis, '
            elif mod == 'a':
                label = 'Aud, '
            else:
                raise ValueError(mod)

            if choice == 'H':
                label += 'high'
            elif choice == 'L':
                label += 'low'
            else:
                raise ValueError(choice)

            linestyle = linestyle_by_choice[choice]
            if linestyle == '-':
                lineprops = dict(linestyle=linestyle, lw=lw)
            else:
                lineprops = dict(linestyle=linestyle, lw=lw, dashes=dashes)
            plot.plot(t, r_sorted[cond][w,unit],
                      color=colors_by_mod[mod],
                      label=label,
                      **lineprops)
            yall.append(r_sorted[cond][w,unit])

        return t, yall

    def on_stimulus(plot, unit):
        w, = np.where((time_a >= -300) & (time_a <= 1000))
        t, yall = plot_sorted(plot, unit, w, r_by_cond_stimulus)

        plot.xlim(t[0], t[-1])

        return yall

    if units is not None:
        for plot, unit in zip(plots, units):
            on_stimulus(plot, unit)
    else:
        figspath, name = plots
        for unit in xrange(N):
            fig  = Figure()
            plot = fig.add()

            #-----------------------------------------------------------------------------

            yall = []
            yall += on_stimulus(plot, unit)

            plot.lim('y', yall, lower=0)
            plot.vline(0)

            plot.xlabel('Time (ms)')
            plot.ylabel('Firing rate (a.u.)')

            #-----------------------------------------------------------------------------

            fig.save(path=figspath, name=name+'_{}{:03d}'.format(network, unit))
            fig.close()