achael / eht-imaging

Imaging, analysis, and simulation software for radio interferometry
https://achael.github.io/eht-imaging/
GNU General Public License v3.0
5.29k stars 496 forks source link

Updating `ehtim` to be compatible with `scipy>1.14` #195

Closed jrfarah closed 1 week ago

jrfarah commented 1 week ago

Summary

scipy has recently deprecated the scipy.interpolate.interp2d function as of version 1.14 (see my issue here: https://github.com/achael/eht-imaging/issues/192) Instructions are given for moving to the new functions, chiefly, scipy.interpolate.RectBivariateSpline. I implemented this in ehtim to return functionality primarily to the regrid_image method in the Image class, which relies on interp2d.

Changes

Previously, interpolation in regrid_image was done via

interpfunc = scipy.interpolate.interp2d(y, x, np.reshape(imvec, (self.ydim, self.xdim)),
                                                    kind=interp) 

The new version of scipy requires we use RectBivariateSpline, which operates a little bit differently than interp2d. Also, the interp keyword of regrid_image can no longer be passed directly to interp2d, as RectBivariateSpline requires the order of the interpolation to be an integer. The new code reads:

interp_order = {"linear": 1, "quadratic": 2, "cubic": 3}.get(interp, 1)
grid_z = np.reshape(imvec, (self.ydim, self.xdim))
interpfunc = scipy.interpolate.RectBivariateSpline(y, x, grid_z, kx=interp_order, ky=interp_order)

Tests

I tested these changes using the following script:

import sys
sys.path.append('../')

import ehtim as eh

import matplotlib.pyplot as plt
import numpy as np

## load the image
im = eh.image.load_txt('../models/avery_sgra_eofn.txt')
im.display()
input()

## resize image 
im_resize = im.regrid_image(300*eh.RADPERUAS, 128)
im_resize.display()
input()

This code works in the current version of ehtim on systems with scipy<1.14 but breaks if scipy>=1.14. However, with the changes, this script works in the new version of scipy and is also backwards compatible, so it will not break even if you do have scipy<1.14 installed.

jrfarah commented 1 week ago

@achael if you or someone with write access could please link this PR to #192 that would be great! Thanks :)

achael commented 1 week ago

thank you, @jrfarah!

achael commented 1 week ago

@jrfarah are you able to look at the interp2d calls in rex.py? I will try to fix them myself later if not.

jrfarah commented 1 week ago

Thanks for the merge @achael ! My bad--I missed the interp2d calls in rex.py. I'll take a look and try to update those function calls as well! (I'll use the same branch for the PR).