Skip to content

Commit

Permalink
Use Numpy rng in FDEM tests (#1449)
Browse files Browse the repository at this point in the history
Replace the usage of the deprecated functions in `numpy.random` module
for the Numpy's random number generator class and its methods, in most
of the FDEM tests. Increase number of iterations for checking
derivatives where needed.

Part of the solution to #1289

---------

Co-authored-by: Lindsey Heagy <lindseyheagy@gmail.com>
  • Loading branch information
santisoler and lheagy committed Jun 7, 2024
1 parent 772429a commit caa60b7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 35 deletions.
22 changes: 16 additions & 6 deletions tests/em/fdem/forward/test_FDEM_casing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
sigma = np.r_[10.0, 5.5e6, 1e-1]
mu = mu_0 * np.r_[1.0, 100.0, 1.0]
srcloc = np.r_[0.0, 0.0, 0.0]
xobs = np.random.rand(n) + 10.0
rng = np.random.default_rng(seed=42)
xobs = rng.uniform(size=n) + 10.0
yobs = np.zeros(n)
zobs = np.random.randn(n)
zobs = rng.normal(size=n)


def CasingMagDipoleDeriv_r(x):
Expand Down Expand Up @@ -63,15 +64,24 @@ def CasingMagDipole2Deriv_z_z(z):

class Casing_DerivTest(unittest.TestCase):
def test_derivs(self):
rng = np.random.default_rng(seed=42)

np.random.seed(1983) # set a random seed for check_derivative
tests.check_derivative(
CasingMagDipoleDeriv_r, np.ones(n) * 10 + np.random.randn(n), plotIt=False
CasingMagDipoleDeriv_r, np.ones(n) * 10 + rng.normal(size=n), plotIt=False
)
tests.check_derivative(CasingMagDipoleDeriv_z, np.random.randn(n), plotIt=False)

np.random.seed(1983) # set a random seed for check_derivative
tests.check_derivative(CasingMagDipoleDeriv_z, rng.normal(size=n), plotIt=False)

np.random.seed(1983) # set a random seed for check_derivative
tests.check_derivative(
CasingMagDipole2Deriv_z_r,
np.ones(n) * 10 + np.random.randn(n),
np.ones(n) * 10 + rng.normal(size=n),
plotIt=False,
)

np.random.seed(1983) # set a random seed for check_derivative
tests.check_derivative(
CasingMagDipole2Deriv_z_z, np.random.randn(n), plotIt=False
CasingMagDipole2Deriv_z_z, rng.normal(size=n), plotIt=False
)
8 changes: 4 additions & 4 deletions tests/em/fdem/forward/test_FDEM_primsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
TOL_JT = 1e-10
FLR = 1e-20 # "zero", so if residual below this --> pass regardless of order

np.random.seed(2016)

# To test the primary secondary-source, we look at make sure doing primary
# secondary for a simple model gives comprable results to just solving a 3D
# problem
Expand Down Expand Up @@ -128,15 +126,17 @@ def fun(x):
lambda x: self.secondarySimulation.Jvec(x0, x, f=self.fields_primsec),
]

np.random.seed(1983) # set a random seed for check_derivative
return tests.check_derivative(fun, x0, num=2, plotIt=False)

def AdjointTest(self):
print("\nTesting adjoint")

m = model
f = self.fields_primsec
v = np.random.rand(self.secondarySurvey.nD)
w = np.random.rand(self.secondarySimulation.sigmaMap.nP)
rng = np.random.default_rng(seed=2016)
v = rng.uniform(size=self.secondarySurvey.nD)
w = rng.uniform(size=self.secondarySimulation.sigmaMap.nP)

vJw = v.dot(self.secondarySimulation.Jvec(m, w, f))
wJtv = w.dot(self.secondarySimulation.Jtvec(m, v, f))
Expand Down
9 changes: 5 additions & 4 deletions tests/em/fdem/forward/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def test_source_properties_validation():
# LineCurrent
with pytest.raises(TypeError):
fdem.sources.LineCurrent([], frequency, location=["a", "b", "c"])
rng = np.random.default_rng(seed=42)
random_locations = rng.normal(size=(5, 3, 2))
with pytest.raises(ValueError):
fdem.sources.LineCurrent([], frequency, location=np.random.rand(5, 3, 2))
fdem.sources.LineCurrent([], frequency, location=random_locations)
random_locations = rng.normal(size=(5, 3))
with pytest.raises(ValueError):
fdem.sources.LineCurrent(
[], frequency, location=np.random.rand(5, 3), current=0.0
)
fdem.sources.LineCurrent([], frequency, location=random_locations, current=0.0)


def test_bad_source_type():
Expand Down
9 changes: 5 additions & 4 deletions tests/em/fdem/inverse/adjoint/test_FDEM_adjointEB.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ def adjointTest(fdemType, comp):
m = np.log(np.ones(prb.sigmaMap.nP) * CONDUCTIVITY)
mu = np.ones(prb.mesh.nC) * MU

rng = np.random.default_rng(seed=42)
if addrandoms is True:
m = m + np.random.randn(prb.sigmaMap.nP) * np.log(CONDUCTIVITY) * 1e-1
mu = mu + np.random.randn(prb.mesh.nC) * MU * 1e-1
m = m + rng.normal(size=prb.sigmaMap.nP) * np.log(CONDUCTIVITY) * 1e-1
mu = mu + rng.normal(size=prb.mesh.nC) * MU * 1e-1

