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

Learner2D.interpolator and Learner2D.interpolated_on_grid give different results #427

Open
MCFlowMace opened this issue May 11, 2023 · 5 comments

Comments

@MCFlowMace
Copy link

MCFlowMace commented May 11, 2023

Hi thanks for this awesome package! I have an issue with the use of the interpolated data of the Learner2D with a function that I'm trying to analyze. This is what I try to do:

n = 201
learner.plot(n=n) #1st plot

data = learner.interpolated_on_grid(n)

plt.imshow(data[2])
plt.show() #2nd plot

xs, ys = [np.linspace(*b, num=n) for b in learner.bounds]
ip = learner.interpolator()
zs = ip(xs[:, None], ys[None, :])
plt.imshow(zs[:,:,0])
plt.show() #3rd plot

In the code above learner is an instance of Learner2D after running it until convergence. First I inspect the result with the internal plot function. Result looks good:

For the 2nd plot I use interpolated_on_grid to get the data and plot it myself. Result is consistent with the other plot:

For the 3rd plot I want to use the interpolator myself. The result of that is not the same:

Using a different function for the learner the difference is a lot less significant but still existing.
2nd plot:

3rd plot:

Am I using the interpolator object in the wrong way or is it a bug? I would prefer to use the interpolator object instead of interpolated_on_grid since that gives more flexibility for the evaluation points. I am using adaptive version 0.15.1

@akhmerov
Copy link
Contributor

Hey @MCFlowMace! The interpolation depends on the triangulation being used. The triangulation, on the other hand, is sensitive to the aspect ratio of the data because that changes the notion of which point is the nearest. Learner 2D internally rescales its data to occupy a unit square and builds a triangulation from that. Your data limits, on the other hand, have a different aspect ratio, which is the origin of the disagreement.

Try rescaling x and y to have the same extent and seeing if the problem persists.

@MCFlowMace
Copy link
Author

Hey @akhmerov thanks for the fast reply! I was already wondering if it has to do with the aspect ratio. I have an aspect ratio of 25 in the first case and only 0.5 in the second. Are you suggesting I should rescale x and y of my function before giving it to the learner or for the interpolator object that I get back?

Also if this is the fault then as a user I am still wondering why interpolated_on_grid and interpolator should give me different results. Apparently the first correctly applies the rescaling while I have to do it myself with the second. Is that design intentional? It's just not intuitive to me.

@MCFlowMace
Copy link
Author

MCFlowMace commented May 11, 2023

Okay this way I get expected results:

xs = np.linspace(-0.5, 0.5, n)
ys = np.linspace(-0.5, 0.5, n)
ip = learner.interpolator(scaled=True)
zs = ip(xs[:, None], ys[None, :])
plt.imshow(zs[:,:,0])
plt.show()

I can work with it this way. But just commenting that this was not clear for me from the documentation. The documentation sounded like I can just use interpolator with the default scaled=False and can work within my given x and y bounds. It would be nice if that was possible without extra effort.

@MrCheatak
Copy link

I have stumbled upon this behaviour as well, has to look into the interpolated_on_grid implementation to figure out how to use interpolator separately.
However, it adds up when you aim to have a non-linear grid, i.e log-axes.

@MrCheatak
Copy link

I managed to obtain an interpolator that produces the exact same data as interpolated_on_grid. Create a LinearNDInterpolator with the points and values from learner and set rescale=True. Without this argument in my case the interpolator produced a lort of random noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants