.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_neuropixel.py: natural scene decoding from V1 ============================== This is an example of how to decode natural images from spikes recorded in V1 .. code-block:: python from __future__ import print_function first, we need to load the data .. code-block:: python data_path = '/allen/aibs/mat/RamIyer/frm_Dan/NWBFilesSev/V1_NI_pkl_data/' import pandas as pd ephys_data = pd.read_pickle(data_path+'M15_ni_data.pkl') Let's get the dataframe of image presentations and rename the columns .. code-block:: python events = ephys_data['stim_table'].rename( columns={ 'Start':'time', 'Frame':'image_id', }, ) print(events.head()) Next, let's reformat the spike times into a single table .. code-block:: python from neuroglia.nwb import SpikeTablizer spikes = SpikeTablizer().fit_transform(ephys_data['spiketimes']) print(spikes.head()) Now, we'll sample spikes near each event & build this into a xarray 3D tensor .. code-block:: python from neuroglia.event import PeriEventSpikeSampler import numpy as np spike_sampler = PeriEventSpikeSampler( spikes=spikes, sample_times=np.arange(0.1,0.35,0.01), ) tensor = spike_sampler.fit_transform(events) print(tensor) We can get the average elicited spike count with the `ResponseReducer` .. code-block:: python from neuroglia.tensor import ResponseReducer import numpy as np reducer = ResponseReducer(func=np.mean) means = reducer.fit_transform(tensor) print(means) Let's use the scikit-learn pipeline to chain these steps into a single decoding pipeline .. code-block:: python from sklearn.pipeline import Pipeline from sklearn.neighbors import KNeighborsClassifier pipeline = Pipeline([ ('spike_sampler',PeriEventSpikeSampler(spikes=spikes,sample_times=np.arange(0.1,0.35,0.01))), ('extract', ResponseReducer(func=np.mean)), ('classify', KNeighborsClassifier()), ]) Now we can train the full pipeline on the training set .. code-block:: python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( events, events['image_id'],test_size=0.33, ) pipeline.fit(X_train,y_train) Finally we'll test the pipeline on the held out data .. code-block:: python score = pipeline.score(X_test,y_test) n_images = len(events['image_id'].unique()) print(score*n_images) .. _sphx_glr_download_gallery_neuropixel.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: neuropixel.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: neuropixel.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_