1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import nixio as nix
- import matplotlib.pyplot as plt
- import numpy as np
- import os
- from IPython import embed
- def metadata_overview(nf):
- assert(nf and isinstance(nf, nix.pycore.file.File) and nf.is_open)
- print("Print some of the stored metadata")
- print_metadata(nix_file, sec_type="recording", sec_name="recording", max_depth=0)
- print("\n")
- print_metadata(nix_file, sec_name="subject")
- print("\n")
- print_metadata(nix_file, sec_name="cell")
- print("\n")
- def print_metadata(nf, sec_type=None, sec_name=None, max_depth=-1):
- """Display stored metadata using the section's pprint function.
- If the desired section name or type are provided, the find_sections
- method is used to filter the metadata, if neither is provided all sections
- will be printed.
- Arguments:
- nf -- opened nix file
- Keyword arguments:
- sec_type --- string: the section type (default None)
- sec_name --- string, the section name (default None)
- max_depth --- int, the maximum depth of the metadata tree (default -1, i.e. unlimited)
- """
- assert(not sec_name or isinstance(sec_name, str))
- assert(not sec_type or isinstance(sec_type, str))
- sec_type = sec_type.lower() if sec_type else None
- sec_name = sec_name.lower() if sec_name else None
- sections = nf.find_sections(lambda s:
- (sec_type and sec_type in s.type.lower() or not sec_type)
- and (sec_name and sec_name in s.name.lower() or not sec_name)
- or (not sec_name and not sec_type))
- for s in sections:
- s.pprint(max_depth=max_depth)
- def data_overview(nf):
- assert(nf and isinstance(nf, nix.pycore.file.File) and nf.is_open)
- b = nf.blocks[0]
- print("Regularly sampled data:")
- regular_sampled_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Sample]
- for rsd in regular_sampled_data:
- print("\t%s, size: %s" % (rsd.name, str(rsd.shape)))
- print("Event data:")
- event_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Range]
- for ed in event_data:
- print("\t%s, size: %s" % (ed.name, str(ed.shape)))
- print("Sets:")
- set_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Set]
- for sd in set_data:
- print("\t%s, size: %s" % (sd.name, str(sd.shape)))
- if __name__ == "__main__":
- example_file = "2018-11-05-ab-invivo-1.nix"
- if not os.path.exists(example_file):
- print("Example file was not found")
- exit()
- nix_file = nix.File.open(example_file, nix.FileMode.ReadOnly)
- metadata_overview(nix_file)
- data_overview(nix_file)
- nix_file.close()
|