Skip to content

Commit

Permalink
1. improve pretty-print for matrices
Browse files Browse the repository at this point in the history
   1. add `pretty_array` function for nice print of arrays/sequences
   1. add some auxillary methods for matrices: `(min/max/minabs/maxabs)_element` and `(min/max)_diagonal` and `(min/max/minabs/maxabs)_element_index`
  • Loading branch information
VanyaBelyaev committed Aug 27, 2024
1 parent e838930 commit 638f028
Show file tree
Hide file tree
Showing 10 changed files with 678 additions and 283 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include(CTest)
set(OSTAP_VERSION_MAJOR 1)
set(OSTAP_VERSION_MINOR 13)
set(OSTAP_VERSION_PATCH 0)
set(OSTAP_VERSION_TWEAK 0)
set(OSTAP_VERSION_TWEAK 1)

set(OSTAP_VERSION ${OSTAP_VERSION_MAJOR}.${OSTAP_VERSION_MINOR}.${OSTAP_VERSION_PATCH}.${OSTAP_VERSION_TWEAK})

Expand Down
11 changes: 10 additions & 1 deletion ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## New features

1. improve prettry-print for matrices
1. add `pretty_array` function for nice print of arrays/sequences
1. add some auxillary methods for matrices: `(min/max/minabs/maxabs)_element` and `(min/max)_diagonal` and `(min/max/minabs/maxabs)_element_index`

## Backward incompatible

## Bug fixes

# v1.13.0.0

