Merge branch 'peakdetection' of https://whale.am28.uni-tuebingen.de/git/raab/GP2023_chirp_detection into peakdetection
This commit is contained in:
commit
7034e9421b
@ -12,6 +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
|
||||
|
||||
ps = PlotStyle()
|
||||
|
||||
@ -317,7 +318,7 @@ def main(datapath: str) -> None:
|
||||
search_freq = config.default_search_freq
|
||||
|
||||
print(f"Search frequency: {search_freq}")
|
||||
#----------- chrips on the two best electrodes-----------
|
||||
# ----------- chrips on the two best electrodes-----------
|
||||
chirps_electrodes = []
|
||||
electrodes_of_chirps = []
|
||||
|
||||
@ -541,7 +542,6 @@ def main(datapath: str) -> None:
|
||||
timestamps)]
|
||||
timestamps = timestamps[np.argsort(timestamps)]
|
||||
|
||||
|
||||
# # get chirps
|
||||
# diff = np.empty(timestamps.shape)
|
||||
# diff[0] = np.inf # always retain the 1st element
|
||||
@ -549,7 +549,6 @@ def main(datapath: str) -> None:
|
||||
# mask = diff < config.chirp_window_threshold
|
||||
# shared_peak_indices = timestamp_idx[mask]
|
||||
|
||||
|
||||
current_chirps = []
|
||||
bool_timestamps = np.ones_like(timestamps, dtype=bool)
|
||||
for bo, tt in enumerate(timestamps):
|
||||
@ -562,11 +561,9 @@ def main(datapath: str) -> None:
|
||||
electrodes_of_chirps.append(el)
|
||||
bool_timestamps[cm] = False
|
||||
|
||||
|
||||
# for checking if there are chirps on multiple electrodes
|
||||
chirps_electrodes.append(current_chirps)
|
||||
|
||||
|
||||
for ct in current_chirps:
|
||||
axs[0, el].axvline(ct, color='r', lw=1)
|
||||
|
||||
@ -593,8 +590,10 @@ def main(datapath: str) -> None:
|
||||
chirps_electrodes = np.asarray(chirps_electrodes)
|
||||
electrodes_of_chirps = np.asarray(electrodes_of_chirps)
|
||||
# sort them
|
||||
sort_chirps_electrodes = chirps_electrodes[np.argsort(chirps_electrodes)]
|
||||
sort_electrodes = electrodes_of_chirps[np.argsort(chirps_electrodes)]
|
||||
sort_chirps_electrodes = chirps_electrodes[np.argsort(
|
||||
chirps_electrodes)]
|
||||
sort_electrodes = electrodes_of_chirps[np.argsort(
|
||||
chirps_electrodes)]
|
||||
bool_vector = np.ones(len(sort_chirps_electrodes), dtype=bool)
|
||||
# make index vector
|
||||
index_vector = np.arange(len(sort_chirps_electrodes))
|
||||
@ -629,10 +628,6 @@ def main(datapath: str) -> None:
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
datapath = "../data/2022-06-02-10_00/"
|
||||
main(datapath)
|
||||
|
106
code/modules/timestamps.py
Normal file
106
code/modules/timestamps.py
Normal file
@ -0,0 +1,106 @@
|
||||
import numpy as np
|
||||
from typing import List, Union
|
||||
|
||||
|
||||
def group_timestamps(timestamps: List[Union[int, float]], time_threshold: float = 0.05) -> List[float]:
|
||||
"""
|
||||
Group timestamps that are less than a certain time threshold apart.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
timestamps : list of float or int
|
||||
List of timestamps to group
|
||||
time_threshold : float, optional
|
||||
The threshold for time difference between two consecutive timestamps in milliseconds. Default is 0.05 milliseconds.
|
||||
|
||||
Returns
|
||||
-------
|
||||
list of float
|
||||
List of mean of each group of timestamps
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> timestamps = [1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65]
|
||||
>>> group_timestamps(timestamps)
|
||||
[1.275, 1.425, 1.575]
|
||||
"""
|
||||
# Create an empty list to store the groups of timestamps
|
||||
groups = []
|
||||
# Create a variable to store the current group of timestamps
|
||||
current_group = []
|
||||
# Iterate through the timestamps
|
||||
for i in range(len(timestamps)):
|
||||
# If the current timestamp is less than 50 milliseconds away from the previous timestamp
|
||||
if i > 0 and timestamps[i] - timestamps[i-1] < time_threshold:
|
||||
# Add the current timestamp to the current group
|
||||
current_group.append(timestamps[i])
|
||||
else:
|
||||
# If the current timestamp is not part of the current group
|
||||
if current_group:
|
||||
# Add the current group to the list of groups
|
||||
groups.append(current_group)
|
||||
# Reset the current group
|
||||
current_group = []
|
||||
# Add the current timestamp to a new group
|
||||
current_group.append(timestamps[i])
|
||||
# If there is a group left after the loop
|
||||
if current_group:
|
||||
# Add the current group to the list of groups
|
||||
groups.append(current_group)
|
||||
# Compute the mean of each group and return it
|
||||
return [np.mean(group) for group in groups]
|
||||
|
||||
|
||||
def group_timestamps_v2(sublists: List[List[Union[int, float]]], n: int, time_threshold: float = 0.05) -> List[float]:
|
||||
"""
|
||||
Group timestamps that are less than a certain time threshold apart and occur in at least n sublists.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
sublists : list of list of float or int
|
||||
List of sublists containing timestamps
|
||||
n : int
|
||||
Minimum number of sublists in which a timestamp should occur to be considered
|
||||
time_threshold : float, optional
|
||||
The threshold for time difference between two consecutive timestamps in milliseconds. Default is 0
|
||||
|
||||
Returns
|
||||
-------
|
||||
list of float
|
||||
List of mean of each group of timestamps
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> sublists = [[1.2, 1.25, 1.3, 1.35, 1.4], [1.3, 1.35, 1.4, 1.45, 1.5], [1.4, 1.45, 1.5, 1.55, 1.6]]
|
||||
>>> group_timestamps_v2(sublists, 2)
|
||||
[1.325, 1.45]
|
||||
"""
|
||||
|
||||
# Create an empty list to store the groups of timestamps
|
||||
groups = []
|
||||
# Create a variable to store the current group of timestamps
|
||||
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])
|
||||
# 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
|
||||
if i > 0 and common_timestamps[i] - common_timestamps[i-1] < time_threshold:
|
||||
# Add the current timestamp to the current group
|
||||
current_group.append(common_timestamps[i])
|
||||
else:
|
||||
# If the current timestamp is not part of the current group
|
||||
if current_group:
|
||||
# Add the current group to the list of groups
|
||||
groups.append(current_group)
|
||||
# Reset the current group
|
||||
current_group = []
|
||||
# Add the current timestamp to a new group
|
||||
current_group.append(common_timestamps[i])
|
||||
# If there is a group left after the loop
|
||||
if current_group:
|
||||
# Add the current group to the list of groups
|
||||
groups.append(current_group)
|
||||
# Compute the mean of each group and return it
|
||||
return [np.mean(group) for group in groups]
|
||||
|
Loading…
Reference in New Issue
Block a user