Skip to content

Open Data

Read a public NWB dataset end to end: inspect it, derive its LFP clock from the data, run a PSTH by cortical layer and LFP band power by depth, check the result, and plot it. The steps apply to any shared session; the example is one from the Allen Institute OpenScope Global/Local Oddball project.

Run the executable tutorial:

python examples/tutorials/09_open_data.py

examples/ ships in neither the wheel nor the sdist, so this line needs a clone, not pip install jnwb.

The data

Dataset DANDI 000253, version 0.240503.0152
DOI 10.48324/dandi.000253/0.240503.0152
License CC-BY-4.0: reuse with attribution
Session 1232959154, subject 649323
Units, electrodes, stimuli sub-649323_ses-1232959154_ogen.nwb, asset 81f9bcf2-d0e3-41a4-97e4-6371dafa3e71
Probe B LFP sub-649323_ses-1232959154_probe-1_ecephys.nwb, asset 56e98383-75da-4da3-a61d-39b6f814d9e5

Cite: Westerberg, J., Durand, S., Cabasco, H., et al. (2024). Allen Institute Openscope - Global/Local Oddball project (Version 0.240503.0152) [Data set]. DANDI archive. https://doi.org/10.48324/dandi.000253/0.240503.0152. The full author list is in examples/data/dandi000253_excerpt.provenance.json.

The two source files are 4.3 GB together. The tutorial reads examples/data/dandi000253_excerpt.nwb, 7.8 MB, cut from them by scripts/build_open_data_excerpt.py without resampling or unit conversion:

Kept Extent
Electrodes the 384 contacts of probe B, with their source ids and atlas locations
Units the 297 units whose peak channel is on probe B, with the source's quality metrics
Spikes the first 10 s, stimulus block 1 padded by 1 s (102.5 to 229.1 s), and the last 10 s; each unit's obs_intervals names the three windows
Stimuli the 125 grating rows of block 1
LFP every second cortical (VISpm) channel, 10 of 20, as three series: the recording's first and last 10 s, and block 1

The provenance file records the asset ids, the SHA-256 of each source as published and as verified before the build, every window, and the source values the clock depends on. To rebuild it, download both assets into one folder from https://api.dandiarchive.org/api/assets/<asset id>/download/ and run

python scripts/build_open_data_excerpt.py --source-dir <that folder>

The script refuses a file whose size or SHA-256 differs from the published asset.

Reading a shared file

  1. Inspect before selecting. jnwb.inspect lists every continuous series, interval table and units column. Here it reports the LFP container with three series and rate_hz: None: the file stores per-sample timestamps and no sampling rate.
  2. Read each time axis's values, and its unit label second. The LFP timestamps are labeled seconds and step by 24 per sample, from 0 to 273 622 056. They are ticks of the acquisition clock. Spike times and stimulus times are in seconds on the session clock.
  3. Derive the rate against an independent clock. Never assume one; rates differ between devices, bands and files.
  4. Check the derivation with a second method, and state the bound it leaves.
  5. Resolve addressing by id. A unit's peak_channel_id is an electrode id, so its location is a lookup in the electrodes table.

Deriving the LFP clock

The excerpt keeps the session's first and last LFP sample and its first and last spike, so both spans cover the whole 9121 s session.

Quantity How Value
Tick step the one value of diff(timestamps) 24
Tick rate LFP tick span / probe B spike span 29 999.968 ticks/s
LFP rate tick rate / tick step 1249.9987 Hz
Sample 0 the probe's first spike 3.5692 s
Spike-LFP lag peak of the spike-count x LFP cross-correlation, strongest channel, per segment +6.4, +6.4, +8.8 ms (start, block, end)

The source metadata declares 1250 Hz. At that rate the last sample lands 9.7 ms earlier than the derived clock places it, a drift as large as the bound below.

The cross-correlation places LFP sample 0 at 3.576 to 3.578 s, 6 to 9 ms after the first spike. The first spike bounds the start of the recording from above; the correlation lag adds the delay between population spiking and the LFP deflection, which varies with depth. The correlation also carries a second peak about 20 ms from the first. The two estimates therefore place sample 0 within ±10 ms, and the tutorial refuses a clock whose lag in any segment exceeds that bound. The three lags agree within 2.4 ms across 9000 s, which checks the tick rate as well as the offset.

The bound is small against the 0.4 s windows the band power uses. It excludes any LFP latency or spike-LFP timing claim at millisecond resolution from this file.

Layers

location holds atlas labels such as VISpm4 and VISpm2/3, one layer each. The tutorial reads it by electrode id. jnwb.map_peak_channel_to_area keeps VISpm2/3 whole but still splits a label such as VISp6a/b into two areas, so it is not used here.

What the tutorial checks

  • one tick step across all three LFP series;
  • spike-LFP lags within ±10 ms in every segment;
  • every PSTH window lies inside each unit's observation intervals, so an unobserved stretch cannot read as silence;
  • good cortical units fire more 30 to 130 ms after grating onset than in the 100 ms before;
  • band power is finite on every channel. It is the trial-averaged 50 to 80 Hz power during the grating over that of the gray screen before it, per channel, in decibels taken once (jnwb.aggregate_to_db, how="ratio_of_means").

The figure

Validation: real data | N = 125 units | seed none | input 049518a12119 | jnwb 0.2.17

PSTH of good cortical units by layer, and 50 to 80 Hz LFP power by depth, from the DANDI 000253 excerpt PSTH of good cortical units by layer, and 50 to 80 Hz LFP power by depth, from the DANDI 000253 excerpt

Empirical, not synthetic: the data are the DANDI 000253 excerpt above. Panel A is the PSTH of good units on probe B's cortical contacts, averaged within each layer (jnwb.raster_psth); the legend gives each layer's unit count. Panel B is the trial-averaged 50 to 80 Hz power of each contact during the grating over the gray screen before it, in decibels taken once (jnwb.aggregate_to_db). The shaded span is the grating on screen, the window the tutorial's band power reads. scripts/figures/generate_figures.py draws it from this script's analyze().

Source

The page below is included from the tutorial script; edit the script, not this block.

"""Tutorial 09: Open data end to end -- inspect, derive the clock, analyze, verify, plot.

Run: python examples/tutorials/09_open_data.py

Reads `examples/data/dandi000253_excerpt.nwb`, a committed excerpt of DANDI 000253 (Allen
Institute OpenScope, Global/Local Oddball project, CC-BY-4.0,
https://doi.org/10.48324/dandi.000253/0.240503.0152), built from the published assets by
`scripts/build_open_data_excerpt.py`. Its figure is drawn by `scripts/figures/generate_figures.py`, from `analyze()`.
"""

from __future__ import annotations

import json
import os
import sys
from pathlib import Path

import numpy as np

# This file is run from a checkout, so prefer that checkout over any installed jnwb:
# Python puts this directory on sys.path, not the repository root. A run that is
# deliberately qualifying an installed copy says so with JNWB_EXPECTED_PACKAGE_ROOT,
# and then this guard stands aside.
_CHECKOUT = Path(__file__).resolve().parents[2]
if not os.environ.get("JNWB_EXPECTED_PACKAGE_ROOT") and (
    _CHECKOUT / "jnwb" / "__init__.py"
).exists():
    sys.path.insert(0, str(_CHECKOUT))
import jnwb

EXCERPT = _CHECKOUT / "examples" / "data" / "dandi000253_excerpt.nwb"
PROVENANCE = EXCERPT.with_name("dandi000253_excerpt.provenance.json")

LFP_CONTAINER = "probe_1_lfp"
SEGMENTS = ("start", "window", "end")      # series probe_1_lfp_<segment>
GRATINGS = "init_grating_presentations"
CORTEX = "VISpm"                           # location prefix of the cortical contacts

# The offset of the LFP clock is known to +-10 ms (see the page). Every check below is held to
# that bound, and nothing here reads an LFP latency finer than it.
OFFSET_BOUND_S = 0.010
XCORR_MAX_LAG_S = 0.050

PSTH_WIN_MS = (-250.0, 750.0)
BIN_MS = 10.0
BASELINE_MS = (-100.0, 0.0)
RESPONSE_MS = (30.0, 130.0)
BAND = jnwb.CANONICAL_BANDS["high_gamma"]
PRE_S = (-0.45, -0.05)                     # gray screen before the grating
POST_S = (0.05, 0.45)                      # grating on screen


class ClockCheckError(RuntimeError):
    """The derived LFP clock disagrees with the spikes by more than the stated bound."""


def read_excerpt(path: Path) -> dict:
    """Everything the analysis needs, as plain arrays, read in one pass."""
    from pynwb import NWBHDF5IO

    with NWBHDF5IO(str(path), "r") as io:
        nwb = io.read()
        electrodes = nwb.electrodes.to_dataframe()
        units = nwb.units.to_dataframe()
        container = nwb.acquisition[LFP_CONTAINER]
        lfp = {}
        for name in SEGMENTS:
            series = container.electrical_series[f"{LFP_CONTAINER}_{name}"]
            lfp[name] = {
                "data": np.asarray(series.data[:], dtype=np.float64) * series.conversion,
                "ticks": np.asarray(series.timestamps[:], dtype=np.float64),
                "timestamps_unit": series.timestamps_unit,
                "rate": series.rate,
            }
        channel_ids = np.asarray(container.electrical_series[f"{LFP_CONTAINER}_window"]
                                 .electrodes.to_dataframe().index)
    return {
        "electrodes": electrodes,
        "trains": [np.asarray(t, dtype=np.float64) for t in units["spike_times"]],
        "obs_intervals": [np.asarray(o, dtype=np.float64) for o in units["obs_intervals"]],
        "peak_channel_id": units["peak_channel_id"].to_numpy(),
        "quality": units["quality"].astype(str).to_numpy(),
        "lfp": lfp,
        "channel_ids": channel_ids,
    }


def derive_clock(lfp: dict, trains: list) -> jnwb.TickClock:
    """The LFP clock from the file: the three LFP segments and every spike train.

    The LFP timestamps are integer ticks labeled seconds. The start and end series hold the
    recording's first and last LFP sample, and the spikes include the probe's first and last
    spike, so both spans cover the whole session and neither depends on how the excerpt was cut.
    """
    segments = [lfp[name]["ticks"] for name in SEGMENTS]
    # The first sample is placed at the first spike; check_clock finds 6 to 9 ms.
    return jnwb.derive_tick_clock(segments, trains)


def xcorr_lag(trace: np.ndarray, times_s: np.ndarray, spikes: np.ndarray, fs: float) -> tuple:
    """Lag (s) of the extreme spike-count x LFP correlation within +-XCORR_MAX_LAG_S.

    A positive lag means the LFP sample carrying the coupling sits later than `times_s` says.
    """
    edges = np.r_[times_s, times_s[-1] + 1.0 / fs] - 0.5 / fs
    counts = np.histogram(spikes, bins=edges)[0].astype(np.float64)
    c = (counts - counts.mean()) / counts.std()
    x = (trace - trace.mean()) / trace.std()
    m = int(round(XCORR_MAX_LAG_S * fs))
    n = x.size
    lags = np.arange(-m, m + 1)
    r = np.array([np.mean(c[max(0, k):n + min(0, k)] * x[max(0, -k):n - max(0, k)]) for k in lags])
    best = int(np.argmax(np.abs(r)))
    return lags[best] / fs, float(r[best])


def check_clock(clock: jnwb.TickClock, data: dict, good: np.ndarray) -> dict:
    """Refine the offset by spike-LFP cross-correlation in all three segments.

    The coupling channel is the one with the strongest correlation in the analysis window; the
    same channel is then used at the start and end of the recording. Agreement of the three lags
    within the bound checks the offset and, because they sit 9000 s apart, the tick rate.
    """
    spikes = np.sort(np.concatenate([t for t, g in zip(data["trains"], good) if g]))
    window = data["lfp"]["window"]
    t_win = clock.seconds(window["ticks"])
    per_channel = [xcorr_lag(window["data"][:, j], t_win, spikes, clock.fs_hz)
                   for j in range(window["data"].shape[1])]
    channel = int(np.argmax([abs(r) for _, r in per_channel]))
    lags = {}
    for name in SEGMENTS:
        seg = data["lfp"][name]
        lags[name] = xcorr_lag(seg["data"][:, channel], clock.seconds(seg["ticks"]), spikes,
                               clock.fs_hz)
    worst = max(abs(lag) for lag, _ in lags.values())
    if worst > OFFSET_BOUND_S:
        raise ClockCheckError(
            f"spike-LFP lag {worst * 1e3:.1f} ms exceeds the {OFFSET_BOUND_S * 1e3:.0f} ms bound "
            f"(lags {', '.join(f'{k} {v[0] * 1e3:+.1f} ms' for k, v in lags.items())})"
        )
    return {"channel_id": int(data["channel_ids"][channel]), "lags_s": lags}


