icecream icon indicating copy to clipboard operation
icecream copied to clipboard

ic have compatible issues with numpy poly

Open FRAOTIAC opened this issue 3 years ago • 2 comments

code to reproduce:

x = np.array([0.0, 1.0, 2.0, 3.0, 4.0,  5.0, 6.0,  7.0,  8.0,  9.0, 10.0,])
y = np.array([0.0, 1.0, 2.0, 3.0, 4.0,  5.0, 4.0,  3.0,  2.0,  1.0,  0.0,])

curve = np.polyfit(x, y, 2)
poly = np.poly1d(curve)
# show curve
ic(poly)
print(poly)

The problem is that ic(poly) only show a array, while print shows the whole formula, like this: image

FRAOTIAC avatar Jan 13 '22 06:01 FRAOTIAC

I don't think that is actually a compatible issue for icecream. I think you are printing curve instead of poly using ic in the picture since the name that you are running with icecream is "curve" instead of "poly".

The problem with ic(poly): I also noticed that ic(poly) is print different output from print(poly). Here is the my input: Screen Shot 2022-04-14 at 22 14 31 And here are the local variables' value in the debugger: Screen Shot 2022-04-14 at 22 15 32 The value for poly is actually poly1d([-0.17482517, 1.74825175, -0.34965035]). If we go further with the debugger on the line ic(poly), the debugger shows that: Screen Shot 2022-04-14 at 22 19 27 Screen Shot 2022-04-14 at 22 19 17 It shows that icecream takes the value of poly as its input argument, and printing that as its output. I believe icecream is doing its job well. The fact that ic(poly) and print(poly) print different outputs might be related to numpy.poly1d has some overload function with print(), while it does not have any with icecream. I will do some further research about numpy.poly1d to see whether this can be fixed or not! Hope it is useful!

zhangck981 avatar Apr 15 '22 02:04 zhangck981

The 'problem' is that print uses the str() function to get the string, whereas icecream uses the repr() function. So, if you want the 'nice' version of poly, just do

ic(str(poly))

With output

ic| str(poly): '         2
                -0.1748 x + 1.748 x - 0.3497'

The only disadvantage is that it shows str(poly) as the variable name and not just poly.

salabim avatar Apr 15 '22 04:04 salabim