pygimli.frameworks#
Unified and method independent inversion frameworks.
Overview#
Functions
|
Generic function fitter. |
|
GIMLi-based curve-fit by harmonic functions. |
|
Python-based curve-fit by harmonic functions. |
|
Carry out line search. |
|
Formal model (and data) resolution matrix (MCM) from inversion. |
Classes
|
Inversion of layered models (including layer thickness). |
|
General forward operator for 1D layered models. |
|
Functor for harmonic functions plus offset and drift. |
alias of |
|
|
Cumulative (joint) forward operator. |
|
Joint inversion targeting at the same parameter through petrophysics. |
|
Quasi-2D Laterally constrained inversion (LCI) framework. |
|
2D Laterally constrained (LC) modelling. |
|
LSQR solver based inversion |
Modelling class for linearized problems with a given matrix. |
|
|
Marquardt scheme, i.e. local damping with decreasing strength. |
|
Method manager for mesh-based modelling and inversion. |
|
Modelling class with a mesh discretization. |
|
General manager to maintenance a measurement method. |
|
Method Manager base class for managers on a 1d discretization. |
|
Abstract Forward Operator. |
|
Full frame (multiple fop parallel) forward modelling. |
|
Framework to invert unconstrained parameters. |
|
Model with symbolic parameter names instead of numbers. |
|
Class for petrophysical inversion (s. |
|
Combine petrophysical relation with the modelling class f(p). |
|
Forward operator for grabbing values out of a mesh (prior data). |
Functions#
- pygimli.frameworks.fit(funct, data, err=None, **kwargs)[source]#
Generic function fitter.
Fit data to a given function.
- Parameters:
funct (callable) – Function with the first argmument as data space, e.g., x, t, f, Nr. .. Any following arguments are the parameters to be fit. Except if a verbose flag if used.
data (iterable (float)) – Data values
err (iterable (float) [None]) – Data error values in %/100. Default is 1% if None are given.
*dataSpace* (iterable) – Keyword argument of the data space of len(data). The name need to fit the first argument of funct.
- Returns:
model (array) – Fitted model parameter.
response (array) – Model response.
Example
>>> import pygimli as pg >>> >>> func = lambda t, a, b: a*np.exp(b*t) >>> t = np.linspace(1, 2, 20) >>> data = func(t, 1.1, 2.2) >>> model, response = pg.frameworks.fit(func, data, t=t) >>> print(pg.core.round(model, 1e-5)) 2 [1.1, 2.2] >>> _ = pg.plt.plot(t, data, 'o', label='data') >>> _ = pg.plt.plot(t, response, label='response') >>> _ = pg.plt.legend()
- pygimli.frameworks.harmfit(y, x=None, error=None, nc=42, resample=None, window=None, verbose=False, **kwargs)[source]#
GIMLi-based curve-fit by harmonic functions.
- Parameters:
y (1d-array) – values to be fitted
x (1d-array(len(y))) – data abscissa data. default: [0 .. len(y))
error (1d-array(len(y))) – error of y. default (absolute error = 0.01)
nc (int) – Number of harmonic coefficients
resample (1d-array) – resample y to x using fitting coeffients
window (int) – just fit data inside window bounds
- Returns:
response (1d-array of len(resample) or len(x)) – smoothed values
inv (pg.Inversion) – inversion instance, coefficients are in inv.model
- pygimli.frameworks.harmfitNative(y, x=None, nc=None, xc=None, error=None, err=None)[source]#
Python-based curve-fit by harmonic functions.
- Parameters:
y (iterable) – values of a curve to be fitted
x (iterable) – abscissa, if none [0..len(y))
nc (int) – number of coefficients
err (iterable) – absolute data error
xc (iterable) – abscissa to predict y on (otherwise equal to x)
- pygimli.frameworks.lineSearch(inv, dm, method='auto', **kwargs)[source]#
Carry out line search.
Optimize step length s such that m + s*dm is minimized.
Parameter#
- invpg.Inversion
Inversion instance
- dmiterable
model update direction
- methodstr [‘auto’]
Method to be used: ‘exact’ : function evaluation for every step ‘interp’ : linear interpolation of response ‘quad’ : fitting a parabola through 3 points ‘auto’: first try ‘inter’, then ‘quad’, else 0.1
- tausarray [None]
array containing the tau values to test, alternatively:
- tauminfloat [0.01]
minimum value
- taumaxfloat [1]
maximum value
- logScalebool [False]
use logarithmic scaling, otherwise linear
- showbool [False]
show line search curve
- pygimli.frameworks.resolutionMatrix(inv, returnRD=False)[source]#
Formal model (and data) resolution matrix (MCM) from inversion.
\[ \begin{align}\begin{aligned}\mathbf{R_M} = (\mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J} + \alpha \mathbf{C}^T\mathbf{C})^{-1} \mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J}\\\mathbf{R_D} = \mathbf{D}\mathbf{J} (\mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J} + \alpha \mathbf{C}^T\mathbf{C})^{-1} \mathbf{J}^T\mathbf{D}^T\end{aligned}\end{align} \]- Parameters:
inv (pg.Inversion) – pygimli inversion instance after inversion
returnRD (bool [false]) – also return data resolution (information) matrix
- Returns:
RM (numpy.array) – model resolution matrix
RD (numpy.array) – data resolution matrix
Examples using pygimli.frameworks.resolutionMatrix
Computing resolution properties
Classes#
- class pygimli.frameworks.Block1DInversion(fop=None, **kwargs)[source]#
Bases:
MarquardtInversion
Inversion of layered models (including layer thickness).
- Variables:
nLayers (int)
- run(dataVals, errorVals, nLayers=None, fixLayers=None, layerLimits=None, paraLimits=None, **kwargs)[source]#
Run inversion with given data and error vectors.
- Parameters:
nLayers (int [4]) – Number of layers.
fixLayers (bool | [thicknesses]) – See:
pygimli.modelling.Block1DInversion.fixLayers
For fixLayers=None, preset or defaults are uses.layerLimits ([min, max]) – Limits the thickness off all layers. For layerLimits=None, preset or defaults are uses.
paraLimits ([min, max] | [[min, max],...]) – Limits the range of the model parameter. If you have multiple parameters you can set them with a list of limits.
**kwargs – Forwarded to the parent class. See:
pygimli.modelling.MarquardtInversion
- class pygimli.frameworks.Block1DModelling(nPara=1, nLayers=4, **kwargs)[source]#
Bases:
Modelling
General forward operator for 1D layered models.
Model space: [thickness_i, parameter_jk], with i = 0 - nLayers-1, j = (0 .. nLayers), k=(0 .. nPara)
- drawData(ax, data, err=None, label=None, **kwargs)[source]#
Default data view.
Modelling creates the data and should know best how to draw them.
Probably ugly and you should overwrite it in your derived forward operator.
- property nLayers#
Number of layers.
- property nPara#
Number of parameters.
- class pygimli.frameworks.HarmFunctor(A, coeff, xmin, xSpan)[source]#
Bases:
object
Functor for harmonic functions plus offset and drift.
- pygimli.frameworks.Inversion#
alias of
ClassicInversion
- class pygimli.frameworks.JointModelling(fopList)[source]#
Bases:
MeshModelling
Cumulative (joint) forward operator.
- class pygimli.frameworks.JointPetroInversionManager(petros, mgrs)[source]#
Bases:
MeshMethodManager
Joint inversion targeting at the same parameter through petrophysics.
This is just syntactic sugar for the combination of
pygimli.frameworks.PetroModelling
andpygimli.frameworks.JointModelling
.
- class pygimli.frameworks.LCInversion(fop=None, **kwargs)[source]#
Bases:
ClassicInversion
Quasi-2D Laterally constrained inversion (LCI) framework.
- class pygimli.frameworks.LCModelling(fop, **kwargs)[source]#
Bases:
Modelling
2D Laterally constrained (LC) modelling.
2D Laterally constrained (LC) modelling based on BlockMatrices.
- createParametrization(nSoundings, nLayers=4, nPar=1)[source]#
Create LCI mesh and suitable constraints informations.
- class pygimli.frameworks.LSQRInversion(*args, **kwargs)[source]#
Bases:
ClassicInversion
LSQR solver based inversion
- class pygimli.frameworks.LinearModelling(A)[source]#
Bases:
Modelling
Modelling class for linearized problems with a given matrix.
- property parameterCount#
Define the number of parameters from the matrix size.
- class pygimli.frameworks.MarquardtInversion(fop=None, **kwargs)[source]#
Bases:
ClassicInversion
Marquardt scheme, i.e. local damping with decreasing strength.
- run(dataVals, errorVals=None, **kwargs)[source]#
Run inversion with given data and error vectors.
- Parameters:
dataVals (iterable) – data vector
errorVals (iterable) – error vector (relative errors), can also be computed from
absoluteError (float | iterable) – absolute error in units of dataVals
relativeError (float | iterable) – relative error related to dataVals
**kwargs – Forwarded to the parent class. See:
pygimli.modelling.Inversion
- class pygimli.frameworks.MeshMethodManager(**kwargs)[source]#
Bases:
MethodManager
Method manager for mesh-based modelling and inversion.
- __init__(**kwargs)[source]#
Initialize by calling super methods.
Attribute#
- mesh: GIMLI::Mesh
Copy of the main mesh to be distributed to inversion and the fop. You can overwrite it with invert(mesh=mesh).
- applyMesh(mesh, ignoreRegionManager=False, **kwargs)[source]#
Pass the mesh along to the forward operator.
- createMesh(data=None, **kwargs)[source]#
Create default inversion mesh.
Inversion mesh for traveltime inversion does not need boundary region.
- Parameters:
data (DataContainer) – Data container to read sensors from.
- Keyword Arguments:
` (Forwarded to)
- invert(data=None, mesh=None, startModel=None, **kwargs)[source]#
Run the full inversion.
- Parameters:
data (pg.DataContainer)
mesh (GIMLI::Mesh [None])
startModel (float | iterable [None]) – If set to None fop.createDefaultStartModel(dataValues) is called.
- Keyword Arguments:
zWeight (float [None]) – Set zWeight or use defaults from regionManager.
correlationLengths ([float, float, float]) – Correlation lengths for geostatistical regularization
dip (float) – rotation axis between first and last dimension (x and z)
strike (float) – rotation axis between first and second dimension (x and y)
limits ([float, float]) – lower and upper value bounds for logarithmic transformation
Inversion.run (Al other are forwarded to)
- Returns:
model – Model mapped for match the paraDomain Cell markers. The calculated model vector (unmapped) is in self.fw.model.
- Return type:
array
- property model#
Inversion model.
- property paraDomain#
Parameter (inversion) domain mesh.
- class pygimli.frameworks.MeshModelling(**kwargs)[source]#
Bases:
Modelling
Modelling class with a mesh discretization.
- __init__(**kwargs)[source]#
Initialize.
- Variables:
fop (pg.frameworks.Modelling)
data (pg.DataContainer)
modelTrans ([pg.trans.TransLog()])
- Parameters:
**kwargs – fop : Modelling
- createRefinedFwdMesh(mesh)[source]#
Refine the current mesh for higher accuracy.
This is called automatic when accessing self.mesh() so it ensures any effect of changing region properties (background, single).
- ensureContent()[source]#
Internal function to ensure there is a valid initialized mesh.
Initialization means the cell marker are recounted and/or there was a mesh refinement or boundary enlargement, all to fit the needs for the method-depending forward problem.
- property paraDomain#
Return parameter (inverse) mesh.
- setCustomConstraints(C)[source]#
Set custom constraints matrix for lazy evaluation.
To remove them set it to ‘None’ again.
- class pygimli.frameworks.MethodManager(fop=None, fw=None, data=None, **kwargs)[source]#
Bases:
object
General manager to maintenance a measurement method.
Method Manager are the interface to end-user interaction and can be seen as simple but complete application classes which manage all tasks of geophysical data processing.
The method manager holds one instance of a forward operator and an appropriate inversion framework to handle modelling and data inversion.
Method Manager also helps with data import and export, handle measurement data error estimation as well as model and data visualization.
- Variables:
verbose (bool) – Give verbose output.
debug (bool) – Give debug output.
fop (
pygimli.frameworks.Modelling
) – Forward Operator instance .. knows the physics. fop is initialized bypygimli.manager.MethodManager.initForwardOperator
and calls a validpygimli.manager.MethodManager.createForwardOperator
method in any derived classes.inv (
pygimli.frameworks.Inversion
.) – Inversion framework instance .. knows the reconstruction approach. The attribute inv is initialized by default but can be changed overwritingpygimli.manager.MethodManager.initInversionFramework
- __init__(fop=None, fw=None, data=None, **kwargs)[source]#
Initialize the class with given fop, inv and data.
- checkError(err, dataVals=None)[source]#
Check and return relative error.
By default we assume ‘err’ as relative values. Overwrite in derived class if needed.
- static createArgParser(dataSuffix='dat')[source]#
Create default argument parser.
TODO move this to some kind of app class
Create default argument parser for the following options:
-Q, –quiet -R, –robustData: options.robustData -B, –blockyModel: options.blockyModel -l, –lambda: options.lam -i, –maxIter: options.maxIter –depth: options.depth
- createForwardOperator(**kwargs)[source]#
Mandatory interface for derived classes.
Here you need to specify which kind of forward operator FOP you want to use. This is called by any initForwardOperator() call.
- Parameters:
**kwargs – Any arguments that are necessary for your FOP creation.
- Returns:
Instance of any kind of
pygimli.framework.Modelling
.- Return type:
- createInversionFramework(**kwargs)[source]#
Create default Inversion framework.
Derived classes may overwrite this method.
- Parameters:
**kwargs – Any arguments that are necessary for your creation.
- Returns:
Instance of any kind of
pygimli.framework.Inversion
.- Return type:
Inversion
- property data#
- property debug#
Debug mode (extensive output).
- estimateError(data, errLevel=0.01, absError=None)[source]#
Estimate data error.
Create an error of estimated measurement error. On default it returns an array of constant relative errors. More sophisticated error estimation should be done in specialized derived classes.
- property fop#
Forward operator.
- property fw#
Inversion framework.
- property inv#
Inversion instance.
- invert(data=None, err=None, **kwargs)[source]#
Invert the data.
Invert the data by calling self.inv.run() with mandatory data and error values.
- TODO
*need dataVals mandatory? what about already loaded data
- Parameters:
dataVals (iterable) – Data values to be inverted.
errVals (iterable | float) – Error value for the given data. If errVals is float we assume this means to be a global relative error and force self.estimateError to be called.
- property model#
Inversion model.
- reinitForwardOperator(**kwargs)[source]#
Reinitialize the forward operator.
Sometimes it can be useful to reinitialize the forward operator. Keyword arguments will be forwarded to ‘self.createForwardOperator’.
- showData(data=None, ax=None, **kwargs)[source]#
Show the data.
Draw data values into a given axes or show the data values from the last run. Forwards on default to the self.fop.drawData function of the modelling operator. If there is no given function given, you have to override this method.
- Parameters:
ax (mpl axes) – Axes object to draw into. Create a new if its not given.
data (iterable | pg.DataContainer) – Data values to be draw.
- Return type:
ax, cbar
- showModel(model, ax=None, **kwargs)[source]#
Show a model.
Draw model into a given axes or show inversion result from last run. Forwards on default to the self.fop.drawModel function of the modelling operator. If there is no function given, you have to override this method.
- Parameters:
ax (mpl axes) – Axes object to draw into. Create a new if its not given.
model (iterable) – Model data to be draw.
- Return type:
ax, cbar
- showResult(model=None, ax=None, **kwargs)[source]#
Show the last inversion result.
- Parameters:
ax (mpl axes) – Axes object to draw into. Create a new if its not given.
model (iterable [None]) – Model values to be draw. Default is self.model from the last run
- Return type:
ax, cbar
- showResultAndFit(**kwargs)[source]#
Call showResults and showFit.
- Keyword Arguments:
saveFig (str[None]) – If not None save figure.
axs ([mpl.Axes]) – Give 3 axes and its plotted into them instead of creating 3 new.
- property verbose#
Verbose output.
- class pygimli.frameworks.MethodManager1d(fop=None, **kwargs)[source]#
Bases:
MethodManager
Method Manager base class for managers on a 1d discretization.
- class pygimli.frameworks.Modelling(**kwargs)[source]#
Bases:
ModellingBaseMT__
Abstract Forward Operator.
Abstract Forward Operator that is or can use a Modelling instance. Can be seen as some kind of proxy Forward Operator.
- STy(y)[source]#
Right-hand side multiplication of Jacobian J.T*y.
By default, uses self.jacobian().transMult(x) Overwrite for efficient use with gradient-type inversion.
- Sx(x)[source]#
Right-hand side multiplication of Jacobian J*x.
By default, uses self.jacobian().mult(x) Overwrite for efficient use with gradient-type inversion.
- __init__(**kwargs)[source]#
Initialize.
- Variables:
fop (pg.frameworks.Modelling)
data (pg.DataContainer)
modelTrans ([pg.trans.TransLog()])
- Parameters:
**kwargs – fop : Modelling
- createDefaultStartModel(dataVals)[source]#
Create the default startmodel as the median of the data values.
- createStartModel(dataVals=None)[source]#
Create the default startmodel as the median of the data values.
Overwriting might be a good idea. Its used by inversion to create a valid startmodel if there are no starting values from the regions.
- property data#
Return data.
- estimateError(data, **kwargs)[source]#
Create data error fallback when the data error is not known.
Should be implemented method-specific.
- property fop#
Forward operator.
- property modelTrans#
Return model transformation.
- property parameterCount#
Return parameter count.
- regionProperties(regionNr=None)[source]#
Return dictionary of all properties for region number regionNr.
- setInterRegionCoupling(region1, region2, weight=1.0)[source]#
Set the weighting for constraints across regions.
- setRegionProperties(regionNr, **kwargs)[source]#
Set region properties. regionNr can be ‘*’ for all regions.
startModel=None, limits=None, trans=None, cType=None, zWeight=None, modelControl=None, background=None, fix=None, single=None, correlationLengths=None, dip=None, strike=None
- Parameters:
regionNr (int, [ints], '*') – Region number, list of numbers, or wildcard “*” for all.
startModel (float) – starting model value
limits ([float, float]) – lower and upper limit for value using a barrier transform
trans (str) – transformation for model barrier: “log”, “cot”, “lin”
cType (int) – constraint (regularization) type
zWeight (float) – relative weight for vertical boundaries
background (bool) – exclude region from inversion completely (prolongation)
fix (float) – exclude region from inversion completely (fix to value)
single (bool) – reduce region to one unknown
correlationLengths ([floats]) – correlation lengths for geostatistical inversion (x’, y’, z’)
dip (float [0]) – angle between x and x’ (first correlation length)
strike (float [0]) – angle between y and y’ (second correlation length)
- class pygimli.frameworks.MultiFrameModelling(modellingOperator, scalef=1.0, **ini)[source]#
Bases:
MeshModelling
Full frame (multiple fop parallel) forward modelling.
- property parameterCount#
Return number of parameters.
- class pygimli.frameworks.ParameterInversionManager(funct=None, fop=None, **kwargs)[source]#
Bases:
MethodManager
Framework to invert unconstrained parameters.
- class pygimli.frameworks.ParameterModelling(funct=None, **kwargs)[source]#
Bases:
Modelling
Model with symbolic parameter names instead of numbers.
- property params#
Return number of parameters.
- class pygimli.frameworks.PetroInversionManager(petro, mgr=None, **kwargs)[source]#
Bases:
MeshMethodManager
Class for petrophysical inversion (s. Rücker et al. 2017).
- class pygimli.frameworks.PetroModelling(fop, petro, **kwargs)[source]#
Bases:
MeshModelling
Combine petrophysical relation with the modelling class f(p).
Combine petrophysical relation \(p(m)\) with a modelling class \(f(p)\) to invert for the petrophysical model \(p\) instead of the geophysical model \(m\).
\(p\) be the petrophysical model, e.g., porosity, saturation, … \(m\) be the geophysical model, e.g., slowness, resistivity, …
- __init__(fop, petro, **kwargs)[source]#
Save forward class and transformation, create Jacobian matrix.
- createJacobian(model)[source]#
Fill the individual jacobian matrices.
J = dF(m) / dm = dF(m) / dp * dp / dm
- property petro#
Petrophysical model transformation.
- class pygimli.frameworks.PriorModelling(mesh=None, pos=None, **kwargs)[source]#
Bases:
MeshModelling
Forward operator for grabbing values out of a mesh (prior data).