def test_brain_observatory_natural_scenes_notebook(boc): data_set = boc.get_ophys_experiment_data(510938357) ns = NaturalScenes(data_set) ns_head = ns.peak.head() assert np.isclose(ns_head['peak_dff_ns'][0], 4.9614532738) assert ns_head['cell_specimen_id'][0] == 517399188
def test_natural_scenes(ns, nwb_b, analysis_b_new): ns_new = NaturalScenes.from_analysis_file(BODS(nwb_b), analysis_b_new) #assert np.allclose(ns.sweep_response, ns_new.sweep_response) assert np.allclose(ns.mean_sweep_response, ns_new.mean_sweep_response, equal_nan=True) assert np.allclose(ns.noise_correlation, ns_new.noise_correlation, equal_nan=True) assert np.allclose(ns.signal_correlation, ns_new.signal_correlation, equal_nan=True) assert np.allclose(ns.representational_similarity, ns_new.representational_similarity, equal_nan=True)
def test_natural_scenes(ns, nwb_b, analysis_b_new): ns_new = NaturalScenes.from_analysis_file(BODS(nwb_b), analysis_b_new) #assert np.allclose(ns.sweep_response, ns_new.sweep_response) assert np.allclose(ns.mean_sweep_response, ns_new.mean_sweep_response, equal_nan=True) assert np.allclose(ns.noise_correlation, ns_new.noise_correlation, equal_nan=True) assert np.allclose(ns.signal_correlation, ns_new.signal_correlation, equal_nan=True) assert np.allclose(ns.representational_similarity, ns_new.representational_similarity, equal_nan=True)
def build_type(nwb_file, data_file, configs, output_dir, type_name): data_set = BrainObservatoryNwbDataSet(nwb_file) try: if type_name == "dg": dga = DriftingGratings.from_analysis_file(data_set, data_file) build_drifting_gratings(dga, configs, output_dir) elif type_name == "sg": sga = StaticGratings.from_analysis_file(data_set, data_file) build_static_gratings(sga, configs, output_dir) elif type_name == "nm1": nma = NaturalMovie.from_analysis_file(data_set, data_file, si.NATURAL_MOVIE_ONE) build_natural_movie(nma, configs, output_dir, si.NATURAL_MOVIE_ONE) elif type_name == "nm2": nma = NaturalMovie.from_analysis_file(data_set, data_file, si.NATURAL_MOVIE_TWO) build_natural_movie(nma, configs, output_dir, si.NATURAL_MOVIE_TWO) elif type_name == "nm3": nma = NaturalMovie.from_analysis_file(data_set, data_file, si.NATURAL_MOVIE_THREE) build_natural_movie(nma, configs, output_dir, si.NATURAL_MOVIE_THREE) elif type_name == "ns": nsa = NaturalScenes.from_analysis_file(data_set, data_file) build_natural_scenes(nsa, configs, output_dir) elif type_name == "sp": nma = NaturalMovie.from_analysis_file(data_set, data_file, si.NATURAL_MOVIE_ONE) build_speed_tuning(nma, configs, output_dir) elif type_name == "lsn_on": lsna = lsna_check_hvas(data_set, data_file) build_locally_sparse_noise(lsna, configs, output_dir, True) elif type_name == "lsn_off": lsna = lsna_check_hvas(data_set, data_file) build_locally_sparse_noise(lsna, configs, output_dir, False) elif type_name == "rf": lsna = lsna_check_hvas(data_set, data_file) build_receptive_field(lsna, configs, output_dir) elif type_name == "corr": build_correlation_plots(data_set, data_file, configs, output_dir) elif type_name == "eye": build_eye_tracking_plots(data_set, configs, output_dir) except MissingStimulusException as e: logging.warning("could not load stimulus (%s)", type_name) except Exception as e: traceback.print_exc() logging.critical("error running stimulus (%s)", type_name) raise e
def session_b(self, plot_flag=False, save_flag=True): ns = NaturalScenes(self.nwb) sg = StaticGratings(self.nwb) nm1 = NaturalMovie(self.nwb, 'natural_movie_one', speed_tuning=True) SessionAnalysis._log.info("Session B analyzed") peak = multi_dataframe_merge( [nm1.peak_run, sg.peak, ns.peak, nm1.peak]) self.append_metadata(peak) self.append_metrics_static_grating(self.metrics_b, sg) self.append_metrics_natural_scene(self.metrics_b, ns) self.verify_roi_lists_equal(sg.roi_id, ns.roi_id) self.metrics_b["roi_id"] = sg.roi_id if save_flag: self.save_session_b(sg, nm1, ns, peak) if plot_flag: cp._plot_3sb(sg, nm1, ns, self.save_dir) cp.plot_ns_traces(ns, self.save_dir) cp.plot_sg_traces(sg, self.save_dir)
# In[6]: #get experiment session id for the first experiment container session_id = expts.id.values[0] print session_id data_set = boc.get_ophys_experiment_data(ophys_experiment_id=session_id) #get stimulus template (aka stimulus frames) for images, movies or locally sparse noise ns_template = data_set.get_stimulus_template( stimulus_name='natural_scenes') #stimulus specific analysis from allensdk.brain_observatory.natural_scenes import NaturalScenes ns = NaturalScenes(data_set) print 'done with natural scenes import' # In[ ]: responsive_cells = ns.peak[ ns.peak.peak_dff_ns >= 5].cell_specimen_id.values timestamps, traces = data_set.get_dff_traces( cell_specimen_ids=responsive_cells) frames_arr, images_arr, traces_arr = get_ns_response_arrays( traces, data_set, ns.stim_table) images = data_set.get_stimulus_template('natural_scenes') save_dir = os.path.join( '/Users/marinag/Data/BrainObservatory/natural_scenes_plots',
def ns(nwb_b, analysis_b): return NaturalScenes.from_analysis_file(BODS(nwb_b), analysis_b)
def test_harness(dataset, trigger): ns = NaturalScenes(dataset) assert ns._stim_table is StimulusAnalysis._PRELOAD assert ns._number_scenes is StimulusAnalysis._PRELOAD assert ns._sweeplength is StimulusAnalysis._PRELOAD assert ns._interlength is StimulusAnalysis._PRELOAD assert ns._extralength is StimulusAnalysis._PRELOAD assert ns._sweep_response is StimulusAnalysis._PRELOAD assert ns._mean_sweep_response is StimulusAnalysis._PRELOAD assert ns._pval is StimulusAnalysis._PRELOAD assert ns._response is StimulusAnalysis._PRELOAD assert ns._peak is StimulusAnalysis._PRELOAD if trigger == 1: print(ns._stim_table) print(ns.sweep_response) print(ns.response) print(ns.peak) if trigger == 2: print(ns.number_scenes) print(ns.sweep_response) print(ns.response) print(ns.peak) elif trigger == 3: print(ns.sweeplength) print(ns.mean_sweep_response) print(ns.response) print(ns.peak) elif trigger == 4: print(ns.interlength) print(ns.sweep_response) print(ns.response) print(ns.peak) elif trigger == 5: print(ns.extralength) print(ns.mean_sweep_response) print(ns.response) print(ns.peak) assert ns._stim_table is not StimulusAnalysis._PRELOAD assert ns._number_scenes is not StimulusAnalysis._PRELOAD assert ns._sweeplength is not StimulusAnalysis._PRELOAD assert ns._interlength is not StimulusAnalysis._PRELOAD assert ns._extralength is not StimulusAnalysis._PRELOAD assert ns._sweep_response is not StimulusAnalysis._PRELOAD assert ns._mean_sweep_response is not StimulusAnalysis._PRELOAD assert ns._pval is not StimulusAnalysis._PRELOAD assert ns._response is not StimulusAnalysis._PRELOAD assert ns._peak is not StimulusAnalysis._PRELOAD # check super properties dataset.get_corrected_fluorescence_traces.assert_called_once_with() assert ns._timestamps != NaturalScenes._PRELOAD assert ns._celltraces != NaturalScenes._PRELOAD assert ns._numbercells != NaturalScenes._PRELOAD assert not dataset.get_roi_ids.called assert ns._roi_id is NaturalScenes._PRELOAD assert dataset.get_cell_specimen_ids.called assert ns._cell_id is NaturalScenes._PRELOAD assert not dataset.get_dff_traces.called assert ns._dfftraces is NaturalScenes._PRELOAD assert ns._dxcm is NaturalScenes._PRELOAD assert ns._dxtime is NaturalScenes._PRELOAD
def build_correlation_plots(data_set, analysis_file, configs, output_dir): sig_corrs = [] noise_corrs = [] avail_stims = si.stimuli_in_session(data_set.get_session_type()) ans = [] labels = [] colors = [] if si.DRIFTING_GRATINGS in avail_stims: dg = DriftingGratings.from_analysis_file(data_set, analysis_file) if hasattr(dg, 'representational_similarity'): ans.append(dg) labels.append(si.DRIFTING_GRATINGS_SHORT) colors.append(si.DRIFTING_GRATINGS_COLOR) setups = [ ( [configs['large']], True ), ( [configs['small']], False )] for cfgs, show_labels in setups: for fn in build_plots("drifting_gratings_representational_similarity", 1.0, cfgs, output_dir): oplots.plot_representational_similarity(dg.representational_similarity, dims=[dg.orivals, dg.tfvals[1:]], dim_labels=["dir", "tf"], dim_order=[1,0], colors=['r','b'], labels=show_labels) if si.STATIC_GRATINGS in avail_stims: sg = StaticGratings.from_analysis_file(data_set, analysis_file) if hasattr(sg, 'representational_similarity'): ans.append(sg) labels.append(si.STATIC_GRATINGS_SHORT) colors.append(si.STATIC_GRATINGS_COLOR) setups = [ ( [configs['large']], True ), ( [configs['small']], False )] for cfgs, show_labels in setups: for fn in build_plots("static_gratings_representational_similarity", 1.0, cfgs, output_dir): oplots.plot_representational_similarity(sg.representational_similarity, dims=[sg.orivals, sg.sfvals[1:], sg.phasevals], dim_labels=["ori", "sf", "ph"], dim_order=[1,0,2], colors=['r','g','b'], labels=show_labels) if si.NATURAL_SCENES in avail_stims: ns = NaturalScenes.from_analysis_file(data_set, analysis_file) if hasattr(ns, 'representational_similarity'): ans.append(ns) labels.append(si.NATURAL_SCENES_SHORT) colors.append(si.NATURAL_SCENES_COLOR) setups = [ ( [configs['large']], True ), ( [configs['small']], False )] for cfgs, show_labels in setups: for fn in build_plots("natural_scenes_representational_similarity", 1.0, cfgs, output_dir): oplots.plot_representational_similarity(ns.representational_similarity, labels=show_labels) if len(ans): for an in ans: sig_corrs.append(an.signal_correlation) extra_dims = range(2,len(an.noise_correlation.shape)) noise_corrs.append(an.noise_correlation.mean(axis=tuple(extra_dims))) for fn in build_plots("correlation", 1.0, [configs['large'], configs['svg']], output_dir): oplots.population_correlation_scatter(sig_corrs, noise_corrs, labels, colors, scale=16.0) oplots.finalize_with_axes() for fn in build_plots("correlation", 1.0, [configs['small']], output_dir): oplots.population_correlation_scatter(sig_corrs, noise_corrs, labels, colors, scale=4.0) oplots.finalize_no_labels() csids = ans[0].data_set.get_cell_specimen_ids() for fn, csid, i in build_cell_plots(csids, "signal_correlation", 1.0, [configs['large']], output_dir): row = ans[0].row_from_cell_id(csid, i) oplots.plot_cell_correlation([ np.delete(sig_corr[row],i) for sig_corr in sig_corrs ], labels, colors) oplots.finalize_with_axes() for fn, csid, i in build_cell_plots(csids, "signal_correlation", 1.0, [configs['small']], output_dir): row = ans[0].row_from_cell_id(csid, i) oplots.plot_cell_correlation([ np.delete(sig_corr[row],i) for sig_corr in sig_corrs ], labels, colors) oplots.finalize_no_labels()
def ns(nwb_b, analysis_b): return NaturalScenes.from_analysis_file(BODS(nwb_b), analysis_b)