Skip to content

CODAP-1461: don't show an LSRL equation when no line can be fit - #2674

Open
kswenson wants to merge 1 commit into
mainfrom
CODAP-1461-lsrl-degenerate-equation
Open

CODAP-1461: don't show an LSRL equation when no line can be fit#2674
kswenson wants to merge 1 commit into
mainfrom
CODAP-1461-lsrl-degenerate-equation

Conversation

@kswenson

Copy link
Copy Markdown
Member

Fixes CODAP-1461

Problem

With a scatter plot whose points are all identical (or vertically aligned) and
the Least Squares Line enabled, the graph showed a bogus equation
x = (not a number) with no line drawn.

Root cause

leastSquaresLinearRegression computes slope = sumOfProductDiffs / xSumSquaredDeviations. When the x values don't vary that is 0/0, so slope,
intercept and r² all come back NaN. The LSRL adornment stores those NaNs, and
its guards test != null — which NaN passes — so the equation string fell into
its vertical-line branch and rendered x = (not a number). The line itself never
appeared because the endpoint code does check for finite values.

Fix

Compute the slope and intercept into locals and return no slope/intercept when
either isn't finite. The existing null checks downstream then correctly treat the
regression as "no line": no line and no equation. The intercept-locked branch
gets the same guard for its own degenerate case (all points at the origin).

Testing

  • New unit tests for leastSquaresLinearRegression covering normal data, fewer
    than two points, identical points, vertically aligned points, horizontally
    aligned points, and both intercept-locked cases.
  • New LSRLAdornmentModel.computeValues tests confirming that a degenerate fit
    yields undefined line values and an invalid stored line.
  • Verified in the browser: before the fix, two identical points reproduce
    x = (not a number); after, no line and no equation. Non-degenerate data still
    renders its line and equation correctly.

🤖 Generated with Claude Code

leastSquaresLinearRegression divided by xSumSquaredDeviations, which is
zero whenever the x values don't vary (e.g. two points with identical
coordinates, or any vertically aligned set). The resulting NaN slope and
intercept passed the adornment's null checks, so the equation rendered as
"x = (not a number)" with no line beside it.

Leave the slope and intercept undefined when they aren't finite so clients
neither draw a line nor show an equation. The intercept-locked branch gets
the same guard for its own degenerate case (all points at the origin).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.69%. Comparing base (ed1154a) to head (739dee8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2674      +/-   ##
==========================================
- Coverage   87.69%   87.69%   -0.01%     
==========================================
  Files         810      810              
  Lines       46933    46938       +5     
  Branches    11562    12013     +451     
==========================================
+ Hits        41160    41162       +2     
- Misses       5759     5760       +1     
- Partials       14       16       +2     
Flag Coverage Δ
cypress 69.50% <100.00%> (-0.01%) ⬇️
jest 62.66% <100.00%> (+<0.01%) ⬆️

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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cypress

cypress Bot commented Jul 26, 2026

Copy link
Copy Markdown

codap-v3    Run #12079

Run Properties:  status check passed Passed #12079  •  git commit 739dee8cab: null
Project codap-v3
Branch Review CODAP-1461-lsrl-degenerate-equation
Run status status check passed Passed #12079
Run duration 09m 05s
Commit git commit 739dee8cab: null
Committer null
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 82
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 382
View all changes introduced in this branch ↗︎

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a degenerate least-squares regression case in the v3 graph (scatter plot) so that when no valid LSRL can be fit (e.g. identical or vertically aligned points), the LSRL adornment no longer shows a bogus equation like x = (not a number).

Changes:

  • Updated leastSquaresLinearRegression() to avoid returning NaN slope/intercept by leaving them undefined when the computed values are not finite.
  • Added unit tests for leastSquaresLinearRegression() covering normal, insufficient, degenerate, and intercept-locked cases.
  • Added LSRLAdornmentModel.computeValues() tests to verify degenerate inputs yield undefined line values and an invalid stored line.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
v3/src/components/graph/utilities/graph-utils.ts Guards regression results so degenerate fits don’t propagate NaN slope/intercept into rendering.
v3/src/components/graph/utilities/graph-utils.test.ts Adds regression unit tests for normal/degenerate/intercept-locked scenarios.
v3/src/components/graph/adornments/lsrl/lsrl-adornment-model.test.ts Adds tests ensuring degenerate LSRL results store as invalid and don’t produce equation/line values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v3/src/components/graph/utilities/graph-utils.ts
@kswenson
kswenson marked this pull request as ready for review July 27, 2026 00:12
@kswenson
kswenson requested a review from bfinzer July 27, 2026 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants