Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fix, single, background #329

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 78 additions & 20 deletions emg3d/inversion/pygimli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,26 @@ def __init__(self, simulation, markers=None, pgthreads=2):
z=simulation.model.grid.nodes_z,
)

# Set mesh and, if provided, markers.
# Set markers.
if markers is not None:
mesh.setCellMarkers(markers.ravel('F'))
self.markers = markers
else:
self.markes = np.zeros(simulation.model.size, dtype=int)
# Store original properties; required if a region is set to ``background``.
self._model = simulation.model.property_x.copy()
# Store volumes; required if a region is set to ``single``.
self._volumes = simulation.model.grid.cell_volumes.reshape(
self._model.shape, order='F')
# Set mesh.
self.setMesh(mesh)

# Create J, store and set it.
self.J = self.Jacobian(
simulation=self.simulation,
data2pygimli=self.data2pygimli,
data2gimli=self.data2gimli,
data2emg3d=self.data2emg3d,
model2pygimli=self.model2pygimli,
model2gimli=self.model2gimli,
model2emg3d=self.model2emg3d,
)
self.setJacobian(self.J)
Expand All @@ -112,17 +121,16 @@ def response(self, model):
_ = self.simulation.misfit

# Return the responses as pyGIMLi array
return self.data2pygimli(self.simulation.data.synthetic.data)
return self.data2gimli(self.simulation.data.synthetic.data)

def createStartModel(self, dataVals=None):
"""Returns the model from the provided simulation."""
return self.model2pygimli(self.simulation.model.property_x)
return self.model2gimli(self.simulation.model.property_x)

def createJacobian(self, model):
"""Dummy to prevent pyGIMLi from doing it the hard way."""
pass # do nothing

def data2pygimli(self, data):
def data2gimli(self, data):
"""Convert an emg3d data-xarray to a pyGIMLi data array."""
out = data[self.simulation.survey.isfinite]
if np.iscomplexobj(out):
Expand All @@ -141,34 +149,85 @@ def data2emg3d(self, data):
out[self.simulation.survey.isfinite] = data[:ind] + 1j*data[ind:]
return out

def model2pygimli(self, model):
def model2gimli(self, model):
"""Convert an emg3d Model property to a pyGIMLi model array.

This function deals with the regions defined in pyGIMLi.
"""
out = np.empty(model.size)
out[self.mesh().cellMarkers()] = model.ravel('F')

# If the inversion model is smaller than the model, we have to
# take care of the regions.
if len(model) != self.simulation.model.size:

out = np.empty(self.simulation.model.size)
i = 0

for n, v in self.regionProperties().items():
ni = self.markers == n
if v['background'] or v['fix']:
ii = 0
elif v['single']:
ii = 1
out[i] = np.average(model[ni], weights=self._volumes[ni])
else:
ii = np.sum(ni)
out[i:i+ii] = model[ni]
i += ii

out = out[:i]

else:
out = np.empty(model.size)
out[self.mesh().cellMarkers()] = model.ravel('F')

return out

def model2emg3d(self, model):
"""Convert a pyGIMLi model array to an emg3d Model property.

This function deals with the regions defined in pyGIMLi.
"""
out = np.asarray(model[self.mesh().cellMarkers()])
return out.reshape(self.simulation.model.shape, order='F')

# If the inversion model is smaller than the model, we have to
# take care of the regions.
if len(model) != self.simulation.model.size:

out = np.empty(self.simulation.model.shape)
i = 0

for n, v in self.regionProperties().items():
ni = self.markers == n
if v['background']:
ii = 0
out[ni] = self._model[ni]
elif v['fix']:
ii = 0
out[ni] = v['startModel']
elif v['single']:
ii = 1
out[ni] = model[i]
else:
ii = np.sum(ni)
out[ni] = model[i:ii+i]
i += ii

else:
out = np.asarray(model[self.mesh().cellMarkers()]).reshape(
self.simulation.model.shape, order='F')

return out

class Jacobian(pygimli.Matrix):
"""Return Jacobian operator for pyGIMLi(emg3d)."""

def __init__(self, simulation,
data2pygimli, data2emg3d, model2pygimli, model2emg3d):
data2gimli, data2emg3d, model2gimli, model2emg3d):
"""Initiate a new Jacobian instance."""
super().__init__()
self.simulation = simulation
self.data2pygimli = data2pygimli
self.data2gimli = data2gimli
self.data2emg3d = data2emg3d
self.model2pygimli = model2pygimli
self.model2gimli = model2gimli
self.model2emg3d = model2emg3d

def cols(self):
Expand All @@ -182,16 +241,15 @@ def rows(self):
def mult(self, x):
"""Multiply the Jacobian with a vector, Jm."""
jvec = self.simulation.jvec(vector=self.model2emg3d(x))
return self.data2pygimli(jvec)
return self.data2gimli(jvec)

def transMult(self, x):
"""Multiply Jacobian transposed with a vector, Jᵀd = (dJᵀ)ᵀ."""
jtvec = self.simulation.jtvec(self.data2emg3d(x))
return self.model2pygimli(jtvec)
return self.model2gimli(jtvec)

def save(self, *args):
"""There is no save for this pseudo-Jacobian."""
pass


@utils._requires('pygimli')
Expand All @@ -213,12 +271,12 @@ def run(self, dataVals=None, errorVals=None, **kwargs):

# Take data from the survey if not provided.
if dataVals is None:
dataVals = self.fop.data2pygimli(
dataVals = self.fop.data2gimli(
self.fop.simulation.data.observed.data)

# Take the error from the survey if not provided.
if errorVals is None:
std_dev = self.fop.data2pygimli(
std_dev = self.fop.data2gimli(
self.fop.simulation.survey.standard_deviation.data)
errorVals = std_dev / abs(dataVals)

Expand Down
Loading