Пример #1
0
def psychometric_choicefamily_2D(model_dir,
                                 rule,
                                 lesion_units=None,
                                 n_coh=8,
                                 n_stim_loc=20,
                                 coh_range=0.1):
    # Generate task parameters for choice tasks
    # coh_range = 0.2
    # coh_range = 0.05
    cohs = np.linspace(-coh_range, coh_range, n_coh)

    batch_size = n_stim_loc * n_coh**2
    batch_shape = (n_stim_loc, n_coh, n_coh)
    ind_stim_loc, ind_stim_mod1, ind_stim_mod2 = np.unravel_index(
        range(batch_size), batch_shape)

    # Looping target location
    stim1_locs = 2 * np.pi * ind_stim_loc / n_stim_loc
    stim2_locs = (stim1_locs + np.pi) % (2 * np.pi)

    stim_mod1_cohs = cohs[ind_stim_mod1]
    stim_mod2_cohs = cohs[ind_stim_mod2]

    params_dict = dict()
    params_dict['dm1'] = \
         {'stim1_locs' : stim1_locs,
          'stim2_locs' : stim2_locs,
          'stim1_strengths' : 1 + stim_mod1_cohs, # Just use mod 1 value
          'stim2_strengths' : 1 - stim_mod1_cohs,
          'stim_time'    : 800
          }
    params_dict['dm2'] = params_dict['dm1']

    params_dict['contextdm1'] = \
         {'stim1_locs' : stim1_locs,
          'stim2_locs' : stim2_locs,
          'stim1_mod1_strengths' : 1 + stim_mod1_cohs,
          'stim2_mod1_strengths' : 1 - stim_mod1_cohs,
          'stim1_mod2_strengths' : 1 + stim_mod2_cohs,
          'stim2_mod2_strengths' : 1 - stim_mod2_cohs,
          'stim_time'    : 800
          }

    params_dict['contextdm2'] = params_dict['contextdm1']

    params_dict['contextdelaydm1'] = params_dict['contextdm1']
    params_dict['contextdelaydm1']['stim_time'] = 800
    params_dict['contextdelaydm2'] = params_dict['contextdelaydm1']

    params_dict['multidm'] = \
         {'stim1_locs' : stim1_locs,
          'stim2_locs' : stim2_locs,
          'stim1_mod1_strengths' : 1 + stim_mod1_cohs,
          'stim2_mod1_strengths' : 1 - stim_mod1_cohs,
          'stim1_mod2_strengths' : 1 + stim_mod1_cohs, # Same as Mod 1
          'stim2_mod2_strengths' : 1 - stim_mod1_cohs,
          'stim_time'    : 800
          }

    params_dict['contextdelaydm1'] = \
         {'stim1_locs' : stim1_locs,
          'stim2_locs' : stim2_locs,
          'stim1_mod1_strengths' : 1 + stim_mod1_cohs,
          'stim2_mod1_strengths' : 1 - stim_mod1_cohs,
          'stim1_mod2_strengths' : 1 + stim_mod2_cohs,
          'stim2_mod2_strengths' : 1 - stim_mod2_cohs,
          'stim_time'    : 800
          }

    model = Model(model_dir)
    hp = model.hp
    with tf.Session() as sess:
        model.restore()
        model.lesion_units(sess, lesion_units)

        params = params_dict[rule]
        trial = generate_trials(rule, hp, 'psychometric', params=params)
        feed_dict = tools.gen_feed_dict(model, trial, hp)
        y_sample, y_loc_sample = sess.run([model.y_hat, model.y_hat_loc],
                                          feed_dict=feed_dict)

    # Compute the overall performance.
    # Importantly, discard trials where no decision was made
    loc_cor = trial.y_loc[-1]  # last time point, correct locations
    loc_err = (loc_cor + np.pi) % (2 * np.pi)
    choose_cor = (get_dist(y_loc_sample[-1] - loc_cor) < THETA).sum()
    choose_err = (get_dist(y_loc_sample[-1] - loc_err) < THETA).sum()
    perf = choose_cor / (choose_cor + choose_err)

    # Compute the proportion of choosing choice 1 and maintain the batch_shape
    stim1_locs_ = np.reshape(stim1_locs, batch_shape)
    stim2_locs_ = np.reshape(stim2_locs, batch_shape)

    y_loc_sample = np.reshape(y_loc_sample[-1], batch_shape)
    choose1 = (get_dist(y_loc_sample - stim1_locs_) < THETA).sum(axis=0)
    choose2 = (get_dist(y_loc_sample - stim2_locs_) < THETA).sum(axis=0)
    prop1s = choose1 / (choose1 + choose2)

    return perf, prop1s, cohs
    def lesions(self):
        labels = self.labels

        from network import get_perf
        from task import generate_trials

        # The first will be the intact network
        lesion_units_list = [None]
        for il, l in enumerate(self.unique_labels):
            ind_l = np.where(labels == l)[0]
            # In original indices
            lesion_units_list += [self.ind_active[ind_l]]

        perfs_store_list = list()
        perfs_changes = list()
        cost_store_list = list()
        cost_changes = list()

        for i, lesion_units in enumerate(lesion_units_list):
            model = Model(self.model_dir)
            hp = model.hp
            with tf.Session() as sess:
                model.restore()
                model.lesion_units(sess, lesion_units)

                perfs_store = list()
                cost_store = list()
                for rule in self.rules:
                    n_rep = 16
                    batch_size_test = 256
                    batch_size_test_rep = int(batch_size_test / n_rep)
                    clsq_tmp = list()
                    perf_tmp = list()
                    for i_rep in range(n_rep):
                        trial = generate_trials(rule,
                                                hp,
                                                'random',
                                                batch_size=batch_size_test_rep)
                        feed_dict = tools.gen_feed_dict(model, trial, hp)
                        y_hat_test, c_lsq = sess.run(
                            [model.y_hat, model.cost_lsq], feed_dict=feed_dict)

                        # Cost is first summed over time, and averaged across batch and units
                        # We did the averaging over time through c_mask

                        # IMPORTANT CHANGES: take overall mean
                        perf_test = np.mean(get_perf(y_hat_test, trial.y_loc))
                        clsq_tmp.append(c_lsq)
                        perf_tmp.append(perf_test)

                    perfs_store.append(np.mean(perf_tmp))
                    cost_store.append(np.mean(clsq_tmp))

            perfs_store = np.array(perfs_store)
            cost_store = np.array(cost_store)

            perfs_store_list.append(perfs_store)
            cost_store_list.append(cost_store)

            if i > 0:
                perfs_changes.append(perfs_store - perfs_store_list[0])
                cost_changes.append(cost_store - cost_store_list[0])

        perfs_changes = np.array(perfs_changes)
        cost_changes = np.array(cost_changes)

        return perfs_changes, cost_changes
