H5_raster.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # The script plots the neural and behavioural data contained in a .h5 file of the Diomedi et al., 2023 dataset
  2. # in the form of a raster plot
  3. # authors: Stefano Diomedi
  4. # date: 10/2022
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. from supportFunctions import get_all_data_from_level_h5_rec
  8. import tkinter as tk
  9. from tkinter import filedialog
  10. import os
  11. import h5py
  12. # Defining the custom colormap
  13. clmap = np.array([
  14. [0, 0, 0.6667],
  15. [0, 0, 1.0000],
  16. [0, 0.3333, 1.0000],
  17. [0, 0.6667, 1.0000],
  18. [0, 1.0000, 1.0000],
  19. [0.3333, 1.0000, 0.6667],
  20. [0.6667, 1.0000, 0.3333],
  21. [1.0000, 1.0000, 0],
  22. [1.0000, 0.6667, 0],
  23. [1.0000, 0.3333, 0]
  24. ])
  25. # Get the current directory
  26. current_dir = os.getcwd()
  27. # Path to open the file dialog window in the data branch
  28. path_folder_data = os.path.abspath(os.path.join(current_dir, "..", ".."))
  29. path_folder_data = os.path.join(path_folder_data, "data")
  30. # Select H5 dataset file
  31. root = tk.Tk()
  32. root.withdraw()
  33. filename = filedialog.askopenfilename(initialdir=path_folder_data)
  34. group_name = '/DATA' # it can be 'unit_XXX', 'unit_XXX/condition_YY' or 'unit_XXX/condition_YY/trial_ZZ'
  35. markers = []
  36. all_strings_mk = []
  37. spikes = []
  38. all_strings_sp = []
  39. markers, all_strings_mk, spikes, all_strings_sp = get_all_data_from_level_h5_rec.get_all_data_from_level_h5_rec(filename, group_name, markers, all_strings_mk, spikes, all_strings_sp)
  40. str2readmk = '/DATA/unit_001/condition_01/trial_01/event_markers'
  41. with h5py.File(filename, 'r') as f:
  42. marker_labels = f[str2readmk].attrs['Marker labels']
  43. count = 1
  44. fig, ax = plt.subplots()
  45. first_iteration = True
  46. for spike_data, marker_data in zip(spikes, markers):
  47. ax.scatter(spike_data, np.ones_like(spike_data) * count, marker='|', color='k')
  48. sz = 30
  49. for mk in range(marker_data.size):
  50. cc = clmap[mk]
  51. if first_iteration:
  52. ax.scatter(marker_data[mk], np.ones_like(marker_data[mk]) * count,
  53. s=sz, color=cc, marker='o', edgecolors=cc, label=marker_labels[mk])
  54. else:
  55. ax.scatter(marker_data[mk], np.ones_like(marker_data[mk]) * count,
  56. s=sz, color=cc, marker='o', edgecolors=cc)
  57. count = count + 1
  58. first_iteration= False
  59. ax.set_ylim([0.5, count ])
  60. ax.set_xlabel('Time (ms)')
  61. ax.set_ylabel('Trial #')
  62. ax.legend()
  63. ax.set_title('Raster Plot')
  64. plt.show()