Skip to content

refactor(ml): extract shared sequence_utils from 8 training scripts - #748

Merged
jsboige merged 1 commit into
mainfrom
refactor/ml-deduplicate-sequence-utils
May 5, 2026
Merged

jsboige merged 1 commit into
mainfrom
refactor/ml-deduplicate-sequence-utils

Conversation

@jsboige

@jsboige jsboige commented May 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • Extract build_sequences and normalize_sequences from 8 training scripts into shared sequence_utils.py
  • Unified API supports both seq2one (LSTM/Transformer) and seq2seq (iTransformer/PatchTST/Mamba/MTGNN/STGAT) with backward-compatible defaults
  • 283 lines of duplicated code removed (-283), replaced by single 93-line module

Test plan

  • 13 new tests for sequence_utils.py (build_sequences: 7, normalize_sequences: 6)
  • All 399 existing tests pass (no regressions)
  • All 7 training scripts import successfully with new shared import
  • Verified callers work correctly despite parameter order differences (normalization is symmetric)

🤖 Generated with Claude Code

Deduplicate build_sequences and normalize_sequences (previously copy-pasted
across train_lstm, train_transformer, train_itransformer, train_patchtst,
train_mamba, train_mtgnn, train_stgat, eval_existing_checkpoints).

Unified API supports both seq2one (pred_len=1, scalar targets) and seq2seq
(pred_len>1, multi-step targets), plus optional validation normalization.
All 399 existing tests pass, 13 new tests added for sequence_utils.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

@clusterManager-Myia clusterManager-Myia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

val/test swap bug in normalize_sequences callers

The shared sequence_utils.normalize_sequences signature is:

def normalize_sequences(X_train, X_test, X_val=None)

But all 5 seq2seq scripts call it with (X_train, X_val, X_test):

  • train_mtgnn.py:837 — normalize_sequences(X_train, X_val, X_test)
  • train_stgat.py:761 — normalize_sequences(X_train, X_val, X_test)
  • train_mamba.py:452 — normalize_sequences(X_train, X_val, X_test)
  • train_itransformer.py:764 — normalize_sequences(X_train, X_val, X_test)
  • train_patchtst.py:523 — normalize_sequences(X_train, X_val, X_test)

The return order from the shared function is (X_train_n, X_test_n, X_val_n, mean, std), but these callers unpack as X_train, X_val, X_test, ... — so val and test data get swapped. Evaluation would run on validation data instead of test data.

Fix: Either:

  1. Change all 5 call sites to normalize_sequences(X_train, X_test, X_val) and adjust unpacking to match, OR
  2. Change the shared function signature to match the callers: normalize_sequences(X_train, X_val=None, X_test=None) and adjust the return order to (X_train_n, X_val_n, X_test_n, mean, std)

Option 2 is less disruptive since the return unpacking in the callers already expects val before test.

@jsboige

jsboige commented May 5, 2026

Copy link
Copy Markdown
Owner Author

CORRECTION — Previous CHANGES_REQUESTED was incorrect

After deeper analysis of all 5 seq2seq scripts on main, the normalize_sequences(X_train, X_test, X_val=None) signature with result.insert(2, X_val_n) return pattern is correct — the insert-at-2 compensates for the positional parameter swap.

Trace for caller normalize_sequences(X_train, X_val, X_test):

  1. Function receives: X_test=X_val_data, X_val=X_test_data
  2. result = [X_train_n, (X_val_data-mean)/std, mean, std]
  3. result.insert(2, (X_test_data-mean)/std) → [X_train_n, val_n, test_n, mean, std]
  4. Return unpacked as X_train, X_val, X_test, ... → correct assignment

Verified on all 4 scripts on main (mamba, itransformer, patchtst, stgat) — same pattern, all correct. The shared version in this PR uses identical logic.

Revised verdict: APPROVE. The parameter naming is confusing (X_test param receives val data) but functionally correct.

@clusterManager-Myia clusterManager-Myia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

APPROVE — previous CHANGES_REQUESTED retracted (see correction comment). The normalize_sequences parameter order + insert-at-2 pattern is functionally correct across all 5 callers. Parameter naming is confusing but not a bug.

@jsboige
jsboige merged commit d525fcf into main May 5, 2026
@jsboige jsboige mentioned this pull request May 7, 2026
10 tasks
@jsboige
jsboige deleted the refactor/ml-deduplicate-sequence-utils branch May 10, 2026 11:50
myia-po-2023 pushed a commit that referenced this pull request Aug 15, 2026
…#748)

Window 05-01..05-07 triage (Epic #11044): PR #748's flagged signals were
the retraction itself - 'CORRECTION - Previous CHANGES_REQUESTED was
incorrect... Revised verdict: APPROVE' plus the bot's 'APPROVE -
previous CHANGES_REQUESTED retracted'. 'previous' can only narrate a
past demand, never issue one. Acceptance re-run: 11 FP unflagged, 40
NON-LEVE still flagged, #748 unflagged, FN corpus #594/#590 and the 5
FLAG-OK of the window still flagged, 25/25 tests.

Co-Authored-By: Claude-Code <noreply@anthropic.com>
jsboige added a commit that referenced this pull request Aug 15, 2026
… flag (+9 regression tests) (#11084)

* feat(guard,#11044): recalibrate FP organ — cited verdicts no longer flag as concerns

Measured defect (triage po-2024:CoursIA-2, window 07-14..07-20): 11/64 flagged
signals were positive reviews quoting a verdict they did not emit ("No
CHANGES_REQUESTED", "Verdict : COMMENT_WITHOUT_CONCERNS", "the CHANGES_REQUESTED
reflects a pre-fix state"). An organ crying wolf 17% of the time is an organ
that gets ignored — the exact mechanism by which the 42 real nits slipped
through.

Four calibrated layers, each anchored to named PRs:
- CITERS (occurrence-level citation guard): negation ("No", "Pas de",
  "without"), definite-article narration ("the"), hypothetical modals
  ("would") in the 30 chars before a marker kill that occurrence.
- VERDICT_POSITIVE ("COMMENT_WITHOUT_CONCERNS"): formal verdict overrides
  prose concern counts (#7583/#7593 resolution narrations).
- POSITIVE_MARKERS (soft: "SAFE/Safe to merge", "Verdict : OK"): rescue only
  bodies with no live concern — mixed Hermes reviews keep flagging.
- New CONCERN_MARKERS "SCOPE FLAG"/"scope mismatch": #7298 was caught only by
  accident; the class was invisible. Recovered 7 true positives on 07-18 alone.

Acceptance (falsifiable, per ai-01 Phase 2): the 11 named FPs (#6291 #6699
#6986 #7233 #7243 #7248 #7252 #7284 #7286 #7583 #7593) no longer flag; the 42
NON-LEVE still flag (40 distinct PRs replayed, all >=1 signal kept); founder
#10761 still flags (2 signals). End-to-end audit 07-14..07-20 (saturated 400,
07-18->07-20): 26 -> 25 flagged, -8 FP, +7 genuine SCOPE FLAGs.

Tests: 13 -> 22 (FP classes, mixed-review and emitted-verdict guards).

Co-Authored-By: Claude-Code <noreply@anthropic.com>

* fix(#11045): cover English 'before merge' demand phrasing (2 measured FNs #594/#590)

Window 04-23..04-30 triage (Epic #11044) exposed 2 false negatives: PRs
merged with zero lift while reviews demanded 'issues that should be
addressed before merge' (#594) and 'CRITICAL - Must fix before merge'
(#590). 'avant merge' was already a marker; its English mirror was not.
One marker covers both FNs. Acceptance re-run: 11 FP unflagged, 40
NON-LEVE still flagged, both FN PRs now flag, 24/24 tests.

Co-Authored-By: Claude-Code <noreply@anthropic.com>

* fix(#11045): treat 'previous' as citation word (narrated retraction FP #748)

Window 05-01..05-07 triage (Epic #11044): PR #748's flagged signals were
the retraction itself - 'CORRECTION - Previous CHANGES_REQUESTED was
incorrect... Revised verdict: APPROVE' plus the bot's 'APPROVE -
previous CHANGES_REQUESTED retracted'. 'previous' can only narrate a
past demand, never issue one. Acceptance re-run: 11 FP unflagged, 40
NON-LEVE still flagged, #748 unflagged, FN corpus #594/#590 and the 5
FLAG-OK of the window still flagged, 25/25 tests.

Co-Authored-By: Claude-Code <noreply@anthropic.com>

* feat(nits,#11044): 3 narration citers from window 05-08..05-14 (pas/stale/needs rebase)

18 flags / 249 PRs triaged on #11044. 11 FLAG-OK (0 DEFECT-ALIVE on main),
7 FLAG-FP of which 3 mechanized as CITERS:

- "pas": FP #860 « **COMMENTED** (pas CHANGES_REQUESTED) » — French
  negation without « de » (bare form not covered by "pas de").
- "stale": FP #977 « pending dismissal of stale CHANGES_REQUESTED » —
  re-review request narrating a past reserve.
- "needs rebase": FP #887 « CONFLICTING — needs rebase before merge » —
  procedural demand satisfied by the merge itself (recidive of #729,
  window 05-01..05-07, retroactively covered).

Acceptance: 28 tests pass; harness replays 51 corpus PRs — 11 FP
unflagged, 40 NON-LEVE still flagged.

Co-Authored-By: Claude-Code <noreply@anthropic.com>

* feat(nits,#11044): arrow-citer for conditional verdicts from window 05-15..05-21

8 flags / 194 PRs triaged on #11044. 6 FLAG-OK (1 DEFECT-UNMEASURED
visual QA #1253, rest DEFECT-NONE), 2 FLAG-FP:

- #1247 « Si Static validation rouge → CHANGES_REQUESTED » : conditional
  future verdict. An arrow immediately before a marker is a derivation,
  never an emission — handled in _is_cited (not CITERS) since the arrow
  is not a word.
- #1138 « Awaiting ... review before merge » : procedural note, accepted
  as residual FP (the awaited review flags on its own concerns).

Acceptance: 30 tests pass; harness replays 51 corpus PRs — 11 FP
unflagged, 40 NON-LEVE still flagged.

Co-Authored-By: Claude-Code <noreply@anthropic.com>

---------

Co-authored-by: jsboige <jsboige@gmail.com>
Co-authored-by: Claude-Code <noreply@anthropic.com>
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.

2 participants