cofi.utils.InversionPool

cofi.utils.InversionPool#

class cofi.utils.InversionPool(list_of_inv_problems: List[BaseProblem] | BaseProblem, list_of_inv_options: List[InversionOptions] | InversionOptions, callback: Callable[[InversionResult, int], None] | None = None, parallel: bool = False)[source]#

This class manages an ensemble of inversions and allows them to be run in parallel or sequentially.

Parameters:
  • list_of_inv_problems (Union[List[BaseProblem], BaseProblem]) – A list of problems for which the inversions need to be performed, or a single problem. Each problem should be an instance of the BaseProblem class.

  • list_of_inv_options (Union[List[InversionOptions], InversionOptions]) – A list of options for the inversions, or a single set of options. Each set of options should be an instance of the InversionOptions class.

  • callback (Callable[[InversionResult, int], None], optional) – A callback function that gets invoked for each problem after its inversion has been performed. It should accept two arguments - the result of the inversion (an instance of InversionResult) and the index of the problem. The callback can return a result that will be collected and returned in the callback results list. If no callback function is provided, None will be used.

  • parallel (bool, optional) – A flag that determines whether the inversions should be performed in parallel or sequentially. If True, the inversions will be performed in parallel using multiple processes. If False or not provided, the inversions will be performed sequentially in the same process.

Examples

An example of using this function to make an L-curve:

>>> import cofi
>>> import matplotlib.pyplot as plt
>>> def my_objective(m, lamda):
...     data_misfit = numpy.sum((m * data_x**2 - data_y) ** 2)
...     regularization = lamda * numpy.sum(m**2)
...     return data_misfit + regularization
...
>>> my_problems = []
>>> my_lamdas = numpy.logspace(-8, 8, 30)
>>> for lamda in my_lamdas:
...     my_problem = cofi.BaseProblem()
...     my_problem.set_objective(my_objective, args=(lamda,))
...     my_problem.set_initial_model(100)
...     my_problems.append(my_problem)
...
>>> my_options = cofi.InversionOptions()
>>> my_options.set_tool("scipy.optimize.minimize")
>>> def my_callback(inv_result, i):
...     m = inv_result.model
...     data_misfit = numpy.linalg.norm(m * data_x**2 - data_y)
...     model_norm = numpy.linalg.norm(m)
...     return data_misfit, model_norm
...
>>> my_ensemble_of_inversions = cofi.utils.InversionPool(my_problems, my_options, my_callback, False)
>>> all_results, all_cb_returns = my_ensemble_of_inversions.run()
>>> l_curve_points = list(zip(*all_cb_returns))
>>> plt.plot(l_curve_points)

Reference Details

run()[source]#

Runs all the inversions in the ensemble, either in parallel or sequentially.

Returns:

A tuple containing two lists - the first list contains the results of the inversions (as InversionResult objects) for each problem in the same order as the input problems list. The second list contains the results returned by the callback function for each problem in the same order as the input problems list.

Return type:

Tuple[List[InversionResult], List]

back to top