Skip to content

Commit

Permalink
Merge branch 'main' into Doc_BaseSimulations
Browse files Browse the repository at this point in the history
  • Loading branch information
santisoler committed Mar 13, 2024
2 parents ec50ad1 + f2e29ad commit 77731f5
Show file tree
Hide file tree
Showing 80 changed files with 1,116 additions and 778 deletions.
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ body:
- type: markdown
attributes:
value: >
Thanks for your use of SimPEG and for taking the time to report a bug! Please
Thanks for using SimPEG and taking the time to report a bug! Please
first double check that there is not already a bug report on this issue by
searching through the existing bugs.
Expand All @@ -19,11 +19,11 @@ body:

- type: textarea
attributes:
label: "Reproducable code example:"
label: "Reproducible code example:"
description: >
Please submit a small, but complete, code sample that reproduces the
bug or missing functionality. It should be able to be copy-pasted
into a Python interpreter and ran as-is.
into a Python interpreter and run as-is.
placeholder: |
import SimPEG
<< your code here >>
Expand Down Expand Up @@ -58,4 +58,4 @@ body:
placeholder: |
<< your explanation here >>
validations:
required: false
required: false
11 changes: 8 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
rev: 23.12.1
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
language_version: python3
additional_dependencies: [flake8-bugbear, flake8-builtins, flake8-mutable, flake8-rst-docstrings, flake8-docstrings]
additional_dependencies:
- flake8-bugbear==23.12.2
- flake8-builtins==2.2.0
- flake8-mutable==1.2.0
- flake8-rst-docstrings==0.3.0
- flake8-docstrings==1.7.0
12 changes: 6 additions & 6 deletions SimPEG/data_misfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class inherits the :py:class:`SimPEG.objective_function.L2ObjectiveFunction`.
create your own data misfit class.
.. math::
\phi_d (\mathbf{m}) = \frac{1}{2} \| \mathbf{W} f(\mathbf{m}) \|_2^2
\phi_d (\mathbf{m}) = \| \mathbf{W} f(\mathbf{m}) \|_2^2
where :math:`\mathbf{m}` is the model vector, :math:`\mathbf{W}` is a linear weighting
matrix, and :math:`f` is a mapping function that acts on the model.
Expand Down Expand Up @@ -152,7 +152,7 @@ def W(self):
For a discrete least-squares data misfit function of the form:
.. math::
\phi_d (\mathbf{m}) = \frac{1}{2} \| \mathbf{W} \mathbf{f}(\mathbf{m}) \|_2^2
\phi_d (\mathbf{m}) = \| \mathbf{W} \mathbf{f}(\mathbf{m}) \|_2^2
:math:`\mathbf{W}` is a linear weighting matrix, :math:`\mathbf{m}` is the model vector,
and :math:`\mathbf{f}` is a discrete mapping function that acts on the model vector.
Expand Down Expand Up @@ -237,7 +237,7 @@ class L2DataMisfit(BaseDataMisfit):
data and predicted data for a given model. I.e.:
.. math::
\phi_d (\mathbf{m}) = \frac{1}{2} \big \| \mathbf{W_d}
\phi_d (\mathbf{m}) = \big \| \mathbf{W_d}
\big ( \mathbf{d}_\text{pred} - \mathbf{d}_\text{obs} \big ) \big \|_2^2
where :math:`\mathbf{d}_\text{obs}` is the observed data vector, :math:`\mathbf{d}_\text{pred}`
Expand Down Expand Up @@ -266,7 +266,7 @@ def __call__(self, m, f=None):
"""Evaluate the residual for a given model."""

R = self.W * self.residual(m, f=f)
return 0.5 * np.vdot(R, R)
return np.vdot(R, R)

@timeIt
def deriv(self, m, f=None):
Expand All @@ -293,7 +293,7 @@ def deriv(self, m, f=None):
if f is None:
f = self.simulation.fields(m)

