Created
March 10, 2025 18:59
-
-
Save rly/3ea9dee34f4b12c36083faafefc7fe27 to your computer and use it in GitHub Desktop.
A Python function to extract specification names and versions from NWB files.
This file contains hidden or 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
""" | |
A Python function to extract specification names and versions from NWB files. | |
Can be easily modified to extract only extensions. core, hdmf-common, hdmf-experimental are not extensions. | |
The script uses h5py to open the NWB file instead of PyNWB because it is faster. | |
""" | |
import h5py | |
from packaging import version | |
def get_nwb_specifications_names_versions(nwb_file_path): | |
""" | |
Extract all specification names and versions from an NWB file. | |
Args: | |
nwb_file_path (str): The path to the NWB file. | |
Returns: | |
dict: A dictionary containing the specification names and versions. | |
""" | |
specifications = {} | |
with h5py.File(nwb_file_path, 'r') as f: | |
if 'specifications' in f: | |
specs_group = f['specifications'] | |
if len(specs_group) > 0: | |
for name in specs_group: | |
extension_group = specs_group[name] | |
assert isinstance(extension_group, h5py.Group), f"{name} is not a group" | |
sorted_versions = sorted(extension_group, key=version.parse) | |
assert len(sorted_versions) > 0, f"Extension {name} has no versions" | |
# Use only the latest version | |
latest_version = sorted_versions[-1] | |
specifications[name] = latest_version | |
else: | |
print("Warning: No extensions found in the specifications group") | |
else: | |
print("Warning: No specifications group found in the NWB file") | |
return specifications | |
if __name__ == "__main__": | |
nwb_file_path = "/Users/rly/Documents/NWB_Data/dandisets/000409/sub-CSHL047/sub-CSHL047_ses-b52182e7-39f6-4914-9717-136db589706e_behavior+ecephys+image.nwb" | |
specifications_names_versions = get_nwb_specifications_names_versions(nwb_file_path) | |
# Remove core, hdmf-common, and hdmf-experimental from the specifications | |
extensions_names_versions = specifications_names_versions.copy() | |
extensions_names_versions.pop("core") | |
extensions_names_versions.pop("hdmf-common") | |
extensions_names_versions.pop("hdmf-experimental") | |
print(extensions_names_versions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment