Skip to content

Commit

Permalink
Improve extraction handling
Browse files Browse the repository at this point in the history
- fix default parameters for smoothing
- allow the graph to go to -0.05 on y range
- fix smoothing issues
- test file for 30FPS
- add best default settings as comment
  • Loading branch information
Timozen committed Apr 10, 2024
1 parent f338dcd commit cb1aec8
Show file tree
Hide file tree
Showing 6 changed files with 2,069 additions and 6 deletions.
2 changes: 1 addition & 1 deletion frontend/jwidgets/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, background="default", x_lim_max=1000, **kargs):

self.plot_item.setLimits(xMin=0, xMax=x_lim_max)
self.plot_item.setXRange(0, x_lim_max)
self.plot_item.setYRange(0, 1)
self.plot_item.setYRange(-0.05, 1)

self.axis_b: pg.AxisItem = self.plot_item.getAxis("bottom")
self.curves: list[pg.PlotDataItem] = []
Expand Down
42 changes: 38 additions & 4 deletions frontend/ui_eye_blinking_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@
from qtpy.QtWidgets import QMessageBox
from PyQt6.QtCore import pyqtSignal


from jefapato import blinking
from frontend import config, jwidgets

logger = structlog.get_logger()

#### Recommended Extracton Settings for Eye Blinking ####
# @30 FPS
# Minimum Distance: 10 Frames
# Minimum Prominence: 0.1 EAR Score
# Minimum Internal Width: 4 Frames
# Maximum Internal Width: 20 Frames
# Maximum Matching Distance: 15 Frames
# Partial Threshold Left: 0.18 EAR Score
# Partial Threshold Right: 0.18 EAR Score
#
# Smoothing
# - Window Size: 7
# - Polynomial Degree: 3
#
# ---
#
# @240 FPS
# Minimum Distance: 50 Frames
# Minimum Prominence: 0.1 EAR Score
# Minimum Internal Width: 20 Frames
# Maximum Internal Width: 100 Frames
# Maximum Matching Distance: 30 Frames
# Partial Threshold Left: 0.18 EAR Score
# Partial Threshold Right: 0.18 EAR Score
#
# Smoothing
# - Window Size: 7
# - Polynomial Degree: 3


DOWNSAMPLE_FACTOR = 8

def to_float(value: str) -> float:
Expand Down Expand Up @@ -254,12 +283,12 @@ def __init__(self, parent):

le_smooth_size = QtWidgets.QLineEdit()
le_smooth_size.setValidator(int_validator)
self.add_handler("smooth_size", le_smooth_size, mapper=I2S, default=91)
self.add_handler("smooth_size", le_smooth_size, mapper=I2S, default=7)
box_smooth_layout.addRow("Window Size", le_smooth_size)

le_smooth_poly = QtWidgets.QLineEdit()
le_smooth_poly.setValidator(int_validator)
self.add_handler("smooth_poly", le_smooth_poly, mapper=I2S, default=5)
self.add_handler("smooth_poly", le_smooth_poly, mapper=I2S, default=3)
box_smooth_layout.addRow("Polynomial Degree", le_smooth_poly)

self.set_algo.addRow(box_smooth)
Expand All @@ -274,7 +303,7 @@ def __init__(self, parent):
self.cb_as_time.stateChanged.connect(self.compute_graph_axis)

btn_reset_graph = QtWidgets.QPushButton(qta.icon("msc.refresh"), "Reset Graph Y Range")
btn_reset_graph.clicked.connect(lambda: self.graph.setYRange(0, 1))
btn_reset_graph.clicked.connect(lambda: self.graph.setYRange(-0.5, 1))
self.set_visuals.addRow(btn_reset_graph)

btn_reset_view = QtWidgets.QPushButton(qta.icon("msc.refresh"), "View Full Graph")
Expand Down Expand Up @@ -635,6 +664,11 @@ def compute_intervals(self) -> bool:
if self.comp_partial_threshold_l is np.nan or self.comp_partial_threshold_r is np.nan:
QMessageBox.warning(None, "Blinking Extraction Warning", "No partial threshold could be found. Continued with default `complete` label.")
self.progress.setValue(50)

# check if the blinking data frames are empty
if self.blinking_l.empty or self.blinking_r.empty:
QMessageBox.warning(None, "Blinking Extraction Warning", "No blinks found in the data. Please check the data and try again.")
return False

