Skip to content

Commit

Permalink
SciPy>=1.9; lazy imports (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
prisae authored Feb 23, 2024
1 parent 977c12b commit d571bdf
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- python-version: "3.9"
name: minimal
os: ubuntu
conda: "'scipy=1.8' 'numba=0.53'"
conda: "'scipy=1.9' 'numba=0.53'"
- python-version: "3.10"
name: full
os: ubuntu
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ latest
- Bumped the minimum requirements to:

- Python 3.9
- SciPy 1.8
- SciPy 1.9
- Numba 0.53

- Testing: added Python 3.12, dropped Python 3.8.
Expand Down
4 changes: 2 additions & 2 deletions emg3d/electrodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from copy import deepcopy

import numpy as np
from scipy.special import sindg, cosdg
import scipy as sp

from emg3d import fields, utils

Expand Down Expand Up @@ -847,7 +847,7 @@ def rotation(azimuth, elevation, deg=True):
"""
if deg:
cos, sin = cosdg, sindg
cos, sin = sp.special.cosdg, sp.special.sindg
else:
cos, sin = np.cos, np.sin

Expand Down
4 changes: 2 additions & 2 deletions emg3d/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import numba as nb
import numpy as np
from scipy.constants import mu_0
import scipy as sp

from emg3d.core import _numba_setting
from emg3d import maps, meshes, models, utils, electrodes
Expand Down Expand Up @@ -271,7 +271,7 @@ def smu0(self):
"""s*mu_0; mu_0 = magn permeability of free space [H/m]."""
if getattr(self, '_smu0', None) is None:
if self.sval is not None:
self._smu0 = self.sval*mu_0
self._smu0 = self.sval*sp.constants.mu_0
else:
self._smu0 = None

Expand Down
22 changes: 11 additions & 11 deletions emg3d/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

import numba as nb
import numpy as np
from scipy.ndimage import map_coordinates
from scipy.interpolate import RegularGridInterpolator, interpnd, interp1d
import scipy as sp

from emg3d.utils import _requires
from emg3d.core import _numba_setting
Expand Down Expand Up @@ -352,9 +351,9 @@ def interpolate(grid, values, xi, method='linear', extrapolate=True,
**({} if kwargs is None else kwargs),
}

values_x = RegularGridInterpolator(
points=points, values=values, method=method,
**opts)(xi=new_points)
values_x = sp.interpolate.RegularGridInterpolator(
points=points, values=values, method=method, **opts
)(xi=new_points)

# Return to linear if log10 was applied.
if log:
Expand Down Expand Up @@ -480,7 +479,7 @@ def _points_from_grids(grid, values, xi, method):
else:
# Replicate the same expansion of xi as used in
# RegularGridInterpolator, so the input xi can be quite flexible.
new_points = interpnd._ndim_coords_from_arrays(
new_points = sp.interpolate.interpnd._ndim_coords_from_arrays(
xi, ndim=3)
shape = new_points.shape[:-1]
new_points = new_points.reshape(-1, 3, order='F')
Expand Down Expand Up @@ -541,16 +540,17 @@ def interp_spline_3d(points, values, xi, **kwargs):
# coordinates to this artificial coordinate system too.
coords = np.empty(xi.T.shape)
for i in range(3):
coords[i] = interp1d(points[i], np.arange(len(points[i])),
kind='cubic', bounds_error=False,
fill_value='extrapolate')(xi[:, i])
coords[i] = sp.interpolate.interp1d(
points[i], np.arange(len(points[i])), kind='cubic',
bounds_error=False, fill_value='extrapolate'
)(xi[:, i])

# `map_coordinates` only works for real data; split it up if complex.
# Note: SciPy 1.6 (12/2020) introduced complex-valued
# ndimage.map_coordinates; replace eventually.
values_x = map_coordinates(values.real, coords, **kwargs)
values_x = sp.ndimage.map_coordinates(values.real, coords, **kwargs)
if 'complex' in values.dtype.name:
imag = map_coordinates(values.imag, coords, **kwargs)
imag = sp.ndimage.map_coordinates(values.imag, coords, **kwargs)
values_x = values_x + 1j*imag

return values_x
Expand Down
8 changes: 4 additions & 4 deletions emg3d/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
from copy import deepcopy

import numpy as np
from scipy.constants import mu_0
from scipy.optimize import brentq
import scipy as sp

from emg3d import maps, utils

Expand Down Expand Up @@ -1051,7 +1050,7 @@ def f(alpha):
return np.sum(tdmin*alpha**np.arange(1, n+1)) - delta

# Required stretching to fit n cells exactly into delta.
alph = brentq(f, 0.5, 10.0)
alph = sp.optimize.brentq(f, 0.5, 10.0)

# If stretching is within tolerance, finish.
if alph < min(alphmax, stretching[1]):
Expand Down Expand Up @@ -1178,7 +1177,8 @@ def skin_depth(frequency, conductivity, mu_r=1.0):
Skin depth (m).
"""
skindepth = 1/np.sqrt(np.pi*abs(frequency)*conductivity*mu_r*mu_0)
mu = mu_r*sp.constants.mu_0
skindepth = 1/np.sqrt(np.pi*abs(frequency)*conductivity*mu)

# For Laplace-domain computations.
if frequency < 0:
Expand Down
6 changes: 3 additions & 3 deletions emg3d/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from copy import deepcopy

import numpy as np
from scipy.constants import epsilon_0
import scipy as sp

from emg3d import maps, meshes, utils

Expand Down Expand Up @@ -679,8 +679,8 @@ def __init__(self, model, sfield):

# Complete version.
else:
eta = -sfield.smu0*vol*(
cond + sfield.sval*epsilon_0*model.epsilon_r)
smu = sfield.sval*sp.constants.epsilon_0*model.epsilon_r
eta = -sfield.smu0*vol*(cond + smu)

setattr(self, '_eta_' + name[-1], eta)

Expand Down
5 changes: 3 additions & 2 deletions emg3d/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import warnings

import numpy as np
from scipy.interpolate import PchipInterpolator as Pchip
from scipy.interpolate import InterpolatedUnivariateSpline as Spline
import scipy as sp

try:
import empymod
Expand Down Expand Up @@ -339,6 +338,7 @@ def interpolate(self, fdata):
# interpolate from fmin to fmax.
if self.freq_coarse.size != self.freq_required.size:

Spline = sp.interpolate.InterpolatedUnivariateSpline
int_real = Spline(np.log(self.freq_compute),
fdata.real)(np.log(self.freq_interpolate))
int_imag = Spline(np.log(self.freq_compute),
Expand All @@ -359,6 +359,7 @@ def interpolate(self, fdata):
data_ext = np.r_[fdata[0].real-1e-100j, fdata]

# 2.b Actual 'extrapolation' (now an interpolation).
Pchip = sp.interpolate.PchipInterpolator
ext_real = Pchip(freq_ext, data_ext.real)(self.freq_extrapolate)
ext_imag = Pchip(freq_ext, data_ext.imag)(self.freq_extrapolate)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
python_requires=">=3.9",
install_requires=[
"scipy>=1.8",
"scipy>=1.9",
"numba>=0.53",
],
extras_require={
Expand Down
3 changes: 2 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import numpy as np
import scipy as sp
from numpy.testing import assert_allclose

import emg3d
Expand Down Expand Up @@ -107,7 +108,7 @@ def test_regression(self, capsys):
assert_allclose(tres*4., model3.property_z)

# Check eta
iomep = sfield.sval*models.epsilon_0
iomep = sfield.sval*sp.constants.epsilon_0
eta_x = -sfield.smu0*(1./model3.property_x - iomep)*gridvol
eta_y = -sfield.smu0*(1./model3.property_y - iomep)*gridvol
eta_z = -sfield.smu0*(1./model3.property_z - iomep)*gridvol
Expand Down

0 comments on commit d571bdf

Please sign in to comment.