def init_from_keywords(keywordstring, meta={}, IsReload=False, **context): if not IsReload: modelspec = init.from_keywords(keyword_string=keywordstring, meta=meta) return {'modelspecs': [modelspec]} else: return {}
def build_toy_combined_cell(base, amplitude, shift, kappa, u, tau): modelspec = from_keywords(combined.split('_')[1]) modelspec = _set_LN_phi(modelspec) stp_idx = find_module('stp', modelspec) modelspec[stp_idx]['phi'] = {'u': u, 'tau': tau} return _set_gc_phi(modelspec, base, amplitude, shift, kappa)
def build_toy_stp_cell(u, tau): if not isinstance(u, np.ndarray): u = np.array(u) if not isinstance(tau, np.ndarray): tau = np.array(tau) modelspec = from_keywords(stp.split('_')[1]) modelspec = _set_LN_phi(modelspec) stp_idx = find_module('stp', modelspec) modelspec[stp_idx]['phi'] = {'u': u, 'tau': tau} return modelspec
def eval_tf_layer( data: np.ndarray, layer_spec: typing.Union[None, str] = None, state_data: np.ndarray = None, stop: typing.Union[None, int] = None, modelspec: mslib.ModelSpec = None, **kwargs, # temporary until state data is implemented ) -> np.ndarray: """Takes in a numpy array and applies a single tf layer to it. :param data: The input data. Shape of (reps, time, channels). :param layer_spec: A layer spec for layers of a modelspec. :param state_data: The input state data. Shape of (reps, time, channels). :param stop: What layer to eval to. Non inclusive. If not passed, will evaluate the whole layer spec. :param modelspec: Optionally use an existing modelspec. Takes precedence over layer_spec. :return: The processed data. """ if layer_spec is None and modelspec is None: raise ValueError( 'Either of "layer_spec" or "modelspec" must be specified.') if modelspec is not None: ms = modelspec else: ms = initializers.from_keywords(layer_spec) layers = ms.modelspec2tf2(use_modelspec_init=True)[:stop] data_shape = data.shape state_shape = None if state_data is not None: state_shape = state_data.shape data = [data, state_data] model = modelbuilder.ModelBuilder(layers=layers).build_model( input_shape=data_shape, state_shape=state_shape) pred = model.predict(data) return pred
def fit_bgfg_model(batch, site): cell_df = nd.get_batch_cells(batch) cellid = [cell for cell in cell_df['cellid'].tolist() if cell[:7] == site][0] fs = 100 manager = BAPHYExperiment(cellid=cellid, batch=batch) options = {'rasterfs': 100, 'stim': False, 'resp': True} rec = manager.get_recording(**options) newrec = ts.generate_psth_from_resp_bgfg(rec, manager) rec = newrec.copy() rec['resp'] = rec['resp'].rasterize() bgfg_psth_signal = rec['psth'].concatenate_channels( (rec['psth_bg'], rec['psth_fg'])) bgfg_psth_signal.name = 'psth_bgfg' rec.add_signal(bgfg_psth_signal) epoch_regex = '^STIM' rec = nems.preprocessing.average_away_epoch_occurrences( rec, epoch_regex=epoch_regex) # mask out epochs with "null" in the name ep = nems.epoch.epoch_names_matching(rec['psth'].epochs, '^STIM') for e in ep: if ('null' not in e) and ('0.5' not in e): print(e) rec = rec.or_mask(e) est = rec.copy() val = rec.copy() outputcount = rec['psth'].shape[0] inputcount = outputcount * 2 insignal = 'psth_bgfg' outsignal = 'psth_sp' modelspec_name = f'wc.{inputcount}x{outputcount}-lvl.{outputcount}' # record some meta data for display and saving meta = { 'cellid': site, 'batch': 1, 'modelname': modelspec_name, 'recording': est.name } modelspec = initializers.from_keywords(modelspec_name, meta=meta, input_name=insignal, output_name=outsignal) init_weights = np.eye(outputcount, outputcount) init_weights = np.concatenate((init_weights, init_weights), axis=1) modelspec[0]['phi']['coefficients'] = init_weights / 2 # RUN AN ANALYSIS # GOAL: Fit your model to your data, producing the improved modelspecs. # Note that: nems.analysis.* will return a list of modelspecs, sorted # in descending order of how they performed on the fitter's metric. # then fit full nonlinear model fit_kwargs = {'tolerance': 1e-5, 'max_iter': 100000} modelspec = nems.analysis.api.fit_basic(est, modelspec, fitter=scipy_minimize, fit_kwargs=fit_kwargs) # GENERATE SUMMARY STATISTICS print('Generating summary statistics ...') # generate predictions est, val = nems.analysis.api.generate_prediction(est, val, modelspec) # evaluate prediction accuracy modelspec = nems.analysis.api.standard_correlation(est, val, modelspec) print("Performance: r_fit={0:.3f} r_test={1:.3f}".format( modelspec.meta['r_fit'][0][0], modelspec.meta['r_test'][0][0])) ctx = {'modelspec': modelspec, 'rec': rec, 'val': val, 'est': est} xfspec = [] #import nems.gui.editors as gui #gui.browse_xform_fit(ctx, xfspec) f, ax = plt.subplots(4, 1, figsize=(12, 6)) cellnumber = 6 dur = 2000 r = val.apply_mask() ax[0].plot(r['pred'].as_continuous()[cellnumber, :dur]) ax[0].plot(r['psth_sp'].as_continuous()[cellnumber, :dur]) ax[1].plot(r['psth_fg'].as_continuous()[cellnumber, :dur]) ax[2].plot(r['psth_bg'].as_continuous()[cellnumber, :dur]) ax[3].plot(r['mask'].as_continuous()[0, :dur]) #plt.legend(('pred','actual','mask')) plt.figure() plt.imshow(modelspec.phi[0]['coefficients']) plt.colorbar() return modelspec, val, r # aw = browse_recording(val, ['psth_sp','pred', 'psth_bg', 'psth_fg'], cellid='ARM017a-01-10') # # batch=329 # cell_df=nd.get_batch_cells(batch) # cell_list=cell_df['cellid'].tolist() # fs=100 # # cell_list = [cell for cell in cell_list if cell[:3] != 'HOD'] # # cell_list = [cell for cell in cell_list if cell[:7] == 'ARM026b'] # cell_dict = {cell[0:7]:cell for cell in cell_list} # # rec_dict = dict() # for site, cell in cell_dict.items(): # manager = BAPHYExperiment(cellid=cell, batch=batch) # options = {'rasterfs': 100, # 'stim': False, # 'resp': True} # rec = manager.get_recording(**options) # rec_dict[site] = ts.generate_psth_from_resp_bgfg(rec, manager) # # cellid='ARM026b' # rec=rec_dict[cellid].copy() # rec['resp']=rec['resp'].rasterize() # # bgfg_psth_signal = rec['psth'].concatenate_channels((rec['psth_bg'], rec['psth_fg'])) # bgfg_psth_signal.name = 'psth_bgfg' # rec.add_signal(bgfg_psth_signal) # # epoch_regex = '^STIM' # rec = nems.preprocessing.average_away_epoch_occurrences(rec, epoch_regex=epoch_regex) # # mask out epochs with "null" in the name # ep = nems.epoch.epoch_names_matching(rec['psth'].epochs, '^STIM') # for e in ep: # if ('null' not in e) and ('0.5' not in e): # print(e) # rec = rec.or_mask(e) # # est=rec.copy() # val=rec.copy() # # outputcount=rec['psth'].shape[0] # inputcount=outputcount*2 # # insignal='psth_bgfg' # outsignal='psth_sp' # # modelspec_name = f'wc.{inputcount}x{outputcount}-lvl.{outputcount}' # # # record some meta data for display and saving # meta = {'cellid': cellid, # 'batch': 1, # 'modelname': modelspec_name, # 'recording': est.name # } # modelspec = initializers.from_keywords(modelspec_name, meta=meta, input_name=insignal, output_name=outsignal) # # init_weights = np.eye(outputcount,outputcount) # init_weights = np.concatenate((init_weights,init_weights), axis=1) # modelspec[0]['phi']['coefficients'] = init_weights/2 # # # RUN AN ANALYSIS # # # GOAL: Fit your model to your data, producing the improved modelspecs. # # Note that: nems.analysis.* will return a list of modelspecs, sorted # # in descending order of how they performed on the fitter's metric. # # # then fit full nonlinear model # fit_kwargs={'tolerance': 1e-5, 'max_iter': 100000} # modelspec = nems.analysis.api.fit_basic(est, modelspec, fitter=scipy_minimize, # fit_kwargs=fit_kwargs) # # # GENERATE SUMMARY STATISTICS # print('Generating summary statistics ...') # # # generate predictions # est, val = nems.analysis.api.generate_prediction(est, val, modelspec) # # # evaluate prediction accuracy # modelspec = nems.analysis.api.standard_correlation(est, val, modelspec) # # print("Performance: r_fit={0:.3f} r_test={1:.3f}".format( # modelspec.meta['r_fit'][0][0], # modelspec.meta['r_test'][0][0])) # # ctx = {'modelspec': modelspec, 'rec': rec, 'val': val, 'est': est} # xfspec=[] # # #import nems.gui.editors as gui # #gui.browse_xform_fit(ctx, xfspec) # # # f,ax=plt.subplots(4,1, figsize=(12,6)) # cellnumber=3 # dur=2000 # r=val.apply_mask() # ax[0].plot(r['pred'].as_continuous()[cellnumber,:dur]) # ax[0].plot(r['psth_sp'].as_continuous()[cellnumber,:dur]) # ax[1].plot(r['psth_fg'].as_continuous()[cellnumber,:dur]) # ax[2].plot(r['psth_bg'].as_continuous()[cellnumber,:dur]) # ax[3].plot(r['mask'].as_continuous()[0,:dur]) # # #plt.legend(('pred','actual','mask')) # # plt.figure() # plt.imshow(modelspec.phi[0]['coefficients']) # plt.colorbar() # # # # aw = browse_recording(val, ['psth_sp','pred', 'psth_bg', 'psth_fg'], cellid='ARM017a-01-10')
def simple_modelspec_with_phi(simple_modelspec): ms = from_keywords('wc.18x2.g-fir.2x15-dexp.1') return set_mean_phi(ms)
def simple_modelspec(): return from_keywords('wc.18x2.g-fir.2x15-dexp.1')
def init_pop_pca(est, modelspec, flip_pcs=False, IsReload=False, **context): """ fit up through the fir module of a population model using the pca signal""" if IsReload: return {} # preserve input modelspec. necessary? modelspec = copy.deepcopy(modelspec) ifir = find_module('filter_bank', modelspec) iwc = find_module('weight_channels', modelspec) chan_count = modelspec[ifir]['fn_kwargs']['bank_count'] chan_per_bank = int(modelspec[iwc]['prior']['mean'][1]['mean'].shape[0] / chan_count) rec = est.copy() tmodelspec = copy.deepcopy(modelspec) kw = [m['id'] for m in modelspec[:iwc]] wc = modelspec[iwc]['id'].split(".") wcs = wc[1].split("x") wcs[1] = str(chan_per_bank) wc[1] = "x".join(wcs) wc = ".".join(wc) fir = modelspec[ifir]['id'].split(".") fircore = fir[1].split("x") fir[1] = "x".join(fircore[:-1]) fir = ".".join(fir) kw.append(wc) kw.append(fir) kw.append("lvl.1") keywordstring = "-".join(kw) keyword_lib = KeywordRegistry() keyword_lib.register_module(default_keywords) keyword_lib.register_plugins(get_setting('KEYWORD_PLUGINS')) if flip_pcs: pc_fit_count = int(np.ceil(chan_count / 2)) else: pc_fit_count = chan_count for pc_idx in range(pc_fit_count): r = rec['pca'].extract_channels([rec['pca'].chans[pc_idx]]) m = np.nanmean(r.as_continuous()) d = np.nanstd(r.as_continuous()) rec['resp'] = r._modified_copy((r._data - m) / d) tmodelspec = init.from_keywords(keyword_string=keywordstring, meta={}, registry=keyword_lib, rec=rec) tolerance = 1e-4 tmodelspec = init.prefit_LN(rec, tmodelspec, tolerance=tolerance, max_iter=700) # save results back into main modelspec itfir = find_module('fir', tmodelspec) itwc = find_module('weight_channels', tmodelspec) if pc_idx == 0: for tm, m in zip(tmodelspec[:(iwc + 1)], modelspec[:(iwc + 1)]): m['phi'] = tm['phi'].copy() modelspec[ifir]['phi'] = tmodelspec[itfir]['phi'].copy() else: for k, v in tmodelspec[iwc]['phi'].items(): modelspec[iwc]['phi'][k] = np.concatenate( (modelspec[iwc]['phi'][k], v)) for k, v in tmodelspec[itfir]['phi'].items(): #if k=='coefficients': # v/=100 # kludge modelspec[ifir]['phi'][k] = np.concatenate( (modelspec[ifir]['phi'][k], v)) if flip_pcs and (pc_idx * 2 < chan_count): # add negative flipped version of fit for k, v in tmodelspec[iwc]['phi'].items(): modelspec[iwc]['phi'][k] = np.concatenate( (modelspec[iwc]['phi'][k], v)) for k, v in tmodelspec[itfir]['phi'].items(): #if k=='coefficients': # v/=100 # kludge modelspec[ifir]['phi'][k] = np.concatenate( (-modelspec[ifir]['phi'][k], v)) respcount = est['resp'].shape[0] fit_set_all, fit_set_slice = _figure_out_mod_split(modelspec) cd_kwargs = {} cd_kwargs.update({ 'tolerance': tolerance, 'max_iter': 100, 'step_size': 0.1 }) for s in range(respcount): log.info('Pre-fit slice %d', s) modelspec = fit_population_slice(est, modelspec, slice=s, fit_set=fit_set_slice, analysis_function=analysis.fit_basic, metric=metrics.nmse, fitter=coordinate_descent, fit_kwargs=cd_kwargs) return {'modelspec': modelspec}
def build_toy_gc_cell(base, amplitude, shift, kappa): modelspec = from_keywords(gc.split('_')[1]) modelspec = _set_LN_phi(modelspec) return _set_gc_phi(modelspec, base, amplitude, shift, kappa)
def build_toy_LN_cell(): modelspec = from_keywords(LN.split('_')[1]) return _set_LN_phi(modelspec)
plt.imshow(np.fliplr(net1_layer_vals[0]['W'][:, :, 0].T), interpolation='none', origin='lower') plt.ylabel('time') plt.xlabel('feature') plt.title('Actual simulated weights') plt.colorbar() else: D = np.reshape(est['resp'].as_continuous().copy().T, data_dims) Dv = np.reshape(val['resp'].as_continuous().copy().T, v_data_dims) net1_seed = 50 tf.reset_default_graph() if USE_LINK: modelspec = init.from_keywords(modelspecname, meta=meta) #layers = cnnlink.modelspec2cnn(modelspec, data_dims=data_dims, n_inputs=n_feats, fs=est['resp'].fs, net_seed=net1_seed) layers = cnnlink.modelspec2cnn(modelspec, n_inputs=n_feats, fs=est['resp'].fs) #layers = [{'act': 'identity', 'n_kern': 1, # 'time_win_sec': 0.01, 'type': 'reweight-positive'}, # {'act': 'relu', 'n_kern': 1, 'rank': None, # 'time_win_sec': 0.15, 'type': 'conv'}] net2 = cnn.Net(data_dims, n_feats, sr_Hz, deepcopy(layers), seed=net1_seed, log_dir=results_dir) #net2.optimizer = 'GradientDescent'