question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interpolation of more then 160 points triggers exception

See original GitHub issue

Bug Report

Issue: When interpolating data arrays with more then 160 elements using PeriodicSpline or EndSlopeSpline a System.InvalidOperationException is triggered. System.InvalidOperationException HResult=0x80131509 Message=Operation is not valid due to the current state of the object. Source=ScottPlot StackTrace: at ScottPlot.Statistics.Interpolation.PeriodicSpline.CalcParameters() at ScottPlot.Statistics.Interpolation.PeriodicSpline…ctor(Double[] xs, Double[] ys, Int32 resolution) at Chart_Test.MainWindow…ctor() in C:\Users\hoot\source\repos\Chart_Test\Chart_Test\MainWindow.xaml.cs:line 35

Reproducing: Generate an array of 161 data points and interpolate using var esi = new PeriodicSpline(dataX, dataY, resolution: 10); or var esi = new EndSlopeSpline(dataX, dataY, resolution: 10);. This issue can also reproduced with NaturalSpline but the maximum length of the array is higher. I have tested 2048 data points and it triggers the same exception.

double[] dataX = new double[161];
double[] dataY = new double[161];
Random random = new Random();
for (int i = 0; i < 161; i++) {
    dataX[i] = -85 + i * 11.43;
    dataY[i] = random.Next(-30, -20);
}

var esi = new PeriodicSpline(dataX, dataY, resolution: 10);
ScatterPlot scatterPlot = new ScatterPlot(dataX, dataY);
ScatterPlot scatterPlotInterpolated = new ScatterPlot(esi.interpolatedXs, esi.interpolatedYs);
scatterPlot.MarkerSize = 0;
scatterPlotInterpolated.MarkerSize = 0;
scatterPlot.Color = System.Drawing.Color.Blue;
scatterPlotInterpolated.Color = System.Drawing.Color.Red;
WpfPlot1.Plot.Add(scatterPlot);
WpfPlot1.Plot.Add(scatterPlotInterpolated);
WpfPlot1.Plot.SetAxisLimitsX(-85, -85 + 11.43 * 4);
WpfPlot1.Plot.SetAxisLimitsY(-110, -20);
WpfPlot1.Refresh();

System Details

  • ScottPlot Version: 4.1.127
  • Operating System: Windows 11
  • Application Type: WPF
  • .NET Version: NET Framework 4.6.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
swhardencommented, Jan 23, 2022

An improved interpolation module will be available in the next release! 👍 🚀

In the mean time you can use the code from this web page to help you smoothly interpolate X/Y data

https://swharden.com/blog/2022-01-22-spline-interpolation/

spline-interpolation

1reaction
swhardencommented, Jan 19, 2022

Restating the problem, it’s not the number of points, but rather something about your original dataset. Extremely sharp changes in slope really confuse this interpolation engine.

No worries - I have a better cubic interpolator I’ll upload in a few minutes!

Test Setup

(double[] xs, double[] ys) = GetSharpData(161);

ScottPlot.Plot plt = new();
var psi = new ScottPlot.Statistics.Interpolation.PeriodicSpline(xs, ys, resolution: 20);

plt.AddScatterPoints(psi.givenXs, psi.givenYs, label: "original");
plt.AddScatterLines(psi.interpolatedXs, psi.interpolatedYs, label: "interpolation");
plt.Legend();

Sharp Data

This throws an exception

private (double[] xs, double[] ys) GetSharpData(int count)
{
    // this dataset has been reported to cause problems
    // https://github.com/ScottPlot/ScottPlot/issues/1433
    Random rand = new(0);
    double[] xs = new double[count];
    double[] ys = new double[count];
    for (int i = 0; i < count; i++)
    {
        xs[i] = -85 + i * 11.43;
        ys[i] = rand.Next(-30, -20);
    }
    return (xs, ys);
}
  Message: 
System.InvalidOperationException : Operation is not valid due to the current state of the object.

  Stack Trace: 
PeriodicSpline.CalcParameters() line 75
PeriodicSpline.ctor(Double[] xs, Double[] ys, Int32 resolution) line 27
Interpolation.Test_Interpolation_ProblematicData() line 68

Smoother Data

This doesn’t crash… but the interpolated spline is obviously wrong ☠️

image

private (double[] xs, double[] ys) GetSmoothData(int count)
{
    Random rand = new(0);
    double[] xs = ScottPlot.DataGen.RandomWalk(rand, count);
    double[] ys = ScottPlot.DataGen.RandomWalk(rand, count);
    return (xs, ys);
}

Conclusion

I think it’s not the number of points, it’s something about the interpolation calculation that’s crashing on this particular dataset. Also the math doesn’t seem to be right in the existing module.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interpolation Module · Issue #459 · ScottPlot ...
ScottPlot could benefit from an interpolation module. Interpolation can take a small ... Interpolation of more then 160 points triggers exception #1433.
Read more >
Rational interpolation - C++, C#, Java library
Bulirsch-Stoer algorithm constructs the rational function by having numerator and denominator degrees equal to N/2 (you can find a detailed description in ...
Read more >
Machine Learning Methods for Spatial Interpolation of Wind
In this study, two popular machine learning approaches and a number of common simple spatial interpolation techniques are applied to spatial estimation of ......
Read more >
Bias Characterization, Vertical Interpolation, and Horizontal ...
First, we evaluate 50 combinations. (10 variants of horizontal interpolation and 5 selected best-performing vertical interpolation techniques) and we then ...
Read more >
(PDF) The Generalized Interpolation Material Point Method
The Material Point Method (MPM) discrete solution procedure for computational solid mechanics is generalized using a variational form and a Petrov- Galerkin ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found