From d7c3fd981d15aff4e4c30c1194ce46ea146d2b7e Mon Sep 17 00:00:00 2001 From: Vanya Belyaev Date: Sun, 8 Sep 2024 12:20:43 +0200 Subject: [PATCH] update --- ostap/fitting/__init__.py | 3 +- ostap/fitting/pdfbasic.py | 16 ++++--- ostap/math/__init__.py | 3 +- ostap/math/bspline.py | 6 +-- ostap/math/integral.py | 18 ++++---- ostap/math/local_minimize.py | 27 +++++++----- ostap/math/minimize.py | 16 ++++--- ostap/math/rootfinder.py | 9 ++-- ostap/math/sp_convolution.py | 80 ++++++++++++++++------------------ ostap/math/sp_interpolation.py | 12 ++--- ostap/parallel/parallel.py | 2 +- ostap/tools/reweighter.py | 9 ++-- 12 files changed, 106 insertions(+), 95 deletions(-) diff --git a/ostap/fitting/__init__.py b/ostap/fitting/__init__.py index be6ce727..21b61902 100755 --- a/ostap/fitting/__init__.py +++ b/ostap/fitting/__init__.py @@ -5,7 +5,8 @@ if sys.warnoptions or os.environ.get ( 'OSTAP_CMAKE_TEST', False ) : import warnings with warnings.catch_warnings(): - warnings.simplefilter ( "always" ) + warnings.simplefilter ( "ignore" , category = DeprecationWarning ) + warnings.simplefilter ( "ignore" , category = UserWarning ) import cppyy # ============================================================================= import ostap.fitting.rooreduce diff --git a/ostap/fitting/pdfbasic.py b/ostap/fitting/pdfbasic.py index 8fa862e0..4fd077c1 100644 --- a/ostap/fitting/pdfbasic.py +++ b/ostap/fitting/pdfbasic.py @@ -554,18 +554,22 @@ def fitTo ( self , ## invoke model.fitTo ( data , *options) command # - merge arguments using RooFit::MultiArg to shorted list def fit_to ( self , model , data , *options ) : - """Invoke `model.fitTo ( data , *options)` command + """ Invoke `model.fitTo ( data , *options)` command - merge arguments using `ROOT.RooFit::MultiArg` to shorted list """ NARGS = 8 - + assert all ( isinstance ( o , ROOT.RooCmdArg ) for o in options ), \ - "fit_to: invalid argument types: %s" % list ( options ) + "fit_to: invalid argument types: %s" % list ( options ) + ## No need to restructure options: if ( 6 , 32 ) <= root_info : - with warnings.catch_warnings(): - warnings.simplefilter("always") + if sys.warnoptions or in_test() : + with warnings.catch_warnings(): + warnings.simplefilter ( "always" , category = RuntimeWarning ) + return Ostap.MoreRooFit.fitTo ( model , data , *options ) + else : return Ostap.MoreRooFit.fitTo ( model , data , *options ) ## for "small" number of arguments use the standard function @@ -577,7 +581,7 @@ def fit_to ( self , model , data , *options ) : if sys.warnoptions or in_test() : with warnings.catch_warnings(): - warnings.simplefilter("always") + warnings.simplefilter ( "always" , category = RuntimeWarning ) return Ostap.MoreRooFit.fitTo ( model , data , cmd ) return Ostap.MoreRooFit.fitTo ( model , data , cmd ) diff --git a/ostap/math/__init__.py b/ostap/math/__init__.py index 2841a53b..ea722754 100755 --- a/ostap/math/__init__.py +++ b/ostap/math/__init__.py @@ -5,7 +5,8 @@ if sys.warnoptions or os.environ.get ( 'OSTAP_CMAKE_TEST', False ) : import warnings with warnings.catch_warnings(): - warnings.simplefilter ( "always" ) + warnings.simplefilter ( "ignore" , category = DeprecationWarning ) + warnings.simplefilter ( "ignore" , category = UserWarning ) import cppyy # ============================================================================= import ostap.math.reduce diff --git a/ostap/math/bspline.py b/ostap/math/bspline.py index 12fbc124..7796be0b 100755 --- a/ostap/math/bspline.py +++ b/ostap/math/bspline.py @@ -212,7 +212,7 @@ def knots_from_abscissas ( abscissas , order , convert = True ) : # @param order (INPUT) the order of spline # @return interpolation spline def interpolate ( func , abscissas , spline , *args ) : - """Construct the interpolation B-spline + """ Construct the interpolation B-spline func : the ``function'' abscissas : abscissas, if None/Empty, Greville's abscissas from the spline will be used @@ -274,9 +274,9 @@ def interpolate ( func , abscissas , spline , *args ) : # ============================================================================= try : - # ========================================================================= + # ========================================================================= with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) import scipy.interpolate # ========================================================================= ## create interpolation spline using scipy machinery diff --git a/ostap/math/integral.py b/ostap/math/integral.py index 35e95b1a..0473e828 100755 --- a/ostap/math/integral.py +++ b/ostap/math/integral.py @@ -425,7 +425,7 @@ def _clenshaw_curtis_ ( f , a , b , ea , er , n0 , depth = 0 ) : try : # ========================================================================= with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) from scipy.integrate import quad as scipy_quad # ========================================================================= ## Calculate the integral (from x0 to x) for the 1D-function @@ -449,9 +449,9 @@ def integral ( fun , func = lambda x : float ( fun ( x , *args ) ) import warnings with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "always" ) result = scipy_quad ( func , xmin , xmax , **kwargs ) - return VE ( result[0] , result[1] * result[1] ) if err else result[0] + return VE ( result [ 0 ] , result [ 1 ] ** 2 ) if err else result [ 0 ] except ImportError : # ========================================================================= @@ -911,12 +911,12 @@ def integral2 ( fun , func = lambda x,y : float ( fun ( x , y , *args ) ) import warnings with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "always" ) result = scipy_dblquad ( func , ymin , ymax , lambda x : xmin , lambda x : xmax , **kwargs ) - return VE( result[0] , result[1] * result[1] ) if err else result[0] + return VE ( result [ 0 ] , result [ 1 ] ** 2 ) if err else result [ 0 ] except ImportError : # ========================================================================= @@ -933,7 +933,7 @@ def integral2 ( fun , try : # ========================================================================= with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) from scipy.integrate import tplquad as scipy_tplquad # ========================================================================= ## Calculate the inteegral for the 3D-function @@ -957,15 +957,15 @@ def integral3 ( fun , """ func = lambda x,y,z : float ( fun ( x , y , z , *args ) ) import warnings - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with warnings.catch_warnings () : + warnings.simplefilter ( "always" ) result = scipy_tplquad ( func , zmin , zmax , lambda z : ymin , lambda z : ymax , lambda y,z : xmin , lambda y,z : xmax , **kwargs ) - return VE( result[0] , result[1] * result[1] ) if err else result[0] + return VE ( result [ 0 ] , result [ 1 ] ** 2 ) if err else result [ 0 ] except ImportError : # ======================================================================== diff --git a/ostap/math/local_minimize.py b/ostap/math/local_minimize.py index e47a7ecd..fa17f790 100644 --- a/ostap/math/local_minimize.py +++ b/ostap/math/local_minimize.py @@ -32,15 +32,18 @@ # ============================================================================= import math, warnings from math import sqrt +# ============================================================================= try : + # ========================================================================= import numpy import numpy as np - + # ========================================================================= with warnings.catch_warnings(): - warnings.simplefilter("ignore") - _epsilon = math.sqrt(numpy.finfo(float).eps) - + warnings.simplefilter ( "ignore" ) + _epsilon = math.sqrt ( numpy.finfo ( float ) . eps ) +# ============================================================================= except ImportError : + # ========================================================================= class numpy(object) : @staticmethod def abs ( value ) : return abs ( value ) @@ -49,29 +52,31 @@ def size ( value ) : return 1 @staticmethod def sign ( value ) : return math.copysign ( 1.0 , value ) @staticmethod - def max ( a , *kwargs ) : return max ( a ) + def max ( a , *kwargs ) : return max ( a ) + # ========================================================================= import sys _epsilon = sys.float_info.epsilon*0.5 np = numpy # ============================================================================= - class OptimizeWarning(UserWarning): pass - +# ============================================================================= +## Test whether `x` is either a scalar or an array scalar def is_array_scalar(x): - """Test whether `x` is either a scalar or an array scalar. - + """ Test whether `x` is either a scalar or an array scalar. """ return np.size(x) == 1 - +# ============================================================================= def _check_unknown_options(unknown_options): if unknown_options: msg = ", ".join(map(str, unknown_options.keys())) # Stack level 4: this is called from _minimize_*, which is # called from another function in Scipy. Level 4 is the first # level in user code. - warnings.warn("Unknown solver options: %s" % msg, OptimizeWarning, 4) + warnings.warn ( "Unknown solver options: %s" % msg, OptimizeWarning, 4) +# ============================================================================= +## Represents the optimization result. class OptimizeResult(dict): """ Represents the optimization result. diff --git a/ostap/math/minimize.py b/ostap/math/minimize.py index f5a0528f..29bc5172 100644 --- a/ostap/math/minimize.py +++ b/ostap/math/minimize.py @@ -32,19 +32,23 @@ if '__main__' == __name__ : logger = getLogger ( 'ostap.math.minimize' ) else : logger = getLogger ( __name__ ) # ============================================================================= - -scipy_OK = False +scipy_OK = False +# ============================================================================= try : + # ========================================================================= with warnings.catch_warnings(): warnings.simplefilter("ignore") from scipy.optimize import minimize_scalar - scipy_OK = True + scipy_OK = True + # ========================================================================= except ImportError : + # ========================================================================= from ostap.math.local_minimize import scalar_minimize as minimize_scalar - scipy_OK = False + scipy_OK = False + # ========================================================================= -if scipy_OK : - +# ============================================================================= +if scipy_OK : # =============================================================== # ========================================================================= ## get a minimum for 1D-function # @code diff --git a/ostap/math/rootfinder.py b/ostap/math/rootfinder.py index 4dea753a..3f2455ef 100644 --- a/ostap/math/rootfinder.py +++ b/ostap/math/rootfinder.py @@ -1244,10 +1244,10 @@ def find_root ( f , ## the function return solver.find ( a , b ) # ============================================================================= -try : +try : # ========================================================================= with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) from scipy.optimize import brentq as scipy_brentq findroot = scipy_brentq # ========================================================================= @@ -1255,7 +1255,7 @@ def find_root ( f , ## the function # ========================================================================= ## logger.warning ("scipy.optimize.brentq is not available, use local ``find_root''-replacement") findroot = find_root - + # ========================================================================= # ============================================================================= ## solve equation \f$ f(x)=C \f$ @@ -1265,7 +1265,7 @@ def find_root ( f , ## the function # x = solve ( fun , 0.0 , 1.0 , C ) # @endcode def sp_solve ( fun , xmin , xmax , C = 0 , args = () ) : - """Solve equation fun(x)=C + """ Solve equation fun(x)=C >>> fun = ... >>> C = ... >>> x = solve ( fun , 0 , 1 , C ) @@ -1277,6 +1277,7 @@ def sp_solve ( fun , xmin , xmax , C = 0 , args = () ) : func = lambda x , *a : fun(x,*a)-C return findroot ( func , xmin , xmax , args = args ) +# ============================================================================= ## solve = sp_solve diff --git a/ostap/math/sp_convolution.py b/ostap/math/sp_convolution.py index c9284841..d581fbe6 100644 --- a/ostap/math/sp_convolution.py +++ b/ostap/math/sp_convolution.py @@ -13,10 +13,6 @@ __author__ = "Vanya BELYAEV Ivan.Belyaev@itep.ru" __date__ = "2020-02-28" __all__ = () -## 'ArrayConvolution' , ## SciPy' FFT-based convolution for two arrays -## 'GaussConvolution' , ## SciPy' FFT-based convolution for function with a gaussian -## 'Convolution' , ## SciPy' FFT-based convolution for functions -## ) # ============================================================================= import warnings from ostap.math.operations import Function @@ -25,18 +21,17 @@ if '__main__' == __name__ : logger = getLogger ( 'ostap.math.sp_convolution' ) else : logger = getLogger ( __name__ ) # ============================================================================= - -try : +try : # ======================================================================= # ========================================================================= with warnings.catch_warnings(): warnings.simplefilter("ignore") import scipy.signal as SS import numpy as np - + # ========================================================================= # ========================================================================= ## simple class for scipy-based interpolation class ArrayConvolution(object) : - """Simple class for SciPy-based convolution + """ Simple class for SciPy-based convolution >>> x = ... ## >>> y = ... ## >>> sp = ArrayConvolution ( x , y , ... ) @@ -49,25 +44,23 @@ class ArrayConvolution(object) : # >>> sp = ArrayConvolution (x , y ) # @endcode def __init__ ( self , x , y , mode = 'full' ) : - """Create the convolution + """ Create the convolution >>> x = ... ## >>> y = ... ## >>> sp = ArrayConvolution (x , y ) - """ - - ## create the interpolation spline + """ self.__result = None self.__result = SS.fftconvolve ( x , y , mode ) @property def result( self ) : - """``result'' : result with array convolution""" + """`result' : result with array convolution""" return self.__result # ========================================================================= ## simple class for SciPy/FFT-based convolution class GaussConvolution(ArrayConvolution,Function) : - """Simple class for SciPy/FFT-based convolution + """ Simple class for SciPy/FFT-based convolution >>> x = ... ## >>> y = ... ## >>> sp = GaussConvolution ( x , y , ... ) @@ -86,12 +79,11 @@ def __init__ ( self , N = 1024 , sigma = 1 , mode = 'same' ) : - """Create the convolution + """ Create the convolution >>> x = ... ## >>> y = ... ## - >>> sp = GaussConvolution (x , y ) - """ - + >>> sp = GaussConvolution ( x , y ) + """ from ostap.core.ostap_types import integer_types assert isinstance ( N , integer_types ) and 1 < N , 'Illegal N %s!' % N @@ -104,8 +96,7 @@ def __init__ ( self , from ostap.math.math_ve import gauss_pdf gpdf = lambda x : gauss_pdf ( x , sigma = sigma ) - vgauss = np.vectorize ( gpdf ) - + vgauss = np.vectorize ( gpdf ) dx = float ( xmax - xmin ) / N a = float ( xmax - xmin ) / 2 @@ -119,27 +110,29 @@ def __init__ ( self , r *= dx self.__spline = None - - def xmin ( self ) : return self.__params[0] - def xmax ( self ) : return self.__params[1] + + # ===================================================================== + def xmin ( self ) : return self.__params [ 0 ] + def xmax ( self ) : return self.__params [ 1 ] @property - def N ( self ) : return self.__params[2] + def N ( self ) : return self.__params [ 2 ] @property - def sigma ( self ) : return self.__params[3] + def sigma ( self ) : return self.__params [ 3 ] @property def params ( self ) : return self.__params @property def spline ( self ) : - """``spline'' : get the spline fnuction for the result of convolution""" + """`spline' : get the spline fnuction for the result of convolution""" if self.__spline : return self.__spline from ostap.math.sp_interpolation import SplineInterpolator x = np.linspace ( self.xmin() , self.xmax() , self.N ) self.__spline = SplineInterpolator ( ( x , self.result ) , 3 ) return self.__spline - + + # ===================================================================== def __call__ ( self , x ) : if not self.__spline : self.spline return self.__spline ( x ) @@ -151,8 +144,8 @@ def __str__ ( self ) : # ===================================================================== ## simple class for SciPy/FFT-based convolution - class Convolution(ArrayConvolution,Function) : - """Simple class for SciPy/FFT-based convolution + class Convolution ( ArrayConvolution,Function) : + """ Simple class for SciPy/FFT-based convolution >>> x = ... ## >>> y = ... ## >>> sp = GaussConvolution ( x , y , ... ) @@ -171,14 +164,13 @@ def __init__ ( self , func2 , xmin2 , xmax2 , - N = 1000 , + N = 1024 , mode = 'same' ) : - """Create the convolution + """ Create the convolution >>> x = ... ## >>> y = ... ## >>> sp = Convolution (x , y ) - """ - + """ from ostap.core.ostap_types import integer_types assert isinstance ( N , integer_types ) and 1 < N , 'Illegal N %s!' % N @@ -202,17 +194,16 @@ def __init__ ( self , r *= dx self.__spline = None - - def xmin ( self ) : return self.__params1[0] - def xmax ( self ) : return self.__params1[1] - def xmin1 ( self ) : return self.__params1[0] - def xmax1 ( self ) : return self.__params1[1] - def xmin2 ( self ) : return self.__params2[0] - def xmax2 ( self ) : return self.__params2[1] + def xmin ( self ) : return self.__params1 [ 0 ] + def xmax ( self ) : return self.__params1 [ 1 ] + def xmin1 ( self ) : return self.__params1 [ 0 ] + def xmax1 ( self ) : return self.__params1 [ 1 ] + def xmin2 ( self ) : return self.__params2 [ 0 ] + def xmax2 ( self ) : return self.__params2 [ 1 ] @property - def N ( self ) : return self.__params1[2] + def N ( self ) : return self.__params1 [ 2 ] @property def params1 ( self ) : return self.__params1 @@ -242,9 +233,12 @@ def __str__ ( self ) : 'GaussConvolution' , ## SciPy' FFT-based convolution for function with a gaussian 'Convolution' , ## SciPy' FFT-based convolution for functions ) - + + # ========================================================================= except ImportError : + # ========================================================================= logger.warning ("No numpy/scipy is available: convolution is disabled") + # ============================================================================= if '__main__' == __name__ : @@ -253,7 +247,7 @@ def __str__ ( self ) : docme ( __name__ , logger = logger ) if not __all__ : - logger.error ("No convolution is available") + logger.error ( "No convolution is available" ) # ============================================================================= ## The END diff --git a/ostap/math/sp_interpolation.py b/ostap/math/sp_interpolation.py index 260eed5b..4b2d1384 100644 --- a/ostap/math/sp_interpolation.py +++ b/ostap/math/sp_interpolation.py @@ -22,13 +22,13 @@ else : logger = getLogger ( __name__ ) # ============================================================================= with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) import scipy.interpolate as SI # ============================================================================== ## simple class for scipy-based interpolation class SplineInterpolator(object) : - """Simple class for scipy-based interpolation + """ Simple class for scipy-based interpolation >>> x = ... ## >>> y = ... ## >>> sp = SplineInterpolator ( (x,y) , 3 ) @@ -68,14 +68,14 @@ def __call__ ( self , x ) : @property def derivative ( self ) : - """``derivative'' : get the spline object for defivative""" + """`derivative' : get the spline object for defivative""" if not self.__derivative : self.__derivative = self.__spline.derivative() return self.__derivative @property def antiderivative ( self ) : - """``antiderivative'' : get the spline object for indefinite itegral""" + """`antiderivative' : get the spline object for indefinite itegral""" if not self.__antiderivative : self.__antiderivative = self.__spline.antiderivative() return self.__antiderivative @@ -88,7 +88,7 @@ def antiderivative ( self ) : # >>> r = spline.integral( a , b ) ## definite integral form a to b # @endcode def integral ( self , a = None , b = None ) : - """Get the integral + """ Get the integral >>> spline = ... >>> o = spline.integral() ## indefinite integral >>> r = spline.integral( a , b ) ## definite integral form a to b @@ -102,7 +102,7 @@ def integral ( self , a = None , b = None ) : @property def spline ( self ) : - """``spline'' : get the underlying bspline/scipy object""" + """`spline' : get the underlying bspline/scipy object""" return self.__spline diff --git a/ostap/parallel/parallel.py b/ostap/parallel/parallel.py index a94864cb..e5feac80 100644 --- a/ostap/parallel/parallel.py +++ b/ostap/parallel/parallel.py @@ -75,7 +75,7 @@ if ( 3 , 6 ) <= sys.version_info : try : with warnings.catch_warnings() : - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) import ipyparallel as _ipp worker = 'IPYPARALLEL' if ( 8 , 0 ) <= _ipp.version_info else '' except ImportError : diff --git a/ostap/tools/reweighter.py b/ostap/tools/reweighter.py index d6e737bd..f788ac93 100644 --- a/ostap/tools/reweighter.py +++ b/ostap/tools/reweighter.py @@ -36,7 +36,7 @@ class Reweighter(object) : def __init__ ( self , **kwargs ) : with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) import numpy if not hasattr ( numpy , 'float' ) : @@ -49,7 +49,7 @@ def __init__ ( self , **kwargs ) : @property def reweighter ( self ) : - """``reweighter'' : get the underlying reweighter object""" + """`reweighter' : get the underlying reweighter object""" return self.__reweighter # ========================================================================= @@ -78,18 +78,19 @@ def reweight ( self , self.__nvars = original.shape[-1] with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) self.reweighter.fit ( original , target , original_weight = original_weight , target_weight = target_weight ) + # ========================================================================= def weight ( self , original , original_weight = None ) : with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.simplefilter ( "ignore" ) return self.reweighter.predict_weights ( original = original , original_weight = original_weight )