1
0

explore.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import nixio as nix
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import os
  5. from IPython import embed
  6. def metadata_overview(nf):
  7. assert(nf and isinstance(nf, nix.pycore.file.File) and nf.is_open)
  8. print("Print some of the stored metadata")
  9. print_metadata(nix_file, sec_type="recording", sec_name="recording", max_depth=0)
  10. print("\n")
  11. print_metadata(nix_file, sec_name="subject")
  12. print("\n")
  13. print_metadata(nix_file, sec_name="cell")
  14. print("\n")
  15. def print_metadata(nf, sec_type=None, sec_name=None, max_depth=-1):
  16. """Display stored metadata using the section's pprint function.
  17. If the desired section name or type are provided, the find_sections
  18. method is used to filter the metadata, if neither is provided all sections
  19. will be printed.
  20. Arguments:
  21. nf -- opened nix file
  22. Keyword arguments:
  23. sec_type --- string: the section type (default None)
  24. sec_name --- string, the section name (default None)
  25. max_depth --- int, the maximum depth of the metadata tree (default -1, i.e. unlimited)
  26. """
  27. assert(not sec_name or isinstance(sec_name, str))
  28. assert(not sec_type or isinstance(sec_type, str))
  29. sec_type = sec_type.lower() if sec_type else None
  30. sec_name = sec_name.lower() if sec_name else None
  31. sections = nf.find_sections(lambda s:
  32. (sec_type and sec_type in s.type.lower() or not sec_type)
  33. and (sec_name and sec_name in s.name.lower() or not sec_name)
  34. or (not sec_name and not sec_type))
  35. for s in sections:
  36. s.pprint(max_depth=max_depth)
  37. def data_overview(nf):
  38. assert(nf and isinstance(nf, nix.pycore.file.File) and nf.is_open)
  39. b = nf.blocks[0]
  40. print("Regularly sampled data:")
  41. regular_sampled_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Sample]
  42. for rsd in regular_sampled_data:
  43. print("\t%s, size: %s" % (rsd.name, str(rsd.shape)))
  44. print("Event data:")
  45. event_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Range]
  46. for ed in event_data:
  47. print("\t%s, size: %s" % (ed.name, str(ed.shape)))
  48. print("Sets:")
  49. set_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Set]
  50. for sd in set_data:
  51. print("\t%s, size: %s" % (sd.name, str(sd.shape)))
  52. if __name__ == "__main__":
  53. example_file = "2018-11-05-ab-invivo-1.nix"
  54. if not os.path.exists(example_file):
  55. print("Example file was not found")
  56. exit()
  57. nix_file = nix.File.open(example_file, nix.FileMode.ReadOnly)
  58. metadata_overview(nix_file)
  59. data_overview(nix_file)
  60. nix_file.close()