Coder Social home page Coder Social logo

aleksandrbazhin / ellipsoid_fit_python Goto Github PK

View Code? Open in Web Editor NEW
75.0 75.0 20.0 299 KB

Find ellipsoid fit for arbitrary data with python/numpy, plot it or write to file.

License: MIT License

Python 100.00%
ellipsoid-fit-python least-squares plotting python

ellipsoid_fit_python's People

Contributors

aleksandrbazhin avatar georacer avatar vrbadev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ellipsoid_fit_python's Issues

Value Error:

Hi,

I found this code recently and wanted to fit an ellipsoid to a set of data point which I have. I got the following error while running the plot_ellipsoid.py code;
python plot_ellipsoid.py
Traceback (most recent call last):
File "plot_ellipsoid.py", line 12, in
center, radii, evecs, v = ellipsoid_fit(data2)
ValueError: too many values to unpac

Also, this error occurred when I tried to run the code with the default values using the same textfile (mag_out.txt)

Data not being plotted/fitted correctly?

Hi,

The code doesn't seem to produce the right result or maybe I'm looking at it the wrong way. I have attached a plot of the result.

chk1

The data seems to plot an oblate spheroid but the fit model (the orange plot) shows a prolate spheroid. Why is that? Is it because it is reading the wrong coordinates maybe?

coordextract1_01259.txt

I have attached the coordinate text file as well. Please let me know if you get the same result or not.

Thank you
Robin

Plotting does not work with newer Matplotlib.

Running plot_ellipsoid.py causes

Traceback (most recent call last):
  File "plot_ellipsoid.py", line 29, in <module>
    ax.set_aspect('equal')
  File "C:\Python37\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 324, in set_aspect
    "Axes3D currently only supports the aspect argument "

Tested on Python 3.7.3 and Matplotlib 3.3.3
Everything works with Python 3.5.3 and Matplotlib 2.2.0.

Uncertanty estimation

Hi! Thank you a lot for your code, it has been really helpful!
Do you have any idea on how to extract the uncertainty of the fit?

Best regard!

Rotation in ellipsoid_plot function is incorrect.

Assuming rotation parameter of the ellipsoid_plot function is meant to take the evecs matrix (as is the case in plot_ellipsoid.py), then in ellipsoid_fit.ellipsoid_plot (line 87 of ellipsoid_fit.py), the rotation matrix needs to be inverted.

ie. Line 87 should be:
[x[i, j], y[i, j], z[i, j]] = np.dot(rotation,[x[i, j], y[i, j], z[i, j]]) + center

Calculating transformation matrix

Hi!

First, thank you for the great implementation.

I would like to ask, what is happening here:

a, b, c = radii
r = (a * b * c) ** (1. / 3.)
D = np.array([[r/a, 0., 0.], [0., r/b, 0.], [0., 0., r/c]])
#http://www.cs.brandeis.edu/~cs155/Lecture_07_6.pdf
#affine transformation from ellipsoid to sphere (translation excluded)
TR = evecs.dot(D).dot(evecs.T)

I can not find in the original matlab code this part. Can you explain please what this do ? :)

Using data from get_calibration_ellipsoid.py

Hello.

First thank you for the great work. It's been a while past my university graduation and I'm having issues with the matrices and all the math stuff. Anyway I?m trying to make sense of all of it.

One dumb question. Once I generate the ellipsoid parameters with get_calibration_ellipsoid.py, how do I then apply those parameters to real-time incoming data from a sensor? Can you add that example too?

Thanks, Marko

Why you may get unpredictable results.

If you are trying to fit an ellipsoid to arbitrary set of data points, you may and most likely will get unpredictable results. Least square method is supposed to be used on data, which is close to ellipse, like magnetometer calibration data. There is a lot of possibilities to use it with any data, but you may have first to refine it. Some times you may want a minimal containing ellipsoid, sometimes you may use a convex hull first and then fit an ellipsoid.

I'll try to explain on fitting a circle on a plain.

Let say you want to fit a circle to 9 points like that (consider center points to be very close to zero).
Points

If you expect to get a "reasonable" result like
Circle 1
The sum of square distances from point to circle is 1^2*5 = 5
To get such a circle, you first have to throw away center points by constructing convex hull, or just directly find minimal enclosing ellipsoid, instead of using this repository.

Maybe you think outer point are outliers and expect a result like
Circle 2
The sum of squares is 1^2*4 = 4
Then you need to preprocess data by throwing away outliers by any empirical criteria for example

Next you will think that using ellipsoid and LSM, like here, will give you something like that
Circle 3
To find optimal radius we need to minimize 4*(1-x)^2 + 5*x^2. The resulting radius is 4/9 and the sum is 20/9 which is 2.22... To get something like this you would need to fix the center of the ellipse, but the purpose of the code in this repository is to find the offset of a fitted ellipse as well as it's parameters. (I think, it can be added as an option, though)

Well you won't get this. Which you'll get is a circle close to circle through 3 points
Circle 4
The sum here is 1^2*2 = 2, which is already the least of all other variants.

But if we move it a little bit to the two points, which are not on the circle, we will get even smaller sum of squares. And also with symmetrical data like in this explanation, the circle is undefined, since it can be placed in any of 4 directions. So a tiny perturbation in any point's position will cause the fitted ellipse to move by a large distance. And in 3D space it is much harder to predict the results.

I hope this explanation helps anyone looking for a silver bullet for their search for optimal ellipsoids.

Convert to python3

It is easy to convert this to python3.

Just replacing the print calls to print() calls in get_calibration_ellipsoid.py and replacing the xrange calls with range calls in ellipsoid_fit.py.

Unexpected results on sets of points

Hi @aleksandrbazhin,

I am interested on using your code for some applications on molecular characterization. However, when I compute the ellipsoid for certain point sets I find unexpected results. See below the screenshots of the outcome.

Set1
Set2

I also find the following warning when running over the 1st set:

plot_ellipsoid.py:19: RuntimeWarning: invalid value encountered in double_scalars
  r = (a * b * c) ** (1. / 3.)

Could you please assist? I can share the data if needed.

Best,
Ismael.

Wrong return function

Hi there,

The return of the "ellipsoid_fit.py" is "return center, radii, evecs, v, chi2" but the "plot_ellipsoid.py" is asking for "center, evecs, radii = ellipsoid_fit(data2)".
So I guess it should be changed to "center, radii, evecs, v, chai = ellipsoid_fit(data2)".

Cheers,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.