Repository navigation
Require 200 Hz when TUEV/TUAB compute the stft field - #1601
Conversation
get_stft_torch is called without a rate, so it always uses a 200-sample window, a 100-sample hop and the first 100 bins. At any resample_rate other than 200 Hz the window length and bin width changed without notice, and TFM-Tokenizer is built around the 200 Hz layout (n_freq=100, raw-signal kernel 200 with stride 100). EEGEventsTUEV and EEGAbnormalTUAB now raise a ValueError in __init__ when compute_stft=True and resample_rate != 200, naming compute_stft=False as the way to use another rate. Both docstrings and the API page describe the stft field and the requirement. Fixes sunlabuiuc#1521
Test CoverageTotal: 86.66% (48008 / 55400 lines)
|
|
/review |
| unittest.main() | ||
|
|
||
|
|
||
| class TestEEGEventsTUEVStftRate(unittest.TestCase): |
There was a problem hiding this comment.
Moved above the guard in d762751. Running python tests/core/test_tuev.py now runs 16 tests instead of 12.
| unittest.main() | ||
|
|
||
|
|
||
| class TestEEGAbnormalTUABStftRate(unittest.TestCase): |
There was a problem hiding this comment.
Moved above the guard in d762751. Running python tests/core/test_tuab.py now runs 26 tests instead of 22.
| task = EEGEventsTUEV(resample_rate=256, compute_stft=False) | ||
| self.assertEqual(task.input_schema, {"signal": "tensor"}) | ||
|
|
||
| def test_stft_shape_at_200_hz(self): |
There was a problem hiding this comment.
Replaced in d762751 by test_stft_field_at_200_hz in both files. It runs the task on a synthetic recording and compares sample["stft"] with get_stft_torch of the returned signal: shape (16, 100, 9) for the 5 s TUEV window and (16, 100, 19) for the 10 s TUAB window.
There was a problem hiding this comment.
Previous review
AI Review
suggestion: approve
decision: approve
Thank you for this fix, Axel. The task now raises a clear ValueError when TUEV or TUAB would compute the stft field at a rate other than 200 Hz. That replaces a silent change in window length and bin width. It looks ready to merge.
The change stays inside the two task classes. The helper is private and called from both init methods, and the docstrings and API page match the code. The tests cover accepted rates (200 and 200.0), rejected rates (100, 256, 200.5), and 256 Hz with compute_stft=False. They also check the STFT shape at 200 Hz. Coverage is 83.06% for temple_university_EEG_tasks.py, above the 80% per-file bar, and 86.23% for the whole repository, above the 60% bar.
Optional suggestions:
- The new test classes sit below the
if __name__ == "__main__"block. Pytest and unittest discovery still collect them, but running the file directly skips them. Moving them above that block would fix this. - The shape test calls get_stft_torch directly rather than the task's own stft output. Testing through the task would tie the check more closely to the behaviour you changed.
Reviewed commit 7d35640
Move the new test classes above the __main__ guard so that running the file directly collects them, and check the stft field that each task returns at 200 Hz instead of calling get_stft_torch on its own. Nested with statements in the tests of this PR are merged into one (ruff SIM117).
| unittest.main() | ||
|
|
||
|
|
||
| class TestEEGEventsTUEVStftRate(unittest.TestCase): |
There was a problem hiding this comment.
Moved above the guard in d762751. Running python tests/core/test_tuev.py now runs 16 tests instead of 12.
| unittest.main() | ||
|
|
||
|
|
||
| class TestEEGAbnormalTUABStftRate(unittest.TestCase): |
There was a problem hiding this comment.
Moved above the guard in d762751. Running python tests/core/test_tuab.py now runs 26 tests instead of 22.
| task = EEGEventsTUEV(resample_rate=256, compute_stft=False) | ||
| self.assertEqual(task.input_schema, {"signal": "tensor"}) | ||
|
|
||
| def test_stft_shape_at_200_hz(self): |
There was a problem hiding this comment.
Replaced in d762751 by test_stft_field_at_200_hz in both files. It runs the task on a synthetic recording and compares sample["stft"] with get_stft_torch of the returned signal: shape (16, 100, 9) for the 5 s TUEV window and (16, 100, 19) for the 10 s TUAB window.
|
/review |
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for this fix, Axel. It stops the TUEV and TUAB tasks from silently building an stft field with the wrong window and bin width when the sampling rate isn't 200 Hz. It now raises a clear ValueError that points users to compute_stft=False.
This looks ready to merge.
- Code: The private
_check_stft_ratehelper is called in both__init__methods. The message is clear, and the behaviour at 200 Hz is unchanged. - Docs: The class docstrings and the API page match the code.
- Tests: The new tests cover accepted rates (200 and 200.0), rejected rates (100, 256, 200.5), the
compute_stft=Falsepath, and the 200 Hz STFT shapes. The shapes are (16, 100, 9) for TUEV and (16, 100, 19) for TUAB. - Coverage:
temple_university_EEG_tasks.pyis at 83.06%, above the 80% bar for a changed file. Total repository coverage is 86.66%, above the 60% bar. - Sharing: No new links between components are introduced.
Optional suggestion: the value check in the new tests compares the output against get_stft_torch itself. It confirms the field is computed from the signal, but the shape assertion is the real check. A fixed-value check would be stronger, though it isn't needed to merge.
Reviewed commit d762751
Contributor: Axel Coiffard-Donaty (@AxelNoun)
Contribution Type: Bug fix
Fixes #1521.
Problem
EEGEventsTUEVandEEGAbnormalTUABcallget_stft_torchwithout a rate, so thestftfield always uses a 200-sample window, a 100-sample hop and the first 100 bins. At anyresample_rateother than 200 Hz the window length and bin width changed without notice (100 Hz: 2 s window, 0.5 Hz bins; 256 Hz: about 0.78 s, about 1.28 Hz bins).Change
As recommended in the issue (option 3), since TFM-Tokenizer is built around 200 Hz (
n_freq=100, raw-signal kernel 200 with stride 100):_check_stft_rate, called in both__init__methods, raises aValueErrorwhencompute_stft=Trueandresample_rate != 200. The message namescompute_stft=Falseas the way to use another rate;stftfield and the 200 Hz requirement;compute_stftand the requirement.Behaviour at 200 Hz (the default, and the rate used by every example and notebook) is unchanged. Code that passed another rate with the default
compute_stft=Truenow gets an error instead of a silently different STFT; that is the intended change.Tests
New
...StftRateclasses intests/core/test_tuev.pyandtests/core/test_tuab.py:compute_stft=Trueare accepted;compute_stft=Trueraise;compute_stft=Falseis accepted;(16, 100, 9)(1 s windows, 0.5 s hop, 100 bins).The rejection checks fail on master and pass with the change.
tests/core/test_tuev.pyandtests/core/test_tuab.py: 42 passed.python tools/check_pr_rules.py --base master --head HEADpasses.This branch and the one for #1522 touch the same file in different places; they merge together cleanly.
Files to Review
pyhealth/tasks/temple_university_EEG_tasks.py:_check_stft_rate, its two calls and the two class docstrings.tests/core/test_tuev.pyandtests/core/test_tuab.py: the new test classes.docs/api/tasks/pyhealth.tasks.temple_university_EEG_tasks.rst: thecompute_stftentry.