inversion package
Submodules
inversion.base module
Base classes for geophysical inversion frameworks.
This module defines:
InversionResult: A base class to store and manage common results from an inversion process, including final model, predicted data, convergence history, and plotting utilities.
TimeLapseInversionResult: A specialized version of InversionResult for time-lapse studies, handling multiple models over time and providing time-slice plotting and animation.
InversionBase: An abstract base class outlining the common structure and interface for various geophysical inversion methods (e.g., ERT, SRT). It handles data, mesh, and basic parameter management.
- class inversion.base.InversionBase(data: pygimli.DataContainer, mesh: pygimli.Mesh | None = None, **kwargs: Any)[source]
Bases:
objectAbstract base class for geophysical inversion methods.
This class provides a foundational structure for specific inversion techniques (e.g., ERT, SRT). It manages observed data, mesh, common inversion parameters, and an InversionResult object. Subclasses must implement methods for setting up the inversion, running the inversion loop, computing Jacobians, and evaluating the objective function.
- compute_jacobian(model: ndarray) ndarray[source]
Abstract method to compute the Jacobian matrix (sensitivity matrix).
The Jacobian J_ij = ∂d_i / ∂m_j relates changes in model parameters (m_j) to changes in observed data (d_i).
- Parameters:
model (np.ndarray) – The current model parameter vector for which to compute the Jacobian.
- Returns:,
np.ndarray: The computed Jacobian matrix, typically of shape (n_data, n_model_params).
- objective_function(model: ndarray, data_to_fit: ndarray | None = None) float[source]
Abstract method to compute the value of the objective function.
The objective function typically includes a data misfit term and one or more regularization terms: Φ(m) = Φ_d(m) + λ * Φ_m(m).
- Parameters:
model (np.ndarray) – The current model parameter vector.
data_to_fit (Optional[np.ndarray], optional) – The observed data to fit. If None, self.data (from DataContainer) is typically used. Defaults to None.
- Returns:
The calculated value of the objective function.
- Return type:
float
- run() InversionResult[source]
Abstract method to run the main inversion loop.
Derived classes must implement this to perform the iterative optimization process. The method should populate and return an InversionResult (or subclass) object.
- Returns:
An object containing the results of the inversion.
- Return type:
- setup() None[source]
Abstract method to set up the inversion specifics.
This should be implemented by derived classes to prepare everything needed for the inversion, such as: - Creating or validating the mesh if not already done. - Initializing forward modeling operators. - Preparing data weighting and model regularization matrices. - Setting up initial models.
- class inversion.base.InversionResult[source]
Bases:
objectBase class to store, save, load, and plot results from a geophysical inversion.
- final_model
The final inverted model parameters (e.g., resistivity, velocity). Typically a 1D array corresponding to mesh cells.
- Type:
Optional[np.ndarray]
- predicted_data
The data predicted by the forward model using the final_model.
- Type:
Optional[np.ndarray]
- coverage
Coverage or sensitivity values for the model parameters, often derived from the Jacobian or resolution matrix.
- Type:
Optional[np.ndarray]
- mesh
The PyGIMLi mesh object used in the inversion.
- Type:
Optional[pg.Mesh]
- iteration_models
A list storing the model parameters at each iteration of the inversion.
- Type:
List[np.ndarray]
- iteration_data_errors
A list storing the data misfit (e.g., residuals) at each iteration.
- Type:
List[np.ndarray]
- iteration_chi2
A list storing the chi-squared (χ²) value or a similar misfit metric at each iteration.
- Type:
List[float]
- meta
A dictionary to store any additional metadata about the inversion run (e.g., inversion parameters, timings, comments).
- Type:
Dict[str, Any]
- classmethod load(filename: str) InversionResult[source]
Load inversion results from a file previously saved by the save method.
- Parameters:
filename (str) – The base path to the saved results file. If saved as name.pkl and name.bms, provide name.pkl or name.
- Returns:
- An instance of InversionResult (or a subclass if called on one)
populated with the loaded data.
- Return type:
- Raises:
FileNotFoundError – If the main data file or associated mesh file (if referenced) is not found.
IOError – If there’s an error during file reading.
pickle.UnpicklingError – If the file cannot be unpickled.
- plot_convergence(ax: Axes | None = None, **kwargs: Any) Tuple[Figure, Axes][source]
Plot the convergence curve (chi-squared misfit vs. iteration number).
- Parameters:
ax (Optional[plt.Axes], optional) – A matplotlib Axes object to plot on. If None, a new figure and axes are created. Defaults to None.
**kwargs (Any) – Additional keyword arguments passed directly to ax.plot (e.g., color, marker, linestyle).
- Returns:
The matplotlib Figure and Axes objects of the plot.
- Return type:
Tuple[plt.Figure, plt.Axes]
- Raises:
ValueError – If self.iteration_chi2 is empty (no convergence data).
- plot_model(ax: Axes | None = None, cmap: str = 'viridis', coverage_threshold: float | None = None, **kwargs: Any) Tuple[Figure, Axes][source]
Plot the final inverted model on its associated mesh.
- Parameters:
ax (Optional[plt.Axes], optional) – A matplotlib Axes object to plot on. If None, a new figure and axes are created. Defaults to None.
cmap (str, optional) – The colormap to use for visualizing model values. Defaults to ‘viridis’.
coverage_threshold (Optional[float], optional) – If provided, cells with coverage values below this threshold will be masked (made transparent or semi-transparent) in the plot. Requires self.coverage to be populated. Defaults to None (no masking).
**kwargs (Any) – Additional keyword arguments passed directly to pygimli.show (e.g., cMin, cMax, orientation, logScale).
- Returns:
The matplotlib Figure and Axes objects of the plot.
- Return type:
Tuple[plt.Figure, plt.Axes]
- Raises:
ValueError – If self.final_model or self.mesh is None.
- save(filename: str) None[source]
Save the inversion results to a file using Python’s pickle format. If a mesh is present, it is saved separately as a PyGIMLi binary mesh file (.bms).
- Parameters:
filename (str) – The base path (including filename without extension) to save the results. The main data will be saved as filename.pkl (or just filename if user includes .pkl). The mesh will be saved as filename.bms or filename.pkl.bms. It’s recommended to provide filename without .pkl.
- Raises:
IOError – If there’s an error during file writing.
pickle.PicklingError – If an object cannot be pickled.
- class inversion.base.TimeLapseInversionResult[source]
Bases:
InversionResultSpecialized class to store and manage results from time-lapse inversions. Inherits from InversionResult and adds attributes specific to time-lapse data.
- final_models
A 2D NumPy array where each column represents the inverted model for a specific timestep (shape: num_cells x num_timesteps).
- Type:
Optional[np.ndarray]
- timesteps
An array or list of time values (e.g., hours, days) corresponding to each model slice in final_models.
- Type:
Optional[np.ndarray]
- all_coverage
A list where each element is the coverage array for the corresponding timestep’s model.
- Type:
List[np.ndarray]
- all_chi2
A list where each element is a list of chi-squared values per iteration for the inversion of that specific timestep or window. (Note: Original was List[float], if chi2 is per window, might need adjustment)
- Type:
List[Any]
- create_time_lapse_animation(output_filename: str, cmap: str = 'viridis', coverage_threshold: float | None = None, dpi: int = 100, fps: int = 2, **kwargs: Any) None[source]
Create and save an animation (e.g., MP4 video) of the time-lapse inversion results.
Requires ffmpeg or another Matplotlib-supported animation writer to be installed.
- Parameters:
output_filename (str) – The filename for the output animation (e.g., ‘timelapse_animation.mp4’).
cmap (str, optional) – Colormap for model values. Defaults to ‘viridis’.
coverage_threshold (Optional[float], optional) – Threshold for coverage masking. Defaults to None.
dpi (int, optional) – Dots Per Inch for the output animation. Defaults to 100.
fps (int, optional) – Frames Per Second for the animation. Defaults to 2.
**kwargs (Any) – Additional keyword arguments passed to plot_time_slice for each frame.
- Raises:
ValueError – If final_models or mesh is missing.
ImportError – If matplotlib.animation cannot be imported.
- classmethod load(filename: str) TimeLapseInversionResult[source]
Load time-lapse inversion results. Overrides base class load.
- plot_time_slice(timestep_idx: int, ax: Axes | None = None, cmap: str = 'viridis', coverage_threshold: float | None = None, **kwargs: Any) Tuple[Figure, Axes][source]
Plot a single time slice (inverted model at a specific timestep) from the results.
- Parameters:
timestep_idx (int) – The zero-based index of the timestep to plot.
ax (Optional[plt.Axes], optional) – Matplotlib Axes to plot on. If None, creates new.
cmap (str, optional) – Colormap for model values. Defaults to ‘viridis’.
coverage_threshold (Optional[float], optional) – Threshold for coverage masking. Defaults to None.
**kwargs (Any) – Additional arguments passed to pg.show.
- Returns:
The matplotlib Figure and Axes objects.
- Return type:
Tuple[plt.Figure, plt.Axes]
- Raises:
ValueError – If models or mesh are missing, or if timestep_idx is out of range.
inversion.ert_inversion module
inversion.time_lapse module
inversion.windowed module
Module contents
Inversion framework for geophysical applications.
- class inversion.ERTInversion(data_file: str, mesh: pygimli.Mesh | None = None, **kwargs)[source]
Bases:
InversionBaseSingle-time ERT inversion class.
- run(initial_model: ndarray | None = None) InversionResult[source]
Run ERT inversion.
- Parameters:
initial_model – Initial model parameters (if None, a homogeneous model is used)
- Returns:
InversionResult with inversion results
- class inversion.InversionBase(data: pygimli.DataContainer, mesh: pygimli.Mesh | None = None, **kwargs: Any)[source]
Bases:
objectAbstract base class for geophysical inversion methods.
This class provides a foundational structure for specific inversion techniques (e.g., ERT, SRT). It manages observed data, mesh, common inversion parameters, and an InversionResult object. Subclasses must implement methods for setting up the inversion, running the inversion loop, computing Jacobians, and evaluating the objective function.
- compute_jacobian(model: ndarray) ndarray[source]
Abstract method to compute the Jacobian matrix (sensitivity matrix).
The Jacobian J_ij = ∂d_i / ∂m_j relates changes in model parameters (m_j) to changes in observed data (d_i).
- Parameters:
model (np.ndarray) – The current model parameter vector for which to compute the Jacobian.
- Returns:,
np.ndarray: The computed Jacobian matrix, typically of shape (n_data, n_model_params).
- objective_function(model: ndarray, data_to_fit: ndarray | None = None) float[source]
Abstract method to compute the value of the objective function.
The objective function typically includes a data misfit term and one or more regularization terms: Φ(m) = Φ_d(m) + λ * Φ_m(m).
- Parameters:
model (np.ndarray) – The current model parameter vector.
data_to_fit (Optional[np.ndarray], optional) – The observed data to fit. If None, self.data (from DataContainer) is typically used. Defaults to None.
- Returns:
The calculated value of the objective function.
- Return type:
float
- run() InversionResult[source]
Abstract method to run the main inversion loop.
Derived classes must implement this to perform the iterative optimization process. The method should populate and return an InversionResult (or subclass) object.
- Returns:
An object containing the results of the inversion.
- Return type:
- setup() None[source]
Abstract method to set up the inversion specifics.
This should be implemented by derived classes to prepare everything needed for the inversion, such as: - Creating or validating the mesh if not already done. - Initializing forward modeling operators. - Preparing data weighting and model regularization matrices. - Setting up initial models.
- class inversion.InversionResult[source]
Bases:
objectBase class to store, save, load, and plot results from a geophysical inversion.
- final_model
The final inverted model parameters (e.g., resistivity, velocity). Typically a 1D array corresponding to mesh cells.
- Type:
Optional[np.ndarray]
- predicted_data
The data predicted by the forward model using the final_model.
- Type:
Optional[np.ndarray]
- coverage
Coverage or sensitivity values for the model parameters, often derived from the Jacobian or resolution matrix.
- Type:
Optional[np.ndarray]
- mesh
The PyGIMLi mesh object used in the inversion.
- Type:
Optional[pg.Mesh]
- iteration_models
A list storing the model parameters at each iteration of the inversion.
- Type:
List[np.ndarray]
- iteration_data_errors
A list storing the data misfit (e.g., residuals) at each iteration.
- Type:
List[np.ndarray]
- iteration_chi2
A list storing the chi-squared (χ²) value or a similar misfit metric at each iteration.
- Type:
List[float]
- meta
A dictionary to store any additional metadata about the inversion run (e.g., inversion parameters, timings, comments).
- Type:
Dict[str, Any]
- classmethod load(filename: str) InversionResult[source]
Load inversion results from a file previously saved by the save method.
- Parameters:
filename (str) – The base path to the saved results file. If saved as name.pkl and name.bms, provide name.pkl or name.
- Returns:
- An instance of InversionResult (or a subclass if called on one)
populated with the loaded data.
- Return type:
- Raises:
FileNotFoundError – If the main data file or associated mesh file (if referenced) is not found.
IOError – If there’s an error during file reading.
pickle.UnpicklingError – If the file cannot be unpickled.
- plot_convergence(ax: Axes | None = None, **kwargs: Any) Tuple[Figure, Axes][source]
Plot the convergence curve (chi-squared misfit vs. iteration number).
- Parameters:
ax (Optional[plt.Axes], optional) – A matplotlib Axes object to plot on. If None, a new figure and axes are created. Defaults to None.
**kwargs (Any) – Additional keyword arguments passed directly to ax.plot (e.g., color, marker, linestyle).
- Returns:
The matplotlib Figure and Axes objects of the plot.
- Return type:
Tuple[plt.Figure, plt.Axes]
- Raises:
ValueError – If self.iteration_chi2 is empty (no convergence data).
- plot_model(ax: Axes | None = None, cmap: str = 'viridis', coverage_threshold: float | None = None, **kwargs: Any) Tuple[Figure, Axes][source]
Plot the final inverted model on its associated mesh.
- Parameters:
ax (Optional[plt.Axes], optional) – A matplotlib Axes object to plot on. If None, a new figure and axes are created. Defaults to None.
cmap (str, optional) – The colormap to use for visualizing model values. Defaults to ‘viridis’.
coverage_threshold (Optional[float], optional) – If provided, cells with coverage values below this threshold will be masked (made transparent or semi-transparent) in the plot. Requires self.coverage to be populated. Defaults to None (no masking).
**kwargs (Any) – Additional keyword arguments passed directly to pygimli.show (e.g., cMin, cMax, orientation, logScale).
- Returns:
The matplotlib Figure and Axes objects of the plot.
- Return type:
Tuple[plt.Figure, plt.Axes]
- Raises:
ValueError – If self.final_model or self.mesh is None.
- save(filename: str) None[source]
Save the inversion results to a file using Python’s pickle format. If a mesh is present, it is saved separately as a PyGIMLi binary mesh file (.bms).
- Parameters:
filename (str) – The base path (including filename without extension) to save the results. The main data will be saved as filename.pkl (or just filename if user includes .pkl). The mesh will be saved as filename.bms or filename.pkl.bms. It’s recommended to provide filename without .pkl.
- Raises:
IOError – If there’s an error during file writing.
pickle.PicklingError – If an object cannot be pickled.
- class inversion.TimeLapseERTInversion(data_files: List[str], measurement_times: List[float], mesh: pygimli.Mesh | None = None, **kwargs)[source]
Bases:
InversionBaseTime-lapse ERT inversion class.
- run(initial_model: ndarray | None = None) TimeLapseInversionResult[source]
Run time-lapse ERT inversion.
- Parameters:
initial_model – Initial model parameters (if None, a homogeneous model is used)
- Returns:
TimeLapseInversionResult with inversion results
- class inversion.TimeLapseInversionResult[source]
Bases:
InversionResultSpecialized class to store and manage results from time-lapse inversions. Inherits from InversionResult and adds attributes specific to time-lapse data.
- final_models
A 2D NumPy array where each column represents the inverted model for a specific timestep (shape: num_cells x num_timesteps).
- Type:
Optional[np.ndarray]
- timesteps
An array or list of time values (e.g., hours, days) corresponding to each model slice in final_models.
- Type:
Optional[np.ndarray]
- all_coverage
A list where each element is the coverage array for the corresponding timestep’s model.
- Type:
List[np.ndarray]
- all_chi2
A list where each element is a list of chi-squared values per iteration for the inversion of that specific timestep or window. (Note: Original was List[float], if chi2 is per window, might need adjustment)
- Type:
List[Any]
- create_time_lapse_animation(output_filename: str, cmap: str = 'viridis', coverage_threshold: float | None = None, dpi: int = 100, fps: int = 2, **kwargs: Any) None[source]
Create and save an animation (e.g., MP4 video) of the time-lapse inversion results.
Requires ffmpeg or another Matplotlib-supported animation writer to be installed.
- Parameters:
output_filename (str) – The filename for the output animation (e.g., ‘timelapse_animation.mp4’).
cmap (str, optional) – Colormap for model values. Defaults to ‘viridis’.
coverage_threshold (Optional[float], optional) – Threshold for coverage masking. Defaults to None.
dpi (int, optional) – Dots Per Inch for the output animation. Defaults to 100.
fps (int, optional) – Frames Per Second for the animation. Defaults to 2.
**kwargs (Any) – Additional keyword arguments passed to plot_time_slice for each frame.
- Raises:
ValueError – If final_models or mesh is missing.
ImportError – If matplotlib.animation cannot be imported.
- classmethod load(filename: str) TimeLapseInversionResult[source]
Load time-lapse inversion results. Overrides base class load.
- plot_time_slice(timestep_idx: int, ax: Axes | None = None, cmap: str = 'viridis', coverage_threshold: float | None = None, **kwargs: Any) Tuple[Figure, Axes][source]
Plot a single time slice (inverted model at a specific timestep) from the results.
- Parameters:
timestep_idx (int) – The zero-based index of the timestep to plot.
ax (Optional[plt.Axes], optional) – Matplotlib Axes to plot on. If None, creates new.
cmap (str, optional) – Colormap for model values. Defaults to ‘viridis’.
coverage_threshold (Optional[float], optional) – Threshold for coverage masking. Defaults to None.
**kwargs (Any) – Additional arguments passed to pg.show.
- Returns:
The matplotlib Figure and Axes objects.
- Return type:
Tuple[plt.Figure, plt.Axes]
- Raises:
ValueError – If models or mesh are missing, or if timestep_idx is out of range.
- class inversion.WindowedTimeLapseERTInversion(data_dir: str, ert_files: List[str], measurement_times: List[float], window_size: int = 3, mesh: pygimli.Mesh | str | None = None, **kwargs)[source]
Bases:
objectClass for windowed time-lapse ERT inversion to handle large temporal datasets.
- run(window_parallel: bool = False, max_window_workers: int | None = None) TimeLapseInversionResult[source]
Run windowed time-lapse ERT inversion.
- Parameters:
window_parallel – Whether to process windows in parallel
max_window_workers – Maximum number of parallel workers (None for auto)
- Returns:
TimeLapseInversionResult with stitched results