Skip to content

Commit b59e5e7

Browse files
committed
resolved comments
1 parent 10957f9 commit b59e5e7

2 files changed

Lines changed: 50 additions & 74 deletions

File tree

src/graphomotor/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def calculate_path_optimality(
293293
return
294294

295295
def calculate_velocity_metrics(self, ink_points: pd.DataFrame) -> None:
296-
"""Get velocity metrics of a LineSegment.
296+
"""Get distance, velocity, and acceleration metrics of a LineSegment.
297297
298298
Args:
299299
self: LineSegment object to calculate velocities for.

tests/unit/test_models.py

Lines changed: 49 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
from typing import Dict, cast
55

6+
import numpy as np
67
import pandas as pd
78
import pytest
89

@@ -242,13 +243,11 @@ def test_valid_ink_trajectory(
242243

243244
def test_uniform_motion() -> None:
244245
"""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-
)
246+
points = pd.DataFrame({
247+
"x": [0, 1, 2, 3],
248+
"y": [0, 0, 0, 0],
249+
"seconds": [0, 1, 2, 3],
250+
})
252251
segment = models.LineSegment(
253252
start_label="1",
254253
end_label="2",
@@ -259,24 +258,20 @@ def test_uniform_motion() -> None:
259258

260259
segment.calculate_velocity_metrics(points)
261260

262-
assert segment.distance == pytest.approx(3.0)
263-
assert segment.mean_speed == pytest.approx(1.0)
264-
assert segment.speed_variance == pytest.approx(0.0)
265-
assert len(segment.velocities) == 3
266-
assert all(v == pytest.approx(1.0) for v in segment.velocities)
267-
assert len(segment.accelerations) == 2
268-
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 np.all(segment.velocities) == 1.0
265+
assert np.all(segment.accelerations) == 0.0
269266

270267

271268
def test_accelerating_motion() -> None:
272269
"""Test with motion accelerating over time."""
273-
points = pd.DataFrame(
274-
{
275-
"x": [0, 1, 4, 9],
276-
"y": [0, 0, 0, 0],
277-
"seconds": [0, 1, 2, 3],
278-
}
279-
)
270+
points = pd.DataFrame({
271+
"x": [0, 1, 4, 9],
272+
"y": [0, 0, 0, 0],
273+
"seconds": [0, 1, 2, 3],
274+
})
280275
segment = models.LineSegment(
281276
start_label="1",
282277
end_label="2",
@@ -287,27 +282,20 @@ def test_accelerating_motion() -> None:
287282

288283
segment.calculate_velocity_metrics(points)
289284

290-
assert segment.distance == pytest.approx(9.0)
291-
assert segment.mean_speed == pytest.approx(3.0)
285+
assert segment.distance == 9.0
286+
assert segment.mean_speed == 3.0
292287
assert segment.speed_variance > 0.0
293-
assert len(segment.velocities) == 3
294-
assert segment.velocities[0] == pytest.approx(1.0)
295-
assert segment.velocities[1] == pytest.approx(3.0)
296-
assert segment.velocities[2] == pytest.approx(5.0)
297-
assert len(segment.accelerations) == 2
298-
assert segment.accelerations[0] == pytest.approx(2.0)
299-
assert segment.accelerations[1] == pytest.approx(2.0)
288+
assert segment.velocities == [1.0, 3.0, 5.0]
289+
assert segment.accelerations == [2.0, 2.0]
300290

301291

302292
def test_velocity_two_points_only() -> None:
303293
"""Test velocity calculation with only two points."""
304-
points = pd.DataFrame(
305-
{
306-
"x": [0, 3],
307-
"y": [0, 4],
308-
"seconds": [0, 2],
309-
}
310-
)
294+
points = pd.DataFrame({
295+
"x": [0, 3],
296+
"y": [0, 4],
297+
"seconds": [0, 2],
298+
})
311299
segment = models.LineSegment(
312300
start_label="1",
313301
end_label="2",
@@ -318,23 +306,20 @@ def test_velocity_two_points_only() -> None:
318306

319307
segment.calculate_velocity_metrics(points)
320308

321-
assert segment.distance == pytest.approx(5.0)
322-
assert segment.mean_speed == pytest.approx(2.5)
323-
assert segment.speed_variance == pytest.approx(0.0)
324-
assert len(segment.velocities) == 1
325-
assert segment.velocities[0] == pytest.approx(2.5)
326-
assert len(segment.accelerations) == 0
309+
assert segment.distance == 5.0
310+
assert segment.mean_speed == 2.5
311+
assert segment.speed_variance == 0.0
312+
assert segment.velocities == [2.5]
313+
assert segment.accelerations == []
327314

328315

329316
def test_decelerating_motion() -> None:
330317
"""Test with decelerating motion (negative acceleration)."""
331-
points = pd.DataFrame(
332-
{
333-
"x": [0, 4, 7, 9],
334-
"y": [0, 0, 0, 0],
335-
"seconds": [0, 1, 2, 3],
336-
}
337-
)
318+
points = pd.DataFrame({
319+
"x": [0, 4, 7, 9],
320+
"y": [0, 0, 0, 0],
321+
"seconds": [0, 1, 2, 3],
322+
})
338323
segment = models.LineSegment(
339324
start_label="1",
340325
end_label="2",
@@ -345,27 +330,20 @@ def test_decelerating_motion() -> None:
345330

346331
segment.calculate_velocity_metrics(points)
347332

348-
assert segment.distance == pytest.approx(9.0)
349-
assert segment.mean_speed == pytest.approx(3.0)
333+
assert segment.distance == 9.0
334+
assert segment.mean_speed == 3.0
350335
assert segment.speed_variance > 0.0
351-
assert len(segment.velocities) == 3
352-
assert segment.velocities[0] == pytest.approx(4.0)
353-
assert segment.velocities[1] == pytest.approx(3.0)
354-
assert segment.velocities[2] == pytest.approx(2.0)
355-
assert len(segment.accelerations) == 2
356-
assert segment.accelerations[0] == pytest.approx(-1.0)
357-
assert segment.accelerations[1] == pytest.approx(-1.0)
336+
assert segment.velocities == [4.0, 3.0, 2.0]
337+
assert segment.accelerations == [-1.0, -1.0]
358338

359339

360340
def test_stationary_motion() -> None:
361341
"""Test with no movement (all points the same)."""
362-
points = pd.DataFrame(
363-
{
364-
"x": [1, 1, 1],
365-
"y": [1, 1, 1],
366-
"seconds": [0, 1, 2],
367-
}
368-
)
342+
points = pd.DataFrame({
343+
"x": [1, 1, 1],
344+
"y": [1, 1, 1],
345+
"seconds": [0, 1, 2],
346+
})
369347
segment = models.LineSegment(
370348
start_label="1",
371349
end_label="2",
@@ -376,10 +354,8 @@ def test_stationary_motion() -> None:
376354

377355
segment.calculate_velocity_metrics(points)
378356

379-
assert segment.distance == pytest.approx(0.0)
380-
assert segment.mean_speed == pytest.approx(0.0)
381-
assert segment.speed_variance == pytest.approx(0.0)
382-
assert len(segment.velocities) == 2
383-
assert all(v == pytest.approx(0.0) for v in segment.velocities)
384-
assert len(segment.accelerations) == 1
385-
assert segment.accelerations[0] == pytest.approx(0.0)
357+
assert segment.distance == 0.0
358+
assert segment.mean_speed == 0.0
359+
assert segment.speed_variance == 0.0
360+
assert segment.velocities == [0.0, 0.0]
361+
assert segment.accelerations == [0.0]

0 commit comments

Comments
 (0)