small pupil, and all balanced pupil trials.") real_dict_all = rec['resp'].extract_epochs(balanced_eps) real_dict_small = rec_sp['resp'].extract_epochs(balanced_eps) real_dict_big = rec_bp['resp'].extract_epochs(balanced_eps) else: # xforms model already has balanced epochs log.info("Extracting spike count dictionaries for big pupil, \ small pupil, and all balanced pupil trials.") epochs = np.unique([s for s in rec.epochs.name if 'STIM' in s]).tolist() real_dict_all = rec['resp'].extract_epochs(epochs) real_dict_small = rec_sp['resp'].extract_epochs(epochs) real_dict_big = rec_bp['resp'].extract_epochs(epochs) # compute noise correlations and save results df_all = nc.compute_rsc(real_dict_all, chans=rec['resp'].chans) df_big = nc.compute_rsc(real_dict_big, chans=rec['resp'].chans) df_small = nc.compute_rsc(real_dict_small, chans=rec['resp'].chans) cols = ['all', 'p_all', 'bp', 'p_bp', 'sp', 'p_sp', 'site'] df = pd.DataFrame(columns=cols, index=df_all.index) df['all'] = df_all['rsc'] df['p_all'] = df_all['pval'] df['bp'] = df_big['rsc'] df['p_bp'] = df_big['pval'] df['sp'] = df_small['rsc'] df['p_sp'] = df_small['pval'] df['site'] = savesite log.info("save noise corr results for model: {0}, site: {1}".format( modelname, savesite))
elif batch == 307: # not really an explicit "catch" for this data di = np.inf diall = np.inf diref = behavior_performance['DI'][epoch.strip('TAR_').strip('CAT_')] direfall = behavior_performance_all['DI'][epoch.strip('TAR_').strip('CAT_')] else: di = np.inf diall = np.inf for tb in time_bins: sidx = int(tb[0] * options['rasterfs']) eidx = int(tb[1] * options['rasterfs']) da = {k: r[:, :, sidx:eidx] for k, r in rec['resp'].extract_epochs([epoch], mask=ra['mask']).items()} dp = {k: r[:, :, sidx:eidx] for k, r in rec['resp'].extract_epochs([epoch], mask=rp['mask']).items()} dfa = nc.compute_rsc(da, chans=rec['resp'].chans).rename(columns={'rsc': 'active', 'pval': 'pa'}) dfp = nc.compute_rsc(dp, chans=rec['resp'].chans).rename(columns={'rsc': 'passive', 'pval': 'pp'}) df = pd.concat([dfa, dfp], axis=1) df['snr'] = thelp.get_snrs([epoch_str])[0] df['f'] = thelp.get_tar_freqs([epoch])[0] df['tbin'] = '_'.join([str(t) for t in tb]) df['DI'] = di df['DIall'] = diall df['DIref'] = diref df['DIrefall'] = direfall df['site'] = site if batch in [324, 302, 307]: area='A1' else: area='PEG' df['area'] = area df['batch'] = batch
da = {k: v[~np.isnan(v.sum(axis=(1, 2))), :, :] for (k, v) in da.items()} if zscore: da = {k: (v.transpose(0, -1, 1) - m).transpose(0, -1, 1) for (k, v) in da.items()} da = {k: (v.transpose(0, -1, 1) / sd).transpose(0, -1, 1) for (k, v) in da.items()} rp = rec.copy().create_mask(True) rp = rp.and_mask(['PASSIVE_EXPERIMENT']) dp = rp['resp'].extract_epochs(sounds, mask=rp['mask']) dp = {k: v[:, :, prestim:poststim] for (k, v) in dp.items()} dp = {k: v[~np.isnan(v.sum(axis=(1, 2))), :, :] for (k, v) in dp.items()} if zscore: dp = {k: (v.transpose(0, -1, 1) - m).transpose(0, -1, 1) for (k, v) in dp.items()} dp = {k: (v.transpose(0, -1, 1) / sd).transpose(0, -1, 1) for (k, v) in dp.items()} # get mean active / passive noise correlations rsc_active = nc.compute_rsc(da, chans=rec['resp'].chans)['rsc'].mean() rsc_passive = nc.compute_rsc(dp, chans=rec['resp'].chans)['rsc'].mean() # project active / passive responses onto PCA plane fig, ax = plt.subplots(1, 2, figsize=(10, 5), sharex=True, sharey=True) colors = plt.cm.get_cmap('rainbow', len(sounds)) for col, e in enumerate(sounds): try: passive = dp[e].mean(axis=-1).dot(tdr_weights.T) ax[0].plot(passive[:, 0], passive[:, 1], alpha=0.3, marker='.', lw=0, color=colors(col)) el = compute_ellipse(passive[:, 0], passive[:, 1]) ax[0].plot(el[0], el[1], lw=2, label=e, color=ax[0].get_lines()[-1].get_color()) except: pass
# USE XFORMS PUPIL MASK rec_bp = rec.copy() rec_bp['mask'] = rec_bp['p_mask'] rec_sp = rec.copy() rec_sp['mask'] = rec_sp['p_mask']._modified_copy(~rec_sp['p_mask']._data) rec_bp = rec_bp.apply_mask(reset_epochs=True) rec_sp = rec_sp.apply_mask(reset_epochs=True) # xforms model already has balanced epochs epochs = np.unique([s for s in rec.epochs.name if 'STIM' in s]).tolist() real_dict_all = rec['resp'].extract_epochs(epochs) real_dict_small = rec_sp['resp'].extract_epochs(epochs) real_dict_big = rec_bp['resp'].extract_epochs(epochs) raw_bp_nc = nc.compute_rsc(real_dict_big) raw_sp_nc = nc.compute_rsc(real_dict_small) # compute model regressed results rec = ctx['val'].apply_mask(reset_epochs=True).copy() rec = preproc.regress_state(rec, state_sigs=['pupil', 'lv'], regress=['pupil', 'lv']) # USE XFORMS PUPIL MASK rec_bp = rec.copy() rec_bp['mask'] = rec_bp['p_mask'] rec_sp = rec.copy() rec_sp['mask'] = rec_sp['p_mask']._modified_copy(~rec_sp['p_mask']._data) rec_bp = rec_bp.apply_mask(reset_epochs=True) rec_sp = rec_sp.apply_mask(reset_epochs=True)
rec_sp = rec_sp.apply_mask(reset_epochs=True) rec_bp = rec.copy() rec_bp['mask'] = bp_mask rec_bp = rec_bp.apply_mask(reset_epochs=True) # Get response dictionaries epochs = 'PreStimSilence' big_r = rec['resp'].extract_epochs(epochs, mask=bp_mask) small_r = rec['resp'].extract_epochs(epochs, mask=sp_mask) cells = big_r['PreStimSilence'].shape[1] big_r['PreStimSilence'] = big_r['PreStimSilence'].transpose( [0, 2, 1]).reshape(-1, cells, 1) small_r['PreStimSilence'] = small_r['PreStimSilence'].transpose( [0, 2, 1]).reshape(-1, cells, 1) nc_big = nc.compute_rsc(big_r, chans=rec['resp'].chans) nc_small = nc.compute_rsc(small_r, chans=rec['resp'].chans) nc = pd.DataFrame(columns=['bp', 'sp'], index=nc_big.index, data=np.vstack( (nc_big['rsc'].values, nc_small['rsc'].values)).T) mi = ld.load_mi() mi = mi[mi['site'] == site] idx_tup = [(i.split('_')[0], i.split('_')[1]) for i in nc.index] bins = np.round(np.arange(-.2, .3, 0.1), 2) val = [] err = [] n = []
rec_sp = create_pupil_mask(rec_sp, **ops) eps = np.unique([s for s in rec.epochs.name if 'STIM' in s]).tolist() log.info("Extracting spike count dictionaries for big pupil, \n \ small pupil, and all pupil trials.") real_dict_all = rec['resp'].extract_epochs(eps) real_dict_small = rec_sp['resp'].extract_epochs(eps, mask=rec_sp['mask']) real_dict_big = rec_bp['resp'].extract_epochs(eps, mask=rec_bp['mask']) if perstim: log.info("Computing noise correlations separately for each stimulus bin") for idx, stim in enumerate(eps): log.info(f"Epoch {idx+1} / {len(eps)}") for b in range(real_dict_all[stim].shape[-1]): _df_all = nc.compute_rsc({stim: real_dict_all[stim][:, :, [b]]}, chans=rec['resp'].chans) _df_big = nc.compute_rsc({stim: real_dict_big[stim][:, :, [b]]}, chans=rec['resp'].chans) _df_small = nc.compute_rsc( {stim: real_dict_small[stim][:, :, [b]]}, chans=rec['resp'].chans) _df_all['stim'] = stim + '_' + str(b + nspont_bins) _df_big['stim'] = stim + '_' + str(b + nspont_bins) _df_small['stim'] = stim + '_' + str(b + nspont_bins) if (idx == 0) & (b == 0): df_all = _df_all df_big = _df_big df_small = _df_small else: df_all = pd.concat([df_all, _df_all]) df_big = pd.concat([df_big, _df_big])
rec = rec.and_mask(['PreStimSilence', 'PostStimSilence'], invert=True) bp = create_pupil_mask( rec, **{ 'state': 'big', 'method': 'median', 'epoch': ['REFERENCE'], 'collapse': True }) sp = create_pupil_mask( rec, **{ 'state': 'small', 'method': 'median', 'epoch': ['REFERENCE'], 'collapse': True }) bp = compute_rsc(bp['resp'].extract_epochs(epochs, mask=bp['mask'])) sp = compute_rsc(sp['resp'].extract_epochs(epochs, mask=sp['mask'])) if significant_pairs: m = (bp['pval'] < 0.05) | (sp['pval'] < 0.05) else: m = [True] * bp.shape[0] before_big = bp['rsc'][m] before_small = sp['rsc'][m] rec['var_mask'] = rec['pupil']._modified_copy(mask) fm = rec['var_mask'].extract_epochs(epochs) # tile bool across full reference so not to split up epochs in weird ways fm = { k: np.concatenate([ np.zeros(v[[i]].shape).astype(bool) if np.any(