Skip to content

Commit

Permalink
1. add isfinite method for SE & WSE counters
Browse files Browse the repository at this point in the history
  1. `data_statistics` : add the check for the finite counters
  1. add `isfinite` method for matrices and vectors
  1. add `isfinite` method for `(W)Covariance` objects
  • Loading branch information
VanyaBelyaev committed Nov 6, 2024
1 parent e03d633 commit 0850a9e
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 56 deletions.
2 changes: 2 additions & 0 deletions ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
1. add generalized Beta' distribution
1. add `isfinite` method for `SE` & `WSE` counters
1. `data_statistics` : add the check for the finite counters
1. add `isfinite` method for matrices and vectors
1. add `isfinite` method for `(W)Covariance` objects

## Backward incompatible

Expand Down
60 changes: 36 additions & 24 deletions ostap/math/linalg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def mgetter ( mtrx , i , j ) :
# corr = correlation ( mtrx , 1 , 2 )
# @endcode
def correlation ( mtrx , i , j ):
"""Get the correlation element from the matrix-like object
""" Get the correlation element from the matrix-like object
>>> mtrx = ...
>>> corr = correlation ( mtrx , 1 , 2 )
"""
Expand All @@ -113,7 +113,7 @@ def correlation ( mtrx , i , j ):
## @class Method
# access and keep the method
class Method(object) :
"""Access and keep the method
""" Access and keep the method
"""
def __init__ ( self , factory ) :

Expand Down Expand Up @@ -161,7 +161,7 @@ def clear ( self ) :
## @class Method2
# access and keep two methods
class Method2(object) :
"""Access and keep two methods
""" Access and keep two methods
"""

def __init__ ( self , factory1 , factory2 ) :
Expand Down Expand Up @@ -204,12 +204,11 @@ def clear ( self ) :
## dummy function
def dummy ( *a, **b ) : return NotImplemented


# ==============================================================================
## @class LinAlg
# collection of decorators for vectors/matrices
class LinAlg(object) :
"""Collection of decorators for vectors/matrices
""" Collection of decorators for vectors/matrices
"""

with_numpy = np
Expand All @@ -228,7 +227,6 @@ class LinAlg(object) :

methods_DIV = Method2 ( Ostap.Math.Ops.Div , Ostap.Math.Ops.CanDiv )
methods_IDIV = Method2 ( Ostap.Math.Ops.IDiv , Ostap.Math.Ops.CanIDiv )


methods_DOT = Method2 ( Ostap.Math.Ops.Dot , Ostap.Math.Ops.CanDot )
methods_CROSS = Method2 ( Ostap.Math.Ops.Cross , Ostap.Math.Ops.CanDot )
Expand All @@ -246,10 +244,8 @@ class LinAlg(object) :
method_EIGEN = Method ( Ostap.Math.Ops.Eigen )
method_TM = Method ( Ostap.Math.Ops.TM )


## method_EQ = Method ( Ostap.Math.Ops.Eq )


known_ssymmatrices = {}
known_smatrices = {}
known_svectors = {}
Expand Down Expand Up @@ -307,7 +303,7 @@ def restore ( klass , delete = ( 'to_numpy' , 'to_array' ,
'column' , 'columns' ,
'cross' , 'dot' ,
'sym' , 'asym' , 'skew' ) ) :
"""restore useful attributes
""" Restore useful attributes
"""

oa = '__old_attributes__'
Expand All @@ -327,7 +323,7 @@ def restore ( klass , delete = ( 'to_numpy' , 'to_array' ,
## cleanup the LinAlg
@staticmethod
def CLEANUP () :
"""Cleanup LinAlg
""" Cleanup LinAlg
"""

while LinAlg.decorated_matrices :
Expand Down Expand Up @@ -449,7 +445,7 @@ def NUMPY ( arr ) :
# @endcode
@staticmethod
def V_NUMPY ( vct ) :
"""Convert vector to numpy array:
""" Convert vector to numpy array:
>>> vct = ...
>>> na = vct.to_numpy()
"""
Expand All @@ -463,7 +459,7 @@ def V_NUMPY ( vct ) :
# @endcode
@staticmethod
def M_NUMPY ( mtrx ) :
"""Convert matrix into numpy.matrix
""" Convert matrix into numpy.matrix
>>> mtrx = ...
>>> m = mtrx.to_numpy()
"""
Expand All @@ -480,7 +476,7 @@ def M_NUMPY ( mtrx ) :
# @endcode
@staticmethod
def S_LA ( obj ) :
"""Convert matrix/vector-like object to SMatrix/SVector
""" Convert matrix/vector-like object to SMatrix/SVector
>>> obj = ...
>>> res = LinAlg.S_LA ( obj )
"""
Expand Down Expand Up @@ -538,7 +534,7 @@ def ADD ( a , b ) :
# @endcode
@staticmethod
def IADD ( a , b ) :
"""Increment of matrix/vector objects
""" Increment of matrix/vector objects
>>> A += B
"""

