-
Notifications
You must be signed in to change notification settings - Fork 74
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
Implemented directional variograms for unstructured
#87
Implemented directional variograms for unstructured
#87
Conversation
Hey @TobiasGlaubach |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for this PR! - I like the very straightforward way of enhancing the variogram estimation and the fact that the interface doesn't break backwards compatibility.
I've added some change requests into the code. Apart from those, here are a few more comments:
- How did you come up with the default value of the
angle_tol = 5°
? I imagine that's a good value for numerical experiments, but in case of actual measurements, it might be far too small. I have no clue what a good default value would be, but I agree, that we should have one :-) - I think some tests for the 3d case should definitely be added.
- I'd like to see some test cases, where the field is more "spread out", but still with very few data points. Then you can test the angle tolerances better and actually exclude some, but not all points from the variogram estimation.
- And the most important thing: Please add yourself to the
AUTHORS.md
in the main directory. Of course only if you want to.
Regarding default angle: regarding additional testcases: regarding AUTHORS.md: |
default angles: additional testcases:
I think when these test cases have been added, we are good to go. |
Hi, @MuellerSeb @LSchueler Most tools give a bin tolerance for overlapping lags/bins for estimating unstructured variograms. GSTools does not seem to have this, what do you think? Should we implement this? |
@MuellerSeb : I checked the covariance model angles and if I checked correctly found them to be deviating from the "normal" right hand side rule (standard rotation matrices) which also align spherical coordinate definitions and standard intrinsic Tait-Brian-Angles. Usually, if the Z+ axis points upwards, and a 'standard' rotation is used, the rotation is counter clockwise. This leads to points on the XY plane rotating from X axis towards Y axis. Same goes for the Tait-Brian-Angles. See here: Taitbrianangles.svg from wikipedia However the definition of the Covariance model rotates the Variogram in clockwise and the major axis aligns with Y. I generated a model like this: model = gs.Gaussian(dim=3, var=2.0, len_scale=[10, 2], angles=[np.deg2rad(azim), np.deg2rad(0)]) And plotted is in 3D as point cloud for azim in [0,10,50,80] and found that:
For the elevarion angle I found that it is also clockwise rotated |
@TobiasGlaubach : import numpy as np
import gstools as gs
model = gs.Gaussian(dim=3, var=2.0, len_scale=[8, 4, 2], angles=[0, np.pi/4, 0])
model.plot("vario_spatial") I found, that the rotation in the GSTools/gstools/tools/geometric.py Line 59 in 2c26d74
|
But following this: it should be correct the way it is! |
This example also shows, that the rotation is counter-clockwise: |
This example is 2D not 3D. Can there be a difference? |
Let me check the rotations from Wikipedia and the implementation in your code. |
Then have a look at this: There you can check, that rotation in the xy and yz plane is counter-clockwise and in the xz plane it is clockwise. |
Hmm... it looks like you're not the only one having problems with the angles :-/ Sorry to be such a pain, but I get different results compared to the e.G: import gstools as gs
elev = 10
model = gs.Gaussian(dim=3, var=2.0, len_scale=[10, 2], angles=[np.deg2rad(0), np.deg2rad(elev)]) results in the following plot (after changing to y slice). When I plot the same data in 3d using scatter plots I get this plot: For me the 'major' axis is y aligned and the rotation in elevation (major-Z plane after first rotation = x') is CCW. I generated the scatter plot with this script: @MuellerSeb : can you check if I am doing something wrong when plotting before we proceed any further? |
Hey there, X,Y,Z = np.meshgrid(xgrid,ygrid,zgrid) That should be: X,Y,Z = np.meshgrid(xgrid,ygrid,zgrid, indexing="ij") Like done here: GSTools/gstools/field/tools.py Line 211 in a01da8e
This indexing is another source of headache! |
OK, thanks a lot and sorry for the inconvenience! I will check my angles vs the covariance model angles then. I am fairly certain, the implementations are equal. Another question: Is GSTools for release on PyPy compiled with OpenMP support? If not, I have a implementation of estimate.pyx which implements caching for distance and angle data. In case of single thread, this can significantly speed up the calculation (especially for large bin vectors). I could pack it into this PR as well. |
OpenMP support can be enabled by the user: Most gstools routines in cython use On Conda, openmp is enabled by default. On pip we didn't want to force the user to provide openmp. What do you mean exactly by "release on pypy"? |
Sorry I ment PyPi --> My version enables caching with an additional parameter otherwise it just uses the previous implementation with prange. Would you want that feature as well or keep it as simple as possible for better maintainability? |
I would need to have a look at it, to decide. But it sounds interesting! ;-) |
I had to make a new branch and commit it first. Here is the full branch: Here is the caching implementation: And here is an example script I wrote for all new functionalities: The caching speeds up the caculation considerably for long
Let me know, what you think. I will implement the 3D test cases for the original PR today or tomorrow. |
I implemented some basic 3d testcases, as well as a fix for the estimator function. Waiting for review now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Thanks again for all the effort you are putting into this!
I really like your example script! That's gonna make a nice addition to the examples.
First things first:
Following test cases in test_variogram_unstructured.py
are not working:
test_uncorrelated_3d
test_sampling_3d
test_angles_3d_estim
You should apply black --line-length 79
to all the files you modified, otherwise, MuellerSeb will get grumpy ;-)
Caching
Here are the results on my machine with OpenMP parallelisation:
len(bins) 2: time per execution: no_caching 0.224 | with_caching 0.174
len(bins) 4: time per execution: no_caching 0.231 | with_caching 0.210
len(bins) 10: time per execution: no_caching 0.377 | with_caching 0.299
len(bins) 20: time per execution: no_caching 0.733 | with_caching 0.418
I've built the Cython code with following command in the main directory of gstools, in case you want to try it:
python setup.py --openmp build_ext --inplace
Unfortunately, the OpenMP support of Cython is still very limited. That's also the reason why the distance functions look so strange.
We have already been discussing some options of how to parallelise more complex loops. When we come to that point, it would be great to use your idea of caching the distances. But I think for now the slight speed up (which is impressive nevertheless) compared to the parallelised version does not justify the increase in maintenance.
I'll definitely keep you updated regarding the caching!
Is there still some progress here? :-) |
Hi, Also if anyone has some reliable test cases for 3d data, It would be greatly appreciated. |
Hi @TobiasGlaubach, |
Hi @LSchueler Thanks for checking in on this. I am lacking some verified test cases for 3D data, but I am fairly certain the algorithms are right and the angles correspond to the angle definitions generally used in GSTools. whats your opinion on this? Shall we add a warning somewhere? |
@TobiasGlaubach why not just generate a field with gstools, so we know the properties of the given field? |
@TobiasGlaubach going through your code, I noticed two problems:
I can take over if you want. I just realized, we don't have to care about trait-bryan angles, since we only check the variogram in one given direction. Retrieving trait brian angles from sample data should be another step. |
…; check both directions between point pairs
I did some updates:
I got some concerns/ideas:
But I am happy with this first implementation. Later on, we can implement a switch to use either angles for direction or vectors. |
I'll merge this into a new branch here to take over. Thanks for everything @TobiasGlaubach ! |
@MuellerSeb Thanks for taking over, I was on vacation for a few weeks and atm I am quite buisy with other projects at the moment. Glad to have been able to contribute to your project :-) keep up the good work! |
@MuellerSeb @LSchueler I finished implementing the angle dependent variogram calculation and implemented a few test cases. Calculation is quasi pure C as soon as the calculation arrives at the calculation heavy loops. Defaults for parameters were chosen in order to preserve original behavior and the implementation should add no computational overhead if no angles are requested.
Can one of you do a code review and look at the test cases?
Tests passed on my system without a problem.
Test values were generated like this:
The implemented test-cases correspond to these "fields":
with