return self.simulation.Jtvec(
return 2 * self.simulation.Jtvec(
m, self.W.T * (self.W * self.residual(m, f=f)), f=f
)

Expand Down Expand Up @@ -330,6 +330,6 @@ def deriv2(self, m, v, f=None):
if f is None:
f = self.simulation.fields(m)

return self.simulation.Jtvec_approx(
return 2 * self.simulation.Jtvec_approx(
m, self.W * (self.W * self.simulation.Jvec_approx(m, v, f=f)), f=f
)
119 changes: 43 additions & 76 deletions SimPEG/directives/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Sparse,
SparseSmallness,
PGIsmallness,
PGIwithNonlinearRelationshipsSmallness,
SmoothnessFirstOrder,
SparseSmoothness,
BaseSimilarityMeasure,
Expand Down Expand Up @@ -65,14 +64,13 @@ class InversionDirective:
_dmisfitPair = [BaseDataMisfit, ComboObjectiveFunction]

def __init__(self, inversion=None, dmisfit=None, reg=None, verbose=False, **kwargs):
# Raise error on deprecated arguments
if (key := "debug") in kwargs.keys():
raise TypeError(f"'{key}' property has been removed. Please use 'verbose'.")
self.inversion = inversion
self.dmisfit = dmisfit
self.reg = reg
debug = kwargs.pop("debug", None)
if debug is not None:
self.debug = debug
else:
self.verbose = verbose
self.verbose = verbose
set_kwargs(self, **kwargs)

@property
Expand All @@ -90,7 +88,7 @@ def verbose(self, value):
self._verbose = validate_type("verbose", value, bool)

debug = deprecate_property(
verbose, "debug", "verbose", removal_version="0.19.0", future_warn=True
verbose, "debug", "verbose", removal_version="0.19.0", error=True
)

@property
Expand Down Expand Up @@ -716,7 +714,6 @@ def initialize(self):
Smallness,
SparseSmallness,
PGIsmallness,
PGIwithNonlinearRelationshipsSmallness,
),
):
smallness += [obj]
Expand Down Expand Up @@ -1060,17 +1057,17 @@ def phi_d_star(self):
-------
float
"""
# the factor of 0.5 is because we do phid = 0.5*||dpred - dobs||^2
# phid = ||dpred - dobs||^2
if self._phi_d_star is None:
nD = 0
for survey in self.survey:
nD += survey.nD
self._phi_d_star = 0.5 * nD
self._phi_d_star = nD
return self._phi_d_star

@phi_d_star.setter
def phi_d_star(self, value):
# the factor of 0.5 is because we do phid = 0.5*||dpred - dobs||^2
# phid = ||dpred - dobs||^2
if value is not None:
value = validate_float(
"phi_d_star", value, min_val=0.0, inclusive_min=False
Expand Down Expand Up @@ -1166,21 +1163,21 @@ def phi_d_star(self):
-------
float
"""
# the factor of 0.5 is because we do phid = 0.5*|| dpred - dobs||^2
# phid = || dpred - dobs||^2
if getattr(self, "_phi_d_star", None) is None:
# Check if it is a ComboObjective
if isinstance(self.dmisfit, ComboObjectiveFunction):
value = np.r_[[0.5 * survey.nD for survey in self.survey]]
value = np.r_[[survey.nD for survey in self.survey]]
else:
value = np.r_[[0.5 * self.survey.nD]]
value = np.r_[[self.survey.nD]]
self._phi_d_star = value
self._DMtarget = None

return self._phi_d_star

@phi_d_star.setter
def phi_d_star(self, value):
# the factor of 0.5 is because we do phid = 0.5*|| dpred - dobs||^2
# phid =|| dpred - dobs||^2
if value is not None:
value = validate_ndarray_with_shape("phi_d_star", value, shape=("*",))
self._phi_d_star = value
Expand Down Expand Up @@ -1288,13 +1285,7 @@ def initialize(self):
np.r_[
i,
j,
(
isinstance(
regpart,
PGIwithNonlinearRelationshipsSmallness,
)
or isinstance(regpart, PGIsmallness)
),
isinstance(regpart, PGIsmallness),
]
)
for i, regobjcts in enumerate(self.invProb.reg.objfcts)
Expand Down Expand Up @@ -1332,13 +1323,7 @@ def initialize(self):
(
np.r_[
j,
(
isinstance(
regpart,
PGIwithNonlinearRelationshipsSmallness,
)
or isinstance(regpart, PGIsmallness)
),
isinstance(regpart, PGIsmallness),
]
)
for j, regpart in enumerate(self.invProb.reg.objfcts)
Expand Down Expand Up @@ -1426,11 +1411,11 @@ def CLtarget(self):
self._CLtarget = self.chiSmall * self.phi_ms_star

elif getattr(self, "_CLtarget", None) is None:
# the factor of 0.5 is because we do phid = 0.5*|| dpred - dobs||^2
# phid = ||dpred - dobs||^2
if self.phi_ms_star is None:
# Expected value is number of active cells * number of physical
# properties
self.phi_ms_star = 0.5 * len(self.invProb.model)
self.phi_ms_star = len(self.invProb.model)

self._CLtarget = self.chiSmall * self.phi_ms_star

Expand Down Expand Up @@ -1747,7 +1732,7 @@ def load_results(self):

self.f = results[:, 7]

self.target_misfit = self.invProb.dmisfit.simulation.survey.nD / 2.0
self.target_misfit = self.invProb.dmisfit.simulation.survey.nD
self.i_target = None

