xl0 / lovely-numpy

NumPy arrays, ready for human consumption
https://xl0.github.io/lovely-numpy
MIT License
63 stars 4 forks source link

Don't fail for non-numeric dtypes #14

Closed baldassarreFe closed 5 months ago

baldassarreFe commented 6 months ago

Currently invoking lo() on arrays that contain non-numeric dtypes fails when checking if the array is all-zero, and when computing min/max/mean/std:

Input:

lo(np.array(["bla", "bla"]))

Output:

-> zeros = ansi_color("all_zeros", "grey", color) if np.equal(x, 0.).all() and x.size > 1 else None
pinf = ansi_color("+Inf!", "red", color) if np.isposinf(x).any() else None
ninf = ansi_color("-Inf!", "red", color) if np.isneginf(x).any() else None
...
UFuncTypeError: ufunc 'equal' did not contain a loop with signature matching types (<class 'numpy.dtype[str_]'>, <class 'numpy.dtype[float64]'>) -> <class 'numpy.dtype[bool_]'>

The ideal behavior would be to catch the exception and still print some information about the array, for example:

Input:

lo(np.linspace(0, 1, 5))
lo(np.array(["bla", "bla"]))

Output:

array[5] x∈[0., 1.000] μ=0.500 σ=0.354 [0., 0.250, 0.500, 0.750, 1.000]
array[2] str
xl0 commented 5 months ago

Heya. Yeah, agree, it should be something other than an exception. I think I'll catch the exception and use the default numpy repr.

xl0 commented 5 months ago

@baldassarreFe I've ended up just checking for non-numeric types. Released 0.2.12, please give it a go (pip in there, conda will take some time to update)

baldassarreFe commented 5 months ago

Hello, I'll update and check that it works. Thanks a lot!!