shgpy.fformfit module¶
Fourier fitting module for fitting RA-SHG data in Fourier space.
This module provides two different methods (up to minor variations) for fitting RA-SHG data in Fourier space. The first is a simple least-square method (using scipy.optimize.least_squares) which is extremely fast and works well for simple problems. The second is a so-called basinhopping method (using scipy.optimize.basinhopping) which is proficient at finding the global minimum of complicated cost functions with many fitting parameters and local minima. See the relevant scipy documentation for more info.
- shgpy.fformfit.basinhopping_fit(fform, fdat, guess_dict, niter, method='BFGS', args=(), stepsize=0.5, basinhopping_kwargs={}, chunk_cost_func=False, save_cost_func_filename=None, load_cost_func_filename=None)¶
Basinhopping fit of RA-SHG data.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- niterint
Number of basinhopping iterations (see scipy documentation)
- methodstr, optional
Minimization method to use, defaults to ‘BFGS’. See scipy documentation for more information.
- argstuple, optional
Additional arguments to give to the minimizer.
- stepsizefloat, optional
Basinhopping stepsize, defaults to 0.5. See scipy documentation for more information.
- basinhopping_kwargsdict, optional
Other options to pass to the basinhopping routine. See scipy documentation for more information.
- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
- shgpy.fformfit.basinhopping_fit_jac(fform, fdat, guess_dict, niter, method='BFGS', args=(), stepsize=0.5, basinhopping_kwargs={}, chunk_cost_func=False, save_cost_func_filename=None, grad_save_cost_func_filename_prefix=None, load_cost_func_filename=None, load_grad_cost_func_filename_prefix=None)¶
Basinhopping fit of RA-SHG data.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- niterint
Number of basinhopping iterations (see scipy documentation)
- methodstr, optional
Minimization method to use, defaults to ‘BFGS’. See scipy documentation for more information.
- argstuple, optional
Additional arguments to give to the minimizer.
- stepsizefloat, optional
Basinhopping stepsize, defaults to 0.5. See scipy documentation for more information.
- basinhopping_kwargsdict, optional
Other options to pass to the basinhopping routine. See scipy documentation for more information.
- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- grad_save_cos_func_filenamepath-like, optional
If provided, save the gradient cost functions as shared libraries at the locations defined by
...0.so
,...1.so
, etc.- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location. Must be used in tandem with
load_grad_cost_func_filename_prefix
.- load_grad_cost_func_filename_prefixpath-like, optional
If provided, load the gradient functions defined by this prefix. Must be used in tandem with
load_cost_func_filename
.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
Notes
This function computes and supplies the gradient function to the scipy basinhopping algorithm. This requires some computational power up front but can speed up the minimization algorithm.
- shgpy.fformfit.basinhopping_fit_jac_with_bounds(fform, fdat, guess_dict, bounds_dict, niter, method='L-BFGS-B', args=(), stepsize=0.5, basinhopping_kwargs={}, chunk_cost_func=False, save_cost_func_filename=None, grad_save_cost_func_filename_prefix=None, load_cost_func_filename=None, load_grad_cost_func_filename_prefix=None)¶
Basinhopping fit of RA-SHG data with bounds.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- bounds_dictdict
Dict of form
{sympy.Symbol:tuple}
. tuple should be of form(lower_bound, upper_bound)
.- niterint
Number of basinhopping iterations (see scipy documentation)
- methodstr, optional
Minimization method to use, defaults to ‘L-BFGS-B’. See scipy documentation for more information.
- argstuple, optional
Additional arguments to give to the minimizer.
- stepsizefloat, optional
Basinhopping stepsize, defaults to 0.5. See scipy documentation for more information.
- basinhopping_kwargsdict, optional
Other options to pass to the basinhopping routine. See scipy documentation for more information.
- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- grad_save_cost_func_filename_prefixpath-like, optional
If provided, save the gradient cost functions as shared libraries at the locations defined by
...0.so
,...1.so
, etc.- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location. Must be used in tandem with
load_grad_cost_func_filename_prefix
.- load_grad_cost_func_filename_prefixpath-like, optional
If provided, load the gradient functions defined by this prefix. Must be used in tandem with
load_cost_func_filename
.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
Notes
This function computes and supplies the gradient function to the scipy basinhopping algorithm. This requires some computational power up front but can speed up the minimization algorithm.
- shgpy.fformfit.basinhopping_fit_with_bounds(fform, fdat, guess_dict, bounds_dict, niter, method='L-BFGS-B', args=(), stepsize=0.5, basinhopping_kwargs={}, chunk_cost_func=False, save_cost_func_filename=None, load_cost_func_filename=None)¶
Basinhopping fit of RA-SHG data with bounds.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- bounds_dictdict
Dict of form
{sympy.Symbol:tuple}
. tuple should be of form(lower_bound, upper_bound)
.- niterint
Number of basinhopping iterations (see scipy documentation)
- methodstr, optional
Minimization method to use, defaults to ‘L-BFGS-B’. See scipy documentation for more information.
- argstuple, optional
Additional arguments to give to the minimizer.
- stepsizefloat, optional
Basinhopping stepsize, defaults to 0.5. See scipy documentation for more information.
- basinhopping_kwargsdict, optional
Other options to pass to the basinhopping routine. See scipy documentation for more information.
- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
- shgpy.fformfit.dual_annealing_fit_with_bounds(fform, fdat, guess_dict, bounds_dict, maxiter=1000, local_search_options={}, initial_temp=5230, restart_temp_ratio=2e-05, visit=2.62, accept=-5.0, maxfun=10000000.0, seed=None, no_local_search=True, callback=None, chunk_cost_func=False, save_cost_func_filename=None, load_cost_func_filename=None)¶
Simulated annealing fit of RA-SHG data with bounds.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- bounds_dictdict
Dict of form
{sympy.Symbol:tuple}
. tuple should be of form(lower_bound, upper_bound)
.- maxiterint
Maximum number of global search iterations.
- local_search_optionsdict, optional
Extra keyword arguments to be passed to the local minimizer.
- initial_tempfloat, optional
Initial temperature. Default is 5230.
- restart_temp_ratiofloat, optional
When the temperature reaches
initial_temp * restart_temp_ratio
, the reannealing process is triggered. Default value is 2e-5. Range is (0,1).- visitfloat, optional
Parameter for visiting distribution. Default value is 2.62.
- acceptfloat, optional
Parameter for acceptance distribution. Default is -5.0.
- maxfunint, optional
Soft limit for the number of objective function calls. Default is 1e7.
- seed{int, RandomState, Generator}, optional
The random seed to use.
- no_local_searchbool, optional
If True, perform traditional generalized simulated annealing with no local search.
- callbackcallable, optional
A callback function with signature
callback(x, f, context)
which will be called for all minima found.- x0ndarray, shape(n,), optional
Initial guess.
- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
Notes
See the
scipy.optimize.dual_annealing
documentation for more info.
- shgpy.fformfit.gen_cost_func(fform, fdat, argument_list=None, chunk=False, save_filename=None, method='gcc')¶
Generate a cost function as an .so file and save it to disk.
- Parameters:
- fformfFormContainer
The Fourier formula to use
- fdatfDataContainer
The fourier data to fit
- argument_listarray_like of sympy.Symbol, optional
Specify an order for the arguments. Default is alphabetical by str(sympy.Symbol).
- chunkbool, optional
Chunk the function generation into multiple steps (one for each Fourier component in fform and fdat). Default is False.
- save_filenamepath_like, optional
Filename to save the result. Default is not to save.
- methodstr, optional
Compiler to use. Tested with gcc and clang. Default is gcc.
- Returns:
- cost_funcfunction
Result after compiling generated C code. Signature is
cost_func(xs)
, where xs is anumpy.ndarray
of floats in the order offform.get_free_symbols
.
- shgpy.fformfit.gen_model_func(fform, save_folder, method='gcc', max_terms_per_file=10)¶
Generate a model function (a folder of .so files) and save them to disk.
- Parameters:
- fformfFormContainer
The Fourier formula to use
- save_folderpath_like
Folder to save the result.
- methodstr, optional
Compiler to use. Tested with gcc and clang. Default is gcc.
- max_terms_per_fileint, optional
If there are too many terms per file, sometimes the compiler fails. This constrains the number of terms per file. Default is 10.
- Returns:
- model_funcfunction
Result after compiling generated C code. Signature is
model_func(xs, pc, m)
, where xs is anumpy.ndarray
of floats in the order offform.get_free_symbols
, pc is the polarization combination, and m is the Fourier component.
- shgpy.fformfit.least_squares_fit(fform, fdat, guess_dict, chunk_cost_func=False, save_cost_func_filename=None, load_cost_func_filename=None, least_sq_kwargs={})¶
No nonsense least-squares fit of RA-SHG data.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location.
- least_sq_kwargsdict, optional
Dictionary of additional options to pass to scipy.optimize.least_squares. Default is
{}
.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
- shgpy.fformfit.least_squares_fit_with_bounds(fform, fdat, guess_dict, bounds_dict, chunk_cost_func=False, save_cost_func_filename=None, load_cost_func_filename=None, least_sq_kwargs={})¶
No nonsense least-squares fit of RA-SHG data.
- Parameters:
- fformfFormContainer
Instance of class
fFormContainer
. This is the (Fourier-transformed) fitting formula.- fdatfDataContainer
Instance of class
fDataContainer
. This is the (Fourier-transformed) data to fit.- guess_dictdict
Dict of form
{sympy.Symbol:float}
. This is the initial guess.- bounds_dictdict
Dict of form
{sympy.Symbol:tuple}
. tuple should be of form(lower_bound, upper_bound)
.- chunk_cost_funcbool, optional
Whether to chunk the cost function generation into multiple parts. Default is False.
- save_cost_func_filenamepath-like, optional
If provided, save the cost function (as a shared library) at this location.
- load_cost_func_filenamepath-like, optional
If provided, load the cost function at this location.
- least_sq_kwargsdict, optional
Dictionary of additional options to pass to scipy.optimize.least_squares. Default is
{}
.
- Returns:
- retscipy.optimize.OptimizeResult
Instance of class
OptimizeResult
. See scipy documentation for further description. Includes additional attributeret.xdict
which is a dict of{sympy.Symbol:float}
indicating the final answer as a dictionary.
- shgpy.fformfit.load_func(load_cost_func_filename, multiprocessing=False)¶
Load a cost function generated by
gen_cost_func
- shgpy.fformfit.load_model_func(fform, save_folder, multiprocessing=False)¶
Load a model function generated by
gen_model_func
- Returns:
- model_funcfunction
Result after compiling generated C code. Signature is
model_func(xs, pc, m)
, where xs is anumpy.ndarray
of floats in the order offform.get_free_symbols
, pc is the polarization combination, and m is the Fourier component.