## New features
Expand All @@ -19,7 +29,6 @@
1. fix a minor bug in `bernstein.solve`
1. fix couple of recent bugs in `histos.graphs` `


# v1.12.0.0

## New features
Expand Down
20 changes: 0 additions & 20 deletions ReleaseNotes/v1.13.0.0.md

This file was deleted.

72 changes: 37 additions & 35 deletions ostap/core/ostap_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
'all_strings' , ## all argumets of string types?
)
# =============================================================================
import math, os
from sys import version_info as python_version
import math, os, array
from sys import version_info as python_version
# =============================================================================
if ( 3 , 0 ) <= python_version :
long = int
string_types = bytes , str
Expand All @@ -56,17 +57,17 @@
integer_types = int , long
long_type = long
python_version = 2
import collections as C
import collections as C
# =============================================================================
if ( 3 , 5 ) <= python_version :
from collections.abc import Collection, Sequence, Iterable, Sized, Generator
from collections.abc import Collection, Sequence, Iterable, Sized, Generator
elif ( 3 , 3 ) <= python_version :
from collections.abc import Collection, Sequence, Iterable, Sized
from types import GeneratorType as Generator
from collections.abc import Collection, Sequence, Iterable, Sized
from types import GeneratorType as Generator
else :
from collections import Sequence , Iterable , Sized
from collections import Container as Collection
from types import GeneratorType as Generator
from collections import Sequence , Iterable , Sized
from collections import Container as Collection
from types import GeneratorType as Generator

# =============================================================================
# logging
Expand All @@ -81,7 +82,6 @@
num_types = integer_types + ( float , )
str_types = str,
list_types = list , tuple
import array
listlike_types = list_types + ( set , C.Sequence , array.array )
# =============================================================================
try :
Expand All @@ -95,10 +95,10 @@
sequence_types = listlike_types + ( Sequence , Collection , Iterable , Generator )
sized_types = Sized ,
path_types = string_types
if (3,6) <= python_version :
if ( 3 , 6 ) <= python_version :
path_types = string_types + ( os.PathLike , )
# =============================================================================
## sometimewe we need to ensure that dictionary is ordered
## sometimes we need to ensure that dictionary is ordered
ordered_dict = dict
if python_version < ( 3 , 7 ) :
from collections import OrderedDict as ordered_dict
Expand All @@ -108,26 +108,35 @@
# =============================================================================
## Is this number of a proper integer?
def is_integer ( v ) :
"""Is this number of a proper integer?"""
""" Is this number of a proper integer?"""
return isinstance ( v , integer_types )

# =============================================================================
## Is this number of a proper numeric type
def is_number ( v ) :
"""Is this number of a proper numeric?"""
""" Is this number of a proper numeric?"""
return isinstance ( v , num_types )

# =============================================================================
## good numeric value
def is_good_number ( v ) :
"""Is numeric type and good value?"""
return isinstance ( v , num_types ) and \
( not math.isinf ( v ) ) and ( not math.isnan ( v ) )
if ( 3 , 2 ) <= python_version :
# =========================================================================
## good numeric value
def is_good_number ( v ) :
""" Is numeric type and good value?"""
return isinstance ( v , num_types ) and math.isfinite ( v )
# =========================================================================
else :
# =========================================================================
## good numeric value
def is_good_number ( v ) :
""" Is numeric type and good value?"""
return isinstance ( v , num_types ) and \
( not math.isinf ( v ) ) and ( not math.isnan ( v ) )

# =============================================================================
## is value of str-type?
def is_string ( v ) :
"""Is value of str-type?"""
""" Is value of str-type?"""
return isinstance ( v , str_types )

# =============================================================================
Expand All @@ -139,42 +148,35 @@ def is_string_like ( v ) :
# =============================================================================
## is list type?
def is_list ( v ) :
"""Is value of list type (list ot tuple)"""
""" Is value of list type (list ot tuple)"""
return isinstance ( v , list_type )

# =============================================================================
## is list-like type?
def is_list_like ( v ) :
"""Is value of list-like type (list but not a string-like!)"""
""" Is value of list-like type (list but not a string-like!)"""
return isinstance ( v , listlike_type ) and not isinstance ( v , string_types )

# =============================================================================
## are all values of integer-line types?
def all_integers ( *args ) :
"""Are all value of integer-line types?
""" Are all value of integer-line types?
"""
for a in args :
if not isinstance ( a , integer_types ) : return False
return True
return all ( isinstance ( a , integer_types ) for a in args )

# =============================================================================
## are all values of numeric types?
def all_numerics ( *args ) :
"""Are all values of numeric types?
""" Are all values of numeric types?
"""
for a in args :
if not isinstance ( a , num_types ) : return False
return True
return all ( isinstance ( a , num_types ) for a in args )

# =============================================================================
## are all values of string types?
def all_strings ( *args ) :
"""Are all values of string types?
""" Are all values of string types?
"""
for a in args :
if not isinstance ( a , stgring_types ) : return False
return True

return all ( isinstance ( a , string_types ) for a in args )

# =============================================================================
if '__main__' == __name__ :
Expand Down
87 changes: 81 additions & 6 deletions ostap/logger/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
'add_expo' , ## add an exponetaion factor for sting representaion of the object
)
# =============================================================================
from ostap.logger.utils import ( pretty_float ,
pretty_err1 ,
pretty_err2 ,
pretty_errs ,
add_expo ,
fmt_pretty_err1 )
from ostap.core.ostap_types import num_types, sequence_types
from ostap.logger.utils import ( pretty_float ,
pretty_err1 ,
pretty_err2 ,
pretty_errs ,
add_expo ,
fmt_pretty_err1 ,
fmt_pretty_float )
# =============================================================================
# logging
# =============================================================================
Expand Down Expand Up @@ -95,6 +97,79 @@ def pretty_ve ( value ,
## delegate
return pretty_err1 ( v , e , width = width , precision = precision )

# =======================================================================
## nice printout of complex number ( string + exponent)
# @code
# ae = complex ( ... )
# s , expo = pretty_complex ( ae )
# @endcode
# @return nice string and the separate exponent
def pretty_complex ( value ,
width = 6 ,
precision = 4 ,
parentheses = True ) :
""" Nice printout of complex number ( string + exponent)
- return nice stirng and the separate exponent
>>> s , expo = pretty_complex( number )
"""
v = complex ( value )
vv = max ( abs ( v.real() ) , abs ( v.imag ) )
##
fmtv , expo = fmt_pretty_float ( value = vv ,
width = width ,
precision = precision ,
parentheses = False )
##
fmt = '%s%sj' % ( fmtv , fmtv )
if parentheses : fmt = '(' + fmt + ')'
##
if expo :
scale = 10 ** expo
v /= scale
return fmt % ( v.real , v.imag ) , expo

# =======================================================================
## Nice pritout of arrays of numerical values
# @code
# array = ...
# result, expo = pretty_array ( array )
# @endcode
# @return nice string and the separate exponent
def pretty_array ( value ,
width = 6 ,
precision = 4 ,
parentheses = True ) :
""" Nice pritout of arrays of numerical values
- return nice stirng and the separate exponent
>>> array = ...
>>> result, expo = pretty_array ( array )
"""
assert isinstance ( value , sequence_types ) , \
"Invalid type of `value':%s" % type ( value )

if not value : return '' if not parentheses else []

assert all ( isinstance ( v , num_types ) for v in value ) , \
"Invalid content of `value': %s" % str ( value )

value1 = value [ 0 ]
value2 = max ( abs ( v ) for v in value )
vv = max ( abs ( value1 ) , abs ( value2 ) )
#
fmtv , expo = fmt_pretty_float ( value = vv ,
width = width ,
precision = precision )

if expo : scale = 10 ** expo
else : scale = 1
##
result = []
for v in value : result.append ( fmtv % ( v / scale ) )
result = ', '.join ( result )
##
if parentheses : result = ' [ ' + result + ' ]'
return result, expo

# =======================================================================
## nice printout of asymmetric errors ( string + exponent)
# @code
Expand Down
Loading

0 comments on commit 638f028

Please sign in to comment.