Skip to main content

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.

Metadata

Release files for partialwrap 2.0

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

Source distribution (sdist)

Source distribution for partialwrap 2.0
File Size Uploaded
partialwrap-2.0.tar.gz 27.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for partialwrap 2.0
File Interpreter ABI Platform
partialwrap-2.0-py3-none-any.whl Python 3 none any Details

Total release size: 45.7 kB

Release files / partialwrap-2.0.tar.gz

Download URL partialwrap-2.0.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1246841e3ef0dd652519f0136606d978b31ba6da9b5f5036e104ac384ca5f57e
BLAKE2b-256 checksum
How to use checksums
ae4d232b568632cde2933914aef6d67ae91ce2c8729eb2f8a4eb3ee847d52d6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / partialwrap-2.0-py3-none-any.whl

Download URL partialwrap-2.0-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e3b91e7a3cf897e1c0c4af31ff994557015c93dfc68ae31d73c84bd502e31a6
BLAKE2b-256 checksum
How to use checksums
2eac78b71559c5dc90288e4789219ce0a66cf42ef31c14e3e9170f7fe1aba85a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

2.0 This release

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4

2 release files

1.3.5

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3

2 release files

1.2

2 release files

1.0.1

2 release files

1.0

2 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