get_all_data_from_level_h5_rec.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import h5py
  2. import numpy as np
  3. def get_all_data_from_level_h5_rec(filename, group_name, markers, all_strings_mk, spikes, all_strings_sp):
  4. """
  5. This function recursively reads data from an HDF5 file.
  6. Parameters:
  7. filename (str): A string that identifies the HDF5 file from which to extract data.
  8. group_name (str): The group from which to start extracting data.
  9. data (list, optional): A list to add and store the extracted data. Defaults to None.
  10. all_strings (list, optional): A list to add and store the paths of the extracted datasets. Defaults to None.
  11. Returns:
  12. data (list): A list containing the extracted data for each dataset.
  13. all_strings (list): A list containing the paths of the extracted datasets.
  14. authors: Stefano Diomedi and Francesco E. Vaccari
  15. date: 03/2024
  16. """
  17. def visitor_func(name, node):
  18. if isinstance(node, h5py.Group):
  19. node.visititems(visitor_func)
  20. if isinstance(node, h5py.Dataset):
  21. # ...read the data from the dataset and append it to the 'data' list.
  22. if name == 'event_markers' and node.name not in all_strings_mk:
  23. markers.append(node[()])
  24. # ...append the path of the dataset to the 'all_strings' list.
  25. all_strings_mk.append(node.name)
  26. if name == 'spike_trains' and node.name not in all_strings_sp:
  27. spikes.append(node[()])
  28. all_strings_sp.append(node.name)
  29. with h5py.File(filename, 'r') as f:
  30. f[group_name].visititems(visitor_func)
  31. return markers, all_strings_mk, spikes, all_strings_sp