Package: partialwrap
A a small Python library providing wrappers for external executables
to be used easily with Python’s functools.partial.About partialwrappartialwrapis a Python library providing easy wrapper functions
to use with Python’sfunctools.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 formfunc(x). This includes Python’smapfunction,
the minimizers inscipy.optimize, and plenty of third-party modules
such asemceeorpyeee.partialwrapallows to use any
external executable as well as any Python function with arbitrary
arguments and keywords to be used withfunctools.partial. It also
allows to use the executables or functions with easy parallelization
of code with, for example, the parallelmapfunction of themultiprocessingmodule or via the Message Passing Interface
(MPI).DocumentationThe complete documentation forpartialwrapis available at Github
pages:https://mcuntz.github.io/partialwrap/Quick usage guidepartialwrapprovides two wrapper functions to work with external
executables:partialwrap.exe_wrapperandpartialwrap.exe_mask_wrapper.partialwrapwrites 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 bypartialwrapin return. This meanspartialwrapneeds to have a
functionparameterwriterthat writes the parameter fileparameterfileneeded by the executableexe.  It then also
needs to have a functionoutputreaderfor reading the output fileoutputfileof the external executable, perhaps calculating an
objective function value.Take theRastrigin 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
of0at allx_i = 0.ainfluences mainly the depth of the
(local and global) minima, whereasbinfluences mainly the size of
the minima. A common form usesa = 10andb = 2*pi. The
parametersx_ishould 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 witha = 10andb = 2*pi, reading in an arbitrary number of
parametersx_ifrom aparameterfile = params.txtand writing
its output intooutputfile = out.txt:# File: rastrigin1.pyimportnumpyasnpfrompartialwrapimportstandard_parameter_reader# Rastrigin function a=10, b=2*pidefrastrigin1(x):return10.*len(x)+np.sum(x**2-10.*np.cos(2.*np.pi*x))# read parametersx=standard_parameter_reader('params.txt')# calc functiony=rastrigin1(x)# write output filewithopen('out.txt','w')asff:print(y,file=ff)This program can be called on the command line (ifparams.txtis
present) with:pythonrastrigin1.pyThe external program calculating the Rastrigin function could, of
course, also be written in any compiled language such as C or
Fortran. See theuserguidefor details. The external program, here
the Python version, can be used with Python’sfunctools.partialand
the wrapper functionpartialwrap.exe_wrapper:importscipy.optimizeasoptfromfunctoolsimportpartialfrompartialwrapimportexe_wrapperfrompartialwrapimportstandard_parameter_writer,standard_output_readerrastrigin_exe=['python3','rastrigin1.py']parameterfile='params.txt'parameterwriter=standard_parameter_writeroutputfile='out.txt'outputreader=standard_output_readerrastrigin_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')Thescipy.optimizefunctionminimize()passes its sampled
parameters toexe_wrapper, which writes it to the fileparameterfile = 'params.txt'. It then callsrastrigin_exe = 'python3 rastrigin1.py'and reads itsoutputfile = 'out.txt'.partialwrap.standard_parameter_readerandpartialwrap.standard_parameter_writerare 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 theuserguide.More elaborate input/output of the external program can simply be done
by replacingstandard_parameter_readerandstandard_parameter_writerwith appropriate functions, while the
rest stays pretty much the same.InstallationThe easiest way to install is viapip:pipinstallpartialwrapor viaconda:condainstall-cconda-forgepartialwrapRequirements:NumPyLicensepartialwrapis distributed under the MIT License. See theLICENSEfile for details.Copyright (c) 2016-2023 Matthias CuntzThe project structure is based on atemplateprovided bySebastian Müller.