示例#1
0
def full_dPCA_dprimes(site, contexts, probes, meta):
    raster, goodcells = _load_site_formated_raste(site, contexts, probes, meta)

    # trialR shape: Trial x Cell x Context x Probe x Time; R shape: Cell x Context x Probe x Time
    trialR, R, _ = cdPCA.format_raster(raster)

    # calculates full dPCA. i.e. considering all 4 categories
    _, trialZ, dpca = cdPCA._cpp_dPCA(R, trialR)
    dPCA_projection = trialZ['ct'][:, 0, ...]
    dprime = cDP.pairwise_dprimes(dPCA_projection,
                                  observation_axis=0,
                                  condition_axis=1,
                                  flip=meta['dprime_absolute'])

    # calculates the variance explained. special case for full dpca, not present in other dprime approaches
    var_capt = cdPCA.variance_captured(dpca, R)

    # Shuffles the rasters n times and organizes in an array with the same shape the raster plus one dimension
    # with size n containing each shuffle
    # calculates the pairwise dprime
    rep, ctx, prb, tme = dPCA_projection.shape
    transition_pairs = list(itt.combinations(contexts, 2))

    print(f"\nshuffling {meta['montecarlo']} times")
    rng = np.random.default_rng(42)

    shuf_projections = np.empty([meta['montecarlo'], rep, ctx, prb, tme])

    shuf_trialR = trialR.copy()

    for rr in range(meta['montecarlo']):
        shuf_trialR = shuffle(shuf_trialR,
                              shuffle_axis=2,
                              indie_axis=0,
                              rng=rng)
        shuf_R = np.mean(shuf_trialR, axis=0)

        # saves the first regularizer to speed things up.
        if rr == 0:
            _, shuf_trialZ, shuf_dpca = cdPCA._cpp_dPCA(shuf_R, shuf_trialR)
            regularizer = shuf_dpca.regularizer
        else:
            _, shuf_trialZ, dpca = cdPCA._cpp_dPCA(
                shuf_R, shuf_trialR, {'regularizer': regularizer})

        shuf_projections[rr, ...] = shuf_trialZ['ct'][:, 0, ...]

    shuffled_dprime = cDP.pairwise_dprimes(shuf_projections,
                                           observation_axis=1,
                                           condition_axis=2,
                                           flip=meta['dprime_absolute'])

    # add dimension for single PC, for compatibility with single cell arrays
    dprime = np.expand_dims(dprime, axis=0)
    shuffled_dprime = np.expand_dims(shuffled_dprime, axis=1)

    return dprime, shuffled_dprime, goodcells, var_capt
示例#2
0
 def fit_transformt(R, trialR):
     _, dPCA_projection, _, dpca = cdPCA._cpp_dPCA(R,
                                                   trialR,
                                                   significance=False,
                                                   dPCA_parms={})
     dPCA_projection = dPCA_projection['ct'][:, 0, ]
     dPCA_transformation = np.tile(dpca.D['ct'][:, 0][:, None, None],
                                   [1, 1, T])
     return dPCA_projection, dPCA_transformation
示例#3
0
def fit_transform(site, probe, meta, part):
    recs = load(site)

    if len(recs) > 2:
        print(f'\n\n{recs.keys()}\n\n')

    rec = recs['trip0']
    sig = rec['resp']

    # calculates response realiability and select only good cells to improve analysis
    r_vals, goodcells = signal_reliability(sig,
                                           r'\ASTIM_*',
                                           threshold=meta['reliability'])
    goodcells = goodcells.tolist()

    raster = src.data.rasters.raster_from_sig(
        sig,
        probe,
        channels=goodcells,
        contexts=meta['transitions'],
        smooth_window=meta['smoothing_window'],
        raster_fs=meta['raster_fs'],
        part=part,
        zscore=meta['zscore'])

    # trialR shape: Trial x Cell x Context x Probe x Time; R shape: Cell x Context x Probe x Time
    trialR, R, _ = cdPCA.format_raster(raster)
    trialR, R = trialR.squeeze(), R.squeeze()  # squeezes out probe

    _, dPCA_projection, _, dpca = cdPCA._cpp_dPCA(R,
                                                  trialR,
                                                  significance=False,
                                                  dPCA_parms={})
    dPCA_projection = dPCA_projection['ct'][:, 0, ...]
    dPCA_weights = np.tile(dpca.D['ct'][:, 0][:, None, None],
                           [1, 1, R.shape[-1]])

    dprime = cDP.pairwise_dprimes(dPCA_projection)

    return dprime, dPCA_projection, dPCA_weights, dpca