if self.invProb.phi_d < self.target_misfit:
Expand All @@ -1765,9 +1750,7 @@ def plot_misfit_curves(
plot_small=False,
plot_smooth=False,
):
self.target_misfit = (
np.sum([dmis.nD for dmis in self.invProb.dmisfit.objfcts]) / 2.0
)
self.target_misfit = np.sum([dmis.nD for dmis in self.invProb.dmisfit.objfcts])
self.i_target = None

if self.invProb.phi_d < self.target_misfit:
Expand Down Expand Up @@ -1821,7 +1804,7 @@ def plot_misfit_curves(
fig.savefig(fname, dpi=dpi)

def plot_tikhonov_curves(self, fname=None, dpi=200):
self.target_misfit = self.invProb.dmisfit.simulation.survey.nD / 2.0
self.target_misfit = self.invProb.dmisfit.simulation.survey.nD
self.i_target = None

if self.invProb.phi_d < self.target_misfit:
Expand Down Expand Up @@ -2062,7 +2045,7 @@ def target(self):
for survey in self.survey:
nD += survey.nD

self._target = nD * 0.5 * self.chifact_target
self._target = nD * self.chifact_target

return self._target

Expand All @@ -2076,10 +2059,10 @@ def start(self):
if isinstance(self.survey, list):
self._start = 0
for survey in self.survey:
self._start += survey.nD * 0.5 * self.chifact_start
self._start += survey.nD * self.chifact_start

else:
self._start = self.survey.nD * 0.5 * self.chifact_start
self._start = self.survey.nD * self.chifact_start
return self._start

@start.setter
Expand Down Expand Up @@ -2537,33 +2520,20 @@ def __init__(
normalization_method="maximum",
**kwargs,
):
if "everyIter" in kwargs.keys():
warnings.warn(
"'everyIter' property is deprecated and will be removed in SimPEG 0.20.0."
"Please use 'every_iteration'.",
stacklevel=2,
# Raise errors on deprecated arguments
if (key := "everyIter") in kwargs.keys():
raise TypeError(
f"'{key}' property has been removed. Please use 'every_iteration'.",
)
every_iteration = kwargs.pop("everyIter")

if "threshold" in kwargs.keys():
warnings.warn(
"'threshold' property is deprecated and will be removed in SimPEG 0.20.0."
"Please use 'threshold_value'.",
stacklevel=2,
if (key := "threshold") in kwargs.keys():
raise TypeError(
f"'{key}' property has been removed. Please use 'threshold_value'.",
)
threshold_value = kwargs.pop("threshold")

if "normalization" in kwargs.keys():
warnings.warn(
"'normalization' property is deprecated and will be removed in SimPEG 0.20.0."
if (key := "normalization") in kwargs.keys():
raise TypeError(
f"'{key}' property has been removed. "
"Please define normalization using 'normalization_method'.",
stacklevel=2,
)
normalization_method = kwargs.pop("normalization")
if normalization_method is True:
normalization_method = "maximum"
else:
normalization_method = None

super().__init__(**kwargs)

Expand All @@ -2590,7 +2560,11 @@ def every_iteration(self, value):
self._every_iteration = validate_type("every_iteration", value, bool)

everyIter = deprecate_property(
every_iteration, "everyIter", "every_iteration", removal_version="0.20.0"
every_iteration,
"everyIter",
"every_iteration",
removal_version="0.20.0",
error=True,
)

@property
Expand Down Expand Up @@ -2619,7 +2593,11 @@ def threshold_value(self, value):
self._threshold_value = validate_float("threshold_value", value, min_val=0.0)

threshold = deprecate_property(
threshold_value, "threshold", "threshold_value", removal_version="0.20.0"
threshold_value,
"threshold",
"threshold_value",
removal_version="0.20.0",
error=True,
)

@property
Expand Down Expand Up @@ -2669,18 +2647,6 @@ def normalization_method(self):
def normalization_method(self, value):
if value is None:
self._normalization_method = value

elif isinstance(value, bool):
warnings.warn(
"Boolean type for 'normalization_method' is deprecated and will be removed in 0.20.0."
"Please use None, 'maximum' or 'minimum'.",
stacklevel=2,
)
if value:
self._normalization_method = "maximum"
else:
self._normalization_method = None

else:
self._normalization_method = validate_string(
"normalization_method", value, string_list=["minimum", "maximum"]
Expand All @@ -2691,6 +2657,7 @@ def normalization_method(self, value):
"normalization",
"normalization_method",
removal_version="0.20.0",
error=True,
)

def initialize(self):
Expand Down

0 comments on commit 77731f5

Please sign in to comment.