aleksandrbazhin / ellipsoid_fit_python Goto Github PK
View Code? Open in Web Editor NEWFind ellipsoid fit for arbitrary data with python/numpy, plot it or write to file.
License: MIT License
Find ellipsoid fit for arbitrary data with python/numpy, plot it or write to file.
License: MIT License
Do you have a plan to publish ellipsoid_fit_python in PIP package repository? For example in PIP already available package for ellipse fitting (https://github.com/bdhammel/least-squares-ellipse-fitting).
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.
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?
I have attached the coordinate text file as well. Please let me know if you get the same result or not.
Thank you
Robin
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 ? :)
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.
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.
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)
Thank you for your work! I tried the code and just want to know how to calculate the rotation angle from the rotation matrix of the ellipsoid obtained from the program?
Hi aleks,
First, thanks for porting the matlab function to python!
I tried fitting the test data using this python port and comparing it with the matlab function by Yury v1.3.0.0 (https://de.mathworks.com/matlabcentral/fileexchange/24693-ellipsoid-fit). However, I get a different fit using Python compared to matlab.
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,
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
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.
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.
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!
Hi @aleksandrbazhin ,
I am interested about this package. Is it possible to use 'ellipsoid_fit_python' to calibrate a static accelerometer ? I am trying to find some useful method to calibrate an accelerometer or a Magnetic sensor.
Thanks!!
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
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).
If you expect to get a "reasonable" result like
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
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
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
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.