Skip to content

Instantly share code, notes, and snippets.

@rly
Last active November 21, 2023 02:46
Show Gist options
  • Save rly/7b3365d157ebb7d0a565bbd48eb12fdd to your computer and use it in GitHub Desktop.
Save rly/7b3365d157ebb7d0a565bbd48eb12fdd to your computer and use it in GitHub Desktop.
Script to fix incorrect namespace attributes in NWB files in dandiset 000114
import glob
import h5py
import argparse
from typing import Union
def replace_namespace(_: str, h5obj: Union[h5py.Dataset, h5py.Group]):
# change the attribute "namespace" from value "hdmf-experimental" to "hdmf-common"
# for all DynamicTableRegion and VectorData neurodata types from ndx-photometry.
if h5obj.attrs.get("namespace") == "hdmf-experimental":
assert h5obj.attrs.get("neurodata_type") in (
"DynamicTableRegion",
"VectorData",
)
h5obj.attrs["namespace"] = "hdmf-common"
print("Changed namespace for", h5obj.name)
def adjust(filepath):
print("-------------------------------------------------------------------")
print("Adjusting NWB file:", filepath)
with h5py.File(filepath, "a") as f:
f.visititems(replace_namespace)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"path", help="path to the NWB file or directory of NWB files to adjust"
)
args = parser.parse_args()
path = args.path
if path.endswith(".nwb"):
filepaths = [path]
else:
filepaths = glob.glob(path + "/**/*.nwb", recursive=True)
print("Adjusting these NWB files:", filepaths, sep="\n")
for filepath in filepaths:
adjust(filepath=filepath)
if __name__ == "__main__":
main()
@rly
Copy link
Author

rly commented Nov 21, 2023

Expected output for each ophys NWB file is:

Changed namespace for /acquisition/RoiResponseSeries/rois
Changed namespace for /general/fiber_photometry/excitation_sources/commanded_voltage
Changed namespace for /general/fiber_photometry/excitation_sources/peak_wavelength
Changed namespace for /general/fiber_photometry/excitation_sources/source_type
Changed namespace for /general/fiber_photometry/fibers/excitation_source
Changed namespace for /general/fiber_photometry/fibers/fluorophores
Changed namespace for /general/fiber_photometry/fibers/location
Changed namespace for /general/fiber_photometry/fibers/notes
Changed namespace for /general/fiber_photometry/fibers/photodetector
Changed namespace for /general/fiber_photometry/fluorophores/coordinates
Changed namespace for /general/fiber_photometry/fluorophores/label
Changed namespace for /general/fiber_photometry/fluorophores/location
Changed namespace for /general/fiber_photometry/photodetectors/gain
Changed namespace for /general/fiber_photometry/photodetectors/peak_wavelength
Changed namespace for /general/fiber_photometry/photodetectors/type
Changed namespace for /processing/behavior/behavioral_events/behavior
Changed namespace for /processing/behavior/behavioral_events/start_time
Changed namespace for /processing/behavior/behavioral_events/stop_time

Expected output for each ecephys NWB file is:

Changed namespace for /acquisition/ElectricalSeries/electrodes
Changed namespace for /general/extracellular_ephys/electrodes/channel_name
Changed namespace for /general/extracellular_ephys/electrodes/gain_to_uV
Changed namespace for /general/extracellular_ephys/electrodes/group
Changed namespace for /general/extracellular_ephys/electrodes/group_name
Changed namespace for /general/extracellular_ephys/electrodes/location
Changed namespace for /general/extracellular_ephys/electrodes/offset_to_uV

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