Skip to content

Truncate StageNet nested visits longer than the fitted width - #1380

Open
Mahdi-Hasan wants to merge 2 commits into
sunlabuiuc:masterfrom
Mahdi-Hasan:fix/stagenet-truncate-wide-visits
Open

Mahdi-Hasan wants to merge 2 commits into
sunlabuiuc:masterfrom
Mahdi-Hasan:fix/stagenet-truncate-wide-visits

Conversation

@Mahdi-Hasan

@Mahdi-Hasan Mahdi-Hasan commented Nov 4, 2026 •

Copy link
Copy Markdown

Contributor: Mehedi Hasan (@Mahdi-Hasan)

Contribution Type: Bug fix

Description:

StageNetProcessor pads each nested visit to the width seen in fit(), but it never truncates a longer visit. Two symptoms follow when the processor is fitted on the training split:

  • process() raises expected sequence of length ... for a sample that mixes visit widths.
  • process() returns a wider tensor for a one-visit sample, and collation fails later.

The fix applies the rule from #1263 to the nested processors. Visits longer than the fitted width keep their first codes, and each processor logs one warning. The existing padding argument still widens the fitted width.

No existing issue covers this. Searched stagenet truncate and truncat in issues and open PRs, with no matches.

Files to Review:

  • pyhealth/processors/stagenet_processor.py - truncation in _encode_nested_codes, docstring updates, import cleanup
  • tests/core/test_nested_processor_truncation.py - TestStageNetTruncation (4 cases, synthetic data)

Tests:

  • tests/core/test_nested_processor_truncation.py: 11 passed
  • tests/core/test_stagenet_processor.py, test_stagenet.py, test_vocab_processors.py, plus the truncation file: 64 passed
  • tools/check_pr_rules.py: passed

Mahdi-Hasan and others added 2 commits November 4, 2026 14:21
StageNetProcessor padded each visit to the width seen in fit() but never
truncated a longer one. When the processor is fitted on the training split,
a test visit with more codes than any training visit made the rows ragged:
process() raised "expected sequence of length ..." for a sample with mixed
widths, and returned a wider tensor that failed later at collation for a
sample with one visit.

Apply the same rule sunlabuiuc#1263 added to the nested processors: keep the first
codes up to the fitted width and log one warning per processor. The
existing padding argument still widens the fitted width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fold the _warn_truncated import into the existing base_processor import
and remove FeatureProcessor, which the module never used.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
@Mahdi-Hasan
Mahdi-Hasan marked this pull request as ready for review November 4, 2026 12:03

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant