Skip to content

fix(metrics): count num_scalings without the off-by-one from shift(-1) - #139

Merged
Anna Pavlenko (apavlen) merged 2 commits into
microsoft:mainfrom
Nanduu24:fix/num-scalings-off-by-one
Sep 24, 2026
Merged

Anna Pavlenko (apavlen) merged 2 commits into
microsoft:mainfrom
Nanduu24:fix/num-scalings-off-by-one

Conversation

@Nanduu24

Copy link
Copy Markdown
Contributor

Fixes #136.

Problem

calculate_metrics in src/vasim/simulator/analysis/plot_utils.py counted scaling events with a backward shift:

num_changes = (merged["CURR_LIMIT"] != merged["CURR_LIMIT"].shift(-1)).sum()

shift(-1) leaves the last element NaN, and NaN != x is always True in pandas, so the final row is always counted as a scaling event — num_scalings is inflated by exactly 1 on every run (a configuration that never scales reports 1).

This isn't only a displayed value: num_scalings feeds ParetoFrontier normalization (num_scalings / num_scalings.max()) and the Pareto ranking, so tuning comparisons between configurations are skewed by it.

Fix

  • plot_utils.py — count against shift() and subtract the single phantom leading change (the first row has no predecessor, so NaN != value is always True). The existing empty-input guard means this never goes negative.
  • ParetoFrontier.preprocess_df — num_scalings_norm was created only inside the num_scalings.max() > 0 guard but used unconditionally on the next line. Today that line is unreachable because of the off-by-one (num_scalings was never 0); fixing the count makes it reachable for any sweep where no configuration scales, which would raise KeyError. The percentile filter is moved inside the guard.

Tests

  • New tests/test_plot_utils_metrics.py with fast, deterministic unit tests: the change-count truth table from the issue, the empty-input guard, and the no-scaling preprocess_df path (regression for the KeyError).
  • Four e2e expectations that hardcoded the inflated counts are decremented by 1 (466→465, 347→346, 109→108, 133→132), each confirmed by running the simulator.

Validation

tests/test_plot_utils_metrics.py .....            # unit tests
tests/test_e2e_single_run_sim.py ..               # updated expectations
tests/test_e2e_multi_run_tune_with_strategy.py ...

black, isort, flake8, and pylint pass on the changed files.

@Nanduu24
Nantha kumar (Nanduu24) requested a review from a team as a code owner September 18, 2026 00:42
calculate_metrics counted CURR_LIMIT changes with shift(-1), which leaves the
last row NaN. Since NaN != x is always True in pandas, the final row was always
counted as a scaling event, inflating num_scalings by exactly 1 on every run.
This skewed ParetoFrontier normalization and ranking, not just the displayed
value. Count against shift() and subtract the single phantom leading change
instead; the empty-input guard above keeps this from going negative.

Fixing the off-by-one also makes a latent crash reachable: preprocess_df built
num_scalings_norm only when num_scalings.max() > 0 but filtered on it
unconditionally, so a sweep where nothing scales (now possible) hit a KeyError.
Move the percentile filter inside the guard.

Add fast unit tests for the count, the empty-input guard, and the no-scaling
preprocess_df path, and correct four e2e expectations that hardcoded the
inflated counts.

Fixes microsoft#136
@codecov-commenter

Codecov Comments Bot (codecov-commenter) commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.28%. Comparing base (5fd115c) to head (fccd26a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #139      +/-   ##
==========================================
+ Coverage   78.19%   78.28%   +0.09%     
==========================================
  Files          23       23              
  Lines        1087     1087              
  Branches      109      109              
==========================================
+ Hits          850      851       +1     
  Misses        187      187              
+ Partials       50       49       -1     
Flag Coverage Δ
unittests 78.28% <100.00%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The localized fixes address both regressions with appropriate unit and end-to-end coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Corrects scaling-event metrics and safely handles configurations with no scaling.

Changes:

  • Fixes the num_scalings off-by-one error.
  • Guards Pareto filtering when all scaling counts are zero.
  • Adds regression coverage and updates end-to-end expectations.
File Description
tests/​test_plot_utils_metrics.py Adds targeted metric and Pareto regression tests.
tests/​test_e2e_single_run_sim.py Corrects single-run scaling expectations.
tests/​test_e2e_multi_run_tune_with_strategy.py Corrects tuning scaling expectations.
src/​vasim/​simulator/​analysis/​plot_utils.py Counts only actual consecutive limit changes.
src/​vasim/​simulator/​analysis/​ParetoFrontier.py Skips scaling normalization filtering when all counts are zero.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Nanduu24

Copy link
Copy Markdown
Contributor Author

Friendly ping — this fixes #136 (a num_scalings off-by-one where shift(-1) leaves the last row NaN, inflating the count by 1 on every run and skewing ParetoFrontier ranking). CI is green across 3.9/3.10 and coverage is up. Happy to adjust anything if a maintainer has feedback. Thanks!

@apavlen
Anna Pavlenko (apavlen) merged commit 07c6cdd into microsoft:main Sep 24, 2026
4 checks passed
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.

[BUG] num_scalings is always overcounted by exactly 1 (shift(-1) makes the last row NaN)

4 participants