Skip to main content

Common Framework for Inference

Project description

CoFI (Common Framework for Inference)

Build Documentation Status codecov Slack

Introduction

These are my main notes on the initial implementation of CoFI. Surely this will all be thrown away and done properly when someone has time to devote to doing it properly, but nevertheless this initial implementation will hopefully serve as a good initial scaffold on which to think clearly about CoFI design and implementation choices.

In the remainder of this document, I will discuss briefly the high level design choices made in this initial implementaton, and in various parts I discuss possible alternatives.

What is CoFI?

CoFI is a framework that will help bring together people doing physical modelling (i.e. solving forward problems) and people working in inversion methods so that each can benefit from the others work without needing to be an expert in the other area. It is also anticipated that CoFI would be a good teaching platform, for students to experiement and learn about inversion and/or physcial modelling.

So there are four user groups that CoFI will target:

  1. Physical modellers (forward problem solvers)
  2. Inverse modellers
  3. Teachers
  4. Students

Design

Language choice

Within each of the target groups, there will be a lot of heterogeneity in terms of the level of experience, the languages favoured (Matlab, python, Fortran, C++), and the libraries used. It is not possible to cater to all of these, so some pragmatic choices have to be made up front about what will be covered. Users, in whatever group, will have to interact with CoFI via either:

  • a simple YML file that defines their experiment/computation
  • code in a compiled language (Fortran, C, C++) that fits a particular specification (more on this in a moment)
  • code in python either through a python script or a jupyter notebook

It is too much work initially to try and support other languages or runtimes, such as (for example) Matlab.

Simple install and examples

Given the heterogeneity of users, it is important that CoFI is as easy as possible to install and get started with. The patience of users for complex installation, configuration, or use of CoFI will probably be the main limited resource affecting CoFI's success. Success thus means:

  • Having a simple/clean installation
  • Having well documented examples that cover all the common cases of what people want to do
  • Having a lot of error handling code within CoFI itself to catch user errors and provide helpful error messages

Installing CoFI

I have NOT made any attempt to do any neat packaging/install of CoFI. But I think it is crucial to CoFI's success. So some thought (and work) needs to be put into packaging for CoFI. This probably means packaging CoFI up as a conda or apt/deb package.

In the absence of an installer, for testing the reference implementation, you will need the following installed:

  • python3.6 or later
  • All the typical python scientific libraries (scipy, numpy, pandas, etc etc). I just install everthing in the jupyter/datascience-notebook docker image.
  • C and fortran compilers (e.g. g++ gfortran)
  • blas and lapack (e.g. libblas-dev liblapack-dev). These are not part of CoFI but needed for one of the examples
  • you need the plotnine python package (just for plotting in the examples)
  • You'll also need to be set up to run a jupyter notebook if you want to step through some of the examples

I have included a Dockerfile in the CoFI repo which is what I have been using to develop and test in. You can refer to that if you want to recreate the build environment. You can pull the actual image at docker.io/peterrickwood/cofi

Forward model interface: cofi_misfit and cofi_init

In we want people to be able to bring along their own forward code and easily plug that in to CoFI, we need to define some standard functions that CoFI will use to interface with their code. We discussed this at length in early May, and decided that the cleanest approach was to require the user to define a misfit function: given a model, calculate the error of that model. This is cleanest because it means we do not need to deal directly with the users forward model or with the error/loss function. This is a good thing, because we can cover the case where a user just has some simple misfit calculation (e.g. squared loss between predicted and observed), but also the case where the misfit represents a probability density for inclusion in somethign like MCMC.

So we require that the user define a function cofi_misfit that calculates a misfit (a single float) based on an arbitrary number of model parameters. This is very similar to the interface that is used by scipy.optimize, with the main difference being that rather than requiring a single parameter vector (as required in scipy.optimze), we will allow an arbitrary number of parameters, each of arbitrary shape, to cofi_misfit. So you can have cofi_misfit(A, v, x) where your model is made up of A (a matrix), v (a vector)), and x (a scalar).

See the examples to see how this works out in practice.

We also require a function cofi_init to be defined. This is called once, before anything else, and gives the user code a chance to do any required setup/processing prior to inversion. This might involve reading in files, doing preliminary calculations, etc.

So, the general setup for CoFI is:

  • User defines cofi_init(...) and cofi_misfit(...) routines, in either python or compiled
  • User defines their experiment in either YML or via python api calls (examples of which later)
  • CoFI performs the experiment by calling cofi_init once and then calling cofi_misfit many times.

Inverse model interface

We want to make it as easy as possible for people to include inverse solvers into CoFI.

I dont have any examples of this so far. A python example would be easy, but a compiled-language solver would require some more thinking and work.

YAML structure

Here is the YAML structure I have landed on for the moment. There are 4 main sections:

  • model specifies the model structure
  • init_info specifies any miscellaneous information needed for the experiment
  • fwd_code specifies where the forward/misfit code lives
  • method specifies what inverse solver to use, and any required arguments

Here is an example

# The model structure, consisting of a list of parameters, of any shape.
# Parameters can have an initial value, or can have a specified distribution (i.e. a pdf)
# specifying the distribution of values the parameter can take. Any distributiion
# in scipy.stats can be used for the pdf.
model: &idmodel 
  parameters:
    - name: x
      bounds: uniform 1 10  # uniform between 1 and 11
      value: 10             # Initial value 10
    - name:
      bounds: norm 0 1      # normal with mean 0 stddev 1
      value: 0              # initial value 0

# General configuration information can be specified as arbitrary key/value pairs
# This information is passeed into cofi_init() on first call, and allows
# the user to keep config information and some parameters out of their code
# and in their yaml config, if they want
init_info:
    - item1: 2.0
    - item2: 42.0
    - item3: 60065
    .....

# User supplied forward code and misfit calculation 
fwd_code: &idfwd
  name: rfc        # the name of the module
  location: path   # the path to where the code lives (either python or compiled code)

method:
  # The name of the inverter. This must be something that is packaged with CoFI
  # We can allow users to plug in their own inversion code, but need to work out how 
  # best to do this. 
  name: DumbDescent 
  # Every inverter takes *at least* two mandatory arguments:
  #     * 'model' specifies the structure of the model being inverted (at this stage I
  #       havent thought about how to deal with reversible jump MCMC type models where
  #       the dimension of the model space is itself part of the search space)
  #     * 'forward' links to the forward code to be used in the inversion. This *must*
  #       implement cofi_misfit() and cofi_init() functions.
  #
  # Additional arguments will be specific to the inverter. In this case, the inverter takes
  # 'step' and 'time' arguments, but different inverters will take different arguments,
  # and you would specify them here.
  args:
      # These first 2 arguments are common to every inverse solver
      model: *idmodel
      forward: *idfwd
      # The remaining ones are arbitrary key/value pairs specific to the solver being used
      time: 30  # run for 30 seconds
      step: 0.2 # size of step in model search space
 

Examples

There is a separate notebook for each of the following examples:

  • Running a CoFI inversion from a YAML file, using a compiled forward problem (receiver function) and one of CoFI's in-built inverse solvers
  • Running a CoFI inversion from python, using a compiler forward problem (receiver function) and one of CoFI's in-built inverse solvers
  • Running a CoFI inversion from YAML, using a python-defined forward problem (rosenbrock) and one of CoFI's in-built inverse solvers
  • Running a CoFI inversion from python, using a python-defined forward problem (rosenbrock) and one of CoFI's in-built inverse solvers

At the moment, CoFI only has a single, very simple, inverse-solver/optimizer, which I have implemented just to test the interfaces. It is called DumbDescent and is used in all the above examples.

Things still to do

