Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ requires-python = ">=3.12"
dependencies = [
"pandas>=2.2.3",
"pydantic>=2.11.1",
"scipy>=1.15.2"
"scipy>=1.15.2",
"shapely>=2.1.0"
]

[dependency-groups]
Expand Down
2 changes: 1 addition & 1 deletion src/graphomotor/features/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _segment_data(data: np.ndarray, start_prop: float, end_prop: float) -> np.nd

def calculate_hausdorff_metrics(
spiral: models.Spiral, reference_spiral: np.ndarray
) -> dict:
) -> dict[str, float]:
"""Calculate Hausdorff distance metrics for a spiral object.

This function computes multiple features based on the Hausdorff distance between a
Expand Down
36 changes: 36 additions & 0 deletions src/graphomotor/features/drawing_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Feature extraction module for drawing error-based metrics in spiral drawing data."""

import numpy as np
from shapely import geometry, ops
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check their license and how it impacts using their package with our license. Not sure what BSD-3-Clause license is

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BSD-3-Clause looks pretty permissive, so I don't think there would be any issues:

BSD 3-Clause License

Copyright (c) 2007, Sean C. Gillies. 2019, Casper van der Wel. 2007-2022, Shapely Contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


from graphomotor.core import models


def calculate_area_under_curve(
drawn_spiral: models.Spiral, reference_spiral: np.ndarray
) -> dict[str, float]:
"""Calculate the area between drawn and reference spirals.

This function measures the deviation between drawn and reference spirals by
computing the enclosed area between them using the shapely library. Lower values
indicate better adherence to the template. The algorithm works by creating polygons
that connect spiral endpoints, finding intersections between lines, and calculating
the total area of the resulting polygons.

Args:
drawn_spiral: The spiral drawn by the subject.
reference_spiral: The reference spiral.

Returns:
Dictionary containing the area under curve metric
"""
spiral = drawn_spiral.data[["x", "y"]].values
line_drawn = geometry.LineString(spiral)
line_reference = geometry.LineString(reference_spiral)
first_segment = geometry.LineString([spiral[0], reference_spiral[0]])
last_segment = geometry.LineString([spiral[-1], reference_spiral[-1]])
merged_line = ops.unary_union(
[line_drawn, line_reference, first_segment, last_segment]
)
polygons = list(ops.polygonize(merged_line))
return {"area_under_curve": sum(p.area for p in polygons)}
23 changes: 23 additions & 0 deletions tests/unit/test_drawing_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Test cases for drawing_error.py functions."""

import numpy as np
import pandas as pd

from graphomotor.core import models
from graphomotor.features import drawing_error


def test_calculate_area_under_curve(valid_spiral: models.Spiral) -> None:
"""Test that the area under the curve is calculated correctly."""
x = np.linspace(-np.pi / 2, 3 * np.pi / 2, 100)
y1 = np.sin(x)
y2 = np.sin(x + np.pi)

expected_area = 8.0

valid_spiral.data = pd.DataFrame({"x": x, "y": y1})
calculated_area = drawing_error.calculate_area_under_curve(
valid_spiral, np.column_stack((x, y2))
)["area_under_curve"]

assert np.isclose(calculated_area, expected_area, rtol=1e-3)
37 changes: 37 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.