| 1 | """ isi_lengths.py |
| 2 | |
| 3 | Support for automatic threshold determination |
| 4 | |
| 5 | Copyright 2023, Thomas Kreuz |
| 6 | |
| 7 | Distributed under the BSD License |
| 8 | """ |
| 9 | import numpy as np |
| 10 | |
| 11 | def isi_lengths(spike_times, t_start, t_end): |
| 12 | """ Plain Python implementation of logic to extract ISI lengths |
| 13 | In: spike_times - spike times |
| 14 | t_start, t_end - interval for ISI calculation |
| 15 | Out: isi_lengths - ISI distance between consecutive elements of spike_events |
| 16 | |
| 17 | Note: the only complexities are with the edges and N==1 |
| 18 | """ |
| 19 | N = len(spike_times) |
| 20 | if N == 0: |
| 21 | return [t_end-t_start] |
| 22 | |
| 23 | if spike_times[0] > t_start: |
| 24 | del_start = max(spike_times[0] - t_start, spike_times[1] - spike_times[0])\ |
| 25 | if N > 1 else spike_times[0] - t_start |
| 26 | i_start = 0 |
| 27 | else: |
| 28 | del_start = spike_times[1] - spike_times[0]\ |
| 29 | if N > 1 else t_start - spike_times[0] |
| 30 | i_start = 1 |
| 31 | |
| 32 | if spike_times[-1] < t_end: |
| 33 | del_end = max(t_end - spike_times[-1], spike_times[-1] - spike_times[-2])\ |
| 34 | if N > 1 else t_end - spike_times[0] |
| 35 | i_end = N |
| 36 | else: |
| 37 | del_end = spike_times[-1] - spike_times[-2]\ |
| 38 | if N > 1 else spike_times[0] - t_end |
| 39 | i_end = N-1 |
| 40 | |
| 41 | dels = [spike_times[i+1]-spike_times[i] for i in range(i_start, i_end-1)] |
| 42 | |
| 43 | isi_lengths = [del_start] + dels + [del_end] |
| 44 | |
| 45 | return isi_lengths |
| 46 | |
| 47 | def default_thresh_(train_list, t_start, t_end): |
| 48 | """ Implements default_thresh() |
| 49 | In: train_list - list of list of spike times |
| 50 | t_start, t_end - begin and end times for spikes |
| 51 | Out: threshold |
| 52 | """ |
| 53 | spike_pool = [] |
| 54 | for t in train_list: |
| 55 | spike_pool += isi_lengths(t, t_start, t_end) |
| 56 | |
| 57 | spike_pool = np.array(spike_pool) |
| 58 | sum_squares = np.sum(spike_pool * spike_pool) |
| 59 | ss_avg = sum_squares/len(spike_pool) |
| 60 | |
| 61 | return np.sqrt(ss_avg) |
| 62 | |
| 63 | def default_thresh(spike_train_list): |
| 64 | """ Computes a default threshold for a list of spike trains |
| 65 | In: spike_train_list - list of list of SpikeTrain object |
| 66 | Out: threshold as specified in section 2.4 of |
| 67 | "Measures of spike train synchrony for data with multiple time scales" |
| 68 | """ |
| 69 | if len(spike_train_list) == 0: |
| 70 | return 0 |
| 71 | |
| 72 | st = spike_train_list[0] |
| 73 | train_list = [st.spikes.tolist() for st in spike_train_list] |
| 74 | |
| 75 | return default_thresh_(train_list, st.t_start, st.t_end) |