Visualizing Neuropixels Probe Locations

Visualizing Neuropixels Probe Locations#

Note: This notebook currently does not work on Google Colab, or Dandihub. Please try running it locally

It can be handy to know the location and trajectory of the probes that obtain the data. Some NWB files have an electrodes field which stores these locations. To be more precise, they contain arrays of CCF coordinates. CCF is a framework which represents locations in the brain with coordinates that are relative to predefined brain structures in a model brain. This notebook uses CCF coordinate data in an extracellular electrophysiology NWB file to render the locations of the Neuropixels probes that were used.

To be able to render locations in a brain, you don’t need to know how CCF works except that it is a system of coordinates. We use the ccf-widget library to 3D render the brain and the probe coordinates.

Environment Setup#

⚠️Note: If running on a new environment, run this cell once and then restart the kernel⚠️

try:
    from databook_utils.dandi_utils import dandi_download_open
except:
    !git clone https://github.com/AllenInstitute/openscope_databook.git
    %cd openscope_databook
    %pip install -e .
import numpy as np

from ccfwidget import CCFWidget

Downloading Ecephys File#

If you don’t already have a file to analyze, you can use a file from The Allen Institute’s Visual Coding - Neuropixels dataset. If you want to choose your own file to download, set dandiset_id and dandi_filepath accordingly.

dandiset_id = "000253"
dandi_filepath = "sub-637908/sub-637908_ses-1213341633_ogen.nwb"
download_loc = "."
# This can sometimes take a while depending on the size of the file
io = dandi_download_open(dandiset_id, dandi_filepath, download_loc)
nwb = io.read()
A newer version (0.62.2) of dandi/dandi-cli is available. You are using 0.61.2
File already exists
Opening file
Ignoring cached namespace 'hdmf-common' version 1.6.0 because version 1.8.0 is already loaded.
Ignoring cached namespace 'hdmf-experimental' version 0.3.0 because version 0.5.0 is already loaded.

Extracting NWB CCF Coordinates#

Here, you can read the NWB file you’re interested in viewing. Specify your file of interest’s relative file path in nwb_filepath. From there, the file will be read and the probe unit coordinates will be extracted and turned into a numpy array.

Note that this will only work with ecephys NWB files which have a valid electrodes field.

### read the x,y,z ccf coordinates and generate points
xs = nwb.electrodes.x
ys = nwb.electrodes.y
zs = nwb.electrodes.z
n = min(len(xs), len(ys), len(zs))
points = np.array([[xs[i], ys[i], zs[i]] for i in range(n)])
# each point has 3 coordinates associated with it
print(points.shape)
(2304, 3)

Rendering#

Rendering is as simple as generating the widget and displaying it. This will create embedded window with the interactive 3D rendering of your scene.

# the 'markers' parameter will expect an iterable where each point is represented as [x,y,z]
ccf = CCFWidget(markers=[points])
ccf