survey = prb.survey
# prb.PropMap.PropModel.mu = mu
# prb.PropMap.PropModel.mui = 1./mu
u = prb.fields(m)

v = np.random.rand(survey.nD)
w = np.random.rand(prb.mesh.nC)
v = rng.uniform(size=survey.nD)
w = rng.uniform(size=prb.mesh.nC)

vJw = v.dot(prb.Jvec(m, w, u))
wJtv = w.dot(prb.Jtvec(m, v, u))
Expand Down
9 changes: 5 additions & 4 deletions tests/em/fdem/inverse/adjoint/test_FDEM_adjointHJ.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ def adjointTest(fdemType, comp, src):
m = np.log(np.ones(prb.sigmaMap.nP) * CONDUCTIVITY)
mu = np.ones(prb.mesh.nC) * MU

rng = np.random.default_rng(seed=42)
if addrandoms is True:
m = m + np.random.randn(prb.sigmaMap.nP) * np.log(CONDUCTIVITY) * 1e-1
mu = mu + np.random.randn(prb.mesh.nC) * MU * 1e-1
m = m + rng.normal(size=prb.sigmaMap.nP) * np.log(CONDUCTIVITY) * 1e-1
mu = mu + rng.normal(size=prb.mesh.nC) * MU * 1e-1

survey = prb.survey
u = prb.fields(m)

v = np.random.rand(survey.nD)
w = np.random.rand(prb.mesh.nC)
v = rng.uniform(size=survey.nD)
w = rng.uniform(size=prb.mesh.nC)

vJw = v.dot(prb.Jvec(m, w, u))
wJtv = w.dot(prb.Jtvec(m, v, u))
Expand Down
7 changes: 3 additions & 4 deletions tests/em/fdem/inverse/derivs/test_FDEM_derivs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@

def derivTest(fdemType, comp, src):
prb = getFDEMProblem(fdemType, comp, SrcType, freq)
# prb.solverOpts = dict(check_accuracy=True)

print(f"{fdemType} formulation {src} - {comp}")
x0 = np.log(np.ones(prb.sigmaMap.nP) * CONDUCTIVITY)
# mu = np.log(np.ones(prb.mesh.nC)*MU)

if addrandoms is True:
x0 = x0 + np.random.randn(prb.sigmaMap.nP) * np.log(CONDUCTIVITY) * 1e-1
# mu = mu + np.random.randn(prb.sigmaMap.nP)*MU*1e-1
rng = np.random.default_rng(seed=42)
x0 = x0 + rng.normal(size=prb.sigmaMap.nP) * np.log(CONDUCTIVITY) * 1e-1

def fun(x):
return prb.dpred(x), lambda x: prb.Jvec(x0, x)

np.random.seed(1983) # set a random seed for check_derivative
return tests.check_derivative(fun, x0, num=2, plotIt=False, eps=FLR)


Expand Down
21 changes: 12 additions & 9 deletions tests/em/fdem/muinverse/test_muinverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def setupMeshModel():
hz = [(cs, npad, -1.3), (cs, nc), (cs, npad, 1.3)]

mesh = discretize.CylindricalMesh([hx, 1.0, hz], "0CC")
muMod = 1 + MuMax * np.random.randn(mesh.nC)
sigmaMod = np.random.randn(mesh.nC)
rng = np.random.default_rng(seed=2016)
muMod = 1 + MuMax * rng.normal(size=mesh.nC)
sigmaMod = rng.normal(size=mesh.nC)

return mesh, muMod, sigmaMod

Expand Down Expand Up @@ -149,7 +150,8 @@ def test_mats_cleared(self):
MfMuiIDeriv_zero = self.simulation.MfMuiIDeriv(utils.Zero())
MeMuDeriv_zero = self.simulation.MeMuDeriv(utils.Zero())

m1 = np.random.rand(self.mesh.nC)
rng = np.random.default_rng(seed=2016)
m1 = rng.uniform(size=self.mesh.nC)
self.simulation.model = m1

self.assertTrue(getattr(self, "_MeMu", None) is None)
Expand All @@ -168,7 +170,6 @@ def JvecTest(
self.setUpProb(prbtype, sigmaInInversion, invertMui)
print("Testing Jvec {}".format(prbtype))

np.random.seed(3321)
mod = self.m0

def fun(x):
Expand All @@ -177,19 +178,21 @@ def fun(x):
lambda x: self.simulation.Jvec(mod, x),
)

dx = np.random.rand(*mod.shape) * (mod.max() - mod.min()) * 0.01
rng = np.random.default_rng(seed=3321)
dx = rng.uniform(size=mod.shape) * (mod.max() - mod.min()) * 0.01

return tests.check_derivative(fun, mod, dx=dx, num=3, plotIt=False)
np.random.seed(1983) # set a random seed for check_derivative
return tests.check_derivative(fun, mod, dx=dx, num=4, plotIt=False)

def JtvecTest(
self, prbtype="ElectricField", sigmaInInversion=False, invertMui=False
):
self.setUpProb(prbtype, sigmaInInversion, invertMui)
print("Testing Jvec {}".format(prbtype))

np.random.seed(31345)
u = np.random.rand(self.simulation.muMap.nP)
v = np.random.rand(self.survey.nD)
rng = np.random.default_rng(seed=3321)
u = rng.uniform(size=self.simulation.muMap.nP)
v = rng.uniform(size=self.survey.nD)

self.simulation.model = self.m0

Expand Down

0 comments on commit caa60b7

Please sign in to comment.