CDAT / cdms

8 stars 10 forks source link

TransientVariable indexing with numpy.int64 #416

Open Xunius opened 5 years ago

Xunius commented 5 years ago

Hi all,

I got this error when doing something like:

val = mydata[y, x]

Error:

raise CDMSError('invalid index: %s' % str(key))

where mydata is a TransientVariable, y and x happen to be numpy.int64. I guess that's why they fail the test in cdms2/avariable's specs2slices() function:

if isinstance(key, int):  # x[i]

So maybe something like:

if isinstance(key, (int, numpy.int64)):  # x[i]

For py2 there is also a long type.

Or we just force user to cast their indices to plain int?

doutriaux1 commented 5 years ago

Thanks @Xunius , just as an aside, in the future, do you mind filing cdms2 bug in the cdms repo? It makes it easier for us maintainer. Please use CDAT only if you do not know for sure which cdsat component the bug is relating to. Thanks. @dnadeau4 we should create a template issue that mentions this,

Xunius commented 5 years ago

@doutriaux1 Sure, that makes good sense, sorry I didnt think of that.

Xunius commented 3 years ago

Coming back to the old issue. I noticed that this is also causing trouble when reading a point value from a matplotlib plot figure. When putting the cursor at a point in a 2D imshow plot, it is calling specs2slices() to slice the data, and fails because numpy.int64 is not counted as an integer.

The following fix seems to work:

key = speclist[i]
if isinstance(key, int):  # x[i]

change to:

key = speclist[i]
if isinstance(key, numbers.Integral):  # x[i]

And we import numbers at the top of avariable.py.

matplotlib version: 3.3.0 cdms2 version: 3.1.5 numpy version: 1.19.1