示例#4
0
def dPCA_site_summary(site, probe):
    # loads the raw data
    recs = load(site, rasterfs=meta['raster_fs'], recache=rec_recache)
    sig = recs['trip0']['resp']

    # calculates response realiability and select only good cells to improve analysis
    r_vals, goodcells = signal_reliability(sig,
                                           r'\ASTIM_*',
                                           threshold=meta['reliability'])
    goodcells = goodcells.tolist()

    # get the full data raster Context x Probe x Rep x Neuron x Time
    raster = src.data.rasters.raster_from_sig(
        sig,
        probe,
        channels=goodcells,
        contexts=meta['transitions'],
        smooth_window=meta['smoothing_window'],
        raster_fs=meta['raster_fs'],
        zscore=meta['zscore'],
        part='probe')

    # trialR shape: Trial x Cell x Context x Probe x Time; R shape: Cell x Context x Probe x Time
    trialR, R, _ = cdPCA.format_raster(raster)
    trialR, R = trialR.squeeze(axis=3), R.squeeze(axis=2)  # squeezes out probe
    Z, trialZ, dpca = cdPCA._cpp_dPCA(R, trialR)

    fig, axes = plt.subplots(2, 3, sharex='all', sharey='row')
    for vv, (marginalization, arr) in enumerate(Z.items()):
        means = Z[marginalization]
        trials = trialZ[marginalization]

        if marginalization == 't':
            marginalization = 'probe'
        elif marginalization == 'ct':
            marginalization = 'context'
        for pc in range(3):  # first 3 principal components

            ax = axes[vv, pc]
            for tt, trans in enumerate(
                    meta['transitions']):  # for each context
                ax.plot(times,
                        means[pc, tt, :],
                        label=trans,
                        color=trans_color_map[trans],
                        linewidth=2)
                # _ = fplt._cint(times, trials[:,pc,tt,:],  confidence=0.95, ax=ax,
                #                fillkwargs={'color': trans_color_map[trans], 'alpha': 0.5})
                ax.tick_params(labelsize=ax_val_size)

            # formats axes labels and ticks
            if pc == 0:  # y labels
                ax.set_ylabel(
                    f'{marginalization} dependent\nfiring rate (z-score)',
                    fontsize=ax_lab_size)
            else:
                ax.axes.get_yaxis().set_visible(True)
                pass

            if vv == 0:
                ax.set_title(f'dPC #{pc + 1}', fontsize=sub_title_size)
                ax.axes.get_xaxis().set_visible(True)
            else:
                ax.set_xlabel('time (ms)', fontsize=ax_lab_size)

            ## Hide the right and top spines
            ax.spines['right'].set_visible(False)
            ax.spines['top'].set_visible(False)
            ax.tick_params(labelsize=ax_val_size)
    # legend in last axis
    axes[-1, -1].legend(loc='upper right',
                        fontsize='x-large',
                        markerscale=10,
                        frameon=False)

    return fig, ax, dpca