def location_of(peak_channel_id, electrodes) -> str:
    # A join on the electrode id. The locations here are atlas labels such as 'VISpm2/3',
    # which name one layer. jnwb.map_peak_channel_to_area keeps such a label whole but still
    # splits a label such as 'VISp6a/b', so a plain join is used for these labels.
    return str(electrodes.loc[int(peak_channel_id), "location"])


def psth_by_layer(data: dict, onsets: np.ndarray, good: np.ndarray) -> dict:
    lo_s = onsets.min() + PSTH_WIN_MS[0] / 1e3
    hi_s = onsets.max() + PSTH_WIN_MS[1] / 1e3
    layers: dict = {}
    for train, obs, pk, g in zip(data["trains"], data["obs_intervals"], data["peak_channel_id"], good):
        where = location_of(pk, data["electrodes"])
        if not (g and where.startswith(CORTEX)):
            continue
        # An excerpt keeps spikes only inside its observation intervals. A PSTH over time the
        # unit was not observed would read the gap as silence.
        if not any(a <= lo_s and hi_s <= b for a, b in obs):
            raise ValueError(f"PSTH window [{lo_s:.2f}, {hi_s:.2f}] s is outside {obs.tolist()}")
        t_ms, rate, _ = jnwb.raster_psth(train, onsets, win_ms=PSTH_WIN_MS, bin_ms=BIN_MS)
        layers.setdefault(where, []).append(rate)
    out = {}
    base = (t_ms >= BASELINE_MS[0]) & (t_ms < BASELINE_MS[1])
    resp = (t_ms >= RESPONSE_MS[0]) & (t_ms < RESPONSE_MS[1])
    for where, rates in sorted(layers.items()):
        mean = np.mean(rates, axis=0)
        out[where] = {"n_units": len(rates), "rate_hz": mean,
                      "baseline_hz": float(mean[base].mean()),
                      "response_hz": float(mean[resp].mean())}
    return {"t_ms": t_ms, "layers": out}


def band_power_by_channel(data: dict, clock: jnwb.TickClock, onsets: np.ndarray) -> dict:
    window = data["lfp"]["window"]
    t0 = float(clock.seconds(window["ticks"][:1])[0])
    local = onsets - t0                    # seconds from the segment's first sample
    powers = {}
    for name, win in (("pre", PRE_S), ("post", POST_S)):
        epochs, _ = jnwb.epoch_continuous(window["data"], local, win_s=win, fs=clock.fs_hz,
                                          boundary_policy="error")
        powers[name] = np.array([[jnwb.band_power(epochs[i, :, j], fs=clock.fs_hz,
                                                  freq_range=BAND, normalize=False)
                                  for j in range(epochs.shape[2])]
                                 for i in range(epochs.shape[0])])
        n_samples = epochs.shape[1]
    # Trial-averaged power over trial-averaged baseline, per channel; decibels taken once.
    db = jnwb.aggregate_to_db(powers["post"], powers["pre"], how="ratio_of_means",
                              aggregate_over=0)
    el = data["electrodes"].loc[data["channel_ids"]]
    return {"db": np.asarray(db), "channel_ids": data["channel_ids"],
            "location": el["location"].astype(str).to_numpy(),
            "depth_um": el["probe_vertical_position"].to_numpy(dtype=float),
            "epoch_n_samples": int(n_samples), "n_trials": int(powers["post"].shape[0])}


