added whitspace to make prettier

This commit is contained in:
weygoldt 2023-01-18 11:24:44 +01:00
parent 741349514a
commit 06c1878947

View File

@ -1,5 +1,5 @@
import numpy as np import numpy as np
from typing import List, Union from typing import List
def purge_duplicates(timestamps: List[float], threshold: float = 0.5) -> List[float]: def purge_duplicates(timestamps: List[float], threshold: float = 0.5) -> List[float]:
@ -22,9 +22,12 @@ def purge_duplicates(timestamps: List[float], threshold: float = 0.5) -> List[fl
""" """
# Initialize an empty list to store the groups of timestamps that are closer to the previous or consecutive timestamp than the threshold # Initialize an empty list to store the groups of timestamps that are closer to the previous or consecutive timestamp than the threshold
groups = [] groups = []
# initialize the first group with the first timestamp # initialize the first group with the first timestamp
group = [timestamps[0]] group = [timestamps[0]]
for i in range(1, len(timestamps)): for i in range(1, len(timestamps)):
# check the difference between current timestamp and previous timestamp is less than the threshold # check the difference between current timestamp and previous timestamp is less than the threshold
if timestamps[i] - timestamps[i-1] < threshold: if timestamps[i] - timestamps[i-1] < threshold:
# add the current timestamp to the current group # add the current timestamp to the current group
@ -33,15 +36,19 @@ def purge_duplicates(timestamps: List[float], threshold: float = 0.5) -> List[fl
# if the difference is greater than the threshold # if the difference is greater than the threshold
# append the current group to the groups list # append the current group to the groups list
groups.append(group) groups.append(group)
# start a new group with the current timestamp # start a new group with the current timestamp
group = [timestamps[i]] group = [timestamps[i]]
# after iterating through all the timestamps, add the last group to the groups list # after iterating through all the timestamps, add the last group to the groups list
groups.append(group) groups.append(group)
# get the mean of each group and only include the ones that have more than 1 timestamp # get the mean of each group and only include the ones that have more than 1 timestamp
means = [np.mean(group) for group in groups if len(group) > 1] means = [np.mean(group) for group in groups if len(group) > 1]
# get the timestamps that are outliers, i.e. the ones that are alone in a group # get the timestamps that are outliers, i.e. the ones that are alone in a group
outliers = [ts for group in groups for ts in group if len(group) == 1] outliers = [ts for group in groups for ts in group if len(group) == 1]
# return the outliers and means in a single list # return the outliers and means in a single list
return outliers + means return outliers + means
@ -67,20 +74,16 @@ def group_timestamps(sublists: List[List[float]], n: int, threshold: float) -> L
a list of the mean of each group. a list of the mean of each group.
""" """
# Flatten the sublists and sort the timestamps
timestamps = [ timestamps = [
timestamp for sublist in sublists if sublist for timestamp in sublist] timestamp for sublist in sublists if sublist for timestamp in sublist]
timestamps.sort() timestamps.sort()
groups = [] groups = []
# Create a variable to store the current group of timestamps current_group = [timestamps[0]]
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])
# convert the set to a list
common_timestamps = list(common_timestamps)
# Iterate through the timestamps
for i in range(1, len(timestamps)):
# Group timestamps that are less than threshold milliseconds apart
for i in range(1, len(timestamps)):
if timestamps[i] - timestamps[i-1] < threshold: if timestamps[i] - timestamps[i-1] < threshold:
current_group.append(timestamps[i]) current_group.append(timestamps[i])
else: else:
@ -89,11 +92,13 @@ def group_timestamps(sublists: List[List[float]], n: int, threshold: float) -> L
groups.append(current_group) groups.append(current_group)
# Retain only groups that contain at least n timestamps
final_groups = [] final_groups = []
for group in groups: for group in groups:
if len(group) >= n: if len(group) >= n:
final_groups.append(group) final_groups.append(group)
# Calculate the mean of each group
means = [np.mean(group) for group in final_groups] means = [np.mean(group) for group in final_groups]
return means return means