Downloading an NWB File#

In order to analyze some data, you’ll need to have some data. The DANDI Archive is used to store NWB files in datasets called dandisets [Rübel et al., 2022]. Typically an NWB file contains the data for just one experimental session, while a dandiset contains all the related data files yielded from a project. This notebook allows you to download from public dandisets or private dandisets (called embargoed dandisets) via the DANDI Python API. To download embargoed dandisets from DANDI, you will need to make an account on the DANDI Archive and must be given access by the owner of the dandiset.

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_stream_open
except:
    !git clone https://github.com/AllenInstitute/openscope_databook.git
    %cd openscope_databook
    %pip install -e .
from dandi import dandiapi

Download Configuration#

Here you can configure the download. Browse the DANDI Archive for a dandiset you’re interested in and use its ID in dandiset_id. Also set download_loc to the relative filepath of the directory you’d like to download to. If you’re accessing an embargoed dandiset, you should set authenticate to True, and set dandi_api_key to your DANDI API Key, which can be found if you click on your profile icon in the top-right corner on the DANDI Archive website.

dandiset_id = "000021"
download_loc = "."
authenticate = False
dandi_api_key = ""
if authenticate:
    client = dandiapi.DandiAPIClient(token=dandi_api_key)
else:
    client = dandiapi.DandiAPIClient()
my_dandiset = client.get_dandiset(dandiset_id)

print(f"Got dandiset {my_dandiset}")
A newer version (0.55.1) of dandi/dandi-cli is available. You are using 0.46.6
Got dandiset DANDI:000021/draft

Downloading Just One File#

Set dandi_filepath to the path of the file you want to download within the dandiset. You can get this by navigating to the file you want to download on the DANDI Archive website and pressing on the i icon. There, you can copy the filepath from the field labeled path. Don’t include a leading /.

dandi_filepath = "sub-699733573/sub-699733573_ses-715093703.nwb"
filename = dandi_filepath.split("/")[-1]
filepath = f"{download_loc}/{filename}"
file = my_dandiset.get_asset_by_path(dandi_filepath)
# this may take awhile, especially if the file to download is large
file.download(filepath)

print(f"Downloaded file to {filepath}")
Downloaded file to ./sub-699733573_ses-715093703.nwb

Downloading Entire Dandiset#

If you’d like to do a lot of work with the files in a dandiset, you might want to download the entire thing or some portion of the dandiset. Be prepared, though; This could take a significant amount of space on your drive and a significant amount of time. If you want to just download all the files within a directory of the dandiset, you can set the first argument of download_directory below to a more specific path within the dandiset.

# patience isn't just a virtue, it's a requirement
my_dandiset.download_directory("./", f"{download_loc}/{dandiset_id}")

print(f"Downloaded directory to {download_loc}/{dandiset_id}")
Downloaded directory to ./000021