Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type, np.ndarray shape #1

Closed
tsoernes opened this issue Feb 14, 2018 · 7 comments
Closed

Type, np.ndarray shape #1

tsoernes opened this issue Feb 14, 2018 · 7 comments

Comments

@tsoernes
Copy link

tsoernes commented Feb 14, 2018

Feature request for having ict print type in addition, and icnp (or just plain ic) ( print shape/dtype if argument is a numpy array

@woodm1979
Copy link

Not entirely sure it's necessary, but I'd +1 like the 2nd command. I'd not want to prevent people from seeing a numpy array if they wanted to see one.

@gruns
Copy link
Owner

gruns commented Feb 18, 2018

I'm not intimately familiar with numpy, but I'll play around and see how ic() handles np.ndarrays.

But, so we're on the same page, please provide an example of what you'd like to see ic() output when provided with an np.ndarray.

@tsoernes
Copy link
Author

tsoernes commented Feb 18, 2018 via email

@gruns
Copy link
Owner

gruns commented Mar 7, 2018

Digging into numpy a bit more: does numpy include a way to seraliaze np.ndarrays to strings in such a way that their type and/or shape is included?

str() and repr() (which ic() uses) don't do it.

>>> import numpy as np
>>> a = np.ndarray(shape=(2,2), dtype=float, order='F')
>>> str(a)
'[[  6.90652664e-310   1.18609510e-316]\n [  1.25709272e-316   1.18609272e-316]]'
>>> repr(a)
'array([[  6.90652664e-310,   1.18609510e-316],\n       [  1.25709272e-316,   1.18609272e-316]])'

Unfortunately I'm not familiar enough with numpy to deftly navigate dir(np.ndarray(...)).

@tsoernes
Copy link
Author

tsoernes commented Mar 7, 2018 via email

@gruns
Copy link
Owner

gruns commented Mar 12, 2018

numpy.ndarrays are now supported in IceCream v1.2.

>>> import numpy as np
>>> from icecream import ic
>>> 
>>> a = np.ndarray(shape=(2,2), dtype=float, order='F')
>>> ic(a)
ic| a: array([[  6.90246722e-310,   4.67090452e-317],
              [  6.90246722e-310,   4.67028793e-317]])

Also new in v1.2 is the ability to customize the argument to string serialization function. So, if you want to include the datatype and shape of ndarrays, you can now do so like

>>> import numpy as np
>>> from icecream import ic, argumentToString
>>> 
>>> def numpyAwareToString(obj):
>>>     s = argumentToString(obj)
>>>     if isinstance(obj, np.ndarray):
>>>         return f'<ndarray: type {obj.dtype}, shape {obj.shape}>\n{s}'
>>>     return s
>>> 
>>> ic.configureOutput(argToStringFunction=numpyAwareToString)
>>> 
>>> a = np.ndarray(shape=(2,2), dtype=float, order='F')
>>> ic(a)
ic| a: <ndarray: type float64, shape (2, 2)>
       array([[ 6.94547807e-310, -8.91366976e+303],
              [ 6.94547807e-310,  2.21091692e-236]])

Does this suffice for your needs, @tsoernes?

@tsoernes
Copy link
Author

tsoernes commented Mar 12, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants