-
Notifications
You must be signed in to change notification settings - Fork 188
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
Comments
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. |
I'm not intimately familiar with numpy, but I'll play around and see how But, so we're on the same page, please provide an example of what you'd like to see |
Sure:
\>>> a = np.random.uniform(size=(2,2))
\>>> a
array([[0.96552581, 0.00289987],
[0.35171792, 0.53700621]])
\>>> type(a)
<class 'numpy.ndarray'>
\>>> a.shape
(2, 2)
\>>> a.dtype
dtype('float64')
The shape is the array shape, i.e. the size of each dimension, and dtype
the datatype of the elements. These two are often more interesting to print
than the array itself when debugging (though I'm not suggesting to view
*only* this information, just in addition) because functions on np arrays
often change either property.
2018-02-18 7:46 GMT+01:00 gruns <notifications@github.com>:
… I'm not intimately familiar with numpy. I'll play around with it 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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGd9xP5Or0JamkcNN2IvWQ2ooyn_ORE2ks5tV8c3gaJpZM4SGGzW>
.
|
Digging into numpy a bit more: does numpy include a way to seraliaze
>>> 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 |
No, I don't think that's built-in
2018-03-07 10:06 GMT+01:00 gruns <notifications@github.com>:
… 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(...)).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGd9xK1Krb6opvN_b78-QuJH0dr0ES9Hks5tb6L1gaJpZM4SGGzW>
.
|
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? |
Cool, thanks!
2018-03-12 8:02 GMT+01:00 gruns <notifications@github.com>:
… 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]])
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGd9xNltZDp2gTgQ8TThQMln2HAolPzAks5tdh2UgaJpZM4SGGzW>
.
|
Feature request for having
ict
print type in addition, andicnp
(or just plainic
) ( print shape/dtype if argument is a numpy arrayThe text was updated successfully, but these errors were encountered: