From 389506efda8808a81b50a20989e57734d74d5f13 Mon Sep 17 00:00:00 2001 From: Vanya Belyaev Date: Thu, 25 Jul 2024 16:17:59 +0200 Subject: [PATCH] update --- ostap/frames/frames.py | 30 ++-- ostap/frames/tests/test_frames_frames.py | 178 +++++++++++------------ 2 files changed, 110 insertions(+), 98 deletions(-) diff --git a/ostap/frames/frames.py b/ostap/frames/frames.py index d109f7b2..8a86f89f 100644 --- a/ostap/frames/frames.py +++ b/ostap/frames/frames.py @@ -885,6 +885,7 @@ 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 ) @@ -892,18 +893,18 @@ def frame_nEff ( frame , 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 @@ -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 ) @@ -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 ) @@ -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) @@ -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' ) @@ -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 @@ -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 diff --git a/ostap/frames/tests/test_frames_frames.py b/ostap/frames/tests/test_frames_frames.py index ce4852ba..d66b4c41 100644 --- a/ostap/frames/tests/test_frames_frames.py +++ b/ostap/frames/tests/test_frames_frames.py @@ -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 () : @@ -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 )