Skip to content

Commit 8e14942

Browse files
committed
updating requested fixes
1 parent ea1379b commit 8e14942

2 files changed

Lines changed: 57 additions & 77 deletions

File tree

src/graphomotor/core/models.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def calculate_velocity_metrics(self) -> None:
296296
297297
Args:
298298
self: LineSegment object to calculate velocities for.
299-
ink_points: DataFrame containing the ink points for the line segment.
300299
"""
301300
dx = np.diff(self.ink_points["x"].values)
302301
dy = np.diff(self.ink_points["y"].values)
@@ -326,8 +325,11 @@ def detect_hesitations(self, threshold_percentile: int = 20) -> None:
326325
duration of hesitations based on the number of points that fall below the
327326
threshold and the time interval between points.
328327
328+
hesitation_count defaults to 0 and hesitation_duration defaults to 0.0 in the
329+
LineSegment object if there are less than 3 velocity points. This function also
330+
assumes uniform sampling.
331+
329332
Args:
330-
ink_points: DataFrame containing the ink points for the line segment.
331333
threshold_percentile: Percentile to determine the velocity threshold for
332334
hesitations (default is 20, meaning the bottom 20% of velocities are
333335
considered hesitations).
@@ -337,8 +339,8 @@ def detect_hesitations(self, threshold_percentile: int = 20) -> None:
337339

338340
dt = np.diff(self.ink_points["seconds"].values)
339341

340-
threshold = np.percentile(self.velocities, threshold_percentile)
341-
hesitations = self.velocities < threshold
342+
threshold_velocity = np.percentile(self.velocities, threshold_percentile)
343+
hesitations = self.velocities < threshold_velocity
342344

343345
hesitation_changes = np.diff(hesitations.astype(int))
344346
hesitation_starts = np.where(hesitation_changes == 1)[0] + 1
@@ -347,9 +349,7 @@ def detect_hesitations(self, threshold_percentile: int = 20) -> None:
347349
if hesitations[0]:
348350
hesitation_count += 1
349351

350-
hesitation_duration = np.sum(hesitations) * dt[0]
351-
352352
self.hesitation_count = hesitation_count
353-
self.hesitation_duration = hesitation_duration
353+
self.hesitation_duration = np.sum(hesitations) * dt[0]
354354

355355
return

tests/unit/test_models.py

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,11 @@ def test_valid_ink_trajectory(
242242

243243
def test_uniform_motion() -> None:
244244
"""Test with points moving at constant velocity."""
245-
points = pd.DataFrame(
246-
{
247-
"x": [0, 1, 2, 3],
248-
"y": [0, 0, 0, 0],
249-
"seconds": [0, 1, 2, 3],
250-
}
251-
)
245+
points = pd.DataFrame({
246+
"x": [0, 1, 2, 3],
247+
"y": [0, 0, 0, 0],
248+
"seconds": [0, 1, 2, 3],
249+
})
252250
segment = models.LineSegment(
253251
start_label="1",
254252
end_label="2",
@@ -260,24 +258,20 @@ def test_uniform_motion() -> None:
260258

261259
segment.calculate_velocity_metrics()
262260

263-
assert segment.distance == pytest.approx(3.0)
264-
assert segment.mean_speed == pytest.approx(1.0)
265-
assert segment.speed_variance == pytest.approx(0.0)
266-
assert len(segment.velocities) == 3
267-
assert all(v == pytest.approx(1.0) for v in segment.velocities)
268-
assert len(segment.accelerations) == 2
269-
assert all(a == pytest.approx(0.0) for a in segment.accelerations)
261+
assert segment.distance == 3.0
262+
assert segment.mean_speed == 1.0
263+
assert segment.speed_variance == 0.0
264+
assert segment.velocities == [1.0, 1.0, 1.0]
265+
assert segment.accelerations == [0.0, 0.0]
270266

271267

272268
def test_accelerating_motion() -> None:
273269
"""Test with motion accelerating over time."""
274-
points = pd.DataFrame(
275-
{
276-
"x": [0, 1, 4, 9],
277-
"y": [0, 0, 0, 0],
278-
"seconds": [0, 1, 2, 3],
279-
}
280-
)
270+
points = pd.DataFrame({
271+
"x": [0, 1, 4, 9],
272+
"y": [0, 0, 0, 0],
273+
"seconds": [0, 1, 2, 3],
274+
})
281275
segment = models.LineSegment(
282276
start_label="1",
283277
end_label="2",
@@ -303,13 +297,11 @@ def test_accelerating_motion() -> None:
303297

304298
def test_velocity_two_points_only() -> None:
305299
"""Test velocity calculation with only two points."""
306-
points = pd.DataFrame(
307-
{
308-
"x": [0, 3],
309-
"y": [0, 4],
310-
"seconds": [0, 2],
311-
}
312-
)
300+
points = pd.DataFrame({
301+
"x": [0, 3],
302+
"y": [0, 4],
303+
"seconds": [0, 2],
304+
})
313305
segment = models.LineSegment(
314306
start_label="1",
315307
end_label="2",
@@ -331,13 +323,11 @@ def test_velocity_two_points_only() -> None:
331323

332324
def test_decelerating_motion() -> None:
333325
"""Test with decelerating motion (negative acceleration)."""
334-
points = pd.DataFrame(
335-
{
336-
"x": [0, 4, 7, 9],
337-
"y": [0, 0, 0, 0],
338-
"seconds": [0, 1, 2, 3],
339-
}
340-
)
326+
points = pd.DataFrame({
327+
"x": [0, 4, 7, 9],
328+
"y": [0, 0, 0, 0],
329+
"seconds": [0, 1, 2, 3],
330+
})
341331
segment = models.LineSegment(
342332
start_label="1",
343333
end_label="2",
@@ -363,13 +353,11 @@ def test_decelerating_motion() -> None:
363353

364354
def test_stationary_motion() -> None:
365355
"""Test with no movement (all points the same)."""
366-
points = pd.DataFrame(
367-
{
368-
"x": [1, 1, 1],
369-
"y": [1, 1, 1],
370-
"seconds": [0, 1, 2],
371-
}
372-
)
356+
points = pd.DataFrame({
357+
"x": [1, 1, 1],
358+
"y": [1, 1, 1],
359+
"seconds": [0, 1, 2],
360+
})
373361
segment = models.LineSegment(
374362
start_label="1",
375363
end_label="2",
@@ -392,13 +380,11 @@ def test_stationary_motion() -> None:
392380

393381
def test_no_hesitations_uniform_motion() -> None:
394382
"""Test with uniform motion where all velocities are equal."""
395-
points = pd.DataFrame(
396-
{
397-
"x": [0, 1, 2, 3],
398-
"y": [0, 0, 0, 0],
399-
"seconds": [0, 1, 2, 3],
400-
}
401-
)
383+
points = pd.DataFrame({
384+
"x": [0, 1, 2, 3],
385+
"y": [0, 0, 0, 0],
386+
"seconds": [0, 1, 2, 3],
387+
})
402388
segment = models.LineSegment(
403389
start_label="1",
404390
end_label="2",
@@ -417,13 +403,11 @@ def test_no_hesitations_uniform_motion() -> None:
417403

418404
def test_hesitation_at_start() -> None:
419405
"""Test when the line starts with a hesitation."""
420-
points = pd.DataFrame(
421-
{
422-
"x": [0, 0.1, 1, 2],
423-
"y": [0, 0.1, 0, 0],
424-
"seconds": [0, 1, 2, 3],
425-
}
426-
)
406+
points = pd.DataFrame({
407+
"x": [0, 0.1, 1, 2],
408+
"y": [0, 0.1, 0, 0],
409+
"seconds": [0, 1, 2, 3],
410+
})
427411
segment = models.LineSegment(
428412
start_label="1",
429413
end_label="2",
@@ -442,13 +426,11 @@ def test_hesitation_at_start() -> None:
442426

443427
def test_multiple_hesitations() -> None:
444428
"""Test when there are multiple hesitation periods."""
445-
points = pd.DataFrame(
446-
{
447-
"x": [0, 100, 100.1, 200, 200.1, 300, 400, 500, 600],
448-
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0],
449-
"seconds": [0, 1, 2, 3, 4, 5, 6, 7, 8],
450-
}
451-
)
429+
points = pd.DataFrame({
430+
"x": [0, 100, 100.1, 200, 200.1, 300, 400, 500, 600],
431+
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0],
432+
"seconds": [0, 1, 2, 3, 4, 5, 6, 7, 8],
433+
})
452434
segment = models.LineSegment(
453435
start_label="1",
454436
end_label="2",
@@ -467,13 +449,11 @@ def test_multiple_hesitations() -> None:
467449

468450
def test_less_than_three_velocities() -> None:
469451
"""Test early return when velocities length is less than 3."""
470-
points = pd.DataFrame(
471-
{
472-
"x": [0, 1],
473-
"y": [0, 0],
474-
"seconds": [0, 1],
475-
}
476-
)
452+
points = pd.DataFrame({
453+
"x": [0, 1],
454+
"y": [0, 0],
455+
"seconds": [0, 1],
456+
})
477457
segment = models.LineSegment(
478458
start_label="1",
479459
end_label="2",

0 commit comments

Comments
 (0)