473,471 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to dynamically access Numeric subarrays

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
Jul 18 '05 #1
6 2694
>>>>> "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

Jul 18 '05 #2
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 N-1 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 :)

Jul 18 '05 #3

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 - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 18 '05 #4
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
Jul 18 '05 #5
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

Jul 18 '05 #6
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 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


Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Satish Kumar Chimakurthi | last post by:
Hi all, An external solver program is dynamically producing files with different names 0000001.dat, 0000002.dat, 0000003.dat etc.....at regular intervals. These files contain all numeric data....
7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
0
by: Mark Oueis | last post by:
Is there any way I can retrieve the result set of a Stored Procedure in a function. ALTER FUNCTION dbo.fn_GroupDeviceLink ( @groupID numeric ) RETURNS @groupDeviceLink TABLE (GroupID numeric,...
3
by: Jon Ole Hedne | last post by:
My Access 2002-application need to work with tables from both Oracle and Access. To solve this, I want to run some querys on three views in Oracle and import the results into temporary...
1
by: mhearne808 | last post by:
I have a question about how dynamically loaded C++ modules work, which I will phrase as a hypothetical scenario involving the Numeric module. Please understand that I don't really care about...
32
by: vonclausowitz | last post by:
Hi All, I have database with names on which I want to use the soundex option. So I have created two seperate fields for the Lastname and Firstname in which I save the Soundex version of a new...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
8
by: saladinator | last post by:
I have created an Excel spreadsheet that has a lot of data. What I want to do is import the spreedsheet to Access and create a form so that I can print each row per page in a proffessional manner....
1
by: pereges | last post by:
I'm trying to build a kdtree for a 3d object. An object contains vertices(3d vectors) and triangles (triangular mesh structure). The idea behind using a kdtree is to split the bounding box(a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.