Skip to content

Size TUEV event windows with round() so float resample rates work - #1600

Merged
solarsys merged 2 commits into
sunlabuiuc:masterfrom
AxelNoun:fix/tuev-float-resample-rate
Nov 27, 2026
Merged

solarsys merged 2 commits into
sunlabuiuc:masterfrom
AxelNoun:fix/tuev-float-resample-rate

Conversation

@AxelNoun

Copy link
Copy Markdown
Contributor

Contributor: Axel Coiffard-Donaty (@AxelNoun)

Contribution Type: Bug fix

Fixes #1522.

Problem

EEGEventsTUEV.BuildEvents sized its buffer with int(fs) * 5 and sliced the signal from start - 2*int(fs) to end + 2*int(fs). At a non-integer resample_rate (documented as a float), the two lengths differ and the assignment raises a broadcast ValueError. BuildEvents runs outside the try in __call__, so the error stops set_task.

Change

As agreed in the issue (option 1), and like #1474 did for TUAB with round(10 * fs):

  • each window now holds round(5 * fs) samples, starting round(2 * fs) samples before the event onset;
  • the docstring describes the window at non-integer rates (it previously said the rate had to be a whole number).

At an integer rate the windows are the same as before: a TUEV event lasts 1 s, so [start - 2 fs, end + 2 fs) and [start - 2 fs, start - 2 fs + 5 fs) are the same slice. A test pins this.

Tests

New TestEEGEventsTUEVFloatResampleRate in tests/core/test_tuev.py:

  • 200.5, 256.5 and 250.25 Hz give (16, round(5 * fs)) windows;
  • the window starts 2 s before the event (200, 200.5, 256.5 Hz);
  • at 200 Hz the output equals the previous slicing;
  • __call__ runs end to end at 256.5 Hz.

Six of these checks fail on master and pass with the change. tests/core/test_tuev.py and tests/core/test_tuab.py: 38 passed. python tools/check_pr_rules.py --base master --head HEAD passes.

This branch and the one for #1521 touch the same file in different places; I checked that they merge together cleanly (46 tests pass on the merge).

Files to Review

  • pyhealth/tasks/temple_university_EEG_tasks.py: EEGEventsTUEV.BuildEvents and the class docstring.
  • tests/core/test_tuev.py: the new test class.

BuildEvents sized its buffer with int(fs) * 5 and sliced from
start - 2*int(fs) to end + 2*int(fs). At a non-integer rate the two lengths
differ and the assignment raised a broadcast ValueError, which stopped
set_task because BuildEvents runs outside the try in __call__.

The window is now round(5 * fs) samples starting round(2 * fs) samples
before the event onset, as sunlabuiuc#1474 did with round(10 * fs) for TUAB. At an
integer rate the windows are unchanged.

Fixes sunlabuiuc#1522
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 86.66% (48004 / 55396 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 82.68% 148 / 179
pyhealth/tasks/value_set_pair_classification.py 98.95% 188 / 190

Workflow run · 7389ffa

@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 b07b1e7

chan = int(EventData[i, 0])
start = np.where((times) >= EventData[i, 1])[0][0]
end = np.where((times) >= EventData[i, 2])[0][0]
first = offset + start - pre

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.

Done in 7389ffa. The docstring now says that the window length is fixed and that the end time in the .rec file is not used. test_window_length_does_not_depend_on_the_event_end pins this on a 2 s event; it fails on master, where that event raised the broadcast ValueError.

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. Sizing the TUEV event windows with round() stops BuildEvents from crashing at non-integer resample rates, and it matches the approach used for TUAB.

The change looks ready to merge. The window is round(5 * fs) samples, starting round(2 * fs) samples before the onset. At integer rates it gives the same slice as before for 1 s events, and a test pins that. The new tests cover several float rates, the 2 s pre-event offset, and an end-to-end call run. The docstring matches the code. Coverage is 82.68% for the modified file and 86.23% for the whole repository, which meets the bar.

Optional suggestions:

  • Events whose duration is not 1 s used to raise a broadcast ValueError. They now silently produce a fixed 5 s window around the onset. A sentence in the description or docstring about this would make the behaviour change explicit.

Reviewed commit b07b1e7

The window is round(5 * fs) samples from 2 s before the onset, so the end
time in the .rec file is no longer used. Say so in the docstring and pin it
with a test on a 2 s event, which the old slicing rejected with a broadcast
ValueError.

Nested with statements in the tests of this PR are merged into one (ruff SIM117).
chan = int(EventData[i, 0])
start = np.where((times) >= EventData[i, 1])[0][0]
end = np.where((times) >= EventData[i, 2])[0][0]
first = offset + start - pre

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.

Done in 7389ffa. The docstring now says that the window length is fixed and that the end time in the .rec file is not used. test_window_length_does_not_depend_on_the_event_end pins this on a 2 s event; it fails on master, where that event raised the broadcast ValueError.

@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.

Reviewed commit 7389ffa

chan = int(EventData[i, 0])
start = np.where((times) >= EventData[i, 1])[0][0]
end = np.where((times) >= EventData[i, 2])[0][0]
first = offset + start - pre

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Event duration is no longer checked; non-1 s events are silently windowed

Before this change, an event whose .rec end time was not exactly one resample period after its onset made the slice length differ from the buffer, so BuildEvents raised a ValueError. The new code always takes a fixed window from the onset and ignores EventData[i, 2], so such events are now accepted without any warning, and the label may refer to a span the window does not contain. The docstring says the end time is not used, and BIOT does the same, so this is a conscious choice. Consider either logging a warning when the .rec duration differs from 1 s, or stating in the PR description that TUEV events are assumed to be 1 s long.

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.

Left as is. The previous code accepted only 1 s events: any other duration raised the broadcast ValueError and stopped set_task, so no TUEV run that worked before had such an event. The fixed 5 s window from the onset is the option agreed in #1522, and the docstring states that the end time is not used.

@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. Sizing the TUEV event windows with round() makes EEGEventsTUEV.BuildEvents work at non-integer resample rates, and it matches what #1474 did for TUAB.

The change looks ready to merge.

  • Scope: It is confined to the self-contained EEGEventsTUEV task. It doesn't touch base classes or shared utilities, and it adds no links between components.
  • Behavior: At integer rates the window is the same as the old slice for 1 s events. A test pins this.
  • Tests: The new tests cover non-integer rates, where the window starts, a 2 s event, and __call__ end to end at 256.5 Hz.
  • Coverage: The whole repository is at 86.66%, above the 60% bar. temple_university_EEG_tasks.py is at 82.68%, above the 80% bar.
  • Docs: The docstring matches the new code.

Optional suggestions:

  • Events longer or shorter than 1 s used to raise an error at integer rates. They are now accepted silently, with a fixed 5 s window. The docstring says so. You could also log a warning when the event duration isn't about 1 s.

Reviewed commit 7389ffa

@solarsys
solarsys merged commit c0f7988 into sunlabuiuc:master Nov 27, 2026
2 checks passed
@AxelNoun
AxelNoun deleted the fix/tuev-float-resample-rate branch November 27, 2026 06:04
NathanCYee pushed a commit to NathanCYee/PyHealth that referenced this pull request Nov 28, 2026
…nlabuiuc#1600)

* Size TUEV event windows with round() so float resample rates work

BuildEvents sized its buffer with int(fs) * 5 and sliced from
start - 2*int(fs) to end + 2*int(fs). At a non-integer rate the two lengths
differ and the assignment raised a broadcast ValueError, which stopped
set_task because BuildEvents runs outside the try in __call__.

The window is now round(5 * fs) samples starting round(2 * fs) samples
before the event onset, as sunlabuiuc#1474 did with round(10 * fs) for TUAB. At an
integer rate the windows are unchanged.

Fixes sunlabuiuc#1522

* Document that TUEV windows ignore the event end time

The window is round(5 * fs) samples from 2 s before the onset, so the end
time in the .rec file is no longer used. Say so in the docstring and pin it
with a test on a 2 s event, which the old slicing rejected with a broadcast
ValueError.

Nested with statements in the tests of this PR are merged into one (ruff SIM117).
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.

EEGEventsTUEV raises a ValueError when resample_rate is not an integer

2 participants