kwolo.ReSample 1.0.1

dotnet add package kwolo.ReSample --version 1.0.1                
NuGet\Install-Package kwolo.ReSample -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="kwolo.ReSample" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add kwolo.ReSample --version 1.0.1                
#r "nuget: kwolo.ReSample, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install kwolo.ReSample as a Cake Addin
#addin nuget:?package=kwolo.ReSample&version=1.0.1

// Install kwolo.ReSample as a Cake Tool
#tool nuget:?package=kwolo.ReSample&version=1.0.1                

Data Interpolation utility for C# .Net

Attribution

Based with thanks on Ashley Davis' sample project at https://www.codeproject.com/Tips/760099/Resampling-and-merging-time-series-data-using-LINQ

See the project site at https://gitlab.com/skotl/datainterpolation for more information.

Purpose

When dealing with a large quantity of time-based metrics, especially when there may be gaps in the data, it is often useful to resample and aggregate that data against a specific time period (for example averaged on a per-day or per-month basis).

My particular use case was a sequence of price change data that is totally dependent on the vendor for any given item so, if I wanted to graph these price changes, for some items there might be 3 in a two year period but for others there are literally thousands of changes.

So rather than make a clumsy, spider graph (which correspondingly takes more compute power to render) I looked for a solution to aggregate that data on a per-month basis. This led me to Ashley's concise and easy to use article at CodeProject.

Usage

This section only shows some basic usage, I'd refer you to the original article at https://www.codeproject.com/Tips/760099/Resampling-and-merging-time-series-data-using-LINQ for a more in-depth explanation.

In my example project there is a CSV file that contains price change data in the format price-change-date, new price: (sample)

2023-02-25 10:00:00.7702865,274.97
2023-02-26 05:30:00.7374996,275.48
2023-02-27 11:30:00.7925322,219.99
2023-04-18 16:00:00.7151256,335.98
2023-05-11 04:30:00.8216993,223.73
2023-05-31 11:30:00.7398993,231.55
2023-08-10 18:36:42.4786804,180.00
...etc...

These are read into an ordered list of DataPoints:

public class DataPoint
{
    public DateTime DateTime { get; set; }
    public double Value { get; set; }
}

From here, I determine the start and end dates from the list:

var startDate = data.Min(dp => dp.DateTime);
var endDate = data.Max(dp => dp.DateTime);

And set the interval to be each 30 days:

var interval = TimeSpan.FromDays(30);

Create a function that can calculate the linear interpolation of two values. In my example, the DataPoint.Value is of type double so the Lerp() method works with doubles - add your own method depending on your data point value types:

private static double Lerp(double v1, double v2, double t)
{
    return v1 + ((v2 - v1) * t);
}

Finally, call the Resample() extension on your list, passing it the start and end dates, the interval, the dataSelector (the property of your data class that represents the date) and the call to your Lerp() function:

var resampled =
    data
        .Resample(startDate, endDate, interval,
            dateSelector: _ => _.DateTime,
            interpolator: (date, r1, r2, t) =>
                new DataPoint(date, Lerp(r1.Value, r2.Value, t))
        )
        .ToList();
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 168 8/17/2023