Skip to main content

partialwrap: a small Python library providing wrappers for external executables and Python functions to be used easily with Python’s functools.partial.

Project description

A a small Python library providing wrappers for external executables to be used easily with Python’s functools.partial.

DOI PyPI version Conda version License Build Status Coverage Status

About partialwrap

partialwrap is a Python library providing easy wrapper functions to use with Python’s functools.partial. Partial’s ingenious mechanism allows to use even very complex functions with many arguments and keyword arguments with routines that need functions in the simple form func(x). This includes Python’s map function, the minimizers in scipy.optimize, and plenty of third-party modules such as emcee or pyeee. partialwrap allows to use any external executable as well as any Python function with arbitrary arguments and keywords to be used with functools.partial. It also allows to use the executables or functions with easy parallelization of code with, for example, the parallel map function of the multiprocessing module or via the Message Passing Interface (MPI).

Documentation

The complete documentation for partialwrap is available at Github pages:

https://mcuntz.github.io/partialwrap/

Quick usage guide

partialwrap provides two wrapper functions to work with external executables: partialwrap.exe_wrapper and partialwrap.exe_mask_wrapper.

partialwrap writes the input arguments to the wrapper functions into files that can be read by the external executable. The executable should write its result to a file that will then be read by partialwrap in return. This means partialwrap needs to have a function parameterwriter that writes the parameter file parameterfile needed by the executable exe. It then also needs to have a function outputreader for reading the output file outputfile of the external executable, perhaps calculating an objective function value.

Take the Rastrigin function, which is a popular function for performance testing of optimization algorithms: y = a*n + sum_i^n(x_i^2 - a*cos(b*x_i)). It has a global minimum of 0 at all x_i = 0. a influences mainly the depth of the (local and global) minima, whereas b influences mainly the size of the minima. A common form uses a = 10 and b = 2*pi. The parameters x_i should then be in the interval [-5.12, 5.12].

Consider for simplicity an external Python program (e.g. rastrigin1.py) that calculates the Rastrigin function with a = 10 and b = 2*pi, reading in an arbitrary number of parameters x_i from a parameterfile = params.txt and writing its output into outputfile = out.txt:

# File: rastrigin1.py
import numpy as np
from partialwrap import standard_parameter_reader

# Rastrigin function a=10, b=2*pi
def rastrigin1(x):
    return 10. * len(x) + np.sum(x**2 - 10. * np.cos(2. * np.pi * x))

# read parameters
x = standard_parameter_reader('params.txt')

# calc function
y = rastrigin1(x)

# write output file
with open('out.txt', 'w') as ff:
    print(y, file=ff)

This program can be called on the command line (if params.txt is present) with:

python rastrigin1.py

The external program calculating the Rastrigin function could, of course, also be written in any compiled language such as C or Fortran. See the userguide for details. The external program, here the Python version, can be used with Python’s functools.partial and the wrapper function partialwrap.exe_wrapper:

import scipy.optimize as opt
from functools import partial
from partialwrap import exe_wrapper
from partialwrap import standard_parameter_writer, standard_output_reader

rastrigin_exe   = ['python3', 'rastrigin1.py']
parameterfile   = 'params.txt'
parameterwriter = standard_parameter_writer
outputfile      = 'out.txt'
outputreader    = standard_output_reader
rastrigin_wrap  = partial(exe_wrapper, rastrigin_exe,
                          parameterfile, parameterwriter,
                          outputfile, outputreader, {})

x0  = [0.1, 0.2, 0.3]
res = opt.minimize(rastrigin_wrap, x0, method='BFGS')

The scipy.optimize function minimize() passes its sampled parameters to exe_wrapper, which writes it to the file parameterfile = 'params.txt'. It then calls rastrigin_exe = 'python3 rastrigin1.py' and reads its outputfile = 'out.txt'. partialwrap.standard_parameter_reader and partialwrap.standard_parameter_writer are convenience functions that read and write one parameter per line in a file without a header. The empty dictionary at the end is explained in the userguide.

More elaborate input/output of the external program can simply be done by replacing standard_parameter_reader and standard_parameter_writer with appropriate functions, while the rest stays pretty much the same.

Installation

The easiest way to install is via pip:

pip install partialwrap

or via conda:

conda install -c conda-forge partialwrap

Requirements:

License

partialwrap is distributed under the MIT License. See the LICENSE file for details.

Copyright (c) 2016-2023 Matthias Cuntz

The project structure is based on a template provided by Sebastian Müller.

Download files

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

Source Distribution

partialwrap-2.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

partialwrap-2.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file partialwrap-2.0.tar.gz.

File metadata

  • Download URL: partialwrap-2.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for partialwrap-2.0.tar.gz
Algorithm Hash digest
SHA256 1246841e3ef0dd652519f0136606d978b31ba6da9b5f5036e104ac384ca5f57e
MD5 d53de7973da84de0beaf720093efcc59
BLAKE2b-256 ae4d232b568632cde2933914aef6d67ae91ce2c8729eb2f8a4eb3ee847d52d6b

See more details on using hashes here.

File details

Details for the file partialwrap-2.0-py3-none-any.whl.

File metadata

  • Download URL: partialwrap-2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for partialwrap-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e3b91e7a3cf897e1c0c4af31ff994557015c93dfc68ae31d73c84bd502e31a6
MD5 822c5c887497a22cad8975c89fe643f9
BLAKE2b-256 2eac78b71559c5dc90288e4789219ce0a66cf42ef31c14e3e9170f7fe1aba85a

See more details on using hashes here.

Supported by

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