explore.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 find_mtags_for_tag(block, tag):
  38. """Finds the MultiTags (prepresentatives of the actual stimulus
  39. outputs) that ran during a RePro run. Relacs makes sure that stimulations
  40. with the same settings end up in the same MultiTag.
  41. Arguments:
  42. block --- nix.Block, the block in which to look for MultiTags
  43. tag --- nix.Tag, the Tag (aka, the RepPro run) defining the search interval
  44. Returns:
  45. a list of MultiTags
  46. a list of lists containing the position indices for the respective MultiTags
  47. """
  48. assert(isinstance(block, nix.pycore.block.Block))
  49. assert(isinstance(tag, nix.pycore.tag.Tag))
  50. mtags = []
  51. positions = []
  52. tag_start = np.atleast_1d(tag.position)
  53. tag_end = tag_start + np.atleast_1d(tag.extent)
  54. for mt in block.multi_tags:
  55. mt_starts = np.atleast_1d(mt.positions[:])
  56. mt_ends = mt_starts + np.atleast_1d(mt.extents[:])
  57. pos = np.argwhere(mt_starts[(mt_starts > tag_start) &
  58. (mt_starts < tag_end) &
  59. (mt_ends <= tag_end)])[:, 0]
  60. if len(pos) > 0:
  61. mtags.append(mt)
  62. positions.append(pos)
  63. return mtags, positions
  64. def data_overview(nf):
  65. assert(nf and isinstance(nf, nix.pycore.file.File) and nf.is_open)
  66. b = nf.blocks[0]
  67. print("Regularly sampled data:")
  68. regular_sampled_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Sample]
  69. for rsd in regular_sampled_data:
  70. print("\t%s, size: %s" % (rsd.name, str(rsd.shape)))
  71. print("Event data:")
  72. event_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Range]
  73. for ed in event_data:
  74. print("\t%s, size: %s" % (ed.name, str(ed.shape)))
  75. print("Sets:")
  76. set_data = [da for da in b.data_arrays if da.dimensions[0].dimension_type == nix.DimensionType.Set]
  77. for sd in set_data:
  78. print("\t%s, size: %s" % (sd.name, str(sd.shape)))
  79. print("\n")
  80. def stimulus_overview(nf):
  81. assert(nf and isinstance(nf, nix.pycore.file.File) and nf.is_open)
  82. b = nf.blocks[0]
  83. print("Stimulations, aka RePro runs:")
  84. repro_runs = [t for t in b.tags if "repro_run" in t.type]
  85. for rr in repro_runs:
  86. start = rr.position[0]
  87. end = start + rr.extent[0]
  88. print("\t RePro %s, time: %.4f --> %.4f%s" % (rr.name, start, end, rr.units[0]))
  89. mts, positions = find_mtags_for_tag(b, rr)
  90. for mt, pos in zip(mts, positions):
  91. print("\t\t with %i stimulus repetitions" % (len(pos)))
  92. print("\n")
  93. if __name__ == "__main__":
  94. example_file = "2018-11-05-ab-invivo-1.nix"
  95. if not os.path.exists(example_file):
  96. print("Example file was not found")
  97. exit()
  98. nix_file = nix.File.open(example_file, nix.FileMode.ReadOnly)
  99. metadata_overview(nix_file)
  100. data_overview(nix_file)
  101. stimulus_overview(nix_file)
  102. nix_file.close()