cofi.solvers.BaseSolver#

class cofi.solvers.BaseSolver(inv_problem, inv_options)[source]#

Base class for backend solver wrappers

This is the point where we connect cofi to other inversion libraries or code. We expose this as a part of cofi’s public interface, to facilitate minimal effort to link cofi to your own inversion code or external libraries that aren’t connected by us yet.

To create your own inversion solver, simply subclass BaseSolver and define the following methods & fields.

Minimal implementation

Define the following minimally. Input validation will be performed automatically when a new instance is created, based on what you’ve returned for the first four class methods below.

BaseSolver.required_in_problem()

a set of components required in BaseProblem instance

BaseSolver.optional_in_problem()

a dictionary of components that are optional in BaseProblem instance

BaseSolver.required_in_options()

a set of solver-specific options required in InversionOptions instance

BaseSolver.optional_in_options()

dict: a dictioanry of solver-specific options that are optional in InversionOptions instance

BaseSolver.__init__(inv_problem, inv_options)

initialisation routine for the solver instance

BaseSolver.__call__()

the method that calls the backend inversion routines

Displaying

In addition, the following class variables help us display properly. Failure to include them won’t cause an error, but may result in some information mismatch in displaying methods like cofi.Inversion.summary.

BaseSolver.short_description

a short introduction about the solver.

BaseSolver.documentation_links

references about the backend solver.

Make it more complete

All backend solvers in cofi also update the following field, and this will be displayed via cofi.Inversion.summary. It’s not required but good to keep track of this:

BaseSolver.components_used

a set of strings describing what components defined in BaseProblem are used in this solving process.

back to top

Reference Details

abstract __call__() dict[source]#

the method that calls the backend inversion routines

This is an abstract method, meaning that you have to implement this on your own in the subclass, otherwise the definition of the subclass will cause an error directly.

Returns

a Python dictionary that has at least model/models and success as keys

Return type

dict

__init__(inv_problem, inv_options)[source]#

initialisation routine for the solver instance

You will need to implement this in the subclass, and it’s recommended to have the following line included:

super().__init__(inv_problem, inv_options)

What it does are: - Attaching the BaseProblem and InversionOptions objects to

self;

Alternatively (if you want), you can also define your own validation routines, then you don’t have to call the __init__ method defined in this super class, and don’t have to add things to the fields.

Parameters
  • inv_problem (BaseProblem) – an inversion problem setup

  • inv_options (InversionOptions) – an object that defines how to run the inversion

abstract classmethod optional_in_options() dict[source]#

dict: a dictioanry of solver-specific options that are optional in InversionOptions instance

This is a standard part for a subclass of BaseSolver and helps validate input InversionOptions instance

abstract classmethod optional_in_problem() dict[source]#

a dictionary of components that are optional in BaseProblem instance

The keys stand for name of the components in BaseProblem, and the values input BaseProblem instance

abstract classmethod required_in_options() set[source]#

a set of solver-specific options required in InversionOptions instance

This is a standard part for a subclass of BaseSolver and helps validate input InversionOptions instance

abstract classmethod required_in_problem() set[source]#

a set of components required in BaseProblem instance

This is a standard part for a subclass of BaseSolver and helps validate input BaseProblem instance

components_used#

a set of strings describing what components defined in BaseProblem are used in this solving process. This is typically the intersection of three sets: cofi.BaseProblem.all_components, BaseSolver.required_in_problem and keys of BaseSolver.optional_in_problem

references about the backend solver. It helps ensure the audience understands what the backend solver does. The list can include the official documentation if the solver wraps an external library, or links to papers and other online resources explaining the approach

For example, we use the following as the documentation_links for solver wrapping scipy.linalg.lstsq, so that users can see the details of what’s in the backend:

documentation_links = [
    "https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.lstsq.html",
    "https://www.netlib.org/lapack/lug/node27.html",
]
Type

list

inv_options#

the inveersion settings

This is the second argument in the constructor of BaseSolver

inv_problem#

the inversion problem to be solved

This is the first argument in the constructor of BaseSolver

short_description = ''#

a short introduction about the solver. This is for display purpose only

Type

str

back to top