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

MV[index] array indexing problem #163

Closed
dnadeau4 opened this issue Aug 30, 2017 · 10 comments
Closed

MV[index] array indexing problem #163

dnadeau4 opened this issue Aug 30, 2017 · 10 comments
Assignees

Comments

@dnadeau4
Copy link
Contributor

>>> import numpy
>>> b=numpy.array([1,2,3])
>>> c=numpy.arange(10)
>>> c
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> c[b]
array([1, 2, 3])
>>> b
array([1, 2, 3])
>>> import cdms2
>>> d=cdms2.MV2.arange(10)
>>> d
variable_2
masked_array(data = [0 1 2 3 4 5 6 7 8 9],
             mask = False,
       fill_value = 999999)


>>> d[b]
> /software/anaconda2/envs/devel/lib/python2.7/site-packages/cdms2/avariable.py(1440)__getitem__()
-> if type(key) is tuple:
(Pdb) key
array([1, 2, 3])
(Pdb) n
> /software/anaconda2/envs/devel/lib/python2.7/site-packages/cdms2/avariable.py(1443)__getitem__()
-> if isinstance(key, int) and key>=len(self):
(Pdb) 
> /software/anaconda2/envs/devel/lib/python2.7/site-packages/cdms2/avariable.py(1445)__getitem__()
-> speclist = self._process_specs([key], {})
(Pdb) 
/software/anaconda2/envs/devel/lib/python2.7/site-packages/cdms2/avariable.py:1198: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if (numpy.newaxis in specs):
ValueError: 'The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()'
> /software/anaconda2/envs/devel/lib/python2.7/site-packages/cdms2/avariable.py(1445)__getitem__()
-> speclist = self._process_specs([key], {})
@dnadeau4 dnadeau4 self-assigned this Aug 30, 2017
@jypeter
Copy link
Member

jypeter commented Sep 1, 2017

I regularly get this problem (or something similar) when passing cdms2 objects to matplotlib or other non-PCMDI packages. This is merely annoying for me, because I know the workaround. But it's much more troublesome for the average user

My workaround is to cast the array back to numpy, or numpy.ma, depending on what I'm doing:

>>> d.filled()[b]
array([1, 2, 3])
>>> numpy.ma.asarray(d)[b]
masked_array(data = [1 2 3],
             mask = False,
       fill_value = 999999)

Can you do some magic so that the beginning users are not frightened away by this horrible error? I don't even understand the error! It's a good thing I found this workaround years ago (must have been on a lucky day)

@doutriaux1
Copy link
Contributor

@jypeter you could also use d.asma()

@jypeter
Copy link
Member

jypeter commented Sep 1, 2017

I could, if I knew about it!

There are lots of available methods if I type dir(d)! But even if I had looked at help(d.asma), I'm not sure I would have found it was what I needed

Help on method asma in module cdms2.tvariable:

asma(self) method of cdms2.tvariable.TransientVariable instance

@dnadeau4
Copy link
Contributor Author

dnadeau4 commented Jan 9, 2018

I am not sure how to fix this one, If we have axes lon/lat/level/time I need to slice them as well. This example is good to extract data from indexes. [1,2,3]. I don't think that can easily be done in CDMS and it would be a new slicer for CDMS.

@doutriaux1
Copy link
Contributor

@dnadeau4 you should do either way, in the subslice function it barfs if it's not a slice (or something like that) just test for an array first and then send the subsetting to numpy.ma. This sort of operation drops all dimensions/attributes anyway. so just return somthing like:

 return MV2.array(self.filled()[input_slicer_array])

@dnadeau4
Copy link
Contributor Author

dnadeau4 commented Jan 9, 2018

ok I can do that....

@dnadeau4
Copy link
Contributor Author

dnadeau4 commented Jan 9, 2018

I thought I could trigger a discussion on a better slicer, similar to numpy.take...

@durack1
Copy link
Member

durack1 commented Jan 9, 2018

@dnadeau4 both CDAT/cdat#1288 and #55 cover this territory

@dnadeau4
Copy link
Contributor Author

dnadeau4 commented Jan 9, 2018

@durack1 nice

@dnadeau4
Copy link
Contributor Author

dnadeau4 commented Jan 9, 2018

8833705

@dnadeau4 dnadeau4 closed this as completed Jan 9, 2018
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

4 participants