Skip to main content

GitHub release (latest by date) Build Status last-commit DOI codecov Code style: black

sotb-wrapper: A Python wrapper for the SEISCOPE optimization toolbox (using Ctypes)

This repo demonstrates how it is possible to use the SEISCOPE optimization toolbox (written in Fortran) from Python. The original code is public domain and was written by Ludovic Métivier and Romain Brossier. Minor changes to the original code have been made to allow the call of the gradient-based optimization subroutines from Python. Such changes and some improvements are listed as follows.

  • The original source organized in 6 subdirectories (each of which is associated with one gradient-based algorithm) was placed in only one folder in a modular fashion. That is, one module for each optimization algorithm grouping the procedures from each one of the old subdirectories.
  • The Euclidean vector norm and scalar product calculations were replaced with calls to the intrinsic Fortran norm2 and dot_product functions, respectively.
  • Removing Trivial Code Duplication: i.e., same procedures in Steepest Descent and Preconditioned Nonlinear Conjugate Gradient subdirectories.
  • Removing unused variable declarations.
  • Vectors of lower and upper bounds (box constraints) are now optional arguments in the optimization subroutines instead of array components of a derived data type

The SEISCOPE toolbox uses a derived data type (optim); functionality that is not yet supported at this time by f2py - and for this reason it is used ctypes. The optim data type is maintained, but without allocatable arrays.

The repo contains a src directory with the modified fortran source files and another named apps where each method is used to find the minimum of the banana Rosenbrock function. The python wrapper for the SEISCOPE optimization toolbox is found inside the sotb_wrapper directory. A test directory includes a script to check that the wrapper has suceeded in reproducing the results of the original fortran code.

Install Seiscope optimization toolbox (sotb)

If you only want to use the Fortran library, you can simply clone the repo and build it with Fortran Package Manager or CMake. In the first case you just need to run

fpm build --profile release

This command creates the library in static form, as originally designed as well as executable files from demo codes.

To use sotb within your fpm project, add the following to your fpm.toml file:

[dependencies]
sotb = { git="https://github.com/ofmla/seiscope_opt_toolbox_w_ctypes.git" }

In the second case, you can run a workflow as the following:

FC=gfortran cmake -B _build -DCMAKE_INSTALL_PREFIX=$PREFIX -DCMAKE_BUILD_TYPE=Release
cmake --build _build
cmake --install _build

where you need to replace $PREFIX with the desired directory.

Examples of use of sotb can be found in the app folder, which contains a folder with an example for each one of the optimization algorithms available in the library. The executable files for each example are built with cmake invocation above and made available at $PREFIX/bin folder. As mentioned before, when you use fpm, executable files for the examples are also created. In this latest case, you can use fpm run --profile release <test_name> to run an specific example. So, if you want to run the example that uses the limited-memory version of Broyden-Fletcher-Goldfarb-Shanno (L-BFGS) algorithm, simply run fpm run --profile release test_LBFGS. If you run fpm run --profile release you can see the names of the six available examples. You can also find a simple example on calling the Fortran subroutines from a C main program in the c_code directory. The example uses the L-BFGS to minimize the Rosenbrock's "banana function". Assuming that $PREFIX points to the repository root directory, you can create the executable from c_code directory, by running

cmake -S. -B _build -DCMAKE_PREFIX_PATH="`pwd`/../../"
cmake --build _build

or

cmake -S. -B _build -Dsotb_DIR="`pwd`/../../lib/cmake/sotb"
cmake --build _build

Install the python wrapper of sotb (sotb-wrapper)

To install the Python API with the embedded sotb shared library you can use pip.

pip install sotb-wrapper

It is also possible to install directly from the GitHub repository. You will need a Fortran compiler such as GFortran to compile the shared library but the whole process is automated via scikit-build.

pip install git+https://github.com/ofmla/seiscope_opt_toolbox_w_ctypes

Usage

The following example demonstrates how to define and solve the classical Rosenbrock problem

import numpy as np
from sotb_wrapper import interface

# Declare the objective function
def rosenbrock(X):
    """
    http://en.wikipedia.org/wiki/Rosenbrock_function
    A generalized implementation is available
    as the scipy.optimize.rosen function
    """
    a = 1. - X[0]
    b = X[1] - X[0]*X[0]
    return a*a + b*b*100., np.array([-a*2. - 400.*X[0]*b, 200.*b], dtype=np.float32)
    
# Create an instance of the SEISCOPE optimization toolbox wrapper (sotb_wrapper) Class. 
sotb = interface.sotb_wrapper()
n = 2 # dimension
flag = 0 # first flag; 0 means initialization
X = np.ones(2, dtype=np.float32)*-1. # initial guess

# computation of the cost and gradient associated with the initial guess
fcost, grad = rosenbrock(X)
# copy of grad in grad_preco: no preconditioning in this test
grad_preco = np.copy(grad)
# Set parameters of the UserDefined derived type in Fortran (ctype structure).
# The first two parameters are mandatory; all others are optional. 
sotb.set_inputs(fcost, 10000, conv=1e-8, l=10)

# optimization loop: while convergence not reached or linesearch not failed, iterate
while (flag != 2 and flag != 4):
    flag = sotb.PSTD(n, X, fcost, grad, grad_preco, flag)
    if flag == 1:
        # compute cost and gradient at point x
        fcost, grad = rosenbrock(X)
        # no preconditioning in this test: simply copy grad in grad_preco
        grad_preco = np.copy(grad)
print('FINAL iterate is : ', X)

