Last active
November 21, 2023 02:46
-
-
Save rly/7b3365d157ebb7d0a565bbd48eb12fdd to your computer and use it in GitHub Desktop.
Script to fix incorrect namespace attributes in NWB files in dandiset 000114
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected output for each ophys NWB file is:
Expected output for each ecephys NWB file is: