The goal is to find a generic method to solve the following task:
I have two python lists of the same length filled with zeros and ones:
detection = [0,0,1,0] # only examples, they can be of any length
ground_truth = [0,1,0,0] # and the ones can be at any indizes
and a integer number
offset = 1 # this number is also variable
The goal is to combine #offset elements in detection around elements equal to 1
and then combine the same index elements of ground_truth logical or, resulting the new lists:
detection = [0,1]
ground_truth = [0,1]
graphical explanation:
Background Info: The detection / ground truth values belong to a binary classification of a time series and The idea is to have a flexible evaluation that results in a TP if the detection fits the ground_truth is within a certain range of time steps (=offset).
Additional Example:
offset = 1
detection = [1,0,0,0,1,1,0]
ground_truth = [0,0,0,1,0,0,0]
would result to:
detection = [1,0,1]
ground_truth = [0,0,1]
解决方案
I found the ultimate solution.
sub questions that solved it:
Code:
# Create Mask from Detection and Offset
w = offset*2 +1
mask = np.convolve(detection, np.ones(w), mode='same').clip(0,1).astype(int)
# Create Soft Detection
soft_detection = mask[~((np.diff(mask,prepend=False)==0) & mask==1)].tolist()
# Create Soft Ground Truth
idx = np.flatnonzero(np.r_[True,np.diff(mask)!=0])
soft_ground_truth = np.bitwise_or.reduceat(ground_truth, idx).tolist()