The code above is part of a tutorial in the form of a Jupyter notebook (rosenbrock.ipynb) provided in the examples subdirectory. The goal of the tutorial is show you how one can use sotb-wrapper to find a minimum for a problem, which can optionally be subject to bound constraints (also called box constraints). The directory also includes examples in the context of geophysical inversion. Note that you must have Devito in order to be able to run them. A python script plot_curves.py is also provide in the examples directory. It may not be the best implementation and is intended for illustrative purposes only.

The following figures were obtained with the plot_curves.py script after ran one of the examples (lsrtm_aniso.py).

License

sotb-wrapper is distributed under the MIT license. See the included LICENSE file for details.

See also

Metadata

Release files for sotb-wrapper 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sotb-wrapper 2.0.2
File Size Uploaded
sotb-wrapper-2.0.2.tar.gz 1.1 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for sotb-wrapper 2.0.2
File
sotb_wrapper-2.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
sotb_wrapper-2.0.2-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details
sotb_wrapper-2.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
sotb_wrapper-2.0.2-cp39-cp39-macosx_10_9_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.9+ x86-64 Details
sotb_wrapper-2.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details
sotb_wrapper-2.0.2-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details
sotb_wrapper-2.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64 Details
sotb_wrapper-2.0.2-cp37-cp37m-macosx_10_9_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64 Details
sotb_wrapper-2.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-64 Details
sotb_wrapper-2.0.2-cp36-cp36m-macosx_10_9_x86_64.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ x86-64 Details

Total release size: 13.3 MB

Release files / sotb-wrapper-2.0.2.tar.gz

Download URL sotb-wrapper-2.0.2.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
7d89410fee715deba446b3129cc17c47332be8ffcca680b283733f8243924ac5
BLAKE2b-256 checksum
How to use checksums
9a22df97443206b0f5be8bbe5ff2383d758ed4ea0ab8db5eceab3e0648f9890b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.1 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
8679c75ceb9336a7c29102050bdfba94ab73dd850118d66f6b9b95bcf49edb00
BLAKE2b-256 checksum
How to use checksums
e12529fd6d831055cd08b9ccf545fe73e891001e0fc2c9e3f3b92c165366f524
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp310-cp310-macosx_10_9_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp310-cp310-macosx_10_9_x86_64.whl
Size 1.3 MB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
9f71c4070c80b68a05a9f430b79f57ee67de767f9f799a161e711760e9ce6dbc
BLAKE2b-256 checksum
How to use checksums
cb0c86d4201e3be75b7533f5ba45cd4623f0787c114985e687f67c425e2598f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.1 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
1b8b0a5a3a78775ee3e6f8657799d05515388fe1651daf9b9c27068598f4e81a
BLAKE2b-256 checksum
How to use checksums
1c5b6d675b1beb40feb2c2b81af6469b6f2f2607251c68e86bed08f47ea06527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp39-cp39-macosx_10_9_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp39-cp39-macosx_10_9_x86_64.whl
Size 1.3 MB
Tags CPython 3.9 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d007c8dd95d1a57d01b17427f6d6a4585ab8927521a62b7220857a7fde5d0b5a
BLAKE2b-256 checksum
How to use checksums
ae384860c681036747b40cbc971e913fe10919d4c46df30e1cade2fb39545143
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.1 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
ffb6ce4f55b490f40641903dd19657d6573f4f5c9d0344649cec844b8f29e96a
BLAKE2b-256 checksum
How to use checksums
fce2dd472dbc6ec349cff76c1e6a1fd8c8b7457738e368738eed6ad1d433124d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp38-cp38-macosx_10_9_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp38-cp38-macosx_10_9_x86_64.whl
Size 1.3 MB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f680ff6e02659d5b4ac8e8d3970dcb1bcedc0335414c530c776998b11cb89832
BLAKE2b-256 checksum
How to use checksums
fc2cd7401016da21bfa72ddfa364ebac2f53ec2fd0a8867c67c40a77a63a4905
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.1 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
9173b06835c6ce4ce822f87549bc06ac78d90dd18025cf52bdeeb8b35489daf3
BLAKE2b-256 checksum
How to use checksums
e1ba5c0551c039dff9d5b40aa05930317576b13051ded1c92abb31086de31232
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp37-cp37m-macosx_10_9_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp37-cp37m-macosx_10_9_x86_64.whl
Size 1.3 MB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
566b1480fd7ca37664556ae75920c3236a2cfcd86e8e14fe2ea44425faa9f7f4
BLAKE2b-256 checksum
How to use checksums
201b381fdbecb6993cbc5efe0f186fe7dfef3ff097a500e924890b9c4a44f86b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.1 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
a7143e246a4fb0c9fabf970c7ac9d9960836e2cddef2bd0820a3c73a6b62fc70
BLAKE2b-256 checksum
How to use checksums
459ea764fb3ce40743c80c01c64ca65a519d1bd84b0a55fb3e703b856bb971e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / sotb_wrapper-2.0.2-cp36-cp36m-macosx_10_9_x86_64.whl

Download URL sotb_wrapper-2.0.2-cp36-cp36m-macosx_10_9_x86_64.whl
Size 1.3 MB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
6db1de3a0a7640bd1ac1238d0ca027ea8b1bbd9d94765d905c0ce964ba52841b
BLAKE2b-256 checksum
How to use checksums
debaef5cacb309b769e5ac54e6fa163367c136ad1efec37d071202fa560c0034
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release history Release notifications | RSS feed

This release

2.0.2 This release

11 release files

2.0.1

6 release files

2.0.0

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page