From eccf9ef7eb55d3894d32eb4f941862b6ac446e89 Mon Sep 17 00:00:00 2001 From: Vanya Belyaev Date: Thu, 1 Aug 2024 08:51:18 +0200 Subject: [PATCH] better treatment of no-scipy case --- ostap/histos/param.py | 85 +++++++++++-------- .../tests/test_histos_parameterisation.py | 5 +- ostap/math/param.py | 6 +- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/ostap/histos/param.py b/ostap/histos/param.py index 5d3362f9..67354e46 100755 --- a/ostap/histos/param.py +++ b/ostap/histos/param.py @@ -752,9 +752,9 @@ def _h1_brational_ ( h1 , refit = refit ) try : - - from ostap.math.param import fourier_sum, cosine_sum - + + # ============================================================================= + from ostap.math.param import fourier_sum # ============================================================================= ## make a histogram representation in terms of Fourier serie # @code @@ -780,33 +780,7 @@ def _h1_fourier_sum_ ( h1 , N , fejer = False , **kwargs ) : xmax = min ( kwargs.get( 'xmax' , h1.xmax() ) , h1.xmax () ) ## return fourier_sum ( h1 , N , xmin , xmax , fejer ) - - # ============================================================================= - ## make a histogram representation in terms of cosine Fourier serie - # @code - # histo = ... - # fsum = histo.cosine_sum ( 4 ) - # print fsum - # x = ... - # print 'FUN(%s) = %s ' % ( x , fsum ( x ) ) - # @endcode - # @see Ostap::Math::CosineSum - # @author Vanya Belyaev Ivan.Belyaev@itep.ru - # @date 2015-07-26 - def _h1_cosine_sum_ ( h1 , N , fejer = False , **kwargs ) : - """Make a histogram representation in terms of cosine Fourier serie - >>> histo = ... - >>> fsum = histo.cosine_sum ( 4 ) - >>> print fsum - >>> x = ... - >>> print 'FUN(%s) = %s ' % ( x , fsum ( x ) ) - """ - ## - xmin = max ( kwargs.get( 'xmin' , h1.xmin() ) , h1.xmin () ) - xmax = min ( kwargs.get( 'xmax' , h1.xmax() ) , h1.xmax () ) - ## - return cosine_sum ( h1 , N , xmin , xmax , fejer ) - + # ============================================================================= ## represent 1D-histo as Fourier polynomial # @code @@ -869,7 +843,51 @@ def _h1_fourier_ ( h1 , params = params , limits = limits , refit = refit ) + + for t in ( ROOT.TH1F , ROOT.TH1D ) : + + t.fourier_sum = _h1_fourier_sum_ + t.fourier = _h1_fourier_ + + _new_methods_ .append ( _h1_fourier_sum_ ) + _new_methods_ .append ( _h1_fourier_ ) + +except ImportError : + pass + + +# ================================================================================= +try : + + # ============================================================================= + from ostap.math.param import cosine_sum + # ============================================================================= + ## make a histogram representation in terms of cosine Fourier serie + # @code + # histo = ... + # fsum = histo.cosine_sum ( 4 ) + # print fsum + # x = ... + # print 'FUN(%s) = %s ' % ( x , fsum ( x ) ) + # @endcode + # @see Ostap::Math::CosineSum + # @author Vanya Belyaev Ivan.Belyaev@itep.ru + # @date 2015-07-26 + def _h1_cosine_sum_ ( h1 , N , fejer = False , **kwargs ) : + """Make a histogram representation in terms of cosine Fourier serie + >>> histo = ... + >>> fsum = histo.cosine_sum ( 4 ) + >>> print fsum + >>> x = ... + >>> print 'FUN(%s) = %s ' % ( x , fsum ( x ) ) + """ + ## + xmin = max ( kwargs.get( 'xmin' , h1.xmin() ) , h1.xmin () ) + xmax = min ( kwargs.get( 'xmax' , h1.xmax() ) , h1.xmax () ) + ## + return cosine_sum ( h1 , N , xmin , xmax , fejer ) + # ============================================================================= ## represent 1D-histo as cosine Fourier polynomial # @code @@ -933,16 +951,13 @@ def _h1_cosine_ ( h1 , limits = limits , refit = refit ) + for t in ( ROOT.TH1F , ROOT.TH1D ) : - - t.fourier_sum = _h1_fourier_sum_ + t.cosine_sum = _h1_cosine_sum_ - t.fourier = _h1_fourier_ t.cosine = _h1_cosine_ - _new_methods_ .append ( _h1_fourier_sum_ ) _new_methods_ .append ( _h1_cosine_sum_ ) - _new_methods_ .append ( _h1_fourier_ ) _new_methods_ .append ( _h1_cosine_ ) except ImportError : diff --git a/ostap/histos/tests/test_histos_parameterisation.py b/ostap/histos/tests/test_histos_parameterisation.py index 2b276cc4..78ac6dc0 100755 --- a/ostap/histos/tests/test_histos_parameterisation.py +++ b/ostap/histos/tests/test_histos_parameterisation.py @@ -640,7 +640,7 @@ def rdif ( x , y ) : logger.info ( 100*'*') logger.info ( 'Parameterizations techniques using histogram values only (in general fast)') logger.info ( 100*'*') - + test_bernstein_sum_orig () test_bernstein_sum_fill () test_bernstein_sum () @@ -665,7 +665,8 @@ def rdif ( x , y ) : test_legendre_3D () test_bernstein_3D () - + """ + # ============================================================================= ## The END # ============================================================================= diff --git a/ostap/math/param.py b/ostap/math/param.py index fb88dec3..2ecdb079 100644 --- a/ostap/math/param.py +++ b/ostap/math/param.py @@ -240,9 +240,6 @@ def chebyshev_sum ( func , N , xmin , xmax ) : return csum - - - # ============================================================================= try : # ========================================================================= @@ -323,7 +320,7 @@ def fourier_sum ( func , N , xmin , xmax , fejer = False ) : try : # ========================================================================= import numpy - import scipy + import scipy.fftpack # ========================================================================= # ============================================================================= @@ -358,7 +355,6 @@ def cosine_sum ( func , N , xmin , xmax , fejer = False ) : vfunc = numpy.vectorize ( lambda x : float ( func ( x ) ) ) ## make cosine fourier transform - import scipy.fftpack r = scipy.fftpack.dct ( vfunc ( t ) , 1 ) / N #