How to dynamically access Numeric subarrays

  • Thread starter Gaubitzer Erwin
  • Start date
G

Gaubitzer Erwin

Hi there

I wrote a short program which reads scientific data from
a file and stores its values in a Numeric array.
At the same time it reads the names of its dimensions which
are then in the same order as the indices of the Numeric
array.

After then I want to access the data in the array by its name
the way that I keep all indices at constant values except the
one I want to read out which I am slicing.

The problem is that the input data varies in its dimensions
so my wanted data can appear at different positions of the
array. I tried to create a list with the slice on the appropriate position
to use it as indices list in the array but this failed.

So my questions to out there:
How can I extract a (Numeric Python) subarray whose indices
have to be built dynamically.

Thanks in advance

Erwin
 
J

John Hunter

Gaubitzer> The problem is that the input data varies in its
Gaubitzer> dimensions so my wanted data can appear at different
Gaubitzer> positions of the array. I tried to create a list with
Gaubitzer> the slice on the appropriate position to use it as
Gaubitzer> indices list in the array but this failed.

Gaubitzer> So my questions to out there: How can I extract a
Gaubitzer> (Numeric Python) subarray whose indices have to be
Gaubitzer> built dynamically.

In Numeric, use the take function
>>> x = arange(100)
>>> ind = [23,24,25]
>>> take(x,ind)
array([23, 24, 25])

In numarray, you can use index arrays. See section 4.8 of the
numarray manual for more information -
http://www.stsci.edu/resources/software_hardware/numarray/manualPDF
>>> x = arange(100)
>>> ind = array([23,24,25])
>>> x[ind]
array([23, 24, 25])

Cheers,
JDH
 
C

Christopher T King

So my questions to out there:
How can I extract a (Numeric Python) subarray whose indices
have to be built dynamically.

The Numeric function take() might meet your needs:
from Numeric import *
a = array([[[1,2],[3,4]],[[5,6],[7,8]]])
take(a,(0,),0)
array([ [[1, 2],
[3, 4]]])array([ [[5, 6],
[7, 8]]])array([[ [1, 2]],
[ [5, 6]]])array([[[1],
[3]],
[[5],
[7]]])

The second argument specifies which indices to take, and the third
argument specifies to which dimension to apply the indices.

Note that take() returns an array of the same rank as that of its input;
this may not be what you want. To obtain an array of one less dimension,
you'll need to reshape it. A function like the following may be helpful:

def takeslice(a,index,dimension):
r = take(a,(index,),dimension)
s = shape(r)
return reshape(r,s[:dimension]+s[dimension+1:])

This will only accept single indexes to slice, rather than a tuple, but
will return you an array of rank N-1 from that which it is passed:
array([[1, 2],
[3, 4]])array([[5, 6],
[7, 8]])array([[1, 2],
[5, 6]])array([[1, 3],
[5, 7]])

Also of tangential interest is the ... operator. This magic operator,
given to a slice, means "replace me with however many : are needed to make
this work". It won't necessarily help your situation, but it's a handy
thing to know:
array([[1, 2],
[3, 4]])
array([[5, 6],
[7, 8]])
array([[1, 3],
[5, 7]])

Hope this helps, and is understandable :)
 
B

beliavsky

Gaubitzer Erwin said:
So my questions to out there:
How can I extract a (Numeric Python) subarray whose indices
have to be built dynamically.

Can the "take" function do what you want?
 
G

Gaubitzer Erwin

Hi again


For example I have an array AR whose shape is
(2, 1, 2, 1, 100, 3).
and I want to access
AR[0,0,0,0,:,1]
which results in an rank 1 array with my wanted
numbers (more advanced I want to loop through another
index).

I can write it manually but I can't insert this
list of indices automatically, because the slice
notation gives me an error when given to a python list.

I tried to use take() but in my 6-dimensional array
I wasn't able to find the right parameter combination
to access the vector above.
Maybe one can give me the right notation.

Thanks again

Erwin
 
G

Gaubitzer Erwin

Hi at last

I found the solution myself
It was my fault not to look at the
Python basics.
The indices in an array is a tuple!
So I am able to create automatically
such ones with standard operations
like

MyTuple = (0,0,2,slice(None),4)

(look at how to do the slice)
and also

MyOtherTuple += MyTuple + (2,)

or

QuickTuple = (0,)*5

and inserting it into an array

MyArrayWithIndices[MyTuple]



Thanks to all for thinking about it

Greetings
Erwin
 
T

Tim Hochberg

Gaubitzer said:
Hi again


For example I have an array AR whose shape is
(2, 1, 2, 1, 100, 3).
and I want to access
AR[0,0,0,0,:,1]
which results in an rank 1 array with my wanted
numbers (more advanced I want to loop through another
index).

I can write it manually but I can't insert this
list of indices automatically, because the slice
notation gives me an error when given to a python list.

I believe you want something like:
>>> index = (0,0,0,0,slice(None,None),1)
>>> AR[index]

The args to slice will vary depending on exactly what you want to do.
slice can take up to three arguments for start, stop, step.

Since you seem to be delving deeply into the mysteries of numeric
slicing, it may eventually help you to know that '...' is spelled
Ellipsis if you want to use it in a tuple as above.

Actually, the little class below will probably help you more than
anything that I can write:

class IndexInspector:
def __getitem__(self, key):
return key

Used like:

II = IndexInspector()
print II[0,0,0,0,:,1]
print II[...,0,0,:,1]

prints:

(0, 0, 0, 0, slice(None, None, None), 1)
(Ellipsis, 0, 0, slice(None, None, None), 1)


Regards,

-tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top