Skip to content

Commit

Permalink
Fix handling of np.bool_ arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
xl0 committed Jul 20, 2024
1 parent 89090d7 commit 37a8595
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lovely_numpy/repr_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def lovely( x :Union[np.ndarray, np.generic], # The data you want to explo

"Pretty-print the stats of a numpy array or scalar"

if plain or not isinstance(x, (np.ndarray, np.generic)) or np.iscomplexobj(x) or not np.issubdtype(x.dtype, np.number):
if (plain or
not isinstance(x, (np.ndarray, np.generic)) or
np.iscomplexobj(x) or
( not np.issubdtype(x.dtype, np.number) and not np.issubdtype(x.dtype, np.bool_) )
):
return repr(x)

conf = get_config()
Expand Down
20 changes: 17 additions & 3 deletions nbs/00_repr_str.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@
"\n",
" \"Pretty-print the stats of a numpy array or scalar\"\n",
"\n",
" if plain or not isinstance(x, (np.ndarray, np.generic)) or np.iscomplexobj(x) or not np.issubdtype(x.dtype, np.number):\n",
" if (plain or\n",
" not isinstance(x, (np.ndarray, np.generic)) or\n",
" np.iscomplexobj(x) or\n",
" ( not np.issubdtype(x.dtype, np.number) and not np.issubdtype(x.dtype, np.bool_) )\n",
" ):\n",
" return repr(x)\n",
"\n",
" conf = get_config()\n",
Expand Down Expand Up @@ -182,7 +186,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/xl0/lovely-numpy/blob/master/lovely_numpy/repr_str.py#L39){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/xl0/lovely-numpy/blob/master/lovely_numpy/repr_str.py#L34){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### lovely\n",
"\n",
Expand All @@ -205,7 +209,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/xl0/lovely-numpy/blob/master/lovely_numpy/repr_str.py#L39){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/xl0/lovely-numpy/blob/master/lovely_numpy/repr_str.py#L34){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### lovely\n",
"\n",
Expand Down Expand Up @@ -559,6 +563,16 @@
"i = np.array([1, 2, 3])\n",
"test_eq(str(lovely(i)), \"array[3] i64 x∈[1, 3] μ=2.000 σ=0.816 [1, 2, 3]\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"i = np.array([True, False, True])\n",
"test_eq(str(lovely(i)), 'array[3] bool x∈[False, True] μ=0.667 σ=0.471 [True, False, True]')"
]
}
],
"metadata": {
Expand Down

0 comments on commit 37a8595

Please sign in to comment.