Expand Down Expand Up @@ -669,7 +665,7 @@ def RSUB ( a , b ) :
# @endcode
@staticmethod
def MUL ( a , b ) :
""" Multiplication/scaling of vector/matrix objects:
""" Multiplication/scaling of vector/matrix objects:
>>> C = A * B
"""

Expand Down Expand Up @@ -721,7 +717,7 @@ def IMUL ( a , b ) :
# @endcode
@staticmethod
def RMUL ( a , b ) :
"""Right-multiplication of matrix/vector objects
""" Right-multiplication of matrix/vector objects
>>> C = B * A
"""

Expand Down Expand Up @@ -770,7 +766,7 @@ def DIV ( a , b ) :
# @endcode
@staticmethod
def IDIV ( a , b ) :
"""Multiplicative decrement/scaling of matrix/vector objects
""" Multiplicative decrement/scaling of matrix/vector objects
>>> A /= B
"""

Expand All @@ -790,7 +786,7 @@ def IDIV ( a , b ) :
# @endcode
@staticmethod
def EQ ( a , b ) :
"""Equality for matrix/vector objects
""" Equality for matrix/vector objects
>>> A == B
"""

Expand Down Expand Up @@ -830,7 +826,7 @@ def EQ ( a , b ) :
# @endcode
@staticmethod
def NE ( a , b ) :
"""Non-equality for matrix/vector objects
""" Non-equality for matrix/vector objects
>>> A != B
"""

Expand Down Expand Up @@ -896,7 +892,7 @@ def DOT ( a , b ) :
# @endcode
@staticmethod
def CROSS ( a , b ) :
"""Cross-product (D1xD2 matrix) of two vectors
""" Cross-product (D1xD2 matrix) of two vectors
>>> matrix = v1.cross ( v2 )
"""

Expand Down Expand Up @@ -924,7 +920,7 @@ def CROSS ( a , b ) :
# @endcode
@staticmethod
def SIM ( a , b ) :
"""Similarity operation: C = B A B^T
""" Similarity operation: C = B A B^T
>>> A = ...
>>> B = ...
>>> C = A.Sim ( B )
Expand Down Expand Up @@ -954,7 +950,7 @@ def SIM ( a , b ) :
raise NotImplementedError ( "No SIM for %s/%s and %s/%s" % ( a , typename ( a ) , b , typename ( b ) ) )

# =========================================================================
## Similarity operation \f$ C = B^T A B \f$
## Similarity operation \f$ C = B^T A B \f$
# @code
# A = ...
# B = ...
Expand All @@ -963,7 +959,7 @@ def SIM ( a , b ) :
# @endcode
@staticmethod
def SIMT ( a , b ) :
"""Similarity operation C = B^T A B
""" Similarity operation C = B^T A B
>>> A = ...
>>> B = ...
>>> C = A.SimT ( B )
Expand Down Expand Up @@ -1261,7 +1257,20 @@ def M_MAXABSDIAGONAL ( mtrx ) :
"""
return Ostap.Math.maxabs_diagonal( mtrx )


# =============================================================================
## Are all elements of matrix/vector finite?
# @code
# mtrx = ...
# mtrx.isfinite()
# @endcode
@staticmethod
def M_ISFINITE ( mtrx ) :
""" Are all elements of matrix/vector finite?
>>> mtrx = ...
>>> mtrx.isfinite()
"""
return Ostap.Math.isfinite ( mtrx )

# =============================================================================
## get matrix shape
# @code
Expand Down Expand Up @@ -2274,6 +2283,7 @@ def deco_vector ( t ) :

t.__reduce__ = LinAlg.V_REDUCE

t.isfinite = LinAlg.M_ISFINITE

s = revct.search ( t.__name__ )
if s :
Expand Down Expand Up @@ -2366,6 +2376,8 @@ def deco_matrix ( m ) :
m.lnorm = LinAlg.M_LNORM
m.mnorm = LinAlg.M_MNORM

m.isfinite = LinAlg.M_ISFINITE

if m.kRows == m.kCols :
m.inverse = LinAlg.M_INVERSE

Expand Down
5 changes: 5 additions & 0 deletions ostap/stats/corr2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ def __init__ ( self ,
self.selection ,
*self.args )

if not self.__wcov.isfinite() : logger.error ( "Invalid covariance for '%s` : `%s'" % ( self.var1 , self.var2 ) )

self.__counter1 = self.__wcov.counter1 ()
self.__counter2 = self.__wcov.counter2 ()

if not self.__counter1.isfinite() : logger.error ( "Invalid statistics for '%s'" % self.var1 )
if not self.__counter2.isfinite() : logger.error ( "Invalid statistics for '%s'" % self.var2 )

## the covariance matrix
self.__cov2 = Ostap.Math.covariance ( self.__wcov )

Expand Down
28 changes: 1 addition & 27 deletions ostap/stats/counters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
# =============================================================================
from ostap.math.ve import Ostap, VE
from ostap.math.base import isequal, isequalf, iszero, isfinite
from ostap.math.base import isequal, isequalf, iszero
from ostap.core.ostap_types import dictlike_types, sequence_types
import ROOT, cppyy, math, sys
# =============================================================================
Expand Down Expand Up @@ -148,18 +148,6 @@ def _se_ne_ ( s1 , s2 ) :
SE.__eq__ = _se_eq_
SE.__ne__ = _se_ne_

# =============================================================================
## Is the content of counter finit?
def _se_isfinite_ ( cnt ) :
""" Is the content of counter finit? """
return \
isfinite ( cnt.min () ) and \
isfinite ( cnt.max () ) and \
isfinite ( cnt.mu () ) and \
isfinite ( cnt.mu2 () )

SE.isfinite = _se_isfinite_

_new_methods_ += [
SE.sum ,
SE.mean ,
Expand All @@ -172,7 +160,6 @@ def _se_isfinite_ ( cnt ) :
SE.__str__ ,
SE.__eq__ ,
SE.__ne__ ,
SE.isfinite ,
]

# =============================================================================
Expand Down Expand Up @@ -218,18 +205,6 @@ def _wse_ne_ ( s1 , s2 ) :
WSE.__repr__ = lambda s : 'WStat: '+ s.toString()
WSE.__str__ = lambda s : 'WStat: '+ s.toString()

# =============================================================================
## Is the content of counter finit?
def _wse_isfinite_ ( cnt ) :
""" Is the content of counter finit? """
return \
isfinite ( cnt.mu () ) and \
isfinite ( cnt.mu2 () ) and \
cnt.values ().isfinite() and \
cnt.weights().isfinite()

WSE.isfinite = _wse_isfinite_

_new_methods_ += [
WSE.sum ,
WSE.mean ,
Expand All @@ -240,7 +215,6 @@ def _wse_isfinite_ ( cnt ) :
WSE.__str__ ,
WSE.__eq__ ,
WSE.__ne__ ,
WSE.isfinite ()
]

# =============================================================================
Expand Down
16 changes: 16 additions & 0 deletions source/include/Ostap/Covariance.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ namespace Ostap
/// reset counters
void reset () ;
// ======================================================================
// everything is finite?
inline bool isfinite () const
{
return std::isfinite ( m_cov2m )
&& m_cnt1.isfinite()
&& m_cnt2.isfinite() ;
}
// ======================================================================
private:
// ======================================================================
Ostap::StatEntity m_cnt1 { } ;
Expand Down Expand Up @@ -206,6 +214,14 @@ namespace Ostap
/// reset counters
void reset () ;
// ======================================================================
// everything is finite?
inline bool isfinite () const
{
return std::isfinite ( m_cov2m )
&& m_cnt1.isfinite()
&& m_cnt2.isfinite() ;
}
// ======================================================================
private:
// ======================================================================
Ostap::WStatEntity m_cnt1 { } ;
Expand Down
20 changes: 20 additions & 0 deletions source/include/Ostap/MatrixUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,26 @@ namespace Ostap
}
} ;
// ========================================================================

// ========================================================================
/// Are all elements of the vector finite?
template <class T, unsigned int D>
inline bool isfinite ( const ROOT::Math::SVector<T,D>& vct )
{
for ( const T& v : vct ) { if ( !std::isfinite ( v ) ) { return false ; } }
return true ;
}
// ========================================================================
// Are all elements of the matrix finite?
template <class T, unsigned int D1, unsigned int D2, class R>
inline bool isfinite ( const ROOT::Math::SMatrix<T,D1,D2,R>& mtrx )
{
for ( const T& v : mtrx ) { if ( !std::isfinite ( v ) ) { return false ; } }
return true ;
}
// ========================================================================

// ========================================================================
} // end of namespace Math
// ==========================================================================
} // end of namespace Ostap::Math
Expand Down
5 changes: 0 additions & 5 deletions source/include/Ostap/MatrixUtils2.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,6 @@ namespace Ostap
const double /* m2 */ ) { return true ; }
} ;





// ======================================================================
// Can be multiplied
// ======================================================================
Expand Down Expand Up @@ -233,7 +229,6 @@ namespace Ostap
} ;
// ========================================================================


// ========================================================================
template <class T,unsigned int D1, unsigned int D2,
class R2>
Expand Down
Loading

0 comments on commit 0850a9e

Please sign in to comment.