Skip to content

Require 200 Hz when TUEV/TUAB compute the stft field - #1601

Merged
solarsys merged 2 commits into
sunlabuiuc:masterfrom
AxelNoun:fix/tuh-stft-requires-200hz
Nov 27, 2026
Merged

solarsys merged 2 commits into
sunlabuiuc:masterfrom
AxelNoun:fix/tuh-stft-requires-200hz

Conversation

@AxelNoun

Copy link
Copy Markdown
Contributor

Contributor: Axel Coiffard-Donaty (@AxelNoun)

Contribution Type: Bug fix

Fixes #1521.

Problem

EEGEventsTUEV and EEGAbnormalTUAB call get_stft_torch without a rate, so the stft field 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 (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):

  • a private helper _check_stft_rate, called in both __init__ methods, raises a ValueError when compute_stft=True and resample_rate != 200. The message names compute_stft=False as the way to use another rate;
  • both class docstrings now describe the stft field and the 200 Hz requirement;
  • the API page lists compute_stft and 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=True now gets an error instead of a silently different STFT; that is the intended change.

Tests

New ...StftRate classes in tests/core/test_tuev.py and tests/core/test_tuab.py:

  • 200 and 200.0 Hz with compute_stft=True are accepted;
  • 100, 256 and 200.5 Hz with compute_stft=True raise;
  • 256 Hz with compute_stft=False is accepted;
  • at 200 Hz the STFT of a 5 s signal has shape (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.py and tests/core/test_tuab.py: 42 passed. python tools/check_pr_rules.py --base master --head HEAD passes.

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.py and tests/core/test_tuab.py: the new test classes.
  • docs/api/tasks/pyhealth.tasks.temple_university_EEG_tasks.rst: the compute_stft entry.

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
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 86.66% (48008 / 55400 lines)

Modified file Coverage Lines
pyhealth/datasets/__init__.py 100.00% 82 / 82
pyhealth/datasets/hmc.py 97.62% 41 / 42
pyhealth/datasets/nsrr_sleep.py 100.00% 46 / 46
pyhealth/interpret/__init__.py 100.00% 4 / 4
pyhealth/interpret/uni_acs.py 82.35% 182 / 221
pyhealth/metrics/survival.py 94.25% 82 / 87
pyhealth/models/__init__.py 100.00% 95 / 95
pyhealth/models/chet.py 92.80% 245 / 264
pyhealth/models/clip_stress.py 91.67% 154 / 168
pyhealth/models/clip_stress_layers.py 98.39% 61 / 62
pyhealth/models/clip_stress_utils.py 86.86% 119 / 137
pyhealth/models/delphi.py 95.24% 240 / 252
pyhealth/models/game.py 86.96% 240 / 276
pyhealth/models/generators/tardiff.py 99.60% 251 / 252
pyhealth/models/joint_seq_image.py 99.29% 278 / 280
pyhealth/models/rasc.py 98.63% 72 / 73
pyhealth/models/stagenet_mha.py 94.33% 266 / 282
pyhealth/models/time_series_images.py 100.00% 126 / 126
pyhealth/processors/stagenet_processor.py 90.20% 184 / 204
pyhealth/tasks/__init__.py 98.11% 104 / 106
pyhealth/tasks/delphi_trajectory.py 91.28% 157 / 172
pyhealth/tasks/mortality_prediction.py 90.41% 396 / 438
pyhealth/tasks/physionet2012_mortality.py 96.20% 76 / 79
pyhealth/tasks/ptbxl_heartbeat.py 97.33% 73 / 75
pyhealth/tasks/readmission_prediction.py 94.69% 214 / 226
pyhealth/tasks/sleep_staging_hmc.py 99.06% 105 / 106
pyhealth/tasks/sleep_staging_nsrr.py 96.15% 125 / 130
pyhealth/tasks/temple_university_EEG_tasks.py 83.06% 152 / 183
pyhealth/tasks/value_set_pair_classification.py 98.95% 188 / 190

Workflow run · d762751

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 26, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Previous review

Reviewed commit 7d35640

Comment thread tests/core/test_tuev.py
unittest.main()


class TestEEGEventsTUEVStftRate(unittest.TestCase):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved above the guard in d762751. Running python tests/core/test_tuev.py now runs 16 tests instead of 12.

Comment thread tests/core/test_tuab.py
unittest.main()


class TestEEGAbnormalTUABStftRate(unittest.TestCase):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved above the guard in d762751. Running python tests/core/test_tuab.py now runs 26 tests instead of 22.

Comment thread tests/core/test_tuev.py Outdated
task = EEGEventsTUEV(resample_rate=256, compute_stft=False)
self.assertEqual(task.input_schema, {"signal": "tensor"})

def test_stft_shape_at_200_hz(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

github-actions[bot]
github-actions Bot previously approved these changes Nov 26, 2026

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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).
Comment thread tests/core/test_tuev.py
unittest.main()


class TestEEGEventsTUEVStftRate(unittest.TestCase):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved above the guard in d762751. Running python tests/core/test_tuev.py now runs 16 tests instead of 12.

Comment thread tests/core/test_tuab.py
unittest.main()


class TestEEGAbnormalTUABStftRate(unittest.TestCase):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved above the guard in d762751. Running python tests/core/test_tuab.py now runs 26 tests instead of 22.

Comment thread tests/core/test_tuev.py Outdated
task = EEGEventsTUEV(resample_rate=256, compute_stft=False)
self.assertEqual(task.input_schema, {"signal": "tensor"})

def test_stft_shape_at_200_hz(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@AxelNoun

Copy link
Copy Markdown
Contributor Author

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_rate helper 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=False path, 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.py is 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

@solarsys
solarsys merged commit 94337e6 into sunlabuiuc:master Nov 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should the stft of EEGEventsTUEV and EEGAbnormalTUAB follow resample_rate?

2 participants