OK, so I hope what we have is useful as a start, but there are still 3 things that I havent tackled that really need to be addressed:

  1. How will pepole plug in their own inverse solvers? Discussed above, but at least 1 example of each type (python or compiled language) are required ASAP so we can better appeciate the work required.
  2. Flesh out yaml spec and interface More examples should be implemented so we can flesh out the YAML specification, and test how well the cofi_init/cofi_misfit interface allows us to cover those examples
  3. Gradient-based inversion/optimization I've only covered direct search/optimization so far. I am not clear on the requirements for situations where the user wants to use the Hessian or Jacobian, so I haven't tried to tackle that in the examples so far. I guess this is really just a special case of 2.
  4. If possible, solve the packaging/installation problem, add detailed error checking/messaging, and some help for people authoring their yaml and auto-annotating their wrapper code.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cofi-0.1.0.dev1.tar.gz (231.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cofi-0.1.0.dev1-cp310-cp310-win_amd64.whl (180.9 kB view details)

Uploaded CPython 3.10Windows x86-64

cofi-0.1.0.dev1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cofi-0.1.0.dev1-cp310-cp310-macosx_10_9_x86_64.whl (201.8 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

cofi-0.1.0.dev1-cp39-cp39-win_amd64.whl (180.8 kB view details)

Uploaded CPython 3.9Windows x86-64

cofi-0.1.0.dev1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cofi-0.1.0.dev1-cp39-cp39-macosx_10_9_x86_64.whl (201.7 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

cofi-0.1.0.dev1-cp38-cp38-win_amd64.whl (180.9 kB view details)

Uploaded CPython 3.8Windows x86-64

cofi-0.1.0.dev1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cofi-0.1.0.dev1-cp38-cp38-macosx_10_9_x86_64.whl (201.4 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

File details

Details for the file cofi-0.1.0.dev1.tar.gz.

File metadata

  • Download URL: cofi-0.1.0.dev1.tar.gz
  • Upload date:
  • Size: 231.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1.tar.gz
Algorithm Hash digest
SHA256 5290ab433207531ad6627329ff19e40266fd1f346bb11f4b39db40d538479c0a
MD5 00d252087b55669bc0c4bf9b3c00fa7f
BLAKE2b-256 412a2dfb0441471322bf359b564527527c1e404fc27ae05a0072c8fcfef425e1

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 180.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2951ddfab04dd2339a051a6d3a5d955227f328767b1a1b531cadf8f96f201f2b
MD5 741d71ae9e7d969fb013627ef60d85c7
BLAKE2b-256 2b61566b9d1cb07848d29faabcfa3e6ae71ef6b701b6e66807dc5d1ddc785ea1

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5f5970572dfb0476cf35067dbffb320f209fd306208d4046cb14b9b9cff7b39e
MD5 97036d62b5bd3ef972ec9ed18a2d41c9
BLAKE2b-256 bed9c77c81b533f2ab35faa149750f79ea26ce51408812398b0cf29850cfae0b

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp310-cp310-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 201.8 kB
  • Tags: CPython 3.10, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3295f0b8a58dd6f0ad3b1a23508d6049a459770ea295e245c035bd095e9a3fbe
MD5 dd07fbb58c8e806492d865983982171c
BLAKE2b-256 db583d5e712e18bfff77b3f84cf4d193fe7a139f75d59cf6b5587537b649917a

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 180.8 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8dfcfd96228c5612cbfde6b35053d27d7b21b7161647ab446813ce430f29f4b8
MD5 a46f8b089992c6a3cb71ecc3d4a2ca87
BLAKE2b-256 29233458fe3a5c4811c81975323656268f095fb7ce99d021ed78bb1a97571d07

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d86b12c58bc4a2f16ec1e4d2d18bc4806cc3f9f9edcb561bcef23bb83aef7be
MD5 e2cfd43f789d8ae54b9a5eab9ead67e0
BLAKE2b-256 240aac1c34c13fe93e0d7c8caa7895677fb866f1a2cb93cddae951f8b72e42b1

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 201.7 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a08f94684f3eb6dccc853bee72dac89cdfb71044735c054803310855a6fd6b29
MD5 a4715e2ecf8dbda3c9876081eccf1013
BLAKE2b-256 067fdd7971cf99c1e133a79793d02177151bb5580d38d70a07688cb093e60182

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 180.9 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a3099c7a8100cb971e2c470f31e21aba7463096ebb7374b5ec2a112d3b71f6e4
MD5 1b470db7ed1b622c620d0f5071e9eb19
BLAKE2b-256 0543eafb5913caad5c687deceb3a823645f8830900dc4a78f79e4e8ce4a3d88d

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4a50960557b53a1cda44598e18065fba5cba6dbdaa8ad64ac315da79f93e6f70
MD5 88d118a723e0eb3c8d8660e884ccc457
BLAKE2b-256 fdcb6218a0fb232d060b701fda22e01999691a3b78cee9853be2e4a38eeb0875

See more details on using hashes here.

File details

Details for the file cofi-0.1.0.dev1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cofi-0.1.0.dev1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 201.4 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for cofi-0.1.0.dev1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ca3a53d037290230d40acc730363cb6bbd4eec8790cc23ce18ad9edb29419b04
MD5 c4141ad543f2b14b4025e1edd991072b
BLAKE2b-256 fee7994618d38e186a360c3f66294b7c11c05a319fa458cef50bd92e174f1870

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page