forked from simpeg/simpeg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_directives.py
129 lines (96 loc) · 3.81 KB
/
test_directives.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import unittest
import warnings
import pytest
import numpy as np
from SimPEG import (
Mesh, Maps, Directives, Regularization, DataMisfit, Optimization,
Inversion, InvProblem
)
from SimPEG import PF
class DirectivesValidation(unittest.TestCase):
def test_validation_pass(self):
betaest = Directives.BetaEstimate_ByEig()
IRLS = Directives.Update_IRLS(
f_min_change=1e-4, minGNiter=3, beta_tol=1e-2
)
update_Jacobi = Directives.UpdatePreconditioner()
dList = [betaest, IRLS, update_Jacobi]
directiveList = Directives.DirectiveList(*dList)
self.assertTrue(directiveList.validate())
def test_validation_fail(self):
betaest = Directives.BetaEstimate_ByEig()
IRLS = Directives.Update_IRLS(
f_min_change=1e-4, minGNiter=3, beta_tol=1e-2
)
update_Jacobi = Directives.UpdatePreconditioner()
dList = [betaest, update_Jacobi, IRLS]
directiveList = Directives.DirectiveList(*dList)
with self.assertRaises(AssertionError):
self.assertTrue(directiveList.validate())
def test_validation_warning(self):
betaest = Directives.BetaEstimate_ByEig()
IRLS = Directives.Update_IRLS(
f_min_change=1e-4, minGNiter=3, beta_tol=1e-2
)
update_Jacobi = Directives.UpdatePreconditioner()
dList = [betaest, IRLS]
directiveList = Directives.DirectiveList(*dList)
with pytest.warns(UserWarning):
self.assertTrue(directiveList.validate())
class ValidationInInversion(unittest.TestCase):
def setUp(self):
mesh = Mesh.TensorMesh([4, 4, 4])
# Magnetic inducing field parameter (A,I,D)
B = [50000, 90, 0]
# Create a MAGsurvey
rx = PF.BaseMag.RxObs(
np.vstack([[0.25, 0.25, 0.25], [-0.25, -0.25, 0.25]])
)
srcField = PF.BaseMag.SrcField([rx], param=(B[0], B[1], B[2]))
survey = PF.BaseMag.LinearSurvey(srcField)
# Create the forward model operator
prob = PF.Magnetics.MagneticIntegral(
mesh, chiMap=Maps.IdentityMap(mesh)
)
# Pair the survey and problem
survey.pair(prob)
# Compute forward model some data
m = np.random.rand(mesh.nC)
survey.makeSyntheticData(m)
reg = Regularization.Sparse(mesh)
reg.mref = np.zeros(mesh.nC)
wr = np.sum(prob.G**2., axis=0)**0.5
reg.cell_weights = wr
reg.norms = np.c_[0, 1, 1, 1]
reg.eps_p, reg.eps_q = 1e-3, 1e-3
# Data misfit function
dmis = DataMisfit.l2_DataMisfit(survey)
dmis.W = 1./survey.std
# Add directives to the inversion
opt = Optimization.ProjectedGNCG(
maxIter=2, lower=-10., upper=10.,
maxIterCG=2
)
invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
self.mesh = mesh
self.invProb = invProb
def test_validation_in_inversion(self):
betaest = Directives.BetaEstimate_ByEig()
# Here is where the norms are applied
IRLS = Directives.Update_IRLS(
f_min_change=1e-4, minGNiter=3, beta_tol=1e-2
)
update_Jacobi = Directives.UpdatePreconditioner()
with self.assertRaises(AssertionError):
# validation should happen and this will fail
# (IRLS needs to be before update_Jacobi)
inv = Inversion.BaseInversion(
self.invProb, directiveList=[betaest, update_Jacobi, IRLS]
)
with self.assertRaises(AssertionError):
# validation should happen and this will fail
# (IRLS needs to be before update_Jacobi)
inv = Inversion.BaseInversion(self.invProb)
inv.directiveList = [betaest, update_Jacobi, IRLS]
if __name__ == '__main__':
unittest.main()