Skip to content

Instantly share code, notes, and snippets.

@rly
Last active January 21, 2022 17:23
Show Gist options
  • Save rly/65f54f0c15a4ce0444b474fd0c1732ed to your computer and use it in GitHub Desktop.
Save rly/65f54f0c15a4ce0444b474fd0c1732ed to your computer and use it in GitHub Desktop.
Demonstration of how to move a container from one location to another after it has been written to disk.
from pynwb import NWBFile, NWBHDF5IO
from pynwb.ecephys import LFP, ElectricalSeries
import numpy as np
import datetime
# Create a test file
nwb = NWBFile(
session_description='session_description',
identifier='identifier',
session_start_time=datetime.datetime.now(datetime.timezone.utc),
)
# Add electrode groups and channels
nChannels = 10
dev0 = nwb.create_device(name='dev0')
elecs_group = nwb.create_electrode_group(name='electrodes', description='', location='ctx', device=dev0)
for i in np.arange(nChannels):
ii = float(i)
nwb.add_electrode(x=ii, y=ii, z=ii, imp=ii, location='', filtering='', group=elecs_group)
# Add ElectricalSeries
elecs_region = nwb.electrodes.create_region(
name='electrodes',
region=np.arange(nChannels).tolist(),
description=''
)
X_data = np.zeros((nChannels, 1000))
signal = ElectricalSeries(
name='ElectricalSeries',
data=X_data,
electrodes=elecs_region,
starting_time=0.,
rate=2.
)
# Put ElectricalSeries in LFP
lfp = LFP(name='Raw')
lfp.add_electrical_series(signal)
# Add LFP to NWBFile
nwb.add_acquisition(lfp)
filename = 'nwbfile.nwb'
dst_file = 'nwbfile2.nwb'
# Write the test file
with NWBHDF5IO(filename, 'w') as io:
io.write(nwb)
with NWBHDF5IO(filename, mode='r') as src_io:
src_nwb = src_io.read()
raw_lfp = src_nwb.acquisition.pop('Raw') # remove LFP object from acquisition
for es_name in raw_lfp.electrical_series:
# The eseries still has the LFP object as the parent, so we need to reset its parent
# so that its parent can be set to src_nwb
raw_lfp._remove_child(raw_lfp[es_name]) # hacky temporary fix until the below is implemented
# raw_lfp[es_name].reset_parent()
src_nwb.add_acquisition(raw_lfp[es_name])
with NWBHDF5IO(dst_file, mode='w') as dst_io:
# link_data: False is necessary because otherwise an incorrect link is made
dst_io.export(src_io=src_io, nwbfile=src_nwb, write_args={'link_data': False})
@rly
Copy link
Author

rly commented Jan 21, 2022

An alternate approach to this code which uses the private method AbstractContainer._remove_child:

    for es_name in raw_lfp.electrical_series:
        # The eseries still has the LFP object as the parent, so we need to reset its parent
        # so that its parent can be set to src_nwb
        raw_lfp._remove_child(raw_lfp[es_name])  # hacky temporary fix until the below is implemented
        # raw_lfp[es_name].reset_parent()
        src_nwb.add_acquisition(raw_lfp[es_name])

is:

    for es_name in raw_lfp.electrical_series:
        # The eseries still has the LFP object as the parent, so we need to reset its parent
        # so that its parent can be set to src_nwb
        raw_lfp.electrical_series.pop(es_name)
        src_nwb.add_acquisition(raw_lfp[es_name])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment