Skip to content

Commit 916b6d1

Browse files
committed
1. Sigmoid type can be also specified by the string/"sigmoid name" (not only by enum!)
1 parent b6dd3ae commit 916b6d1

File tree

10 files changed

+384
-75
lines changed

10 files changed

+384
-75
lines changed

ReleaseNotes/release_notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## New features
22

3+
1. Sigmoid type can be also specified by the string/"sigmoid name" (not only by enum!)
4+
35
## Bug fixes
46

57
1, Fix recently introduced bug (thanks to @DmitryYuGolubkov Dima Golubkov)

ostap/fitting/background.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,22 +1335,7 @@ def __init__ ( self ,
13351335
None , 0 , -4 * math.pi , +4 * math.pi )
13361336

13371337
if isinstance ( sigmoid_type , string_types ) :
1338-
stl = str ( sigmoid_type ).lower()
1339-
if stl in ( 'logistic' , ) : sigmoid_type = Ostap.Math.SigmoidType.Logistic
1340-
elif stl in ( 'hyperbolic' , 'tanh' ) : sigmoid_type = Ostap.Math.SigmoidType.Hyperbolic
1341-
elif stl in ( 'trigonometric' , 'atan' ) : sigmoid_type = Ostap.Math.SigmoidType.Trigonometric
1342-
elif stl in ( 'error' , 'erf' ) : sigmoid_type = Ostap.Math.SigmoidType.Error
1343-
elif stl in ( 'gudermannian' , 'gd' ) : sigmoid_type = Ostap.Math.SigmoidType.Gudermannian
1344-
elif stl in ( 'algebraic' , ) : sigmoid_type = Ostap.Math.SigmoidType.Algebdraic
1345-
elif stl in ( 'smooth' , ) : sigmoid_type = Ostap.Math.SigmoidType.SmoothTransition
1346-
elif stl in ( 'poly0' , 'p0' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n0
1347-
elif stl in ( 'poly1' , 'p1' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n1
1348-
elif stl in ( 'poly2' , 'p2' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n2
1349-
elif stl in ( 'poly3' , 'p3' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n3
1350-
elif stl in ( 'poly4' , 'p4' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n4
1351-
elif stl in ( 'poly5' , 'p5' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n5
1352-
elif stl in ( 'poly6' , 'p6' ) : sigmoid_type = Ostap.Math.SigmoidType.Polynomial_n6
1353-
else : raise TypeError ( "Invalid Sigmoid type %s" % sigmoid_type )
1338+
sigmoid_type = Ostap.Math.sigmoid_type ( sigmoid_type )
13541339

13551340
sigmoid_type = int ( sigmoid_type )
13561341
assert Ostap.Math.Sigmoidtype.First <= sigmoid_type <= Ostap.Math.SigmoidType.Last , \
@@ -1423,10 +1408,14 @@ def delta ( self , value ) :
14231408

14241409
@property
14251410
def sigmoid_type ( self ) :
1426-
"""`sigmoid_type` : the actual sigmois type, see `Ostap.Math.Sigmoid.SigmoidType`"""
1411+
"""`sigmoid_type` : the actual sigmoid type, see `Ostap.Math.Sigmoid.SigmoidType`"""
14271412
return self.pdf.sigmoid_type()
14281413

1429-
1414+
@property
1415+
def sigmoid_name ( self ) :
1416+
"""`sigmoid_name` : the name of the actual sigmoid type, see `Ostap.Math.Sigmoid.SigmoidType`"""
1417+
return self.pdf.sigmoid_name ()
1418+
14301419
models.append ( Sigmoid_pdf )
14311420
# =============================================================================
14321421
## @class PSpline_pdf

ostap/math/math_ve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
'asinh' , 'acosh' , 'atanh' ,
2424
'erf' , 'erfc' , 'erfi' , 'erfcx' ,
2525
'sinc' ,
26-
'probit' , 'logit' , pochhammer' ,
26+
'probit' , 'logit' ,
27+
'pochhammer' ,
2728
'gamma' , 'tgamma' , 'lgamma' , 'igamma' ,
2829
'psi' , 'polygamma' , 'digamma' , 'trigamma' ,
2930
'beta' , 'lnbeta' ,

source/include/Ostap/Models.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,32 @@ namespace Ostap
16051605
const double x0 = 0 ,
16061606
const double delta = 0 ,
16071607
const Ostap::Math::SigmoidType st = Ostap::Math::SigmoidType::Hyperbolic ) ;
1608+
// ========================================================================
1609+
/// constructor from polynomial and parameters "alpha" and "x0"
1610+
Sigmoid
1611+
( const std::string& sigmoid_name ,
1612+
const Ostap::Math::Positive& poly ,
1613+
const double scale = 1 ,
1614+
const double x0 = 0 ,
1615+
const double delta = 0 ) ;
1616+
/// constructor from polynomial and parameter "alpha"
1617+
Sigmoid
1618+
( const std::string& sigmoid_name ,
1619+
const unsigned short N = 0 ,
1620+
const double xmin = 0 ,
1621+
const double xmax = 1 ,
1622+
const double scale = 1 ,
1623+
const double x0 = 0 ,
1624+
const double delta = 0 ) ;
1625+
/// constructor from polynomial and parameters "alpha" and "x0"
1626+
Sigmoid
1627+
( const std::string& sigmoid_name ,
1628+
const std::vector<double>& pars ,
1629+
const double xmin = 0 ,
1630+
const double xmax = 1 ,
1631+
const double scale = 1 ,
1632+
const double x0 = 0 ,
1633+
const double delta = 0 ) ;
16081634
// ======================================================================
16091635
public:
16101636
// ======================================================================
@@ -1635,7 +1661,9 @@ namespace Ostap
16351661
inline double delta () const { return m_delta ; }
16361662
inline Ostap::Math::SigmoidType sigmoid_type () const { return m_type ; }
16371663
inline double sin2delta () const { return m_sin2delta ; }
1638-
inline double cos2delta () const { return 1 - m_sin2delta ; }
1664+
inline double cos2delta () const { return 1 - m_sin2delta ; }
1665+
/// the name of sigmoid function
1666+
std::string sigmoid_name () const ;
16391667
// ======================================================================
16401668
public: // sigmoid setters
16411669
// ======================================================================
@@ -1691,15 +1719,15 @@ namespace Ostap
16911719
private:
16921720
// ======================================================================
16931721
/// sigmoid location
1694-
double m_x0 { 0 } ; // sigmoid location
1722+
double m_x0 { 0 } ; // sigmoid location
16951723
/// sigmoid scale
1696-
double m_scale { 1 } ; // sigmoid scale
1724+
double m_scale { 1 } ; // sigmoid scale
16971725
/// sigmoid delta
1698-
double m_delta { 0 } ; // sigmoid delta
1726+
double m_delta { 0 } ; // sigmoid delta
16991727
/// sigmoid type
1700-
Ostap::Math::SigmoidType m_type { Ostap::Math::SigmoidType::Hyperbolic } ;
1728+
Ostap::Math::SigmoidType m_type { Ostap::Math::SigmoidType::Hyperbolic } ;
17011729
/// constant fraction f = sin^2 delta
1702-
double m_sin2delta { 0 } ; // sin^2 delta
1730+
double m_sin2delta { 0 } ; // sin^2 delta
17031731
// ======================================================================
17041732
private:
17051733
// ======================================================================

source/include/Ostap/MoreMath.h

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,39 +2557,55 @@ namespace Ostap
25572557
// ========================================================================
25582558
/// the sigmoid type
25592559
enum class SigmoidType
2560-
{
2561-
//
2562-
Logistic , // based on logistic function
2563-
Hyperbolic , // based on tanh
2564-
Trigonometric , // based on atan
2565-
Error , // Based on error function
2566-
Gudermannian , // Based on Gudermannian function
2567-
Algebraic , // 0.5 * ( 1 + 2*x / hypot ( 1 , 2*x ) )
2568-
SmoothTransition , // Based on "smooth transition" function
2569-
//
2570-
Polynomial_n0 , // Based on "smooth step" with n=0
2571-
Polynomial_n1 , // Based on "smooth step" with n=1
2572-
Polynomial_n2 , // Based on "smooth step" with n=2
2573-
Polynomial_n3 , // Based on "smooth step" with n=3
2574-
Polynomial_n4 , // Based on "smooth step" with n=4
2575-
Polynomial_n5 , // Based on "smooth step" with n=5
2576-
Polynomial_n6 , // Based on "smooth step" with n=6
2577-
//
2578-
First = Logistic ,
2579-
Last = Polynomial_n6
2580-
} ;
2581-
// ========================================================================
2582-
/** Sigmoid function
2560+
{
2561+
//
2562+
Logistic , // based on logistic function
2563+
Hyperbolic , // based on tanh
2564+
Trigonometric , // based on atan
2565+
Error , // Based on error function
2566+
Gudermannian , // Based on Gudermannian function
2567+
Algebraic , // 0.5 * ( 1 + 2*x / hypot ( 1 , 2*x ) )
2568+
SmoothTransition , // Based on "smooth transition" function
2569+
//
2570+
Polynomial_n0 , // Based on "smooth step" with n=0
2571+
Polynomial_n1 , // Based on "smooth step" with n=1
2572+
Polynomial_n2 , // Based on "smooth step" with n=2
2573+
Polynomial_n3 , // Based on "smooth step" with n=3
2574+
Polynomial_n4 , // Based on "smooth step" with n=4
2575+
Polynomial_n5 , // Based on "smooth step" with n=5
2576+
Polynomial_n6 , // Based on "smooth step" with n=6
2577+
//
2578+
First = Logistic ,
2579+
Last = Polynomial_n6
2580+
} ;
2581+
// ========================================================================
2582+
/** sigmoid type
2583+
* @param name the case-insensitive name of of sigmoid function
2584+
* @return ID of sigmoid function
2585+
*/
2586+
SigmoidType sigmoid_type
2587+
( const std::string& name = "Hyperbolic" ) ;
2588+
// ========================================================================
2589+
/* sigmoid type
2590+
* @param sigmoid ID
2591+
* @return the name of of sigmoid function
2592+
*/
2593+
// ========================================================================
2594+
std::string sigmoid_name
2595+
( const SigmoidType stype ) ;
2596+
// =======================================================================
2597+
/** Sigmoid functions
25832598
* All sigmoid fuctions \f$ \sigma(z) \f$ are normalized & scaled such
25842599
* - \f$ \sigma(-\infty) =0\f$
25852600
* - \f$ \sigma(+\infty) =1\f$
2586-
* - \f$ \sigma^\prime(0)=1\f$
2601+
* - \f$ \sigma^\prime(0)=1\f$
2602+
* @see Ostap::Math::SigmoidType
25872603
*/
25882604
double sigmoid
25892605
( const double x ,
25902606
const SigmoidType t = SigmoidType::Hyperbolic ) ;
25912607
// ========================================================================
2592-
2608+
25932609
// ========================================================================
25942610
// Moebius ransformation
25952611
// ========================================================================

source/include/Ostap/MoreRooFit.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,13 @@ namespace Ostap
29422942
RooAbsReal& a ,
29432943
RooAbsReal& b ,
29442944
const Ostap::Math::SigmoidType st = Ostap::Math::SigmoidType::Hyperbolic ) ;
2945+
/// constructor with two variables
2946+
Sigmoid
2947+
( const std::string& name ,
2948+
const std::string& title ,
2949+
RooAbsReal& a ,
2950+
RooAbsReal& b ,
2951+
const std::string& sigmoid_name ) ;
29452952
/// constructor with two variables
29462953
Sigmoid
29472954
( RooAbsReal& a ,
@@ -2983,7 +2990,8 @@ namespace Ostap
29832990
Sigmoid () = default ;
29842991
// ======================================================================
29852992
// copy
2986-
Sigmoid ( const Sigmoid& right , const char* newname = 0 )
2993+
Sigmoid ( const Sigmoid& right ,
2994+
const char* newname = 0 )
29872995
: TwoVars ( right , newname )
29882996
, m_stype ( right.m_stype )
29892997
{}
@@ -2993,7 +3001,10 @@ namespace Ostap
29933001
// ======================================================================
29943002
public:
29953003
// ======================================================================
3004+
/// get the sigmoid type
29963005
inline Ostap::Math::SigmoidType sigmoid_type () const { return m_stype ; }
3006+
/// get the sigmoid name
3007+
std::string sigmoid_name () const ;
29973008
// ======================================================================
29983009
protected:
29993010
// ======================================================================

source/include/Ostap/PDFs.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4501,7 +4501,7 @@ namespace Ostap
45014501
// ======================================================================
45024502
public :
45034503
// ======================================================================
4504-
ClassDefOverride(Ostap::Models::PolySigmoid, 2) ;
4504+
ClassDefOverride(Ostap::Models::PolySigmoid, 3) ;
45054505
// ======================================================================
45064506
public:
45074507
// ======================================================================
@@ -4528,6 +4528,29 @@ namespace Ostap
45284528
RooAbsReal& scale ,
45294529
RooAbsReal& x0 ,
45304530
const Ostap::Math::SigmoidType st = Ostap::Math::SigmoidType::Hyperbolic ) ;
4531+
/// general
4532+
PolySigmoid
4533+
( const char* name ,
4534+
const char* title ,
4535+
RooAbsReal& x ,
4536+
const RooArgList& coeffs ,
4537+
const double xmin ,
4538+
const double xmax ,
4539+
RooAbsReal& scale ,
4540+
RooAbsReal& x0 ,
4541+
RooAbsReal& delta ,
4542+
const std::string& sigmoid_name ) ;
4543+
/// delta = 0
4544+
PolySigmoid
4545+
( const char* name ,
4546+
const char* title ,
4547+
RooAbsReal& x ,
4548+
const RooArgList& coeffs ,
4549+
const double xmin ,
4550+
const double xmax ,
4551+
RooAbsReal& scale ,
4552+
RooAbsReal& x0 ,
4553+
const std::string& sigmoid_name ) ;
45314554
/// copy
45324555
PolySigmoid
45334556
( const PolySigmoid& right ,
@@ -4584,8 +4607,12 @@ namespace Ostap
45844607
// ======================================================================
45854608
inline double xmin () const { return m_sigmoid.xmin () ; }
45864609
inline double xmax () const { return m_sigmoid.xmax () ; }
4610+
/// he type of sigmoid function
45874611
inline Ostap::Math::SigmoidType sigmoid_type () const
45884612
{ return m_sigmoid.sigmoid_type () ; }
4613+
/// the name of signoif function
4614+
inline std::string sigmoid_name () const
4615+
{ return m_sigmoid.sigmoid_name () ; }
45894616
// ======================================================================
45904617
protected :
45914618
// ======================================================================

source/src/Models.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,52 @@ Ostap::Math::Sigmoid::Sigmoid
25772577
INVALID_PARAMETER , __FILE__ , __LINE__ ) ;
25782578
//
25792579
}
2580+
// ============================================================================
2581+
// constructor from polynomial and parameters "alpha" and "x0"
2582+
// ============================================================================
2583+
Ostap::Math::Sigmoid::Sigmoid
2584+
( const std::string& sigmoid_name ,
2585+
const Ostap::Math::Positive& poly ,
2586+
const double scale ,
2587+
const double x0 ,
2588+
const double delta )
2589+
: Sigmoid ( poly ,
2590+
scale ,
2591+
x0 ,
2592+
delta ,
2593+
Ostap::Math::sigmoid_type ( sigmoid_name ) )
2594+
{}
2595+
// ============================================================================
2596+
// constructor from polynomial and parameter "alpha"
2597+
// ============================================================================
2598+
Ostap::Math::Sigmoid::Sigmoid
2599+
( const std::string& sigmoid_name ,
2600+
const unsigned short N ,
2601+
const double xmin ,
2602+
const double xmax ,
2603+
const double scale ,
2604+
const double x0 ,
2605+
const double delta )
2606+
: Sigmoid ( N , xmin , xmax , scale , x0 , delta ,
2607+
Ostap::Math::sigmoid_type ( sigmoid_name ) )
2608+
{}
2609+
// ============================================================================
2610+
// constructor from polynomial and parameters "alpha" and "x0"
2611+
// ============================================================================
2612+
Ostap::Math::Sigmoid::Sigmoid
2613+
( const std::string& sigmoid_name ,
2614+
const std::vector<double>& pars ,
2615+
const double xmin ,
2616+
const double xmax ,
2617+
const double scale ,
2618+
const double x0 ,
2619+
const double delta )
2620+
: Sigmoid ( pars , xmin , xmax , scale , x0 , delta ,
2621+
Ostap::Math::sigmoid_type ( sigmoid_name ) )
2622+
{}
2623+
// ============================================================================
2624+
2625+
25802626
// ============================================================================
25812627
// set new value for alpha
25822628
// ============================================================================
@@ -2704,9 +2750,13 @@ std::size_t Ostap::Math::Sigmoid::tag () const
27042750
m_scale ,
27052751
m_x0 ,
27062752
m_type ,
2707-
m_delta ) ;
2753+
m_delta ) ;
27082754
}
27092755
// ============================================================================
2756+
// the name of sigmoid function
2757+
// ============================================================================
2758+
std::string Ostap::Math::Sigmoid::sigmoid_name () const
2759+
{ return Ostap::Math::sigmoid_name ( m_type ) ; }
27102760

27112761
// ============================================================================
27122762
Ostap::Math::TwoExpos::TwoExpos

0 commit comments

Comments
 (0)