From 364047622114758cd83544cd82bb59a14b92a90c Mon Sep 17 00:00:00 2001 From: Vanya Belyaev Date: Wed, 20 Mar 2024 12:51:31 +0100 Subject: [PATCH] 1. improve printput from `load_params` 1. add copy of `draw_options` and `fit_options` for FUN/PDF cloning ` --- ostap/fitting/background.py | 40 +++++++++++++++++++++++++++++-------- ostap/fitting/funbasic.py | 36 ++++++++++++++++++++++++++++----- ostap/fitting/pdfbasic.py | 20 ------------------- 3 files changed, 63 insertions(+), 33 deletions(-) diff --git a/ostap/fitting/background.py b/ostap/fitting/background.py index 1d962c11..59e566b7 100755 --- a/ostap/fitting/background.py +++ b/ostap/fitting/background.py @@ -2921,47 +2921,71 @@ def make_bkg ( bkg , name , xvar , logger = None , **kwargs ) : return model import re - poly = re.search ( r'(poly|pol|p)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + poly = re.search ( r'(poly|pol|p)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if poly : degree = -1 * abs ( int ( poly.group ( 'degree' ) ) ) return make_bkg ( degree , name , xvar , logger = logger , **kwargs ) - expo = re.search ( r'(expo|exp|e)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + expo = re.search ( r'(expo|exp|e)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if expo : degree = int ( expo.group ( 'degree' ) ) return make_bkg ( degree , name , xvar , logger = logger , **kwargs ) - incr = re.search ( r'(increasing|increase|incr|inc|i)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + incr = re.search ( r'(increasing|increase|incr|inc|i)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if incr : degree = int ( incr.group ( 'degree' ) ) bkg = Monotonic_pdf ( name , xvar , power = degree , increasing = True ) return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) - decr = re.search ( r'(decreasing|decrease|decr|dec|d)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + decr = re.search ( r'(decreasing|decrease|decr|dec|d)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if decr : degree = int ( decr.group ( 'degree' ) ) bkg = Monotonic_pdf ( name , xvar , power = degree , increasing = False ) return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) - decr = re.search ( r'(convex|cx)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + decr = re.search ( r'(convex|cx)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if decr : degree = int ( decr.group ( 'degree' ) ) bkg = ConvexOnly_pdf ( name , xvar , power = degree , convex = True ) return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) - decr = re.search ( r'(concave|cv)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + decr = re.search ( r'(concave|cv)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if decr : degree = int ( decr.group ( 'degree' ) ) bkg = ConvexOnly_pdf ( name , xvar , power = degree , convex = False ) return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) - decr = re.search ( r'(roochebyshev|roocheb|chebyshev|cheb|rc)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + decr = re.search ( r'(convex|cx)(( *)|(_*))(decreasing|dec)(( *)|(_*))(?P\d{1,3})' , bkg , re.IGNORECASE ) + if desc : + degree = int ( decr.group ( 'degree' ) ) + bkg = Convex_pdf ( name , xvar , power = degree , increasing = False, convex = True ) + return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) + + decr = re.search ( r'(convex|cx)(( *)|(_*))(increasing|inc)(( *)|(_*))(?P\d{1,3})' , bkg , re.IGNORECASE ) + if desc : + degree = int ( decr.group ( 'degree' ) ) + bkg = Convex_pdf ( name , xvar , power = degree , increasing = True , convex = True ) + return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) + + decr = re.search ( r'(concave|cv)(( *)|(_*))(decreasing|dec)(( *)|(_*))(?P\d{1,3})' , bkg , re.IGNORECASE ) + if desc : + degree = int ( decr.group ( 'degree' ) ) + bkg = Convex_pdf ( name , xvar , power = degree , increasing = False, convex = False ) + return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) + + decr = re.search ( r'(concave|cv)(( *)|(_*))(increasing|inc)(( *)|(_*))(?P\d{1,3})' , bkg , re.IGNORECASE ) + if desc : + degree = int ( decr.group ( 'degree' ) ) + bkg = Convex_pdf ( name , xvar , power = degree , increasing = True , convex = False ) + return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) + + decr = re.search ( r'(roochebyshev|roocheb|chebyshev|cheb|rc)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if decr : degree = int ( decr.group ( 'degree' ) ) bkg = RooCheb_pdf ( name , xvar , power = degree ) return make_bkg ( bkg , name , xvar , logger = logger , **kwargs ) - decr = re.search ( r'(roopoly|rp|r)(( *)|(_*))(?P\d)' , bkg , re.IGNORECASE ) + decr = re.search ( r'(roopoly|rp|r)(( *)|(_*))(?P\d{1,2})' , bkg , re.IGNORECASE ) if decr : degree = int ( decr.group ( 'degree' ) ) bkg = RooPoly_pdf ( name , xvar , power = degree ) diff --git a/ostap/fitting/funbasic.py b/ostap/fitting/funbasic.py index cb402c58..d437c87b 100644 --- a/ostap/fitting/funbasic.py +++ b/ostap/fitting/funbasic.py @@ -97,7 +97,10 @@ def __init__ ( self , name , xvar , tricks = True , **kwargs ) : self.__draw_var = None ## predefined drawing options for this FUN/PDF self.__draw_options = cidict ( transform = key_transform ) + + self.__fit_options = () ## predefined fit options for this PDF + self.__checked_keys = set() self.vars.add ( self.xvar ) @@ -264,6 +267,25 @@ def draw_options ( self ) : """ return self.__draw_options + @property + def fit_options ( self ) : + """'fit_options' : the predefined 'fitTo'-options for this PDF + - tuple of ROOT.RooArgCmd + pdf = ... + pdf.fit_options = ROOT.RooFit.Optimize ( 1 ) + pdf.fit_options = ROOT.RooFit.Optimize ( 1 ) , ROOT.RooFit.PrintEvalError ( 2 ) + """ + return self.__fit_options + @fit_options.setter + def fit_options ( self , value ) : + if isinstance ( value , ROOT.RooCmdArg ) : value = value , + assert isinstance ( value , list_types ), 'Invalid fitTo-options %s' % value + _opts = [] + for v in value : + assert isinstance ( v , ROOT.RooCmdArg ), 'Invalid fitTo-option %s' % v + _opts.append ( v ) + self.__fit_options = tuple ( _opts ) + # ========================================================================= # ========================================================================= @@ -416,7 +438,7 @@ def load_params ( self , params = {} , dataset = None , silent = False , **kwarg vv = float ( v ) if vv != pv : p.setVal ( vv ) - item = p.name , "%-15.7g" % pv , "%-+15.7g" % vv + item = p.name , "%-+15.7g" % pv , "%-+15.7g" % vv table.append ( item ) keys.add ( key ) @@ -437,7 +459,7 @@ def load_params ( self , params = {} , dataset = None , silent = False , **kwarg pv = float ( p ) if vv != pv : p.setVal ( vv ) - item = p.name , "%-15.7g" % pv , "%-+15.7g" % vv + item = p.name , "%-+15.7g" % pv , "%-+15.7g" % vv table.append ( item ) keys.add ( i ) @@ -458,7 +480,7 @@ def load_params ( self , params = {} , dataset = None , silent = False , **kwarg pv = float ( p ) if vv != pv : p.setVal ( vv ) - item = p.name , "%-15.7g" % pv , "%-+15.7g" % vv + item = p.name , "%-+15.7g" % pv , "%-+15.7g" % vv table.append ( item ) keys.add ( key ) @@ -470,7 +492,8 @@ def load_params ( self , params = {} , dataset = None , silent = False , **kwarg npars = len ( table ) if npars : - title = 'Parameters loaded: %s' % npars + if 1 == npars : title = '%s parameter loaded ' % npars + else : title = '%s parameters loaded ' % npars table = [ ('Parameter' ,'old value' , 'new value' ) ] + table import ostap.logger.table table = ostap.logger.table.table ( table , title , prefix = "# " ) @@ -629,7 +652,10 @@ def clone ( self , **kwargs ) : ## cloned = KLASS ( **conf ) cloned = self.factory ( conf ) - + + cloned.draw_options.update ( self.draw_options ) + cloned.fit_options = self .fit_options + return cloned # ========================================================================= diff --git a/ostap/fitting/pdfbasic.py b/ostap/fitting/pdfbasic.py index ba540815..14f6748f 100644 --- a/ostap/fitting/pdfbasic.py +++ b/ostap/fitting/pdfbasic.py @@ -262,7 +262,6 @@ def __init__ ( self ) : ## take care about sPlots self.__splots = [] self.__histo_data = None - self.__fit_options = () ## predefined fit options for this PDF self.__fit_result = None @@ -323,25 +322,6 @@ def histo_data ( self , value ) : self.__histo_data = value else : raise AttributeError("'histo_data' has invalid type %s/%s" % ( value , type(value) ) ) - @property - def fit_options ( self ) : - """'fit_options' : the predefined 'fitTo'-options for this PDF - - tuple of ROOT.RooArgCmd - pdf = ... - pdf.fit_options = ROOT.RooFit.Optimize ( 1 ) - pdf.fit_options = ROOT.RooFit.Optimize ( 1 ) , ROOT.RooFit.PrintEvalError ( 2 ) - """ - return self.__fit_options - @fit_options.setter - def fit_options ( self , value ) : - if isinstance ( value , ROOT.RooCmdArg ) : value = value , - assert isinstance ( value , list_types ), 'Invalid fitTo-options %s' % value - _opts = [] - for v in value : - assert isinstance ( v , ROOT.RooCmdArg ), 'Invalid fitTo-option %s' % v - _opts.append ( v ) - self.__fit_options = tuple ( _opts ) - # ========================================================================= ## make the actual fit (and optionally draw it!)