diff --git a/.gitignore b/.gitignore index 1767f2597..6566ba43c 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..4bcf8c55b 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 to existing groups +# ---------------------------------- +# You can add new datasets to existing groups using PyNWB by calling ``set_modified()``. +# Here's an example of adding a genotype to a Subject: + +from pynwb import NWBFile, NWBHDF5IO +from pynwb.file import Subject + +# First, let's create a file with a Subject that is missing the genotype, which is optional +nwbfile = NWBFile( + session_description="example file with subject", + identifier="EXAMPLE_ID", + session_start_time=datetime.now(tzlocal()), + 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 the genotype using PyNWB and set_modified() +with NWBHDF5IO("test_edit3.nwb", "a") as io: + nwbfile = io.read() + nwbfile.subject.genotype = "Sst-IRES-Cre" + nwbfile.subject.set_modified() # Required to mark the container as modified + io.write(nwbfile) + +# Verify the dataset was added +with NWBHDF5IO("test_edit3.nwb", "r") as io: + nwbfile = io.read() + print(f"Subject genotype: {nwbfile.subject.genotype}") + # Output: Subject genotype: Sst-IRES-Cre