-
Notifications
You must be signed in to change notification settings - Fork 0
feature/issue-19/implement-drawing-error-features #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
92067b7
1454f32
0723f64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| from graphomotor.core import models | ||
|
|
||
|
|
||
| def calculate_area_under_curve( | ||
| drawn_spiral: models.Spiral, reference_spiral: np.ndarray | ||
| ) -> dict: | ||
| """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)} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """Test cases for drawing_error.py functions.""" | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| import pytest | ||
| from scipy import integrate | ||
|
|
||
| from graphomotor.core import models | ||
| from graphomotor.features import drawing_error | ||
|
|
||
|
|
||
| def test_calculate_area_under_curve(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Test that the area under the curve is calculated correctly.""" | ||
| x = np.linspace(-np.pi / 2, 6 * np.pi / 4, 100) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why such a complicated x-axis data points? If you just do 0-2pi I'm pretty sure this is a nice easy calculation for AUC of the difference (and an exact value)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| y1 = np.sin(x) | ||
| y2 = np.sin(x + np.pi) | ||
|
|
||
| expected_area, _ = integrate.quad( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This begs the question: If we can use integration to find the area under the curve here why do we need the shapely method?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use integration for this test which uses only sine waves, but the spiral data is a lot trickier. My initial goal was to convert Cartesian coordinates to polar and just use |
||
| lambda x: np.abs(np.sin(x) - np.sin(x + np.pi)), -np.pi / 2, 6 * np.pi / 4 | ||
| ) | ||
|
|
||
| class MockSpiral: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't have a dedicated class within a unit test
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually couldn't find a better way of doing this test so I left it like this hoping you might suggest a better way lol |
||
| def __init__(self, data: pd.DataFrame, metadata: dict) -> None: | ||
| self.data = data | ||
| self.metadata = metadata | ||
|
|
||
| monkeypatch.setattr(models, "Spiral", MockSpiral) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No part of this mocking is needed?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want the feature extraction functions to input the |
||
|
|
||
| calculated_area = drawing_error.calculate_area_under_curve( | ||
| models.Spiral(data=pd.DataFrame({"x": x, "y": y1}), metadata={}), | ||
| np.array([x, y2]).T, | ||
| )["area_under_curve"] | ||
|
|
||
| assert np.isclose(calculated_area, expected_area, rtol=1e-3) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

There was a problem hiding this comment.
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 licenseisThere was a problem hiding this comment.
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: