Skip to content

Commit

Permalink
SciPy>=1.9; lazy imports (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
prisae committed Feb 23, 2024
1 parent 0ee2107 commit 5e2c3c4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 76 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'"
test: ""
- python-version: "3.10"
name: plain
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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
27 changes: 13 additions & 14 deletions empymod/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@


import numpy as np
import scipy as sp
import numba as nb

__all__ = ['wavenumber', 'angle_factor', 'fullspace', 'greenfct',
Expand Down Expand Up @@ -946,8 +947,6 @@ def halfspace(off, angle, zsrc, zrec, etaH, etaV, freqtime, ab, signal,
the input and solution parameters.
"""
from scipy import special # Lazy for faster CLI load

xco = np.cos(angle)*off
yco = np.sin(angle)*off
res = np.real(1/etaH[0, 0])
Expand Down Expand Up @@ -1023,10 +1022,10 @@ def halfspace(off, angle, zsrc, zrec, etaH, etaV, freqtime, ab, signal,

elif abs(signal) == 1: # Time-domain step response
# Replace F(m) with F(m-2)
f0p = special.erfc(np.sqrt(tp/time))
f0m = special.erfc(np.sqrt(tm/time))
fs0p = special.erfc(np.sqrt(tsp/time))
fs0m = special.erfc(np.sqrt(tsm/time))
f0p = sp.special.erfc(np.sqrt(tp/time))
f0m = sp.special.erfc(np.sqrt(tm/time))
fs0p = sp.special.erfc(np.sqrt(tsp/time))
fs0m = sp.special.erfc(np.sqrt(tsm/time))

f1p = np.exp(-tp/time)/np.sqrt(np.pi*time)
f1m = np.exp(-tm/time)/np.sqrt(np.pi*time)
Expand Down Expand Up @@ -1117,15 +1116,15 @@ def halfspace(off, angle, zsrc, zrec, etaH, etaV, freqtime, ab, signal,
# Bessel functions for airwave
def BI(gamH, hp, nr, xim):
r"""Return BI_nr."""
return np.exp(-np.real(gamH)*hp)*special.ive(nr, xim)
return np.exp(-np.real(gamH)*hp)*sp.special.ive(nr, xim)

def BK(xip, nr):
r"""Return BK_nr."""
if np.isrealobj(xip):
# To keep it real in Laplace-domain [exp(-1j*0) = 1-0j].
return special.kve(nr, xip)
return sp.special.kve(nr, xip)
else:
return np.exp(-1j*np.imag(xip))*special.kve(nr, xip)
return np.exp(-1j*np.imag(xip))*sp.special.kve(nr, xip)

# Airwave calculation
def airwave(sval, hp, rp, res, fab, delta):
Expand Down Expand Up @@ -1167,9 +1166,9 @@ def airwave(sval, hp, rp, res, fab, delta):
def coeff_dk(k, K):
r"""Return coefficients Dk for k, K."""
n = np.arange((k+1)//2, min([k, K/2])+.5, 1)
Dk = n**(K/2)*special.factorial(2*n)/special.factorial(n)
Dk /= special.factorial(n-1)*special.factorial(k-n)
Dk /= special.factorial(2*n-k)*special.factorial(K/2-n)
Dk = n**(K/2)*sp.special.factorial(2*n)/sp.special.factorial(n)
Dk /= sp.special.factorial(n-1)*sp.special.factorial(k-n)
Dk /= sp.special.factorial(2*n-k)*sp.special.factorial(K/2-n)
return Dk.sum()*(-1)**(k+K/2)

for k in range(1, K+1):
Expand All @@ -1181,9 +1180,9 @@ def coeff_dk(k, K):
thp = mu_0*hp**2/(4*res)
trh = mu_0*rh**2/(8*res)
P1 = (mu_0**2*hp*np.exp(-thp/time))/(res*32*np.pi*time**3)
P2 = 2*(delta - (x*y)/rh**2)*special.ive(1, trh/time)
P2 = 2*(delta - (x*y)/rh**2)*sp.special.ive(1, trh/time)
P3 = mu_0/(2*res*time)*(rh**2*delta - x*y)-delta
P4 = special.ive(0, trh/time) - special.ive(1, trh/time)
P4 = sp.special.ive(0, trh/time) - sp.special.ive(1, trh/time)

air = P1*(P2 - P3*P4)

Expand Down
22 changes: 11 additions & 11 deletions empymod/scripts/fdesign.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ def rhs(r):

import os
import numpy as np
import scipy as sp
from copy import deepcopy as dc
from scipy.constants import mu_0

from empymod.filters import DigitalFilter
from empymod.model import dipole, dipole_k
Expand Down Expand Up @@ -368,8 +368,6 @@ def design(n, spacing, shift, fI, fC=False, r=None, r_def=(1, 1, 2), reim=None,
`full_output` is True.)
"""
from scipy.optimize import brute, fmin_powell # Lazy for faster CLI load

# === 1. LET'S START ============
t0 = printstartfinish(verb)

Expand All @@ -396,7 +394,7 @@ def check_f(f):

# Check default input values
if finish and not callable(finish):
finish = fmin_powell
finish = sp.optimize.fmin_powell
if name is None:
name = 'dlf_'+str(n)
if r is None:
Expand Down Expand Up @@ -427,9 +425,11 @@ def check_f(f):
_call_qc_transform_pairs(n, ispacing, ishift, fI, fC, r, r_def, reim)

# === 3. RUN BRUTE FORCE OVER THE GRID ============
full = brute(_get_min_val, (ispacing, ishift), full_output=True,
args=(n, fI, fC, r, r_def, error, reim, cvar, verb, plot,
log), finish=finish)
full = sp.optimize.brute(
_get_min_val, (ispacing, ishift), full_output=True,
args=(n, fI, fC, r, r_def, error, reim, cvar, verb, plot, log),
finish=finish
)

# Add cvar-information to full: 0 for 'amp', 1 for 'r'
if cvar == 'r':
Expand Down Expand Up @@ -927,7 +927,7 @@ def j0_4(f=1, rho=0.3, z=50):
"""

gam = np.sqrt(2j*np.pi*mu_0*f/rho)
gam = np.sqrt(2j*np.pi*sp.constants.mu_0*f/rho)

def lhs(x):
beta = np.sqrt(x**2 + gam**2)
Expand Down Expand Up @@ -955,7 +955,7 @@ def j0_5(f=1, rho=0.3, z=50):
"""

gam = np.sqrt(2j*np.pi*mu_0*f/rho)
gam = np.sqrt(2j*np.pi*sp.constants.mu_0*f/rho)

def lhs(x):
beta = np.sqrt(x**2 + gam**2)
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def j1_4(f=1, rho=0.3, z=50):
"""

gam = np.sqrt(2j*np.pi*mu_0*f/rho)
gam = np.sqrt(2j*np.pi*sp.constants.mu_0*f/rho)

def lhs(x):
beta = np.sqrt(x**2 + gam**2)
Expand Down Expand Up @@ -1052,7 +1052,7 @@ def j1_5(f=1, rho=0.3, z=50):
"""

gam = np.sqrt(2j*np.pi*mu_0*f/rho)
gam = np.sqrt(2j*np.pi*sp.constants.mu_0*f/rho)

def lhs(x):
beta = np.sqrt(x**2 + gam**2)
Expand Down

0 comments on commit 5e2c3c4

Please sign in to comment.