-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
92067b7
Add shapely dependency and implement area under curve calculation for…
alperkent 1454f32
Update MockSpiral class to include metadata in area under curve test
alperkent 0723f64
Refactor function signatures to specify return types as dict[str, flo…
alperkent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[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)} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: