473,809 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ 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 2716
>>>>> "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,ind ex,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(No ne),4)

(look at how to do the slice)
and also

MyOtherTuple += MyTuple + (2,)

or

QuickTuple = (0,)*5

and inserting it into an array

MyArrayWithIndi ces[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__(sel f, 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
1957
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. Is it possible to read each of these dynamically in python ?? If so, how should my code look like ?? If it was not dynamically required, then, I would change the name of the file in my *open* statement every time and read the corresponding one. But...
7
3975
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 arguments, got 0". Tested with Python 2.3.4 on OpenBSD and Python 2.4 on Win98; same results. Is this a bug in deepcopy, how I dynamically assign the instance method or something else? (See example code for how I did it.) If you're curious as...
0
1614
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, DeviceID numeric) AS BEGIN
3
23465
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 Access-tables. I have tried this: conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.ConnectionString = "data source=" & datafil & ";Jet OLEDB:Database Password=" conn.Open datafil
1
1286
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 Numeric per se, it's just a useful example of a module that defines a generally useful data type. Let's say I want to create a C++ Python extension module that has methods accepting the Numeric array type as input, and also create these arrays as...
32
8615
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 name I save in the database. I have the soundex code with the 6 numeric option. So I save for example in the field LastnameSE = 600192 and in the FirstnameSE = 545910.
12
13283
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 same Tag number. (I thought it might help) The checkboxes name is a concatenation of "chkCancel" and a number that represents the order in which they were created: chkCancel0 (Tag = 0) chkCancel1 (Tag = 1)
8
2908
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. The problem is that whenever I import the data to access my dates show up in 38478 instead of 05/06/05. How can I convert this number back to the date format in access?
1
288
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 minimum volume box which encompasses the entire object) at the root node (i.e. the one which contains the object itself) recursively into smaller boxes until some stopping condition is satisfied ( I'm using a combination of depth and maximum...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.