try:
self.blinking_matched = blinking.match(
Expand Down
4 changes: 4 additions & 0 deletions src/jefapato/blinking/peaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def otsu_thresholding(values: np.ndarray) -> float:
for th in th_range:
r = np.nansum([np.mean(cls) * np.var(values, where=cls) for cls in [values>=th, values<th]])
res.append(r)

if len(res) == 0:
# if the result is empty, return np.nan
return np.nan
return th_range[np.argmin(res)]

def peaks(
Expand Down
6 changes: 5 additions & 1 deletion src/jefapato/blinking/smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@


def smooth(time_series: np.ndarray, smooth_size:int=91, smooth_poly:int=4) -> np.ndarray:
return signal.savgol_filter(
sig = signal.savgol_filter(
time_series,
window_length=smooth_size,
polyorder=smooth_poly,
)
# check all values to be 0 or 1
sig[sig < 0] = 0
sig[sig > 1] = 1
return sig
251 changes: 251 additions & 0 deletions tests/files/blinking/blinking_30FPS.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
frame,EAR2D6_l,EAR2D6_r,EAR2D6_valid,BS_Valid
0, 0.30839515, 0.31717056,True,True
1, 0.30697849, 0.31643425,True,True
2, 0.31780323, 0.32533049,True,True
3, 0.31657635, 0.31336477,True,True
4, 0.31764210, 0.31694362,True,True
5, 0.31386926, 0.31409277,True,True
6, 0.31484655, 0.31612779,True,True
7, 0.30915708, 0.31312242,True,True
8, 0.30693714, 0.31124881,True,True
9, 0.30799023, 0.31312242,True,True
10, 0.30571419, 0.30799125,True,True
11, 0.30042486, 0.30502849,True,True
12, 0.28607607, 0.30721214,True,True
13, 0.30121833, 0.30183604,True,True
14, 0.29187137, 0.30210629,True,True
15, 0.29767902, 0.29821654,True,True
16, 0.29061937, 0.30378166,True,True
17, 0.29875644, 0.29564100,True,True
18, 0.29875644, 0.30551011,True,True
19, 0.30571419, 0.29564100,True,True
20, 0.29262141, 0.29620622,True,True
21, 0.29262141, 0.29330723,True,True
22, 0.27219442, 0.27695657,True,True
23, 0.01769842, 0.01282979,True,True
24, 0.01231288, 0.02070684,True,True
25, 0.02101940, 0.01370297,True,True
26, 0.04426898, 0.02246624,True,True
27, 0.03302003, 0.00803064,True,True
28, 0.07497124, 0.02872804,True,True
29, 0.19807905, 0.19991081,True,True
30, 0.23124061, 0.24272208,True,True
31, 0.25739383, 0.26800041,True,True
32, 0.26600419, 0.27675620,True,True
33, 0.26620216, 0.28076742,True,True
34, 0.27219442, 0.28631641,True,True
35, 0.27817795, 0.28247069,True,True
36, 0.27817795, 0.28490235,True,True
37, 0.27819434, 0.28490235,True,True
38, 0.28417787, 0.29053216,True,True
39, 0.28076650, 0.29130211,True,True
40, 0.29126256, 0.29442629,True,True
41, 0.29036380, 0.29442629,True,True
42, 0.28054342, 0.28490235,True,True
43, 0.28688787, 0.29672592,True,True
44, 0.28076650, 0.29109611,True,True
45, 0.28670108, 0.28816167,True,True
46, 0.28076650, 0.28816167,True,True
47, 0.28417787, 0.29109611,True,True
48, 0.28784117, 0.29340709,True,True
49, 0.28790582, 0.29821654,True,True
50, 0.28435252, 0.29672592,True,True
51, 0.28076650, 0.29055364,True,True
52, 0.06502978, 0.03658265,True,True
53, 0.02024917, 0.02625834,True,True
54, 0.02000166, 0.01876873,True,True
55, 0.00874105, 0.02432521,True,True
56, 0.01492190, 0.02768382,True,True
57, 0.00863546, 0.02092892,True,True
58, 0.01492190, 0.03351722,True,True
59, 0.01236170, 0.01641662,True,True
60, 0.01233816, 0.01856745,True,True
61, 0.01831858, 0.00802237,True,True
62, 0.04230399, 0.00000000,True,True
63, 0.06181197, 0.02005528,True,True
64, 0.20705129, 0.22071539,True,True
65, 0.24511024, 0.24825420,True,True
66, 0.26281608, 0.27651996,True,True
67, 0.27138363, 0.28679968,True,True
68, 0.27138363, 0.29316770,True,True
69, 0.29029085, 0.30401200,True,True
70, 0.29152860, 0.30015060,True,True
71, 0.29287058, 0.30957440,True,True
72, 0.27435362, 0.29465200,True,True
73, 0.27774988, 0.29391477,True,True
74, 0.29281205, 0.29593631,True,True
75, 0.28205807, 0.30210629,True,True
76, 0.28360660, 0.29316770,True,True
77, 0.28491334, 0.30566975,True,True
78, 0.29736310, 0.29793630,True,True
79, 0.28449054, 0.30401200,True,True
80, 0.28346620, 0.30158417,True,True
81, 0.28915019, 0.29647832,True,True
82, 0.28346620, 0.28974773,True,True
83, 0.29029085, 0.29873282,True,True
84, 0.29520791, 0.29873282,True,True
85, 0.29047551, 0.30129435,True,True
86, 0.28481250, 0.29235676,True,True
87, 0.29038721, 0.30075286,True,True
88, 0.29399576, 0.29478969,True,True
89, 0.28474855, 0.29793630,True,True
90, 0.29544620, 0.29760003,True,True
91, 0.29047551, 0.29761966,True,True
92, 0.29734401, 0.30129435,True,True
93, 0.30882459, 0.30856107,True,True
94, 0.30317851, 0.30566975,True,True
95, 0.29734401, 0.30210629,True,True
96, 0.29985247, 0.30129435,True,True
97, 0.29991155, 0.30691690,True,True
98, 0.29411627, 0.30639529,True,True
99, 0.29736310, 0.30210629,True,True
100, 0.30554130, 0.32070147,True,True
101, 0.30754054, 0.31882648,True,True
102, 0.31910433, 0.32414189,True,True
103, 0.28094941, 0.27651996,True,True
104, 0.01744879, 0.02406710,True,True
105, 0.02247274, 0.03177595,True,True
106, 0.00860217, 0.03313757,True,True
107, 0.03164379, 0.01356214,True,True
108, 0.06768018, 0.01817901,True,True
109, 0.20271529, 0.22393133,True,True
110, 0.24316228, 0.25774618,True,True
111, 0.26001681, 0.28631641,True,True
112, 0.28056569, 0.28220608,True,True
113, 0.28056569, 0.29362676,True,True
114, 0.27485479, 0.29931613,True,True
115, 0.28094941, 0.29305686,True,True
116, 0.26910210, 0.28466462,True,True
117, 0.27036253, 0.28789544,True,True
118, 0.27151763, 0.27901948,True,True
119, 0.27870788, 0.29593631,True,True
120, 0.28456170, 0.29593631,True,True
121, 0.28913736, 0.29388577,True,True
122, 0.28790582, 0.30129435,True,True
123, 0.29387780, 0.30158417,True,True
124, 0.29047551, 0.30639529,True,True
125, 0.29152860, 0.30639529,True,True
126, 0.29387249, 0.30034817,True,True
127, 0.29174260, 0.30349620,True,True
128, 0.29174260, 0.30202917,True,True
129, 0.29274450, 0.30152513,True,True
130, 0.29876218, 0.30924539,True,True
131, 0.28607607, 0.30349620,True,True
132, 0.28933412, 0.30017115,True,True
133, 0.29876218, 0.31084737,True,True
134, 0.28156328, 0.30535200,True,True
135, 0.29876218, 0.31084737,True,True
136, 0.30084331, 0.30034817,True,True
137, 0.28824583, 0.30265839,True,True
138, 0.28947251, 0.29738223,True,True
139, 0.28476367, 0.29465200,True,True
140, 0.29399576, 0.30616180,True,True
141, 0.28122135, 0.29793630,True,True
142, 0.08696232, 0.07316529,True,True
143, 0.00872440, 0.02460670,True,True
144, 0.01941004, 0.02893588,True,True
145, 0.02526354, 0.03793540,True,True
146, 0.02328699, 0.04282244,True,True
147, 0.01221239, 0.01796818,True,True
148, 0.03546387, 0.00549417,True,True
149, 0.11005832, 0.05299722,True,True
150, 0.22507869, 0.22884677,True,True
151, 0.24808096, 0.25126705,True,True
152, 0.26451688, 0.28204309,True,True
153, 0.26857695, 0.28416769,True,True
154, 0.27301256, 0.28416769,True,True
155, 0.27997872, 0.28442911,True,True
156, 0.28101880, 0.28939994,True,True
157, 0.27989908, 0.28838177,True,True
158, 0.27870788, 0.29340709,True,True
159, 0.29038721, 0.29316770,True,True
160, 0.27761239, 0.28227340,True,True
161, 0.26704718, 0.28760540,True,True
162, 0.26704718, 0.28281947,True,True
163, 0.26590579, 0.28760540,True,True
164, 0.28326359, 0.28204309,True,True
165, 0.27880359, 0.27729200,True,True
166, 0.27286259, 0.28416769,True,True
167, 0.26590579, 0.28416769,True,True
168, 0.28248591, 0.28733513,True,True
169, 0.27448633, 0.27867187,True,True
170, 0.27566412, 0.28389907,True,True
171, 0.27556948, 0.28442911,True,True
172, 0.27870788, 0.28389907,True,True
173, 0.27884592, 0.28416769,True,True
174, 0.26715126, 0.28913396,True,True
175, 0.27424381, 0.29465200,True,True
176, 0.26278444, 0.28389907,True,True
177, 0.27884592, 0.28122140,True,True
178, 0.26965958, 0.28706944,True,True
179, 0.27884592, 0.29031120,True,True
180, 0.27556948, 0.28706944,True,True
181, 0.28467151, 0.28132200,True,True
182, 0.27435362, 0.28939994,True,True
183, 0.25727224, 0.26536923,True,True
184, 0.02088370, 0.01338259,True,True
185, 0.01360123, 0.01368535,True,True
186, 0.01218968, 0.02069220,True,True
187, 0.01489348, 0.02813706,True,True
188, 0.01208685, 0.01339907,True,True
189, 0.06587889, 0.02260906,True,True
190, 0.07325627, 0.03493840,True,True
191, 0.21407690, 0.22462453,True,True
192, 0.24230951, 0.24911082,True,True
193, 0.26417941, 0.27670971,True,True
194, 0.26573676, 0.27592677,True,True
195, 0.27774988, 0.29235676,True,True
196, 0.27448633, 0.28363814,True,True
197, 0.26603749, 0.28363814,True,True
198, 0.27894167, 0.28387229,True,True
199, 0.27190170, 0.28493478,True,True
200, 0.27448633, 0.28733513,True,True
201, 0.27774988, 0.28883339,True,True
202, 0.28933412, 0.29235676,True,True
203, 0.28607607, 0.28915618,True,True
204, 0.28593776, 0.29492306,True,True
205, 0.28028817, 0.28966629,True,True
206, 0.27566412, 0.28957076,True,True
207, 0.27661347, 0.29143903,True,True
208, 0.26603749, 0.28915618,True,True
209, 0.27904043, 0.28913396,True,True
210, 0.28499447, 0.29465200,True,True
211, 0.27566412, 0.29813491,True,True
212, 0.27674727, 0.29465200,True,True
213, 0.27566412, 0.28363814,True,True
214, 0.28607607, 0.29465200,True,True
215, 0.27129853, 0.29194358,True,True
216, 0.28810647, 0.29543364,True,True
217, 0.28476367, 0.28671125,True,True
218, 0.28132988, 0.29170131,True,True
219, 0.28217403, 0.29220632,True,True
220, 0.28364877, 0.30042672,True,True
221, 0.28476367, 0.30304016,True,True
222, 0.28378281, 0.29687768,True,True
223, 0.28396498, 0.30566975,True,True
224, 0.28387867, 0.30017115,True,True
225, 0.29422187, 0.30233665,True,True
226, 0.28378281, 0.30233665,True,True
227, 0.28607607, 0.30616180,True,True
228, 0.07306278, 0.04001991,True,True
229, 0.01190308, 0.02002629,True,True
230, 0.01767767, 0.01979643,True,True
231, 0.02384842, 0.01097841,True,True
232, 0.03500224, 0.01201562,True,True
233, 0.14034599, 0.10153823,True,True
234, 0.20675459, 0.22546911,True,True
235, 0.24530257, 0.25126705,True,True
236, 0.24239267, 0.26500531,True,True
237, 0.26391243, 0.27287946,True,True
238, 0.25075540, 0.28025274,True,True
239, 0.27674727, 0.28389907,True,True
240, 0.26921031, 0.29143903,True,True
241, 0.27909436, 0.29714485,True,True
242, 0.27323818, 0.28154177,True,True
243, 0.27372611, 0.28974508,True,True
244, 0.26667730, 0.28667322,True,True
245, 0.27129853, 0.28688769,True,True
246, 0.26921031, 0.28736231,True,True
247, 0.27690214, 0.28432221,True,True
248, 0.27806570, 0.28974508,True,True
249, 0.26818388, 0.28227222,True,True
Loading

0 comments on commit cb1aec8

Please sign in to comment.