Skip to content

Commit

Permalink
better treatment of no-scipy case
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Aug 1, 2024
1 parent 046b4af commit eccf9ef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
85 changes: 50 additions & 35 deletions ostap/histos/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [email protected]
# @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
Expand Down Expand Up @@ -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 [email protected]
# @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
Expand Down Expand Up @@ -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 :
Expand Down
5 changes: 3 additions & 2 deletions ostap/histos/tests/test_histos_parameterisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand All @@ -665,7 +665,8 @@ def rdif ( x , y ) :

test_legendre_3D ()
test_bernstein_3D ()

"""

# =============================================================================
## The END
# =============================================================================
6 changes: 1 addition & 5 deletions ostap/math/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,6 @@ def chebyshev_sum ( func , N , xmin , xmax ) :
return csum





# =============================================================================
try :
# =========================================================================
Expand Down Expand Up @@ -323,7 +320,7 @@ def fourier_sum ( func , N , xmin , xmax , fejer = False ) :
try :
# =========================================================================
import numpy
import scipy
import scipy.fftpack
# =========================================================================

# =============================================================================
Expand Down Expand Up @@ -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

#
Expand Down

0 comments on commit eccf9ef

Please sign in to comment.