Skip to content

Commit

Permalink
Fix numpy scalar repr
Browse files Browse the repository at this point in the history
  • Loading branch information
patnr committed Feb 19, 2025
1 parent ad8bc6e commit 8c2ff04
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions dapper/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ def split_decimal(x):
def frmt(x):
if x is None:
return missingval
if isinstance(x, tuple):
x = tuple(np2builtin(v) for v in x)
ints, decs = split_decimal(x)
x = f"{ints.rjust(nInt, pad)}"
if decs == "int":
Expand Down Expand Up @@ -701,6 +703,11 @@ def tabulate_avrgs(avrgs_list, statkeys=(), decimals=None):
return columns


def np2builtin(v):
"Sometimes necessary since NEP-50"
return v.item() if isinstance(v, np.generic) else v


def center(E, axis=0, rescale=False):
r"""Center ensemble.
Expand Down
6 changes: 4 additions & 2 deletions dapper/xp_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tabulate import tabulate

from dapper.dpr_config import rc
from dapper.stats import align_col, unpack_uqs
from dapper.stats import align_col, np2builtin, unpack_uqs
from dapper.tools.colors import color_text, stripe
from dapper.tools.rounding import UncertainQtty
from dapper.tools.viz import NoneDict, default_styles
Expand Down Expand Up @@ -101,7 +101,9 @@ def __init__(self, dims):

def repr2(c, keys=False, str_or_repr=repr):
if keys:
lst = [f"{k}={str_or_repr(v)}" for k, v in c._asdict().items()]
lst = [
f"{k}={str_or_repr(np2builtin(v))}" for k, v in c._asdict().items()
]
else:
lst = [str_or_repr(v) for v in c]
return "(" + ", ".join(lst) + ")"
Expand Down

0 comments on commit 8c2ff04

Please sign in to comment.