Skip to content

Commit

Permalink
1. improve printput from load_params
Browse files Browse the repository at this point in the history
  1. add copy of `draw_options` and `fit_options` for FUN/PDF cloning
`
  • Loading branch information
VanyaBelyaev committed Mar 20, 2024
1 parent f5dd6f5 commit 3640476
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
40 changes: 32 additions & 8 deletions ostap/fitting/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<degree>\d)' , bkg , re.IGNORECASE )
poly = re.search ( r'(poly|pol|p)(( *)|(_*))(?P<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
expo = re.search ( r'(expo|exp|e)(( *)|(_*))(?P<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
incr = re.search ( r'(increasing|increase|incr|inc|i)(( *)|(_*))(?P<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
decr = re.search ( r'(decreasing|decrease|decr|dec|d)(( *)|(_*))(?P<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
decr = re.search ( r'(convex|cx)(( *)|(_*))(?P<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
decr = re.search ( r'(concave|cv)(( *)|(_*))(?P<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
decr = re.search ( r'(convex|cx)(( *)|(_*))(decreasing|dec)(( *)|(_*))(?P<degree>\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<degree>\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<degree>\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<degree>\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<degree>\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<degree>\d)' , bkg , re.IGNORECASE )
decr = re.search ( r'(roopoly|rp|r)(( *)|(_*))(?P<degree>\d{1,2})' , bkg , re.IGNORECASE )
if decr :
degree = int ( decr.group ( 'degree' ) )
bkg = RooPoly_pdf ( name , xvar , power = degree )
Expand Down
36 changes: 31 additions & 5 deletions ostap/fitting/funbasic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )

# =========================================================================

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

Expand All @@ -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 )

Expand All @@ -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 )

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

# =========================================================================
Expand Down
20 changes: 0 additions & 20 deletions ostap/fitting/pdfbasic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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!)
Expand Down

0 comments on commit 3640476

Please sign in to comment.