Пример #3
0
def load_data(model_dir=None, sigma_rec=0, lesion_units=None, n_rep=1):
    """Generate model data into standard format.

    Returns:
        data: standard format, list of dict of arrays/dict
            list is over neurons
            dict is for response array and task variable dict
            response array has shape (n_trial, n_time)
    """
    if model_dir is None:
        model_dir = './mantetemp'  # TEMPORARY SETTING

    # Get rules and regressors
    rules = ['contextdm1', 'contextdm2']

    n_rule = len(rules)

    data = list()

    model = Model(model_dir, sigma_rec=sigma_rec)
    hp = model.hp
    with tf.Session() as sess:
        model.restore()
        if lesion_units is not None:
            model.lesion_units(sess, lesion_units)

        # Generate task parameters used
        # Target location
        stim1_loc_list = np.arange(0, 2*np.pi, 2*np.pi/12)
        for stim1_loc in stim1_loc_list:
            params, batch_size = _gen_taskparams(stim1_loc=stim1_loc, n_rep=n_rep)
            stim1_locs_tmp = np.tile(params['stim1_locs'], n_rule)

            x = list() # Network input
            y_loc = list() # Network target output location

            # Start computing the neural activity
            for i, rule in enumerate(rules):
                # Generating task information
                trial = generate_trials(rule, hp, 'psychometric',
                                        params=params, noise_on=True)
                x.append(trial.x)
                y_loc.append(trial.y_loc)

            x = np.concatenate(x, axis=1)
            y_loc = np.concatenate(y_loc, axis=1)

            # Coherences
            stim_mod1_cohs = params['stim1_mod1_strengths'] - params[
                'stim2_mod1_strengths']
            stim_mod2_cohs = params['stim1_mod2_strengths'] - params[
                'stim2_mod2_strengths']
            stim_mod1_cohs /= stim_mod1_cohs.max()
            stim_mod2_cohs /= stim_mod2_cohs.max()

            # Get neural activity
            fetches = [model.h, model.y_hat, model.y_hat_loc]
            H, y_sample, y_sample_loc = sess.run(
                fetches, feed_dict={model.x: x})

            # Downsample in time
            dt_new = 50
            every_t = int(dt_new / hp['dt'])
            # Only analyze the target epoch
            epoch = trial.epochs['stim1']
            H = H[epoch[0]:epoch[1], ...][int(every_t / 2)::every_t, ...]

            # Get performance and choices
            # perfs = get_perf(y_sample, y_loc)
            # y_choice is 1 for choosing stim1_loc, otherwise -1
            y_actual_choice = 2*(get_dist(y_sample_loc[-1]-stim1_loc)<np.pi/2)-1
            y_target_choice = 2*(get_dist(y_loc[-1]-stim1_loc)<np.pi/2)-1

            # Get task variables
            task_var = dict()
            task_var['targ_dir'] = y_actual_choice
            task_var['stim_dir'] = np.tile(stim_mod1_cohs, n_rule)
            task_var['stim_col2dir'] = np.tile(stim_mod2_cohs, n_rule)
            task_var['context'] = np.repeat([1, -1], batch_size)
            task_var['correct'] = (y_actual_choice == y_target_choice).astype(int)
            task_var['stim_dir_sign'] = (task_var['stim_dir']>0).astype(int)*2-1
            task_var['stim_col2dir_sign'] = (task_var['stim_col2dir']>0).astype(int)*2-1


            n_unit = H.shape[-1]
            for i_unit in range(n_unit):
                unit_dict = {
                    'rate': H[:, :, i_unit].T,  # standard format (n_trial, n_time)
                    'task_var': copy.deepcopy(task_var)
                }
                data.append(unit_dict)
    return data