def make_state_signal(rec, state_signals=['pupil'], permute_signals=[], new_signalname='state', **context): rec = preproc.make_state_signal(rec, state_signals=state_signals, permute_signals=permute_signals, new_signalname=new_signalname) return {'rec': rec}
def make_state_signal(rec, state_signals=['pupil'], permute_signals=[], new_signalname='state', **context): rec = preproc.make_state_signal(rec, state_signals=state_signals, permute_signals=permute_signals, new_signalname=new_signalname) rec = preproc.remove_invalid_segments(rec) # rec = preproc.nan_invalid_segments(rec) return {'rec': rec}
def load_tbp_recording(siteid, batch, **options): options['resp'] = True options['pupil'] = options.get('pupil', True) options['rasterfs'] = options.get('rasterfs', 50) manager = BAPHYExperiment(siteid=siteid, batch=batch) rec = manager.get_recording(**options) rec['resp'] = rec['resp'].rasterize() onsetsec = 0.1 offsetsec = 0.4 onset = int(onsetsec * options['rasterfs']) offset = int(offsetsec * options['rasterfs']) # PCA on trial averaged responses ref_stims, sounds, all_sounds = get_sound_labels(rec) """ try: targets = [f for f in rec['resp'].epochs.name.unique() if 'TAR_' in f] catch = [f for f in rec['resp'].epochs.name.unique() if 'CAT_' in f] sounds = targets + catch ref_stims = [x for x in rec['resp'].epochs.name.unique() if 'STIM_' in x] idx = np.argsort([int(s.split('_')[-1]) for s in ref_stims]) ref_stims = np.array(ref_stims)[idx].tolist() sounds=sort_targets(sounds) except: ref_stims=['REFERENCE'] sounds = ['TARGET'] all_sounds = ref_stims + sounds """ rall = rec.and_mask(['ACTIVE_EXPERIMENT', 'PASSIVE_EXPERIMENT']) # can't simply extract evoked for refs because can be longer/shorted if it came after target # and / or if it was the last stim.So, masking prestim / postim doesn't work.Do it manually d = rall['resp'].extract_epochs(all_sounds, mask=rall['mask']) d = { k: v[~np.isnan(v[:, :, onset:offset].sum(axis=(1, 2))), :, :] for (k, v) in d.items() } d = {k: v[:, :, onset:offset] for (k, v) in d.items()} Rall_u = np.vstack([d[k].sum(axis=2).mean(axis=0) for k in d.keys()]) pca = PCA(n_components=2) pca.fit(Rall_u) pc_axes = pca.components_ # project onto first PC and plot trial psth rec['pc1'] = rec['resp']._modified_copy(rec['resp']._data.T.dot( pc_axes.T).T[[0], :]) rec['pc2'] = rec['resp']._modified_copy(rec['resp']._data.T.dot( pc_axes.T).T[[1], :]) #rec = make_state_signal(rec, state_signals=['pupil','active'], permute_signals=['pupil']) rec = make_state_signal(rec, state_signals=['pupil', 'active']) return rec
# ---------------------------------------------------------------------------- # DATA LOADING # GOAL: Get your data loaded into memory as a Recording object logging.info('Loading data...') # Method #1: Load the data from a local directory rec = Recording.load(os.path.join(signals_dir, 'TAR010c-18-1.tgz')) # Method #2: Load the data from baphy using the (incomplete, TODO) HTTP API: #URL = "http://potoroo:3004/baphy/271/bbl086b-11-1?rasterfs=200" #rec = Recording.load_url(URL) logging.info('Generating state signal...') rec = preproc.make_state_signal(rec, ['pupil'], [''], 'state') # ---------------------------------------------------------------------------- # INITIALIZE MODELSPEC # GOAL: Define the model that you wish to test logging.info('Initializing modelspec...') # Method #1: create from "shorthand" keyword string #modelspec = nems.initializers.from_keywords('pup_wcg18x1_fir15x1_lvl1_dexp1') modelspec = nems.initializers.from_keywords('wcgNx2_fir15x2_lvl1_stategain2') #modelspec = nems.initializers.from_keywords('wcgNx2_fir15x2_lvl1') #modelspec = nems.initializers.from_keywords('pup_wcg18x2_fir15x2_lvl1_stategain2') # ----------------------------------------------------------------------------
#recording.get_demo_recordings(signals_dir) #rec = recording.load_recording(recording_uri) nwb_filepath = Path('/auto/users/tomlinsa/code/allen/data/session_759883607/session_759883607.nwb') rec=from_nwb_pupil(nwb_filepath,'neuropixel',with_pupil=True,fs=40) rec=rec[cellid] #try out allen data # ---------------------------------------------------------------------------- # PREPROCESSING # create a new signal that will be used to modulates the output of the linear # predicted response logging.info('Generating state signal...') #rec = preproc.make_state_signal(rec, ['active','pupil'], [''], 'state') rec = preproc.make_state_signal(rec, state_signals=['pupil'], permute_signals=[''], new_signalname='state') #rec = preproc.make_state_signal(rec, state_signals=['pupil'], # permute_signals=['pupil'], new_signalname='state') #shuffled pupil # mask out data from incorrect trials #rec = preproc.mask_all_but_correct_references(rec)#not for allen? # calculate a PSTH response for each stimulus, save to a new signal 'psth' #epoch_regex="^STIM_" epoch_regex="^natural_scene" #only natural scene stimulus #rec = preproc.generate_psth_from_resp(rec, epoch_regex=epoch_regex, smooth_resp=False) rec=nwb_resp_psth(rec,epoch_regex) #should return similar results as above for allen data # ---------------------------------------------------------------------------- # INSPECT THE DATA
recording.get_demo_recordings(signals_dir) rec = recording.load_recording(recording_uri) # Method #2: Load the data from baphy using the (incomplete, TODO) HTTP API: #URL = "http://potoroo:3004/baphy/271/bbl086b-11-1?rasterfs=200" #rec = Recording.load_url(URL) # ---------------------------------------------------------------------------- # PREPROCESSING # create a new signal that will be used to modulate the output of the linear # predicted response logging.info('Generating state signal...') #rec = preproc.make_state_signal(rec, ['active','pupil_bs','pupil_ev'], [''], 'state') #rec = preproc.make_state_signal(rec, ['active','pupil'], [''], 'state') rec = preproc.make_state_signal(rec, ['pupil', 'each_file'], [''], 'state') # mask out data from incorrect trials rec = preproc.mask_all_but_correct_references(rec) # calculate a PSTH response for each stimulus, save to a new signal 'psth' epoch_regex = "^STIM_" rec = preproc.generate_psth_from_resp(rec, epoch_regex, smooth_resp=False) # ---------------------------------------------------------------------------- # INSPECT THE DATA resp = rec['resp'].rasterize() epochs = resp.epochs epoch_regex = "^STIM_" epoch_list = ep.epoch_names_matching(epochs, epoch_regex)
def model_per_time_wrapper(cellid, batch=307, loader="psth.fs20.pup-ld-", fitter="_jk.nf20-basic", basemodel="-ref-psthfr_stategain.S", state_list=None, plot_halves=True, colors=None): """ batch = 307 # A1 SUA and MUA batch = 309 # IC SUA and MUA alternatives: basemodels = ["-ref-psthfr.s_stategain.S", "-ref-psthfr.s_sdexp.S", "-ref.a-psthfr.s_sdexp.S"] state_list = ['st.pup0.hlf0','st.pup0.hlf','st.pup.hlf0','st.pup.hlf'] state_list = ['st.pup0.far0.hit0.hlf0','st.pup0.far0.hit0.hlf', 'st.pup.far.hit.hlf0','st.pup.far.hit.hlf'] state_list = ['st.pup0.fil0','st.pup0.fil','st.pup.fil0','st.pup.fil'] """ # pup vs. active/passive if state_list is None: state_list = [ 'st.pup0.hlf0', 'st.pup0.hlf', 'st.pup.hlf0', 'st.pup.hlf' ] #state_list = ['st.pup0.far0.hit0.hlf0','st.pup0.far0.hit0.hlf', # 'st.pup.far.hit.hlf0','st.pup.far.hit.hlf'] #state_list = ['st.pup0.fil0','st.pup0.fil','st.pup.fil0','st.pup.fil'] modelnames = [] contexts = [] for i, s in enumerate(state_list): modelnames.append(loader + s + basemodel + fitter) xf, ctx = xhelp.load_model_xform(cellid, batch, modelnames[i], eval_model=False) ctx, l = xforms.evaluate(xf, ctx, start=0, stop=-2) ctx['val'] = preproc.make_state_signal(ctx['val'], state_signals=['each_half'], new_signalname='state_f') contexts.append(ctx) #import pdb; #pdb.set_trace() plt.figure() #if ('hlf' in state_list[0]) or ('fil' in state_list[0]): if plot_halves: files_only = True else: files_only = False for i, ctx in enumerate(contexts): rec = ctx['val'].apply_mask() modelspec = ctx['modelspec'] epoch = "REFERENCE" rec = ms.evaluate(rec, modelspec) if i == len(contexts) - 1: ax = plt.subplot(len(contexts) + 1, 1, 1) nplt.state_vars_timeseries(rec, modelspec, ax=ax) ax.set_title('{} {}'.format(cellid, modelnames[-1])) ax = plt.subplot(len(contexts) + 1, 1, 2 + i) nplt.state_vars_psth_all(rec, epoch, psth_name='resp', psth_name2='pred', state_sig='state_f', colors=colors, channel=None, decimate_by=1, ax=ax, files_only=files_only, modelspec=modelspec) ax.set_ylabel(state_list[i]) ax.set_xticks([])