rafael-fuente / diffractsim

✨🔬 A flexible diffraction simulator for exploring and visualizing physical optics.
https://rafael-fuente.github.io/simulating-diffraction-patterns-with-the-angular-spectrum-method-and-python.html
Other
754 stars 93 forks source link

Issue with Scipy interp2d #55

Closed angeAnonyme closed 1 month ago

angeAnonyme commented 1 month ago

With the latest version of Scipy, I ran into some error due to interp2d been depleated:

"NotImplementedError: interp2d has been removed in SciPy 1.14.0."

The solution was to change, it in the .util.image_handling.py, the resize_array function. Here is the new code:

def resize_array(img_array, new_shape):

    Ny, Nx = img_array.shape

    """from scipy.interpolate import interp2d
    fun = interp2d(
        np.linspace(0, 1, Nx),
        np.linspace(0, 1, Ny),
        img_array,
        kind="cubic",
    )
    resize_img_array = fun(np.linspace(0, 1, new_shape[1]), np.linspace(0, 1, new_shape[0]))
    """
    from scipy.interpolate import RectBivariateSpline
    r = RectBivariateSpline(np.linspace(0, 1, Nx), np.linspace(0, 1, Ny), img_array.T)

    rt = lambda xnew, ynew: r(xnew, ynew).T
    resize_img_array = rt(np.linspace(0, 1, new_shape[1]), np.linspace(0, 1, new_shape[0]))

    return resize_img_array

Solution inspired from: https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff

LtqxWYEG commented 1 month ago

I just replaced the word interp2dwith RectBivariateSplineand commented out kind="cubic". works flawlessly, afaik and by what scipy told me in the error message.
BTW, in your code example is the old not-working code

angeAnonyme commented 1 month ago

Awesome, thanks for the great work. Indeed I had the old code but it was commented ("""..."""). It was for reference. I am unsure why my solution was using the transpose function, but if it works without, it's easier. I close the issue.

LtqxWYEG commented 1 month ago

Indeed I had the old code but it was commented ("""...""").

Oh! You know, you can use code highlighting by adding the language used after the first three ' here in github.

Print("like for example...."+23+variable)

So, like '''python.