Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Jul 25, 2024
1 parent 8995487 commit 389506e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 98 deletions.
30 changes: 21 additions & 9 deletions ostap/frames/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,25 +885,26 @@ def frame_nEff ( frame , cuts = '' ) :
>>> data = ...
>>> neff = data.nEff('b1*b1')
"""
if isinstance ( frame , ROOT.TTree ) : frame = DataFrame ( frame )
node = as_rnode ( frame )
return SV.data_nEff ( node , cuts )

# =============================================================================
## Get statistics for the given expression in data frame
# @code
# data = ...
# c1 = data.statVar( 'S_sw' , 'pt>10' )
# c2 = data.statVar( 'S_sw' , 'pt>0' )
# c1 = frame_statVar ( 'S_sw' , 'pt>10' )
# c2 = frame_statVar ( 'S_sw' , 'pt>0' )
# @endcode
def frame_statVar ( frame , expression , cuts = '' ) :
"""Get statistics for the given expression in data frame
>>> data = ...
>>> c1 = data.statVar( 'S_sw' , 'pt>10' )
>>> c2 = data.statVar( 'S_sw' )
>>> c1 = frame_statVar( 'S_sw' , 'pt>10' )
>>> c2 = framestatVar( 'S_sw' )
"""
if isinstance ( frame , ROOT.TTree ) : frame = DataFrame ( frame )
node = as_rnode ( frame )
return SV.data_statVar ( node , str ( expression ) , str ( cuts ) )
return SV.data_statistics ( node , expression, cuts = cuts )

# =============================================================================
## get the statistic for pair of expressions in DataFrame
Expand Down Expand Up @@ -945,6 +946,7 @@ def frame_get_moment ( frame , order , center , expression , cuts = '' ) :
>>> value = frame_get_moment ( 3 , 1.234 , 'b1*b2' , 'b3>0' )
- see `Ostap.StatVar.get_moment`
"""
if isinstance ( frame , ROOT.TTree ) : frame = DataFrame ( frame )
node = as_rnode ( frame )
return SV.data_get_moment ( node , order = order , center = center , expression = expression , cuts = cuts )

