Open In Colab

Installing requirements¶

The cell below installs every Python package needed to run this notebook, at fully pinned versions, using uv for fast resolution. In Colab the cell is collapsed by default — click the ▶ button to run it.

In [1]:
# install cell skipped during CI (deps preinstalled into system Python)

⚠️ Restart runtime after install

The install may upgrade packages already loaded in the kernel. Go to Runtime → Restart session, then Run all cells below (skip this install cell on re-run).

000458 - summarize contents¶

This notebook summarizes the contents of all the NWB files in Dandiset 000458 including the file size, session description, and trial information. Readers can quickly and easily use these descriptions to determine which NWB files to investigate further.

In [2]:
# Suppress hdmf warnings about cached namespaces
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="hdmf.utils")
In [3]:
from helpers.stream_nwbfile import stream_nwbfile
import dandi.dandiarchive as da
from IPython.display import display, Markdown


DANDISET_ID = "000458"
DANDISET_VERSION = "0.230317.0039"

parsed_url = da.parse_dandi_url(f"https://dandiarchive.org/dandiset/{DANDISET_ID}")

with parsed_url.navigate() as (client, dandiset, assets):
    if dandiset is None:
        raise Exception(f"Dandiset {DANDISET_ID} not found.")

    num_consecutive_not_nwb = 0
    asset_index = 0
    for asset_obj in dandiset.get_assets('path'):
        if not asset_obj.path.endswith(".nwb"):
            num_consecutive_not_nwb += 1
            if num_consecutive_not_nwb >= 20:
                # For example, this is important for 000026 because there are so many non-nwb assets
                raise Exception("Too many consecutive non-NWB files.")
            continue
        else:
            num_consecutive_not_nwb = 0
        asset = {
            "identifier": asset_obj.identifier,
            "path": asset_obj.path,
            "size": asset_obj.size,
            "download_url": asset_obj.download_url,
            "dandiset_id": DANDISET_ID,
        }
        nwbfile, io = stream_nwbfile(DANDISET_ID, asset["path"], DANDISET_VERSION)
        md = ''
        ns_url = f'https://neurosift.app/?p=/nwb&url={asset["download_url"]}&dandisetId={DANDISET_ID}'
        md += f'**Asset {asset_index + 1}: [{asset["path"]}]({ns_url})**\n\n'
        experiment_description = nwbfile.experiment_description  # type: ignore
        session_description = nwbfile.session_description  # type: ignore
        stimulus_notes = nwbfile.stimulus_notes  # type: ignore
        md += experiment_description + '\n\n'
        md += session_description + '\n\n'
        md += stimulus_notes + '\n\n'
        trials = nwbfile.intervals['trials'].to_dataframe()  # type: ignore
        num_trials = len(trials)
        md += f'* Size: {asset["size"] / 1e6:.2f} MB\n'
        md += f"* {num_trials} trials\n"
        behavioral_epochs = trials['behavioral_epoch']
        is_valid = trials['is_valid']
        is_running = trials['is_running']
        unique_behavioral_epochs = behavioral_epochs.unique()
        md += f"* BEHAVIORAL EPOCHS\n"
        for be in unique_behavioral_epochs:
            num = len(behavioral_epochs[behavioral_epochs == be])
            num_valid = len(is_valid[(behavioral_epochs == be) & is_valid])
            num_valid_and_not_running = len(is_valid[(behavioral_epochs == be) & is_valid & ~is_running])
            md += f"  - {be}: {num_valid_and_not_running} valid and not running\n"
        stimulus_types = trials['stimulus_type']
        unique_stimulus_types = stimulus_types.unique()
        md += f"* STIMULUS TYPES\n"
        for st in unique_stimulus_types:
            num = len(stimulus_types[stimulus_types == st])
            num_valid = len(is_valid[(stimulus_types == st) & is_valid])
            num_valid_and_not_running = len(is_valid[(stimulus_types == st) & is_valid & ~is_running])
            md += f"  - {st}: {num_valid_and_not_running} valid and not running\n"
        stimulus_descriptions = trials['stimulus_description']
        unique_stimulus_descriptions = stimulus_descriptions.unique()
        md += f"* STIMULUS DESCRIPTIONS\n"
        for sd in unique_stimulus_descriptions:
            num = len(stimulus_descriptions[stimulus_descriptions == sd])
            num_valid = len(is_valid[(stimulus_descriptions == sd) & is_valid])
            num_valid_and_not_running = len(is_valid[(stimulus_descriptions == sd) & is_valid & ~is_running])
            md += f"  - {sd}: {num_valid_and_not_running} valid and not running\n"
        estim_target_regions = trials['estim_target_region']
        unique_estim_target_regions = estim_target_regions.unique()
        md += f"* ESTIM TARGET REGIONS\n"
        for etr in unique_estim_target_regions:
            num = len(estim_target_regions[estim_target_regions == etr])
            num_valid = len(is_valid[(estim_target_regions == etr) & is_valid])
            num_valid_and_not_running = len(is_valid[(estim_target_regions == etr) & is_valid & ~is_running])
            md += f"  - {etr}: {num_valid_and_not_running} valid and not running\n"
        display(Markdown(md))
        asset_index += 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_2547/39476564.py in <cell line: 0>()
     31             "dandiset_id": DANDISET_ID,
     32         }
---> 33         nwbfile, io = stream_nwbfile(DANDISET_ID, asset["path"], DANDISET_VERSION)
     34         md = ''
     35         ns_url = f'https://neurosift.app/?p=/nwb&url={asset["download_url"]}&dandisetId={DANDISET_ID}'

TypeError: stream_nwbfile() takes 2 positional arguments but 3 were given