|
| 1 | +"""Test cases for the segment_strokes utility function.""" |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +from graphomotor.utils import alphabet_utils |
| 6 | + |
| 7 | + |
| 8 | +def _make_drawing_data() -> pd.DataFrame: |
| 9 | + """Create drawing data with three strokes in distinct spatial regions. |
| 10 | +
|
| 11 | + Stroke 0 has centroid near (5, 85) - top-left of a 2x2 grid. |
| 12 | + Stroke 1 has centroid near (55, 85) - top-right. |
| 13 | + Stroke 2 has centroid near (5, 15) - bottom-left. |
| 14 | + """ |
| 15 | + return pd.DataFrame( |
| 16 | + { |
| 17 | + "line_number": [0, 0, 0, 1, 1, 1, 2, 2, 2], |
| 18 | + "x": [3.0, 5.0, 7.0, 53.0, 55.0, 57.0, 3.0, 5.0, 7.0], |
| 19 | + "y": [83.0, 85.0, 87.0, 83.0, 85.0, 87.0, 13.0, 15.0, 17.0], |
| 20 | + "seconds": [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], |
| 21 | + } |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +class TestSegmentStrokes: |
| 26 | + """Tests for the segment_strokes function.""" |
| 27 | + |
| 28 | + def test_strokes_assigned_to_correct_cells(self) -> None: |
| 29 | + """Each stroke should be placed in the cell containing its centroid.""" |
| 30 | + data = _make_drawing_data() |
| 31 | + grid = alphabet_utils.segment_strokes( |
| 32 | + data=data, |
| 33 | + x_min=0.0, x_max=100.0, y_min=0.0, y_max=100.0, |
| 34 | + n_rows=2, n_cols=2, labels=["TL", "TR", "BL", "BR"], |
| 35 | + ) |
| 36 | + |
| 37 | + assert len(grid.cells[0].strokes) == 1 |
| 38 | + assert grid.cells[0].strokes[0].line_number == 0 |
| 39 | + assert len(grid.cells[1].strokes) == 1 |
| 40 | + assert grid.cells[1].strokes[0].line_number == 1 |
| 41 | + assert len(grid.cells[2].strokes) == 1 |
| 42 | + assert grid.cells[2].strokes[0].line_number == 2 |
| 43 | + assert len(grid.cells[3].strokes) == 0 |
| 44 | + |
| 45 | + def test_total_stroke_count_matches_line_numbers(self) -> None: |
| 46 | + """Total strokes across all cells should equal the number of line groups.""" |
| 47 | + data = _make_drawing_data() |
| 48 | + grid = alphabet_utils.segment_strokes( |
| 49 | + data=data, |
| 50 | + x_min=0.0, x_max=100.0, y_min=0.0, y_max=100.0, |
| 51 | + n_rows=2, n_cols=2, |
| 52 | + ) |
| 53 | + |
| 54 | + total_strokes = sum(len(c.strokes) for c in grid.cells) |
| 55 | + assert total_strokes == 3 |
| 56 | + |
| 57 | + def test_stroke_points_are_correct(self) -> None: |
| 58 | + """Each Stroke should contain the correct subset of points.""" |
| 59 | + data = _make_drawing_data() |
| 60 | + grid = alphabet_utils.segment_strokes( |
| 61 | + data=data, |
| 62 | + x_min=0.0, x_max=100.0, y_min=0.0, y_max=100.0, |
| 63 | + n_rows=2, n_cols=2, |
| 64 | + ) |
| 65 | + |
| 66 | + stroke_0 = grid.cells[0].strokes[0] |
| 67 | + assert len(stroke_0.points) == 3 |
| 68 | + assert list(stroke_0.points["x"]) == [3.0, 5.0, 7.0] |
| 69 | + |
| 70 | + def test_empty_dataframe(self) -> None: |
| 71 | + """An empty DataFrame should produce a grid with no strokes.""" |
| 72 | + data = pd.DataFrame(columns=["line_number", "x", "y", "seconds"]) |
| 73 | + grid = alphabet_utils.segment_strokes( |
| 74 | + data=data, |
| 75 | + x_min=0.0, x_max=10.0, y_min=0.0, y_max=10.0, |
| 76 | + n_rows=1, n_cols=1, |
| 77 | + ) |
| 78 | + |
| 79 | + assert all(len(c.strokes) == 0 for c in grid.cells) |
| 80 | + |
| 81 | + def test_stroke_outside_grid_is_not_assigned(self) -> None: |
| 82 | + """Strokes whose centroids fall outside the grid should be dropped.""" |
| 83 | + data = pd.DataFrame( |
| 84 | + { |
| 85 | + "line_number": [0, 0], |
| 86 | + "x": [500.0, 600.0], |
| 87 | + "y": [500.0, 600.0], |
| 88 | + "seconds": [0.0, 0.1], |
| 89 | + } |
| 90 | + ) |
| 91 | + grid = alphabet_utils.segment_strokes( |
| 92 | + data=data, |
| 93 | + x_min=0.0, x_max=10.0, y_min=0.0, y_max=10.0, |
| 94 | + n_rows=1, n_cols=1, |
| 95 | + ) |
| 96 | + |
| 97 | + assert len(grid.cells[0].strokes) == 0 |
| 98 | + |
| 99 | + def test_multiple_strokes_in_same_cell(self) -> None: |
| 100 | + """Multiple strokes in the same spatial region should all land in one cell.""" |
| 101 | + data = pd.DataFrame( |
| 102 | + { |
| 103 | + "line_number": [0, 0, 1, 1, 2, 2], |
| 104 | + "x": [5.0, 6.0, 5.5, 6.5, 4.5, 5.5], |
| 105 | + "y": [5.0, 6.0, 5.5, 6.5, 4.5, 5.5], |
| 106 | + "seconds": [0.0, 0.1, 0.2, 0.3, 0.4, 0.5], |
| 107 | + } |
| 108 | + ) |
| 109 | + grid = alphabet_utils.segment_strokes( |
| 110 | + data=data, |
| 111 | + x_min=0.0, x_max=10.0, y_min=0.0, y_max=10.0, |
| 112 | + n_rows=1, n_cols=1, |
| 113 | + ) |
| 114 | + |
| 115 | + assert len(grid.cells[0].strokes) == 3 |
| 116 | + |
| 117 | + def test_grid_structure_matches_parameters(self) -> None: |
| 118 | + """Returned grid should have the correct number of labeled cells.""" |
| 119 | + data = _make_drawing_data() |
| 120 | + labels = ["A", "B", "C", "D", "E", "F"] |
| 121 | + grid = alphabet_utils.segment_strokes( |
| 122 | + data=data, |
| 123 | + x_min=0.0, x_max=100.0, y_min=0.0, y_max=100.0, |
| 124 | + n_rows=2, n_cols=3, labels=labels, |
| 125 | + ) |
| 126 | + |
| 127 | + assert len(grid.cells) == 6 |
| 128 | + assert [c.label for c in grid.cells] == labels |
| 129 | + |
| 130 | + def test_stroke_index_is_reset(self) -> None: |
| 131 | + """Stroke points should have a reset index starting from 0.""" |
| 132 | + data = pd.DataFrame( |
| 133 | + { |
| 134 | + "line_number": [5, 5, 5], |
| 135 | + "x": [1.0, 2.0, 3.0], |
| 136 | + "y": [1.0, 2.0, 3.0], |
| 137 | + "seconds": [0.0, 0.1, 0.2], |
| 138 | + }, |
| 139 | + index=[10, 11, 12], |
| 140 | + ) |
| 141 | + grid = alphabet_utils.segment_strokes( |
| 142 | + data=data, |
| 143 | + x_min=0.0, x_max=10.0, y_min=0.0, y_max=10.0, |
| 144 | + n_rows=1, n_cols=1, |
| 145 | + ) |
| 146 | + |
| 147 | + stroke = grid.cells[0].strokes[0] |
| 148 | + assert list(stroke.points.index) == [0, 1, 2] |
0 commit comments