Sfoglia il codice sorgente

Carica file su 'code/python'

Stefano Diomedi 2 mesi fa
parent
commit
8d06a9ef8d
2 ha cambiato i file con 147 aggiunte e 0 eliminazioni
  1. 66 0
      code/python/H5_ISI.py
  2. 81 0
      code/python/H5_raster.py

+ 66 - 0
code/python/H5_ISI.py

@@ -0,0 +1,66 @@
+# The script calculates the Inter Spike Intervals (ISIs) and plots the
+# results in a histogram. The proportion of ISIs violating a threshold
+# is also calculated and indicated in the histogram.
+
+# authors: Stefano Diomedi
+# date: 10/2022
+
+import numpy as np
+import matplotlib.pyplot as plt
+from supportFunctions import get_all_data_from_level_h5_rec
+import tkinter as tk
+from tkinter import filedialog
+import os
+
+
+current_dir = os.getcwd()
+
+# Path to open the file dialog window in the data branch
+path_folder_data = os.path.abspath(os.path.join(current_dir, "..", ".."))
+path_folder_data = os.path.join(path_folder_data, "data")
+
+# Select H5 dataset file
+root = tk.Tk()
+root.withdraw()
+filename = filedialog.askopenfilename(initialdir=path_folder_data)
+
+group_name = '/DATA/unit_001'  # it can be 'unit_XXX', 'unit_XXX/condition_YY' or 'unit_XXX/condition_YY/trial_ZZ'
+threshold = 1  # threshold for violations in ms
+
+markers = []
+all_strings_mk = []
+spikes = []
+all_strings_sp = []
+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)
+
+
+
+ISIs = []
+
+# Iterate over each array of spike data
+for spike_data in spikes:
+    # Calculate the differences for each spike_data array
+    spike_data=np.array(spike_data)
+    diff_data = np.diff(spike_data.flatten())
+    # Append the differences to the list
+    ISIs.append(diff_data)
+
+# Concatenate all the differences into a single array
+ISIs = np.concatenate(ISIs)
+
+# Calculate the violations
+violations = (ISIs < threshold).sum() / ISIs.size * 100
+
+
+# Creation of Histogram
+fig, ax = plt.subplots()
+time = np.arange(0, 100.5, 0.5)
+ax.hist(ISIs, bins=time)
+ax.axvline(threshold, linestyle='--')
+xLimits = ax.get_xlim()
+yLimits = ax.get_ylim()
+ax.text(xLimits[1] / 4, yLimits[1] * 2 / 3, f'Violations (ISI below {threshold}ms threshold) = {violations:.2f}%', fontsize=12)
+ax.set_xlabel('ISI duration (ms)')
+ax.set_ylabel('events num.')
+ax.set_title('ISI')
+plt.show()

+ 81 - 0
code/python/H5_raster.py

@@ -0,0 +1,81 @@
+# The script plots the neural and behavioural data contained in a .h5 file of the Diomedi et al., 2023 dataset
+# in the form of a raster plot
+# authors: Stefano Diomedi
+# date: 10/2022
+
+import numpy as np
+import matplotlib.pyplot as plt
+from supportFunctions import get_all_data_from_level_h5_rec
+import tkinter as tk
+from tkinter import filedialog
+import os
+import h5py
+
+# Defining the custom colormap
+clmap = np.array([
+    [0, 0, 0.6667],
+    [0, 0, 1.0000],
+    [0, 0.3333, 1.0000],
+    [0, 0.6667, 1.0000],
+    [0, 1.0000, 1.0000],
+    [0.3333, 1.0000, 0.6667],
+    [0.6667, 1.0000, 0.3333],
+    [1.0000, 1.0000, 0],
+    [1.0000, 0.6667, 0],
+    [1.0000, 0.3333, 0]
+])
+
+# Get the current directory
+current_dir = os.getcwd()
+
+# Path to open the file dialog window in the data branch
+path_folder_data = os.path.abspath(os.path.join(current_dir, "..", ".."))
+path_folder_data = os.path.join(path_folder_data, "data")
+
+# Select H5 dataset file
+root = tk.Tk()
+root.withdraw()
+filename = filedialog.askopenfilename(initialdir=path_folder_data)
+
+
+group_name = '/DATA/unit_001'  # it can be 'unit_XXX', 'unit_XXX/condition_YY' or 'unit_XXX/condition_YY/trial_ZZ'
+
+markers = []
+all_strings_mk = []
+spikes = []
+all_strings_sp = []
+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)
+
+str2readmk = '/DATA/unit_001/condition_01/trial_01/event_markers'
+
+with h5py.File(filename, 'r') as f:
+    marker_labels = f[str2readmk].attrs['Marker labels']
+
+count = 1
+fig, ax = plt.subplots()
+first_iteration = True   
+for spike_data, marker_data in zip(spikes, markers):
+
+    ax.scatter(spike_data, np.ones_like(spike_data) * count, marker='|', color='k')
+    sz = 30
+
+    for mk in range(marker_data.size):
+        cc = clmap[mk]
+
+        if first_iteration:
+            ax.scatter(marker_data[mk], np.ones_like(marker_data[mk]) * count,
+                       s=sz, color=cc, marker='o', edgecolors=cc, label=marker_labels[mk])
+        else:
+            ax.scatter(marker_data[mk], np.ones_like(marker_data[mk]) * count,
+                       s=sz, color=cc, marker='o', edgecolors=cc)
+
+
+    count = count + 1
+    first_iteration= False
+
+ax.set_ylim([0.5, count ])
+ax.set_xlabel('Time (ms)')
+ax.set_ylabel('Trial #')
+ax.legend()
+ax.set_title('Raster Plot')
+plt.show()