Expand Down Expand Up @@ -977,6 +979,7 @@ def frame_central_moment ( frame , order , expression , cuts = '' ) :
>>> value = frame_central_moment ( 3 , 'b1*b2' , 'b3>0' )
- see `Ostap.StatVar.central_moment`
"""
if isinstance ( frame , ROOT.TTree ) : frame = DataFrame ( frame )
node = as_rnode ( frame )
return SV.data_central_moment ( node , order = order , expression = expression , cuts = cuts )

Expand Down Expand Up @@ -1304,12 +1307,12 @@ def screator ( node , var_name , cut_name ) :
# frame = ...
# nEff = frame_the_nEff ( 'x*x' , 'y>0' )
# @endcode
def _fr_the_nEff_ ( frame , expressions , cuts = '' , lazy = False ) :
def _fr_the_nEff_ ( frame , cuts = '' , lazy = False ) :
"""Get nEff through action
>>> frame = ...
>>> nEff = frame_the_nEff ( 'x*x' , 'y>0' )
"""
return _fr_statVar_ ( frame , expressions , cuts = cuts , lazy = lazy )
return _fr_the_statVar_ ( frame , '1.0' , cuts = cuts , lazy = lazy )

# ==================================================================================
## get statistics of variable(s)
Expand Down Expand Up @@ -1607,7 +1610,7 @@ def lcreator ( node , var_name , cut_name ) :
# stat = frame_statVar ( frame , 'pt' )
# stat = frame_statVar ( frame , 'pt' , 'eta>0' )
# @endcode
def frame_statVar_ ( frame , expressions , cuts = '' ) :
def frame_statVar ( frame , expressions , cuts = '' ) :
"""Get statistics of variable(s)
>>> frame = ....
>>> stat = frame.statVar ( 'pt' )
Expand Down Expand Up @@ -1710,7 +1713,7 @@ def frame_skewness ( frame , expressions , cuts = '' , errors = True ) :
>>> skew = frame_skewness ( 'x'x' , 'y<0' )
>>> mean = stat.skreness ()
"""
return frame_the_skewness ( frame , expressions , cuts = cuts , errros = errors , lazy = lazy ).skewness()
return frame_the_skewness ( frame , expressions , cuts = cuts , errors = errors , lazy = False ).skewness()
# ============================================================================
## Get a kurtosis via moment&actgions
# @code
Expand Down Expand Up @@ -1858,6 +1861,15 @@ def frame_param ( frame , poly , expressions , cuts = '' ) :
"""
return frame_the_param ( frame , poly , expressions , cuts = cuts , lazy = False )

__all__ += (
'frame_arithmetic_mean' ,
'frame_geometric_mean' ,
'frame_harmonic_mean' ,
'frame_power_mean' ,
'frame_lehmer_mean' ,
'frame_param' ,
)


# ===============================================================================
## Print the frame report data
Expand Down
178 changes: 89 additions & 89 deletions ostap/frames/tests/test_frames_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,100 +236,100 @@ def test_frame2 ( ) :
row = 'StatVar mean (b1,b1>0)' , s1.mean().toString ( '%+.2f +/- %-.2f' ) , s2.mean().toString ( '%+.2f +/- %-.2f' )
rows.append ( row )

s1 = tree.arithmetic_mean ( 'b1' )
s2 = frame_arithmetic_mean ( frame , 'b1' )

row = 'Arithmetic mean (b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.arithmetic_mean ( 'b1' , '1/b1')
s2 = frame_arithmetic_mean ( frame , 'b1' , '1/b1')

row = 'Arithmetic mean (b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.arithmetic_mean ( 'b1' , 'b1>0')
s2 = frame_arithmetic_mean ( frame , 'b1' , 'b1>0')
row = 'Arithmetic mean (b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.geometric_mean ( 'b1' )
s2 = frame_geometric_mean ( frame , 'b1' )

row = 'Geometric mean (b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.geometric_mean ( 'b1' , '1/b1')
s2 = frame_geometric_mean ( frame , 'b1' , '1/b1')

row = 'Geometric mean (b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.geometric_mean ( 'b1' , 'b1>0')
s2 = frame_geometric_mean ( frame , 'b1' , 'b1>0')

row = 'Geometric mean (b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )


s1 = tree.harmonic_mean ( 'b1' )
s2 = frame_harmonic_mean ( frame , 'b1' )

row = 'Harmonic mean (b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.harmonic_mean ( 'b1' , '1/b1')
s2 = frame_harmonic_mean ( frame , 'b1' , '1/b1')

row = 'Harmonic mean (b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.harmonic_mean ( 'b1' , 'b1>0')
s2 = frame_harmonic_mean ( frame , 'b1' , 'b1>0')
if Frames_OK :

s1 = tree.arithmetic_mean ( 'b1' )
s2 = frame_arithmetic_mean ( frame , 'b1' )

row = 'Arithmetic mean (b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.arithmetic_mean ( 'b1' , '1/b1')
s2 = frame_arithmetic_mean ( frame , 'b1' , '1/b1')

row = 'Arithmetic mean (b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

row = 'Harmonic mean (b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )
s1 = tree.arithmetic_mean ( 'b1' , 'b1>0')
s2 = frame_arithmetic_mean ( frame , 'b1' , 'b1>0')
row = 'Arithmetic mean (b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.geometric_mean ( 'b1' )
s2 = frame_geometric_mean ( frame , 'b1' )

s1 = tree.power_mean ( 2 , 'b1' )
s2 = frame_power_mean ( frame , 2 , 'b1' )

row = 'Power mean (2,b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.power_mean ( 2 , 'b1' , '1/b1')
s2 = frame_power_mean ( frame , 2 , 'b1' , '1/b1')

row = 'Power mean (2,b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.power_mean ( 2 , 'b1' , 'b1>0')
s2 = frame_power_mean ( frame , 2 , 'b1' , 'b1>0')

row = 'Power mean (2,b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.lehmer_mean ( 3 , 'b1' )
s2 = frame_lehmer_mean ( frame , 3 , 'b1' )

row = 'Lehmer mean (3,b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )
row = 'Geometric mean (b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.lehmer_mean ( 3 , 'b1' , '1/b1')
s2 = frame_lehmer_mean ( frame , 3 , 'b1' , '1/b1')

row = 'Lehmer mean (3,b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )

s1 = tree.lehmer_mean ( 3 , 'b1' , 'b1>0')
s2 = frame_lehmer_mean ( frame , 3 , 'b1' , 'b1>0')
s1 = tree.geometric_mean ( 'b1' , '1/b1')
s2 = frame_geometric_mean ( frame , 'b1' , '1/b1')

row = 'Geometric mean (b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.geometric_mean ( 'b1' , 'b1>0')
s2 = frame_geometric_mean ( frame , 'b1' , 'b1>0')

row = 'Geometric mean (b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

row = 'Lehmer mean (3,b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.GetValue().value()
rows.append ( row )
s1 = tree.harmonic_mean ( 'b1' )
s2 = frame_harmonic_mean ( frame , 'b1' )

row = 'Harmonic mean (b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.harmonic_mean ( 'b1' , '1/b1')
s2 = frame_harmonic_mean ( frame , 'b1' , '1/b1')

row = 'Harmonic mean (b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.harmonic_mean ( 'b1' , 'b1>0')
s2 = frame_harmonic_mean ( frame , 'b1' , 'b1>0')

row = 'Harmonic mean (b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.power_mean ( 2 , 'b1' )
s2 = frame_power_mean ( frame , 2 , 'b1' )

row = 'Power mean (2,b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.power_mean ( 2 , 'b1' , '1/b1')
s2 = frame_power_mean ( frame , 2 , 'b1' , '1/b1')

row = 'Power mean (2,b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.power_mean ( 2 , 'b1' , 'b1>0')
s2 = frame_power_mean ( frame , 2 , 'b1' , 'b1>0')

row = 'Power mean (2,b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.lehmer_mean ( 3 , 'b1' )
s2 = frame_lehmer_mean ( frame , 3 , 'b1' )

row = 'Lehmer mean (3,b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.lehmer_mean ( 3 , 'b1' , '1/b1')
s2 = frame_lehmer_mean ( frame , 3 , 'b1' , '1/b1')

row = 'Lehmer mean (3,b1,1/b1)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

s1 = tree.lehmer_mean ( 3 , 'b1' , 'b1>0')
s2 = frame_lehmer_mean ( frame , 3 , 'b1' , 'b1>0')

row = 'Lehmer mean (3,b1,b1>0)' , '%+.2f' % s1.mean() , '%+.2f' % s2.value()
rows.append ( row )

title = 'Frame/Tree statistics'
logger.info ( '%s\n%s' % ( title , T.table ( rows , title = title , prefix = '# ' , alignment = 'lcc' ) ) )



# =============================================================================
def test_frame3 () :

Expand All @@ -348,8 +348,8 @@ def test_frame3 () :

for i in range ( 5 ) :

frame = DataFrame ( tname , fname )
pb = frame_progress ( frame , len ( tree ) )
frame = DataFrame ( tname , fname )
fr, pb = frame_progress ( frame , len ( tree ) )
message = 'Value: %d %s' % ( i , pb.GetValue() )
logger.info ( message )

Expand Down

0 comments on commit 389506e

Please sign in to comment.