diff --git a/.gitignore b/.gitignore index 95f08686e..e54d2cd91 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,11 @@ docs/build/ docs/source/pynwb.*.rst +# Virtual Environment +venv/ +env/ +ENV/ + # setuptools build/ dist/ diff --git a/docs/gallery/advanced_io/plot_editing.py b/docs/gallery/advanced_io/plot_editing.py index b08b2adfd..32a1b15f7 100644 --- a/docs/gallery/advanced_io/plot_editing.py +++ b/docs/gallery/advanced_io/plot_editing.py @@ -162,3 +162,41 @@ "synthetic_timeseries_renamed", "/analysis/synthetic_timeseries_renamed", ) + +############################################## +# Adding datasets using h5py +# -------------------------- +# You can also use h5py to add new datasets to existing groups. Here's an example of +# adding a genotype dataset to the Subject object: + +from pynwb import NWBFile, NWBHDF5IO +from pynwb.file import Subject +import h5py +from datetime import datetime + +# First, let's create a file with a Subject that is missing a genotype +nwbfile = NWBFile( + session_description="example file with subject", + identifier="EXAMPLE_ID", + session_start_time=datetime.now(), + session_id="LONELYMTN", + subject=Subject( + subject_id="mouse001", + species="Mus musculus", + age="P30D", + ) +) + +with NWBHDF5IO("test_edit3.nwb", "w") as io: + io.write(nwbfile) + +# Now add a genotype dataset to the Subject using h5py +with h5py.File("test_edit3.nwb", "r+") as f: + # Create the genotype dataset in the subject group + subject_group = f["general/subject"] + subject_group.create_dataset("genotype", data="Sst-IRES-Cre") + +# Verify the dataset was added +with NWBHDF5IO("test_edit3.nwb", "r") as io: + nwbfile = io.read() + print(f"Subject genotype: {nwbfile.subject.genotype}")