def create_ptd_masks(rec, act_pup_range=2): """ Create active behavior mask, passive mask, passive big pupil mask, and passive small pupil mask. return new recording with the four masks as signals. Modified 2/20/2020, CRH. Now, define large pupil as pupil matched to active (w/in 2sd). Small pupil is the (smaller tail) leftover. Modified 07/13/2020, CRH. Now, specify how many sd from the mean of active counts as big pupil. default is still 2. """ r = rec.copy() r = r.create_mask(True) act_mask = r.and_mask(['HIT_TRIAL'])['mask'] pass_mask = r.and_mask(['PASSIVE_EXPERIMENT'])['mask'] miss_mask = r.and_mask(['MISS_TRIAL'])['mask'] # define the cutoff as two sd less than the mean of active pupil cutoff = r['pupil']._data[act_mask._data].mean() - (act_pup_range * r['pupil']._data[act_mask._data].std()) options = {'state': 'big', 'method': 'user_def_value', 'cutoff': cutoff, 'collapse': True, 'epoch': ['REFERENCE', 'TARGET']} pass_big_mask = create_pupil_mask(r.and_mask(['PASSIVE_EXPERIMENT']), **options)['mask'] options['state'] = 'small' pass_small_mask = create_pupil_mask(r.and_mask(['PASSIVE_EXPERIMENT']), **options)['mask'] r['a_mask'] = r['mask']._modified_copy(act_mask._data) r['p_mask'] = r['mask']._modified_copy(pass_mask._data) r['miss_mask'] = r['mask']._modified_copy(miss_mask._data) r['pb_mask'] = r['mask']._modified_copy(pass_big_mask._data) r['ps_mask'] = r['mask']._modified_copy(pass_small_mask._data) return r
def get_pupil_balanced_epochs(rec, rec_sp=None, rec_bp=None): """ Given big/small pupil recordings return list of epochs that are balanced between the two. """ all_epochs = np.unique( [str(ep) for ep in rec.epochs.name if 'STIM_00' in ep]).tolist() if (rec_sp is None) | (rec_bp is None): pup_ops = {'state': 'big', 'epoch': ['REFERENCE'], 'collapse': True} rec_bp = create_pupil_mask(rec.copy(), **pup_ops) pup_ops['state'] = 'small' rec_sp = create_pupil_mask(rec.copy(), **pup_ops) rec_bp = rec_bp.apply_mask(reset_epochs=True) rec_sp = rec_sp.apply_mask(reset_epochs=True) # get rid of pre/post stim silence #rec_bp = rec_bp.and_mask(['PostStimSilence'], invert=True) #rec_sp = rec_sp.and_mask(['PostStimSilence'], invert=True) #rec = rec.and_mask(['PostStimSilence'], invert=True) #rec_bp = rec_bp.apply_mask(reset_epochs=True) #rec_sp = rec_sp.apply_mask(reset_epochs=True) #rec = rec.apply_mask(reset_epochs=True) # find pupil matched epochs balanced_eps = [] for ep in all_epochs: sp = rec_sp['resp'].extract_epoch(ep).shape[0] bp = rec_bp['resp'].extract_epoch(ep).shape[0] if len(all_epochs) == 3: if abs(sp - bp) < 3: balanced_eps.append(ep) else: if sp == bp: balanced_eps.append(ep) if len(balanced_eps) == 0: log.info("no balanced epochs at site {}".format(rec.name)) else: log.info("found {0} balanced epochs:".format(len(balanced_eps))) return balanced_eps
low = 0.5 high = 2 # for filtering the projection cells, _ = parse_cellid(ops) rec = generate_state_corrected_psth(batch=batch, modelname=xmodel, cellids=cells, siteid=site, cache_path=path, gain_only=False, recache=False) rec = rec.apply_mask(reset_epochs=True) pupil = rec['pupil']._data.squeeze() epochs = [e for e in rec.epochs.name.unique() if 'STIM' in e] rec['resp2'] = rec['resp']._modified_copy(rec['resp']._data) rec['pupil2'] = rec['pupil']._modified_copy(rec['pupil']._data) # ===================================== perform analysis on raw data ======================================= rec_bp = rec.copy() ops = {'state': 'big', 'method': 'median', 'epoch': ['REFERENCE'], 'collapse': True} rec_bp = create_pupil_mask(rec_bp, **ops) ops = {'state': 'small', 'method': 'median', 'epoch': ['REFERENCE'], 'collapse': True} rec_sp = rec.copy() rec_sp = create_pupil_mask(rec_sp, **ops) real_dict_small = rec_sp['resp'].extract_epochs(epochs, mask=rec_sp['mask']) real_dict_big = rec_bp['resp'].extract_epochs(epochs, mask=rec_bp['mask']) real_dict_all = rec['resp'].extract_epochs(epochs) pred_dict_all = rec['psth'].extract_epochs(epochs) real_dict_small = cpreproc.zscore_per_stim(real_dict_small, d2=real_dict_small, with_std=True) real_dict_big = cpreproc.zscore_per_stim(real_dict_big, d2=real_dict_big, with_std=True) real_dict_all = cpreproc.zscore_per_stim(real_dict_all, d2=real_dict_all, with_std=True) pred_dict_all = cpreproc.zscore_per_stim(pred_dict_all, d2=pred_dict_all, with_std=True) eps = list(real_dict_big.keys())
ops = { 'batch': batch, 'siteid': site, 'rasterfs': 4, 'pupil': 1, 'rem': 1, 'stim': 0 } uri = nb.baphy_load_recording_uri(**ops) rec = Recording.load(uri) rec = rec.and_mask(['PreStimSilence']) rec = rec.apply_mask(reset_epochs=True) pup_ops = {'state': 'big', 'epoch': ['REFERENCE'], 'collapse': True} bp_mask = create_pupil_mask(rec, **pup_ops)['mask'] sp_mask = bp_mask._modified_copy(~bp_mask._data) rec_sp = rec.copy() rec_sp['mask'] = sp_mask 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)
#rec1 = preproc.regress_state(rec1, state_sigs=['pupil'], regress=['pupil']) #rec12 = preproc.regress_state(rec12, state_sigs=['pupil', 'lv'], regress=['pupil', 'lv']) psth = preproc.generate_psth(rec12) data = rec1['resp']._data - rec1['pred']._data + psth['psth']._data rec1['resp'] = rec1['resp']._modified_copy(data) psth = preproc.generate_psth(rec12) data = rec12['resp']._data - rec12['pred']._data + psth['psth']._data rec12['resp'] = rec12['resp']._modified_copy(data) # mask pupil, compute noise corr, plot noise corr f, ax = plt.subplots(1, 2) for j, (r, s) in enumerate(zip([rec, rec1, rec12], ['raw', '1st', '1st+2nd'])): pup_ops = {'state': 'big', 'epoch': ['REFERENCE'], 'collapse': True} rec_bp = create_pupil_mask(r.copy(), **pup_ops) pup_ops['state'] = 'small' rec_sp = create_pupil_mask(r.copy(), **pup_ops) rec_bp = rec_bp.apply_mask(reset_epochs=True) rec_sp = rec_sp.apply_mask(reset_epochs=True) eps = np.unique([e for e in rec.epochs.name if 'STIM' in e]).tolist() real_dict_all = r['resp'].extract_epochs(eps) real_dict_small = rec_sp['resp'].extract_epochs(eps) real_dict_big = rec_bp['resp'].extract_epochs(eps) real_dict_all = preproc.zscore_per_stim(real_dict_all, d2=real_dict_all) real_dict_small = preproc.zscore_per_stim(real_dict_small, d2=real_dict_small) real_dict_big = preproc.zscore_per_stim(real_dict_big, d2=real_dict_big)
ax[2].legend(frameon=False) ax[2].set_xlim((0, rec['resp'].shape[-1])) ax[3].set_title("Spike raster") ax[3].imshow(rec['resp']._data - rec['psth_sp']._data, aspect='auto', cmap='Greys') f.tight_layout() # mask data and plot before / after noise correlations 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)