From 00d6fed1612fbbda80801a1b197cfbf788299f3c Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Wed, 18 Jan 2023 09:16:34 +0100 Subject: [PATCH 1/4] adding with ittertools the combinations --- code/chirpdetection.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/code/chirpdetection.py b/code/chirpdetection.py index 194bbdf..a5a2ff4 100644 --- a/code/chirpdetection.py +++ b/code/chirpdetection.py @@ -1,4 +1,4 @@ -import os +import itertools import numpy as np from IPython import embed @@ -598,25 +598,31 @@ def main(datapath: str) -> None: bool_vector = np.ones(len(sort_chirps_electrodes), dtype=bool) # make index vector index_vector = np.arange(len(sort_chirps_electrodes)) + # make it more than only two electrodes for the search after chirps + combinations_best_elctrodes = list(itertools.combinations(range(3), 2)) the_real_chirps = [] for chirp_index, seoc in enumerate(sort_chirps_electrodes): if bool_vector[chirp_index] == False: continue - else: - cm = index_vector[(sort_chirps_electrodes >= seoc) & ( - sort_chirps_electrodes <= seoc + config.chirp_window_threshold)] - - if set([0,1]).issubset(sort_electrodes[cm]): - the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - elif set([1,0]).issubset(sort_electrodes[cm]): - the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - elif set([0,2]).issubset(sort_electrodes[cm]): - the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - elif set([1,2]).issubset(sort_electrodes[cm]): + cm = index_vector[(sort_chirps_electrodes >= seoc) & ( + sort_chirps_electrodes <= seoc + config.chirp_window_threshold)] + + for combination in combinations_best_elctrodes: + if set(combination).issubset(sort_electrodes[cm]): the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - - bool_vector[cm] = False + """ + if set([0,1]).issubset(sort_electrodes[cm]): + the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) + elif set([1,0]).issubset(sort_electrodes[cm]): + the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) + elif set([0,2]).issubset(sort_electrodes[cm]): + the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) + elif set([1,2]).issubset(sort_electrodes[cm]): + the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) + """ + + bool_vector[cm] = False for ct in the_real_chirps: axs[0, el].axvline(ct, color='b', lw=1) embed() From 844f65e24e886c3428d2cc0ecdb33c93fa003335 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Wed, 18 Jan 2023 09:38:23 +0100 Subject: [PATCH 2/4] final peak version with bool --- code/chirpdetection.py | 19 +++++++++++-------- code/modules/timestamps.py | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/code/chirpdetection.py b/code/chirpdetection.py index 1391f7c..26a9b52 100644 --- a/code/chirpdetection.py +++ b/code/chirpdetection.py @@ -12,7 +12,7 @@ from sklearn.preprocessing import normalize from modules.filters import bandpass_filter, envelope, highpass_filter from modules.filehandling import ConfLoader, LoadData from modules.plotstyle import PlotStyle -from modules.timestamps import group_timestamps, group_timestamp_v2 +from modules.timestamps import group_timestamps, group_timestamps_v2 ps = PlotStyle() @@ -527,6 +527,7 @@ def main(datapath: str) -> None: if len(baseline_ts) == 0 or len(search_ts) == 0 or len(freq_ts) == 0: continue + # get index for each feature baseline_idx = np.zeros_like(baseline_ts) search_idx = np.ones_like(search_ts) @@ -562,6 +563,7 @@ def main(datapath: str) -> None: bool_timestamps[cm] = False # for checking if there are chirps on multiple electrodes + chirps_electrodes.append(current_chirps) for ct in current_chirps: @@ -597,20 +599,22 @@ def main(datapath: str) -> None: bool_vector = np.ones(len(sort_chirps_electrodes), dtype=bool) # make index vector index_vector = np.arange(len(sort_chirps_electrodes)) - # make it more than only two electrodes for the search after chirps - combinations_best_elctrodes = list(itertools.combinations(range(3), 2)) + # make it more than only two electrodes for the search after chirps + combinations_best_elctrodes = list( + itertools.combinations(range(3), 2)) the_real_chirps = [] for chirp_index, seoc in enumerate(sort_chirps_electrodes): if bool_vector[chirp_index] == False: continue cm = index_vector[(sort_chirps_electrodes >= seoc) & ( - sort_chirps_electrodes <= seoc + config.chirp_window_threshold)] - + sort_chirps_electrodes <= seoc + config.chirp_window_threshold)] + for combination in combinations_best_elctrodes: if set(combination).issubset(sort_electrodes[cm]): - the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - """ + the_real_chirps.append( + np.mean(sort_chirps_electrodes[cm])) + if set([0,1]).issubset(sort_electrodes[cm]): the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) elif set([1,0]).issubset(sort_electrodes[cm]): @@ -619,7 +623,6 @@ def main(datapath: str) -> None: the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) elif set([1,2]).issubset(sort_electrodes[cm]): the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - """ bool_vector[cm] = False for ct in the_real_chirps: diff --git a/code/modules/timestamps.py b/code/modules/timestamps.py index a342271..1f3aa8d 100644 --- a/code/modules/timestamps.py +++ b/code/modules/timestamps.py @@ -82,6 +82,7 @@ def group_timestamps_v2(sublists: List[List[Union[int, float]]], n: int, time_th current_group = [] # Create a set to store the timestamps that occur in at least n of the sublists common_timestamps = set.intersection(*[set(lst) for lst in sublists]) + embed() # Iterate through the timestamps for i in range(len(common_timestamps)): # If the current timestamp is less than 50 milliseconds away from the previous timestamp From ad888734dd273f9ee23f5411e937eddd123b1bf3 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Wed, 18 Jan 2023 10:25:48 +0100 Subject: [PATCH 3/4] saved chirps --- code/chirpdetection.py | 22 ++++++++++++++++------ code/modules/timestamps.py | 3 ++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/code/chirpdetection.py b/code/chirpdetection.py index 26a9b52..9e71f5e 100644 --- a/code/chirpdetection.py +++ b/code/chirpdetection.py @@ -527,7 +527,10 @@ def main(datapath: str) -> None: if len(baseline_ts) == 0 or len(search_ts) == 0 or len(freq_ts) == 0: continue + #current_chirps = group_timestamps_v2( + # [list(baseline_ts), list(search_ts), list(freq_ts)], 3) + # get index for each feature baseline_idx = np.zeros_like(baseline_ts) search_idx = np.ones_like(search_ts) @@ -561,7 +564,7 @@ def main(datapath: str) -> None: current_chirps.append(np.mean(timestamps[cm])) electrodes_of_chirps.append(el) bool_timestamps[cm] = False - + # for checking if there are chirps on multiple electrodes chirps_electrodes.append(current_chirps) @@ -610,11 +613,16 @@ def main(datapath: str) -> None: cm = index_vector[(sort_chirps_electrodes >= seoc) & ( sort_chirps_electrodes <= seoc + config.chirp_window_threshold)] + + chirps_unique = [] for combination in combinations_best_elctrodes: if set(combination).issubset(sort_electrodes[cm]): - the_real_chirps.append( - np.mean(sort_chirps_electrodes[cm])) + chirps_unique.append(np.mean(sort_chirps_electrodes[cm])) + + the_real_chirps.append(np.mean(chirps_unique)) + + """ if set([0,1]).issubset(sort_electrodes[cm]): the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) elif set([1,0]).issubset(sort_electrodes[cm]): @@ -623,12 +631,14 @@ def main(datapath: str) -> None: the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) elif set([1,2]).issubset(sort_electrodes[cm]): the_real_chirps.append(np.mean(sort_chirps_electrodes[cm])) - + """ bool_vector[cm] = False + chirps.append(the_real_chirps) for ct in the_real_chirps: axs[0, el].axvline(ct, color='b', lw=1) - embed() - plt.show() + embed() + + if __name__ == "__main__": diff --git a/code/modules/timestamps.py b/code/modules/timestamps.py index 1f3aa8d..9ad6add 100644 --- a/code/modules/timestamps.py +++ b/code/modules/timestamps.py @@ -82,7 +82,8 @@ def group_timestamps_v2(sublists: List[List[Union[int, float]]], n: int, time_th current_group = [] # Create a set to store the timestamps that occur in at least n of the sublists common_timestamps = set.intersection(*[set(lst) for lst in sublists]) - embed() + # convert the set to a list + common_timestamps = list(common_timestamps) # Iterate through the timestamps for i in range(len(common_timestamps)): # If the current timestamp is less than 50 milliseconds away from the previous timestamp From ef61cec6958a71f2b0a513fc073e1c9427a0171b Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Wed, 18 Jan 2023 11:04:58 +0100 Subject: [PATCH 4/4] adding plot with multiple windows --- code/chirpdetection.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/code/chirpdetection.py b/code/chirpdetection.py index 9e71f5e..a45d81d 100644 --- a/code/chirpdetection.py +++ b/code/chirpdetection.py @@ -517,6 +517,7 @@ def main(datapath: str) -> None: axs[6, el].set_title( "Filtered absolute instantaneous frequency") + # DETECT CHIRPS IN SEARCH WINDOW ------------------------------- baseline_ts = time_oi[baseline_peaks] @@ -569,6 +570,7 @@ def main(datapath: str) -> None: chirps_electrodes.append(current_chirps) + for ct in current_chirps: axs[0, el].axvline(ct, color='r', lw=1) @@ -588,6 +590,7 @@ def main(datapath: str) -> None: np.ones_like((time_oi)[baseline_peaks]) * 600, c=ps.red, ) + # make one array chirps_electrodes = np.concatenate(chirps_electrodes) @@ -634,9 +637,17 @@ def main(datapath: str) -> None: """ bool_vector[cm] = False chirps.append(the_real_chirps) + for ct in the_real_chirps: axs[0, el].axvline(ct, color='b', lw=1) + embed() + fig, ax = plt.subplots() + t0 = (3 * 60 * 60 + 6 * 60 + 43.5) + data_oi = data.raw[window_starts[0]:window_starts[-1]+ int(dt*data.raw_rate), 10] + plot_spectrogram(ax, data_oi, data.raw_rate, t0) + for ch in chirps: + ax. axvline(ch, color='b', lw=1)