def analyze(path: Path = EXCERPT) -> dict:
    data = read_excerpt(path)
    good = data["quality"] == "good"
    clock = derive_clock(data["lfp"], data["trains"])
    check = check_clock(clock, data, good)

    events = jnwb.events(path, table=GRATINGS, code_column="orientation")
    orientation = np.asarray(events.codes, dtype=np.float64)
    onsets = events.onsets[np.isfinite(orientation)]   # NaN rows: no grating was drawn

    return {
        "clock": clock,
        "check": check,
        "n_units": len(data["trains"]),
        "n_good": int(good.sum()),
        "n_onsets": int(onsets.size),
        "n_rows": int(orientation.size),
        "last_tick": float(data["lfp"]["end"]["ticks"][-1]),
        "timestamps_unit": data["lfp"]["window"]["timestamps_unit"],
        "stored_rate": data["lfp"]["window"]["rate"],
        "psth": psth_by_layer(data, onsets, good),
        "lfp": band_power_by_channel(data, clock, onsets),
    }


def main() -> None:
    provenance = json.loads(PROVENANCE.read_text(encoding="utf-8"))
    ds = provenance["dandiset"]
    print(f"Data: {ds['name']}, {ds['identifier']} v{ds['version']}, {ds['license']}")
    print(f"Cite: https://doi.org/{ds['doi']}")

    # 1. Inspect before selecting. The LFP container holds three series and reports no rate,
    #    because the file stores timestamps rather than a sampling rate.
    info = jnwb.inspect(EXCERPT)
    for acq in info["acquisitions"]:
        print(f"Continuous: {acq['name']} series={acq['series']} rate_hz={acq['rate_hz']}")
    print(f"Units: {info['units']['n_rows']} rows; interval tables: "
          f"{[t['name'] for t in info['interval_tables']]}")

    res = analyze(EXCERPT)
    clock = res["clock"]

    # 2. The clock, derived from the file.
    print(f"LFP timestamps are labeled '{res['timestamps_unit']}', stored rate={res['stored_rate']}, "
          f"step {clock.tick_step:g} per sample")
    print(f"Tick rate {clock.tick_rate:.3f} per s; LFP rate {clock.fs_hz:.4f} Hz; "
          f"first sample at {clock.offset_s:.4f} s")
    for name, (lag, r) in res["check"]["lags_s"].items():
        print(f"  spike-LFP lag in {name} segment: {lag * 1e3:+.1f} ms (r={r:+.3f}, "
              f"electrode {res['check']['channel_id']})")
    declared = provenance["source_values"]["declared_rates_hz"]["electrode_group_lfp_sampling_rate_hz"]
    last_sample = (res["last_tick"] - clock.first_tick) / clock.tick_step
    drift_ms = last_sample * (1.0 / declared - 1.0 / clock.fs_hz) * 1e3
    print(f"The source metadata declares {declared:g} Hz; at that rate the last sample would sit "
          f"{abs(drift_ms):.1f} ms {'earlier' if drift_ms < 0 else 'later'} than the derived "
          f"clock puts it")

    # 3. Verify, then report.
    layers = res["psth"]["layers"]
    assert layers, "no good cortical units in the excerpt"
    for where, layer in layers.items():
        print(f"{where:>9}: {layer['n_units']:3d} units, baseline {layer['baseline_hz']:.2f} Hz, "
              f"response {layer['response_hz']:.2f} Hz")
    pooled_base = sum(v["baseline_hz"] * v["n_units"] for v in layers.values())
    pooled_resp = sum(v["response_hz"] * v["n_units"] for v in layers.values())
    assert pooled_resp > pooled_base, "cortical units do not respond to the gratings"
    lf = res["lfp"]
    assert np.all(np.isfinite(lf["db"])), "band power is not finite"
    for cid, where, db in zip(lf["channel_ids"], lf["location"], lf["db"]):
        print(f"  electrode {cid} {where:>9}: {db:+.2f} dB")
    print(f"{res['n_onsets']} grating onsets of {res['n_rows']} rows; "
          f"{lf['n_trials']} trials x {lf['epoch_n_samples']} samples per LFP epoch")


if __name__ == "__main__":
    main()

References

  • Westerberg et al. (2024), DANDI 000253, cited in The data.