-
Notifications
You must be signed in to change notification settings - Fork 0
69 task write trails time feature functions error time #95
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
cgmaiorano
merged 6 commits into
main
from
69-task-write-trails-time-feature-functions-error_time
Feb 9, 2026
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e78a5c
create time file
cgmaiorano 6ebdd0c
create test_trails_time.py and unit test for no errors
cgmaiorano 14b5e92
bug fixes in function and new unit tests
cgmaiorano eb317a8
ruff format
cgmaiorano 3996556
reviewed changes and new case handling for error at start
cgmaiorano 4783230
small docstring edit
cgmaiorano 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Feature extraction module for time-based metrics in trails drawing data.""" | ||
|
|
||
| from graphomotor.core import models | ||
|
|
||
|
|
||
| def calculate_total_error_time(drawing: models.Drawing) -> dict[str, float]: | ||
| """Calculate the total time spent making errors. | ||
|
|
||
| A contiguous "error chunk" is any sequence of rows where df["error"] != "E0". | ||
| For each chunk, we find the midpoint time when the error started and the midpoint | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
| time when the error ended. The total error time is the sum of the durations of all | ||
| error chunks. | ||
|
|
||
| Args: | ||
| drawing: Drawing object containing drawing data. | ||
|
|
||
| Returns: | ||
| Dictionary containing the total time spent in error states. | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
| """ | ||
| mask = drawing.data["error"] != "E0" | ||
| if not mask.any(): | ||
| return {"total_error_time": 0.0} | ||
|
|
||
| chunk_start = (~mask.shift(fill_value=False) & mask).to_numpy().nonzero()[0] | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
| chunk_end = (mask.shift(fill_value=False) & ~mask).to_numpy().nonzero()[0] | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
|
|
||
| if mask.iloc[-1]: | ||
| chunk_end = list(chunk_end) + [len(drawing.data) - 1] | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
|
|
||
| seconds = drawing.data["seconds"].to_numpy() | ||
| total_error_time = 0.0 | ||
|
|
||
| for start_idx, end_idx in zip(chunk_start, chunk_end): | ||
| start_mid = ( | ||
| (seconds[start_idx - 1] + seconds[start_idx]) / 2 | ||
| if start_idx > 0 | ||
| else seconds[0] | ||
| ) | ||
|
|
||
| if end_idx + 1 < len(drawing.data): | ||
| end_mid = (seconds[end_idx] + seconds[end_idx - 1]) / 2 | ||
| else: | ||
| if mask.iloc[end_idx]: | ||
| end_mid = seconds[end_idx] | ||
| else: | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
| end_mid = (seconds[end_idx] + seconds[end_idx - 1]) / 2 | ||
| print(start_mid, end_mid) | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
| total_error_time += end_mid - start_mid | ||
|
cgmaiorano marked this conversation as resolved.
Outdated
|
||
|
|
||
| return {"total_error_time": float(total_error_time)} | ||
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,73 @@ | ||
| """Tests for trails time.py.""" | ||
|
|
||
| import pandas as pd | ||
|
|
||
| from graphomotor.core import models | ||
| from graphomotor.features.trails import time | ||
|
|
||
|
|
||
| def test_total_error_time_no_errors() -> None: | ||
| """Test case with no errors.""" | ||
| df = pd.DataFrame( | ||
| { | ||
| "error": ["E0", "E0", "E0", "E0"], | ||
| "seconds": [0, 1, 2, 3], | ||
| } | ||
| ) | ||
| drawing = models.Drawing(data=df, task_name="trails", metadata={"id": "5555555"}) | ||
|
|
||
| result = time.calculate_total_error_time(drawing) | ||
| assert result == {"total_error_time": 0.0} | ||
|
|
||
|
|
||
| def test_single_error_chunk() -> None: | ||
| """Test case with a single error chunk.""" | ||
| df = pd.DataFrame( | ||
| { | ||
| "error": ["E0", "E1", "E1", "E0", "E0"], | ||
| "seconds": [0.0, 1.0, 3.0, 5.0, 6.0], | ||
| } | ||
| ) | ||
| drawing = models.Drawing(data=df, task_name="trails", metadata={"id": "5555555"}) | ||
|
|
||
| result = time.calculate_total_error_time(drawing) | ||
| assert result == {"total_error_time": 3.5} | ||
|
|
||
|
|
||
| def test_multiple_error_chunks() -> None: | ||
| """Test case with multiple error chunks.""" | ||
| df = pd.DataFrame( | ||
| { | ||
| "error": ["E0", "E1", "E1", "E0", "E2", "E2", "E0"], | ||
| "seconds": [0, 1, 3, 5, 6, 7, 9], | ||
| } | ||
| ) | ||
| drawing = models.Drawing(data=df, task_name="trails", metadata={"id": "5555555"}) | ||
| result = time.calculate_total_error_time(drawing) | ||
| assert result == {"total_error_time": 6.0} | ||
|
|
||
|
|
||
| def test_error_at_end() -> None: | ||
| """Test case with an error chunk that goes to the end of the drawing.""" | ||
| df = pd.DataFrame( | ||
| { | ||
| "error": ["E0", "E0", "E2", "E2"], | ||
| "seconds": [0.0, 1.0, 2.0, 4.0], | ||
| } | ||
| ) | ||
| drawing = models.Drawing(data=df, task_name="trails", metadata={"id": "5555555"}) | ||
| result = time.calculate_total_error_time(drawing) | ||
| assert result == {"total_error_time": 2.5} | ||
|
|
||
|
|
||
| def test_error_at_start() -> None: | ||
| """Test case with an error chunk that starts at the beginning of the drawing.""" | ||
| df = pd.DataFrame( | ||
| { | ||
| "error": ["E1", "E1", "E0", "E0"], | ||
| "seconds": [0.0, 1.0, 3.0, 4.0], | ||
| } | ||
| ) | ||
| drawing = models.Drawing(data=df, task_name="trails", metadata={"id": "5555555"}) | ||
| result = time.calculate_total_error_time(drawing) | ||
| assert result == {"total_error_time": 2.0} |
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.
Uh oh!
There was an error while loading. Please reload this page.