Skip to content

Commit

Permalink
implement ephys-viz interface and provide an example
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Oct 4, 2019
1 parent 962659b commit 379f219
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
101 changes: 101 additions & 0 deletions examples/YutaMouse41-ephys-viz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## YutaMouse41-ephys-viz\n",
"\n",
"To use this notebook you will need to install the ephys_viz package.\n",
"\n",
"Roughly speaking that involves installing ephys_viz from PyPI and installing the reactpoya_jup notebook and/or lab extensions.\n",
"\n",
"See: https://github.com/flatironinstitute/ephys-viz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Imports and initialization of ephys-viz in this notebook\n",
"import pynwb\n",
"from pynwb import NWBHDF5IO\n",
"from nwbwidgets import nwb2widget\n",
"import ephys_viz as ev\n",
"from nwbwidgets.ephys_viz_interface import ephys_viz_neurodata_vis_spec as vis_spec\n",
"ev.init_jupyter()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# You need to have an .nwb file on your computer\n",
"file_name = 'YutaMouse41-150903.nwb'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Lazy-load the nwb file\n",
"nwb_io = NWBHDF5IO(file_name, mode='r')\n",
"nwb = nwb_io.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Display the LFP using ephys-viz\n",
"nwb2widget(nwb.fields['processing']['ecephys']['LFP'], vis_spec)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The ephys-viz widget is integrated into nwbwidgets\n",
"nwb2widget(nwb, vis_spec)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions nwbwidgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .view import nwb2widget
from .ephys_viz_interface import ephys_viz_neurodata_vis_spec
55 changes: 55 additions & 0 deletions nwbwidgets/ephys_viz_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import matplotlib.pyplot as plt
import ipywidgets as widgets
import pynwb
from pynwb import TimeSeries
import numpy as np
from nwbwidgets import view
from pynwb.ecephys import LFP
from .view import default_neurodata_vis_spec
import spikeextractors as se
from pynwb.ecephys import LFP

ephys_viz_neurodata_vis_spec = default_neurodata_vis_spec
def _set_spec():
ephys_viz_neurodata_vis_spec[pynwb.ecephys.LFP] = show_lfp

def show_lfp(node: LFP, **kwargs):
import spikeextractors as se
import ephys_viz as ev
try:
recording = LFPRecordingExtractor(lfp_node=node)
except:
return widgets.Text('Problem creating LFPRecordingExtractor')
return ev.TimeseriesView(
recording=recording,
initial_y_scale_factor=5
).show(render=False)

class LFPRecordingExtractor(se.RecordingExtractor):
def __init__(self, lfp_node: LFP):
super().__init__()
lfp = list(lfp_node.electrical_series.values())[0]
self._samplerate = lfp.rate
self._data = lfp.data
self._num_channels = self._data.shape[1]
self._num_timepoints = self._data.shape[0]

def get_channel_ids(self):
return list(range(self._num_channels))

def get_num_frames(self):
return self._num_timepoints

def get_sampling_frequency(self):
return self._samplerate

def get_traces(self, channel_ids=None, start_frame=None, end_frame=None):
if start_frame is None:
start_frame = 0
if end_frame is None:
end_frame = self.get_num_frames()
if channel_ids is None:
channel_ids = self.get_channel_ids()
return self._data[start_frame:end_frame, :][:, channel_ids].T

_set_spec()

0 comments on commit 379f219

Please sign in to comment.