@@ -75,26 +75,45 @@ def get_local_max_and_min_idx(score :list, fps: int, shift_min :int = 0, shift_m
7575 dict: dict with 2 lists with all local max and min indexes ({'min':[], 'max':[]})
7676 """
7777 avg = moving_average (score , w = round (fps * HYPERPARAMETER ['avg_sec_for_local_min_max_extraction' ]))
78- result = {'min' : [], 'max' : []}
78+ changepoints = {'min' : [], 'max' : []}
7979 tmp_min_idx , tmp_max_idx = - 1 , - 1
8080 for pos in range (len (score )):
8181 if score [pos ] < avg [pos ]:
8282 if tmp_min_idx < 0 : tmp_min_idx = pos
8383 elif score [tmp_min_idx ] >= score [pos ]: tmp_min_idx = pos
8484 elif tmp_min_idx >= 0 :
85- if tmp_min_idx >= - 1 * shift_min and tmp_min_idx + shift_min < len (score ):
86- result ['min' ].append (tmp_min_idx + shift_min )
87- else :
88- result ['min' ].append (tmp_min_idx )
85+ changepoints ['min' ].append (tmp_min_idx )
8986 tmp_min_idx = - 1
9087
9188 if score [pos ] > avg [pos ]:
9289 if tmp_max_idx < 0 : tmp_max_idx = pos
9390 elif score [tmp_max_idx ] <= score [pos ]: tmp_max_idx = pos
9491 elif tmp_max_idx >= 0 :
95- if tmp_max_idx >= - 1 * shift_max and tmp_max_idx + shift_max < len (score ):
96- result ['max' ].append (tmp_max_idx + shift_max )
97- else :
98- result ['max' ].append (tmp_max_idx )
92+ changepoints ['max' ].append (tmp_max_idx )
9993 tmp_max_idx = - 1
100- return result
94+
95+ delta = (max (score ) - min (score )) * 0.01 * min ((10.0 , float (HYPERPARAMETER ['local_max_min_delta_in_percent' ]) ))
96+
97+ # shift points to the real change points
98+ for k , idx in enumerate (changepoints ['min' ]):
99+ new_pos = idx
100+ while new_pos + 1 < len (score ) and score [idx ] + delta > score [new_pos + 1 ]:
101+ new_pos += 1
102+ changepoints ['min' ][k ] = new_pos
103+
104+ for k , idx in enumerate (changepoints ['max' ]):
105+ new_pos = idx
106+ while new_pos + 1 < len (score ) and score [idx ] - delta < score [new_pos + 1 ]:
107+ new_pos += 1
108+ changepoints ['max' ][k ] = new_pos
109+
110+ # apply shift
111+ if shift_min != 0 :
112+ for k , idx in enumerate (changepoints ['min' ]):
113+ changepoints ['min' ][k ] = max ((0 , min ((len (score )- 1 , idx + shift_min )) ))
114+
115+ if shift_max != 0 :
116+ for k , idx in enumerate (changepoints ['max' ]):
117+ changepoints ['max' ][k ] = max ((0 , min ((len (score )- 1 , idx + shift_max )) ))
118+
119+ return changepoints
0 commit comments