P: n/a

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  
Share this Question
P: n/a

>>>>> "Gaubitzer" == Gaubitzer Erwin <a9******@unet.univie.ac.at> writes:
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/softw...rray/manualPDF
x = arange(100) ind = array([23,24,25]) x[ind]
array([23, 24, 25])
Cheers,
JDH  
P: n/a

On Tue, 3 Aug 2004, Gaubitzer Erwin wrote: 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]]]) take(a,(1,),0)
array([ [[5, 6],
[7, 8]]]) take(a,(0,),1)
array([[ [1, 2]],
[ [5, 6]]]) take(a,(0,),2)
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 N1 from that which it is passed:
takeslice(a,0,0)
array([[1, 2],
[3, 4]]) takeslice(a,1,0)
array([[5, 6],
[7, 8]]) takeslice(a,0,1)
array([[1, 2],
[5, 6]]) takeslice(a,0,2)
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:
a[0,...]
array([[1, 2],
[3, 4]]) a[1,...]
array([[5, 6],
[7, 8]]) a[...,0]
array([[1, 3],
[5, 7]])
Hope this helps, and is understandable :)  
P: n/a

Gaubitzer Erwin <a9******@unet.univie.ac.at> wrote: 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?
== Posted via Newsfeed.Com  UnlimitedUncensoredSecure Usenet News== http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
= 19 East/WestCoast Specialized Servers  Total Privacy via Encryption =  
P: n/a

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 6dimensional 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  
P: n/a

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  
P: n/a

Gaubitzer Erwin wrote: 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 I tried to use take() but in my 6dimensional 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   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2367
 replies: 6
 date asked: Jul 18 '05
