|
8 | 8 | LGR = logging.getLogger(__name__) |
9 | 9 |
|
10 | 10 |
|
| 11 | +def estimate_ntp_and_tr(phys_in, thr=None, ci=1): |
| 12 | + """ |
| 13 | + Find groups of trigger in a spiky signal like the trigger channel signal. |
| 14 | + """ |
| 15 | + LGR.info('Running automatic clustering of triggers to find timepoints and tr of each "take"') |
| 16 | + trigger = phys_in.timeseries[phys_in.trigger_idx] |
| 17 | + |
| 18 | + thr = np.mean(trigger) if thr is None else thr |
| 19 | + timepoints = trigger > thr |
| 20 | + spikes = np.flatnonzero(np.ediff1d(timepoints.astype(np.int8)) > 0) |
| 21 | + interspike_interval = np.diff(spikes) |
| 22 | + unique_isi, counts = np.unique(interspike_interval, return_counts=True) |
| 23 | + |
| 24 | + # The following line is for python < 3.12. From 3.12, ci.is_integer() is enough. |
| 25 | + if isinstance(ci, int) or isinstance(ci, float) and ci.is_integer(): |
| 26 | + upper_ci_isi = unique_isi + ci |
| 27 | + elif isinstance(ci, float) and ci < 1: |
| 28 | + upper_ci_isi = unique_isi * (1 + ci) |
| 29 | + elif isinstance(ci, float) and ci > 1: |
| 30 | + raise ValueError("Confidence intervals above 1 are not supported.") |
| 31 | + else: |
| 32 | + raise ValueError("Confidence intervals must be either integers or floats.") |
| 33 | + |
| 34 | + # Loop through the uniques ISI and group them within the specified CI. |
| 35 | + # Also compute the average TR of the group. |
| 36 | + isi_groups = {} |
| 37 | + average_tr = {} |
| 38 | + k = 0 |
| 39 | + current_group = [unique_isi[0]] |
| 40 | + |
| 41 | + for n, i in enumerate(range(1, len(unique_isi))): |
| 42 | + if unique_isi[i] <= upper_ci_isi[n]: |
| 43 | + current_group.append(unique_isi[i]) |
| 44 | + else: |
| 45 | + isi_groups[k] = current_group |
| 46 | + average_tr[k] = np.mean(current_group) / phys_in.freq[0] |
| 47 | + k += 1 |
| 48 | + current_group = [unique_isi[i]] |
| 49 | + |
| 50 | + isi_groups[k] = current_group |
| 51 | + average_tr[k] = np.mean(current_group) / phys_in.freq[0] |
| 52 | + |
| 53 | + # Invert the isi_group into value per group |
| 54 | + group_by_isi = {isi: group for group, isis in isi_groups.items() for isi in isis} |
| 55 | + |
| 56 | + # Use the found groups to find the number of timepoints and assign the right TR |
| 57 | + estimated_ntp = [] |
| 58 | + estimated_tr = [] |
| 59 | + |
| 60 | + i = 0 |
| 61 | + while i < interspike_interval.size - 1: |
| 62 | + current_group = group_by_isi.get(interspike_interval[i]) |
| 63 | + for n in range(i + 1, interspike_interval.size): |
| 64 | + if current_group != group_by_isi.get(interspike_interval[n]): |
| 65 | + break |
| 66 | + # Repeat one last time outside of for loop |
| 67 | + estimated_ntp += [n - i] |
| 68 | + estimated_tr += [average_tr[current_group]] |
| 69 | + i = n |
| 70 | + |
| 71 | + if len(estimated_ntp) < 1: |
| 72 | + raise Exception("This should not happen. Something went very wrong.") |
| 73 | + # The algorithm found n groups, the last of which has two timepoints less due to |
| 74 | + # diff computations. Each real group of n>1 triggers counts one trigger less but is |
| 75 | + # followed by a "fake" group of 1 trigger that is actually the interval to the next |
| 76 | + # group. That does not hold if there is a real group of 1 trigger. |
| 77 | + # Loop through the estiamtions to fix all that. |
| 78 | + ntp = [] |
| 79 | + tr = [] |
| 80 | + i = 0 |
| 81 | + |
| 82 | + while i < len(estimated_ntp): |
| 83 | + if estimated_ntp[i] == 1: |
| 84 | + ntp.append(estimated_ntp[i]) |
| 85 | + tr.append(estimated_tr[i]) |
| 86 | + i += 1 |
| 87 | + elif i + 1 < len(estimated_ntp): |
| 88 | + ntp.append(estimated_ntp[i] + estimated_ntp[i + 1]) |
| 89 | + tr.append(estimated_tr[i]) |
| 90 | + i += 2 |
| 91 | + else: |
| 92 | + ntp.append(estimated_ntp[i] + 2) |
| 93 | + tr.append(estimated_tr[i]) |
| 94 | + i += 1 |
| 95 | + |
| 96 | + LGR.info( |
| 97 | + f"The automatic clustering found {len(ntp)} groups of triggers long: {ntp} with respective TR: {tr}" |
| 98 | + ) |
| 99 | + return ntp, tr |
| 100 | + |
| 101 | + |
11 | 102 | def find_takes(phys_in, ntp_list, tr_list, thr=None, padding=9): |
12 | 103 | """ |
13 | 104 | Find takes slicing index. |
|
0 commit comments