示例#5
0
def probewise_dPCA_dprimes(site, contexts, probes, meta):
    """
    performs dimensionality reduction with dPCA done independently for each probe. Then uses the first context dependent
    demixed principal component to calculated the dprime between context for all probes in the site. Calculates
    significance using montecarlo shuffling of the context identity.
    :param site: string  identifying the site
    :param probes: list of probe numbers
    :param meta: dict with meta parameters
    :return: dprime (ndarray with shape PC x Ctx_pair x Probe x Time),
             shuffled_dprimes (ndarray with shape Montecarlo x PC x Ctx_pair x Probe x Time),
             goocells (list of strings)
    """
    raster, goodcells = _load_site_formated_raste(site, contexts, probes, meta)

    # trialR shape: Trial x Cell x Context x Probe x Time; R shape: Cell x Context x Probe x Time
    trialR, R, _ = cdPCA.format_raster(raster)
    rep, unt, ctx, prb, tme = trialR.shape
    transition_pairs = list(itt.combinations(contexts, 2))

    # iterates over each probe
    dprime = np.empty([len(transition_pairs), prb, tme])
    shuffled_dprime = np.empty(
        [meta['montecarlo'],
         len(transition_pairs), prb, tme])

    var_capt = list()
    for pp in probes:
        probe_idx = probes.index(pp)
        probe_trialR = trialR[..., probe_idx, :]
        probe_R = R[..., probe_idx, :]

        # calculates dPCA considering all 4 categories
        _, trialZ, dpca = cdPCA._cpp_dPCA(probe_R, probe_trialR)
        var_capt.append(cdPCA.variance_captured(dpca, probe_R))
        dPCA_projection = trialZ['ct'][:, 0, ...]
        dprime[:, probe_idx, :] = cDP.pairwise_dprimes(
            dPCA_projection,
            observation_axis=0,
            condition_axis=1,
            flip=meta['dprime_absolute'])

        # Shuffles the rasters n times and organizes in an array with the same shape as the original raster plus one
        # dimension with size meta['montecarlo'] containing each shuffle
        # calculates the pairwise dprime
        print(f"\nshuffling {meta['montecarlo']} times")
        rng = np.random.default_rng(42)

        shuf_projections = np.empty([meta['montecarlo'], rep, ctx, tme])
        shuf_projections[:] = np.nan

        shuf_trialR = probe_trialR.copy()
        for rr in range(meta['montecarlo']):
            shuf_trialR = shuffle(shuf_trialR,
                                  shuffle_axis=2,
                                  indie_axis=0,
                                  rng=rng)
            shuf_R = np.mean(shuf_trialR, axis=0)

            #saves the first regularizer to speed things up.
            if rr == 0:
                _, shuf_trialZ, shuf_dpca = cdPCA._cpp_dPCA(
                    shuf_R, shuf_trialR)
                regularizer = shuf_dpca.regularizer
            else:
                _, shuf_trialZ, dpca = cdPCA._cpp_dPCA(
                    shuf_R, shuf_trialR, {'regularizer': regularizer})

            shuf_projections[rr, :] = shuf_trialZ['ct'][:, 0, ...]

        shuffled_dprime[:, :, probe_idx, :] = cDP.pairwise_dprimes(
            shuf_projections,
            observation_axis=1,
            condition_axis=2,
            flip=meta['dprime_absolute'])

    # add dimension for single PC, for compatibility with single cell arrays
    dprime = np.expand_dims(dprime, axis=0)
    shuffled_dprime = np.expand_dims(shuffled_dprime, axis=1)

    return dprime, shuffled_dprime, goodcells, var_capt
示例#6
0
    sim_aca = [sim_trialR[:, :, ctx, :] for ctx in range(S)]
    sim_dPCA_proj = [cLDA.transform_over_time(a, dpca_axes) for a in sim_aca]
    dPCA_ceil_d[ii, :] = cpd.dprime(sim_dPCA_proj[c0][:, 0, :],
                                    sim_dPCA_proj[c1][:, 0, :])

    # dprime floor: shuffle context(dim2) identity by trial(dim0), projection and dprime calculation
    ctx_shuffle = shuffle(ctx_shuffle, shuffle_axis=2, indie_axis=0)
    # trial-average data
    shuf_R = np.mean(ctx_shuffle, 0)
    # center data per neuron i.e. first dime
    centers = np.mean(shuf_R.reshape((shuf_R.shape[0], -1)), axis=1)[:, None,
                                                                     None]
    shuf_R -= centers
    # calculates dpca and projections
    _, shuf_trialZ, _, _ = cdPCA._cpp_dPCA(shuf_R,
                                           ctx_shuffle,
                                           significance=False)
    shuf_dPCA_proj = [
        shuf_trialZ['ct'][:, 0, ctx, :]
        for ctx in range(shuf_trialZ['ct'].shape[2])
    ]
    dPCA_floor_d[ii, :] = cpd.dprime(shuf_dPCA_proj[c0].squeeze(),
                                     shuf_dPCA_proj[c1].squeeze())

# plots figure

fig, axes = plt.subplots(1, 2, sharey=True)
axes = np.ravel(axes)

ci = 0.99
axes[0].plot(np.mean(LDA_floor_d, axis=0), color='red', alpha=1, label='floor')