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

Set up cofi.utils and create Regularisation utilities #65

Merged
merged 14 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

<!--next-version-placeholder-->

## v0.1.2.dev10 (NOT YET RELEASED)

### CoFI Utils

- [#65](https://github.com/inlab-geo/cofi/issues/54) Utility functions using findiff to generate the difference matrices


## v0.1.2.dev9 (13/09/2022)

### CoFI Core
Expand Down
18 changes: 8 additions & 10 deletions docs/_templates/autosummary/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@

.. rubric:: Reference Details

.. ----

{% for item in members %}
{% if item in ['__call__'] %}
.. automethod:: {{ objname }}.{{ item }}
.. automethod:: {{ objname }}.{{ item }}
{% endif %}
{% if "BaseRegularisation" in objname and item == '__add__' %}
.. automethod:: {{ objname }}.{{ item }}
{% endif %}
{% endfor %}

.. :ref:`back to top <top_{{objname}}>`

.. ----

{% for item in methods %}

{% if "BaseSolver" in objname and item == '__init__' %}
.. automethod:: {{ objname }}.{{ item }}
.. automethod:: {{ objname }}.{{ item }}
{% endif %}

{% if item != '__init__' %}
.. automethod:: {{ objname }}.{{ item }}
.. automethod:: {{ objname }}.{{ item }}
{% endif %}
{% endfor %}

.. :ref:`back to top <top_{{objname}}>`

----

{% for item in attributes %}
.. autoattribute:: {{ objname }}.{{ item }}
.. autoattribute:: {{ objname }}.{{ item }}
{% endfor %}

:ref:`back to top <top_{{objname}}>`
Expand All @@ -51,7 +49,7 @@

.. rubric:: Reference Details

.. automethod:: cofi.SamplingResult.to_arviz
.. automethod:: cofi.SamplingResult.to_arviz

:ref:`back to top <top_{{objname}}>`
{% endif %}
Expand Down
12 changes: 12 additions & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ tools, simply create a subclass of :code:`BaseSolver` and implements :code:`__in
:toctree: generated/

cofi.solvers.BaseSolver


CoFI Utils
----------

Module :mod:`cofi.utils` contains a set of helper classes and routines to enable
easier problem definitions.

.. autosummary::
:toctree: generated/

cofi.utils
2 changes: 1 addition & 1 deletion docs/cofi-examples
Submodule cofi-examples updated 66 files
+ notebooks/fmm_tomography/fmm_gaussian_prior_naive_newton.png
+ notebooks/fmm_tomography/fmm_gaussian_prior_scipy_Newton-CG.png
+249 −0 notebooks/fmm_tomography/fmm_tomography.ipynb
+102 −0 notebooks/fmm_tomography/fmm_tomography_bootstrap.py
+102 −0 notebooks/fmm_tomography/fmm_tomography_gaussian_prior.py
+115 −0 notebooks/fmm_tomography/fmm_tomography_gaussian_prior_bootstrap.py
+121 −0 notebooks/fmm_tomography/fmm_tomography_hyperparams_search.py
+122 −0 notebooks/fmm_tomography/fmm_tomography_solver_script.py
+ notebooks/fmm_tomography/fmm_true_model.png
+100 −100 notebooks/gravity/gravity_density.ipynb
+471 −471 notebooks/gravity/gravity_density_lab.ipynb
+325 −325 notebooks/linear_regression/linear_regression.ipynb
+443 −443 notebooks/linear_regression/linear_regression_lab.ipynb
+1 −1 notebooks/pygimli_dcip/tmp/pygimli_dcip.ipynb
+1 −1 notebooks/pygimli_ert/archived/pygimli_ert.ipynb
+525 −507 notebooks/pygimli_ert/pygimli_ert.ipynb
+1 −1 notebooks/simpeg_gravity/tmp/simpeg_gravity.ipynb
+785 −0 notebooks/xray_tomography/xray_tomography.ipynb
+32 −0 notebooks/xray_tomography/xray_tomography_solver_script.py
+1 −1 tools/generate_example/_template/example_name.ipynb
+2 −2 tools/generate_example/create_new_example.py
+2 −2 tools/sphinx_gallery/generated/gravity_density.ipynb
+1 −1 tools/sphinx_gallery/generated/gravity_density.py
+1 −1 tools/sphinx_gallery/generated/gravity_density.py.md5
+22 −39 tools/sphinx_gallery/generated/gravity_density.rst
+ tools/sphinx_gallery/generated/gravity_density_codeobj.pickle
+ tools/sphinx_gallery/generated/images/sphx_glr_gravity_density_001.png
+ tools/sphinx_gallery/generated/images/sphx_glr_gravity_density_002.png
+ tools/sphinx_gallery/generated/images/sphx_glr_linear_regression_001.png
+ tools/sphinx_gallery/generated/images/sphx_glr_linear_regression_002.png
+ tools/sphinx_gallery/generated/images/sphx_glr_linear_regression_003.png
+ tools/sphinx_gallery/generated/images/sphx_glr_linear_regression_004.png
+ tools/sphinx_gallery/generated/images/sphx_glr_linear_regression_005.png
+ tools/sphinx_gallery/generated/images/sphx_glr_linear_regression_006.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_001.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_002.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_003.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_004.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_005.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_006.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_007.png
+ tools/sphinx_gallery/generated/images/sphx_glr_pygimli_ert_008.png
+ tools/sphinx_gallery/generated/images/sphx_glr_xray_tomography_001.png
+ tools/sphinx_gallery/generated/images/thumb/sphx_glr_xray_tomography_thumb.png
+21 −3 tools/sphinx_gallery/generated/index.rst
+3 −3 tools/sphinx_gallery/generated/linear_regression.ipynb
+3 −4 tools/sphinx_gallery/generated/linear_regression.py
+1 −1 tools/sphinx_gallery/generated/linear_regression.py.md5
+86 −140 tools/sphinx_gallery/generated/linear_regression.rst
+ tools/sphinx_gallery/generated/linear_regression_codeobj.pickle
+5 −5 tools/sphinx_gallery/generated/pygimli_ert.ipynb
+10 −15 tools/sphinx_gallery/generated/pygimli_ert.py
+1 −1 tools/sphinx_gallery/generated/pygimli_ert.py.md5
+6,689 −7,181 tools/sphinx_gallery/generated/pygimli_ert.rst
+ tools/sphinx_gallery/generated/pygimli_ert_codeobj.pickle
+6 −4 tools/sphinx_gallery/generated/sg_execution_times.rst
+266 −0 tools/sphinx_gallery/generated/xray_tomography.ipynb
+194 −0 tools/sphinx_gallery/generated/xray_tomography.py
+1 −0 tools/sphinx_gallery/generated/xray_tomography.py.md5
+438 −0 tools/sphinx_gallery/generated/xray_tomography.rst
+ tools/sphinx_gallery/generated/xray_tomography_codeobj.pickle
+1 −1 tools/sphinx_gallery/ipynb_to_gallery.py
+1 −1 tools/sphinx_gallery/scripts/gravity_density.py
+3 −4 tools/sphinx_gallery/scripts/linear_regression.py
+10 −15 tools/sphinx_gallery/scripts/pygimli_ert.py
+194 −0 tools/sphinx_gallery/scripts/xray_tomography.py
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def setup(app):
"scipy": ("https://docs.scipy.org/doc/scipy/", None),
"numpy": ("https://numpy.org/doc/stable/", None),
"arviz": ("https://python.arviz.org/en/latest/", None),
"findiff": ("https://findiff.readthedocs.io/en/latest/", None),
}

# Disable including boostrap CSS for sphinx_panels since it's already included
Expand Down Expand Up @@ -156,6 +157,7 @@ def setup(app):
"dollarmath",
"html_image",
]
nb_execution_mode = "cache"


# -- Cutomised variables ------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CoFI requires Python 3.6+, and the following dependencies:
- scipy>=1.0.0
- emcee>=3.1.0
- arviz>=0.9.0
- findiff>=0.7.0


Virtual environment setup
Expand Down
1 change: 1 addition & 0 deletions envs/environment_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ dependencies:
- watermark
- sphinxcontrib-svg2pdfconverter==1.2.0
- papermill
- findiff>=0.7.0
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from io import StringIO
import sys
from pathlib import Path
from setuptools import find_namespace_packages

try:
from skbuild import setup
Expand Down Expand Up @@ -54,8 +55,7 @@
"License :: OSI Approved :: BSD License",
]
PACKAGE_DIR = {"": "src"}
# PACKAGES = find_packages("src")
PACKAGES = ["cofi"]
PACKAGES = find_namespace_packages(where='src')
CMAKE_INSTALL_DIR = "src/cofi"
CMAKE_ARGS=['-DSKBUILD=ON']
PYTHON_REQUIRES = ">=3.6"
Expand All @@ -64,6 +64,7 @@
"scipy>=1.0.0",
"emcee>=3.1.0",
"arviz>=0.9.0",
"findiff>=0.7.0",
]
EXTRAS_REQUIRE = {
"petsc": ["petsc4py>=3.16.0"],
Expand Down
2 changes: 1 addition & 1 deletion src/cofi/base_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ def set_model_shape(self, model_shape: Tuple):
np.reshape(self.initial_model, model_shape)
except ValueError as err:
raise DimensionMismatchError(
entered_dimenion=model_shape,
entered_dimension=model_shape,
entered_name="model shape",
expected_dimension=self.initial_model.shape,
expected_source="initial model"
Expand Down
11 changes: 6 additions & 5 deletions src/cofi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,23 @@ class DimensionMismatchError(CofiError, ValueError):
def __init__(
self,
*args,
entered_dimenion: Tuple,
entered_dimension: Tuple,
entered_name: str,
expected_dimension: Tuple,
expected_source: str,
) -> None:
super().__init__(*args)
self._entered_dimension = entered_dimenion
self._entered_dimension = entered_dimension
self._entered_name = entered_name
self._expected_dimension = expected_dimension
self._expected_source = expected_source

def __str__(self) -> str:
super_msg = super().__str__()
msg = f"the {self._entered_name} you've provided {self._entered_dimension}" \
f" doesn't match and cannot be reshaped into the dimension you've set" \
f" for {self._expected_source} which is {self._expected_dimension}"
msg = f"the {self._entered_name} you've provided (shape: " \
f"{self._entered_dimension}) doesn't match and cannot be reshaped " \
f"into the dimension you've set for {self._expected_source} which is " \
f"{self._expected_dimension}"
return self._form_str(super_msg, msg)


Expand Down
13 changes: 13 additions & 0 deletions src/cofi/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Utility classes and functions (e.g. to generate regularisation terms and more)
"""

from .regularisation import (
BaseRegularisation,
QuadraticReg,
)


__all__ = [
"BaseRegularisation",
"QuadraticReg",
]
Loading