Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Padding.pad drops a data element, and the end padding is backwards #265

Closed
marklam opened this issue Apr 27, 2023 · 3 comments
Closed
Labels

Comments

@marklam
Copy link

marklam commented Apr 27, 2023

Describe the bug
FSharp.Stats.Signal.Padding.pad loses the last data point, and the end padding is reversed.

If padding (X,100) .. (Y-1,199); (Y,200) with 3 zero-valued items and a minDistance of 1.0 you should get:

(X-3,0); (X-2,0); (X-1,0); (X,100) ..  (Y-1,199); (Y,200); (Y+1,0); (Y+2,0); (Y+3,0)

but you actually get

(X-3,0); (X-2,0); (X-1,0); (X,100) .. (Y-1,199); (Y+3,0); (Y+2,0); (Y+1,0)

so (Y,200) is lost, and the padding elements at the end are reversed.

To Reproduce

#r "nuget:FSharp.Stats"
#r "nuget:Expecto"

open System
open FSharp.Stats.Signal
open Expecto

let dataLength = 20
let padding = 10

let data =
    Array.init dataLength (
        fun i ->
            (3.0 + float i, 7.0 - float i)
    )

let expectLeadIn  = Array.init padding (fun i -> (3.0 - float (padding-i), 0.0))
let expectLeadOut = Array.init padding (fun i -> (3.0 + float (dataLength + i), 0.0))
let expectedPadded = Array.concat [expectLeadIn; data; expectLeadOut]

let padded = Padding.pad data 1.0 Double.PositiveInfinity (-) (+) padding Padding.BorderPaddingMethod.Zero Padding.InternalPaddingMethod.NaN Padding.HugeGapPaddingMethod.NaN

Expect.equal (Array.sub padded 0 padding) expectLeadIn
Expect.equal (Array.sub padded (padded.Length - padding) padding) expectLeadOut
Expect.equal (Array.sub padded padding data.Length) data "All the original data should be contained in the padded data"
Expect.equal padded.Length (data.Length + 2 * padding) "Length should be the original data length plus padding at each end"
Expect.equal (padded |> Array.sortBy fst) expectedPadded "Result should be the lead-in, whole data, then lead-out (maybe not in order?)"
Expect.equal padded expectedPadded "Result should be the lead-in, whole data, then lead-out"

Expected behavior
The padded data should have all the original data, with the same number of padding elements at each end. The fst of each padding data element should increase by minDistance.

Screenshots
If applicable, add screenshots to help explain your problem.

OS and framework information (please complete the following information):

  • OS: [e.g. Windows 10]
  • OS Version [e.g. 1809]
  • .Net core SDK version [e.g. 2.2.101 ]

Additional context
Add any other context about the problem here.

@marklam marklam added the bug label Apr 27, 2023
@bvenn
Copy link
Member

bvenn commented Apr 27, 2023

Thanks for reporting!:rocket:
I've traced down the issue to the following two lines:

The Array.rev must be removed at:

Array.init borderpadding (fun i ->
let paddX = addToXValue maxX ((float i + 1.) * minDistance)
let paddY = 0.
paddX,paddY)
|> Array.rev

The last point has to be integrated here:

if i = n-1 then
acc
|> List.rev
|> List.concat

After these modifications the result looks as follows:

//#r "nuget:FSharp.Stats"
#r @"C:\Users\bvenn\source\repos\FSharp.Stats\src\FSharp.Stats\bin\Release\netstandard2.0\FSharp.Stats.dll"
#r "nuget:Expecto"
#r "nuget: Plotly.NET"

open Plotly.NET

open System
open FSharp.Stats.Signal
open Expecto

let dataLength = 20
let padding = 10

let data =
    Array.init dataLength (
        fun i ->
            (3.0 + float i, 7.0 - float i)
    )


let expectLeadIn  = Array.init padding (fun i -> (3.0 - float (padding-i), 0.0))
let expectLeadOut = Array.init padding (fun i -> (3.0 + float (dataLength + i), 0.0))
let expectedPadded = Array.concat [expectLeadIn; data; expectLeadOut]

let padded = 
    Padding.pad data 1.0 Double.PositiveInfinity (-) (+) padding Padding.BorderPaddingMethod.Zero Padding.InternalPaddingMethod.NaN Padding.HugeGapPaddingMethod.NaN

let indices = Array.init padded.Length (fun x -> string x)

[
Chart.Point(data,Name="raw data")           |> Chart.withMarkerStyle (Size=10)
Chart.Point(expectedPadded,Name="expected") |> Chart.withMarkerStyle (Size=8)
Chart.Point(padded,Name="actual",MultiText=indices,TextPosition=StyleParam.TextPosition.TopRight)           |> Chart.withMarkerStyle (Size=6)
]
|> Chart.combine
|> Chart.show

image

@marklam, if it is ok for you I would integrate your tests into the test project. Of course you can add them yourself and file a PR if you want to.

@marklam
Copy link
Author

marklam commented Apr 27, 2023

@bvenn I'm more than happy for you to integrate those tests. Thanks for responding so quickly!

bvenn pushed a commit that referenced this issue Apr 28, 2023
bvenn pushed a commit that referenced this issue Apr 28, 2023
@bvenn
Copy link
Member

bvenn commented Apr 28, 2023

closed by 153d95e

@bvenn bvenn closed this as completed Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants