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

[Feature Request] Surface fitting for simple data, and example that shows the surface in plotly #309

Open
7sharp9 opened this issue Oct 5, 2023 · 5 comments

Comments

@7sharp9
Copy link

7sharp9 commented Oct 5, 2023

I want to produce a surface from a combination of data currently in 3 axis, I am using matlab to produce a thin plate spline model and also poly44 and wondered if there are any options to do this with this lib?

Describe the solution you'd like
I would like a sample that generates a surface fit for data in 3 axis and produces a Plotly ploy showing the surface and the original data scattered on top.

Describe alternatives you've considered
Matlab/Python examples are easy to google but I can include one if needed.

@bvenn
Copy link
Member

bvenn commented Oct 5, 2023

Do you just want to have some kind of regression or just a smoothed interpolation of each point?
Currently FSharp.Stats is capable of smoothing two-dimensional data only, but if you just need and interpolation and the visualization to be smooth, you can just use a Plotly surface plot. The smooting is performed automatically and may even be adjustable (https://plotly.net/reference/plotly-net-chart3d-chart.html#Surface).

An example can be seen here or here.

@7sharp9
Copy link
Author

7sharp9 commented Oct 8, 2023

Hi, thanks for the reply, to be honest just looking for any particular solutions, I have a certain amount of data which I want tto plot a surface for, the surface points should not stray away from the data set I have to interpolation like the thin plate spline seem to match my data the best but Ive also managed to replicate in 2d each dimension using tanh or arctan curves. So a F# solution of any sort would suffice for me. F# is th eeasy bits its just figuring out how from samples really.

@7sharp9
Copy link
Author

7sharp9 commented Oct 8, 2023

If regression is a really close match to my input data I could use that too, but only if the surface is only ever higher than my plotted points, and not by very much.

@kMutagene
Copy link
Member

@7sharp9 if you just want to visualize your data and do not care about the fit function that creates the surface, you can just use Chart.Surface from Plotly.NET as @bvenn suggested. However i think if you are interested in the coefficients that fit the surface, we'd need an equivalent of numpy.polyfit which to my knowledge is not implemented in FSharp.Stats.

As i do not know how your data looks like, here is a example that contains some boilerplate to generate some 2D data that also visualizes the points from the input across the surface:

//---------------------- Generate linearly spaced vector ----------------------
let linspace (min, max, n) =
    if n <= 2 then
        failwithf "n needs to be larger then 2"

    let bw = float (max - min) / (float n - 1.)
    Array.init n (fun i -> min + (bw * float i))

//---------------------- Create example data ----------------------
let size = 100

let x = linspace (-2. * Math.PI, 2. * Math.PI, size) // 100 evenly spaced samples between -2π and 2π
let y = linspace (-2. * Math.PI, 2. * Math.PI, size) // 100 evenly spaced samples between -2π and 2π

let f x y = -(5. * x / (x ** 2. + y ** 2. + 1.)) // function to generate z based on x and y: f(x,y) = -(5 * x / (x^2 + y^2 + 1))

let xyz = Array.init size (fun i -> Array.init size (fun j -> x.[j], y.[i], f x.[j] y.[i])) |> Array.concat // generate x,y,z triplets for point plot
let zArray = Array.init size (fun i -> Array.init size (fun j -> f x.[j] y.[i])) // generate z values for surface plot

[
    Chart.Point3D(xyz, Marker = Marker.init(Size = 1, Color = Color.fromString "blue"))
    Chart.Surface(zData = z, X = x, Y = y)
]
|> Chart.combine

image

@7sharp9
Copy link
Author

7sharp9 commented Oct 31, 2023

Thanks @kMutagene I'm a little slow to respond but thanks for that, I've ended up using Matlab for a lot of my stuff as it has been quick to generate. I'm mainly using a scatter plot of data and then using thin plate interpolation to produce a surface. The surface is then used to create increments over the scatter data, which will be used in C++. I'm getting Matlab to produce C++ code of the interpolated data points. If I had enough time I would try to implement this in F# but its tricky finding the time!

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

No branches or pull requests

3 participants