Re: [Numpy-discussion] Getting an item in an array with its coordinates given by another array

2007-10-29 Thread [EMAIL PROTECTED]
Take a look at numpy.ix_
http://www.scipy.org/Numpy_Example_List_With_Doc#head-603de8bdb62d0412798c45fe1db0648d913c8a9c

This method creates the index array for you. You only have to specify
the coordinates in each dimesion.

Bernhard

On Oct 29, 8:46 am, Matthieu Brucher [EMAIL PROTECTED]
wrote:
   Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]],
  which
   is even stranger.

  c[(2,3)] is the same as c[2,3] and obviously works as you expected.

 Well, this is not indicated in the documentation.

 c[[2,3]] is refered to as 'advanced indexing' in the numpy book.

  It will return elements 2 and 3 along the first dimension. To get what you
  want, you need to put it like this:

  c[[2],[3]]

  In [118]: c = N.arange(0.,3*4*5).reshape((3,4,5))

  In [119]: c[[2],[3]]

  Out[119]: array([[ 55.,  56.,  57.,  58.,  59.]])

 This is very strange to say the least, as tuple do not work in the same way.

 This does not work however using a ndarray holding the indices.





  In [120]: ind = N.array([[2],[3]])

  In [121]: c[ind]

  ---
  type 'exceptions.IndexError'Traceback (most recent call
  last)

  /media/hda6/home/ck/ipython console in module()

  type 'exceptions.IndexError': index (3) out of range (0=index=2) in
  dimension 0

  so you have to convert it to a list before:

  In [122]: c[ind.tolist()]
  Out[122]: array([[ 55.,  56.,  57.,  58.,  59.]])

 But if I have the coordinates of the points in an array, I have to reshape
 it and then convert it into a list. Or convert it into a list and then
 convert it to a tuple. I know that advanced indexing is useful, but here it
 is not coherent. tuples and lists should have the same result on the array,
 or at least it should be documented.
 So there is not way to get a sub-array based on coordinates in an array ?

 Matthieu

 --
 French PhD student
 Website :http://miles.developpez.com/
 Blogs :http://matt.eifelle.comandhttp://blog.developpez.com/?blog=92

 ___
 Numpy-discussion mailing list
 [EMAIL PROTECTED]://projects.scipy.org/mailman/listinfo/numpy-discussion

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Getting an item in an array with its coordinates given by another array

2007-10-29 Thread Christian K .
Matthieu Brucher matthieu.brucher at gmail.com writes:

 But if I have the coordinates of the points in an array, I have to reshape it
 and then convert it into a list. Or convert it into a list and then convert it
 to a tuple. I know that advanced indexing is useful, but here it is not
 coherent. tuples and lists should have the same result on the array, or at
 least it should be documented.

I can't give you the reasons why that behaviour was chosen. But it's simply
wrong that it's not documented. Before answering you I read the corresponding
chapter in the numpy book to be sure not to tell you nonsense. So go ahead and
do so, too.

 So there is not way to get a sub-array based on coordinates in an array?

It's just a guess, but probably you can't use an ndarray as index because
indices like this

[[2],[3,4]] 

aren't valid input for a ndarray.

Christian


___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Getting an item in an array with its coordinates given by another array

2007-10-29 Thread Matthieu Brucher

  But if I have the coordinates of the points in an array, I have to
 reshape it
  and then convert it into a list. Or convert it into a list and then
 convert it
  to a tuple. I know that advanced indexing is useful, but here it is not
  coherent. tuples and lists should have the same result on the array, or
 at
  least it should be documented.

 I can't give you the reasons why that behaviour was chosen. But it's
 simply
 wrong that it's not documented. Before answering you I read the
 corresponding
 chapter in the numpy book to be sure not to tell you nonsense. So go ahead
 and
 do so, too.



Thank you for this answer, I suppose we'll have to wait the answer of a
numpy guru ;)

Matthieu
-- 
French PhD student
Website : http://miles.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy distutils patch

2007-10-29 Thread Jarrod Millman
Hey,

I was looking at numpy/distutils/ccompiler.py and noticed that it has
a fix for distutils.util.split_quoted.

Here is the relevant code from split_quoted in numpy.distutils.ccompiler:
---
def split_quoted(s):

snip

   if _has_white_re.search(s[beg+1:end-1]):
s = s[:beg] + s[beg+1:end-1] + s[end:]
pos = m.end() - 2
else:
# Keeping quotes when a quoted word does not contain
# white-space. XXX: send a patch to distutils
pos = m.end()

snip
---

Here is the relevant code from split_quoted in distutils.util:
---
def split_quoted(s):

snip

s = s[:beg] + s[beg+1:end-1] + s[end:]
pos = m.end() - 2

snip
---

Does anyone know if a patch was ever submitted upstream?  If not, is
there any reason that a patch shouldn't be submitted?

Thanks,

-- 
Jarrod Millman
Computational Infrastructure for Research Labs
10 Giannini Hall, UC Berkeley
phone: 510.643.4014
http://cirl.berkeley.edu/
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Getting an item in an array with its coordinates given by another array

2007-10-29 Thread Timothy Hochberg
On 10/28/07, Matthieu Brucher [EMAIL PROTECTED] wrote:

  Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]],
  which
   is even stranger.
 
  c[(2,3)] is the same as c[2,3] and obviously works as you expected.



 Well, this is not indicated in the documentation.


This is true at the Python level and is not related to numpy. x=c[2,3] is
equivalent to x=c.__getitem__((2,3)).  Note that the index pair is passed
as a tuple. On the other hand, a single index is not passed as a tuple, but
is instead passed as is. For example: x = c[a] gets passed as
x=c.__getitem__(a). If 'a' happens to be '(2,3)' you get the behavior
above.

So, although lists and arrays can be used for fancy-indexing, tuples
cannot be since you can't tell the difference between a tuple of indices and
multiple indices inside square brackets.


[SNIP]





-- 
.  __
.   |-\
.
.  [EMAIL PROTECTED]
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Getting an item in an array with its coordinates given by another array

2007-10-29 Thread Matthieu Brucher

 In this case the constructor

 tuple(arr)

 should work just fine.


Sorry, I didn't know it could work (shame on me I should have tested).

-- 
French PhD student
Website : http://miles.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy version and numpy.asarray behavior issue

2007-10-29 Thread Ray S

I have 2 PCs with 2 different installs:
ActivePython 2.4.3 Build 12 with numpy version '1.0b1'
and
Enthought 2.4.3 (1.0.0 #69)  with numpy version '0.9.7.2476'

The attached runs Ok on numpy v1.0, but on Enthought's,  print a1[0] 
gives:

IndexError: 0-d arrays can't be indexed.

It seems that the 0.9.7 numpy.asarray is not creating a true array 
from Dummy class in the code below. Enthought only comes with 
0.9.9.2706 (now).
When was the asarray behavior supported, or, is there some other 
issue I'm missing?
I'll use Activestate's distro if needed, but I'd like to keep 
Enthought for that one...




def fromaddress(address, dtype, shape, strides=None):
 Create a numpy array from an integer address, a dtype
or dtype string, a shape tuple, and possibly strides.

import numpy
# Make sure our dtype is a dtype, not just f or whatever.
dtype = numpy.dtype(dtype)

class Dummy(object):
pass
d = Dummy()
d.__array_interface__ = dict(
data = (address, False),
typestr = dtype.str,
descr = dtype.descr,
shape = shape,
strides = strides,
version = 3,
)
return numpy.asarray(d)

Thanks,
Ray nFromAddress.py


def fromaddress(address, dtype, shape, strides=None):
 Create a numpy array from an integer address, a dtype 
or dtype string, a shape tuple, and possibly strides.

import numpy
# Make sure our dtype is a dtype, not just f or whatever.
dtype = numpy.dtype(dtype)

class Dummy(object):
pass
d = Dummy()
d.__array_interface__ = dict(
data = (address, False),
typestr = dtype.str,
descr = dtype.descr,
shape = shape,
strides = strides,
version = 3,
)
return numpy.asarray(d)


##Numeric example, with address kludge
import Numeric, numpy, ctypes, string
a0 = Numeric.zeros((1), Numeric.Int16)
nAddress = int(string.split(repr(a0.__copy__))[-1][:-1], 16)
tmp=(ctypes.c_long*1)(0)
ctypes.memmove(tmp, nAddress+8, 4)
nAddress = tmp[0]
a1 = fromaddress(nAddress, numpy.int16, (1,))
a0[0] = 5
print a1[0]

## numpy example
a2 = numpy.zeros(1, numpy.int16)
nAddress = a2.__array_interface__['data'][0]
nType = a2.__array_interface__['typestr']
nShape = a2.__array_interface__['shape']
a3 = fromaddress(nAddress, nType, nShape)
a2[0] = 5
print a3[0]___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy version and numpy.asarray behavior issue

2007-10-29 Thread Ray S

I just installed 1.0.3.1 on top of Enthought's and asarray() works.

But...
Although the creation of an array from an address via a Dummy class 
is kosher in one process (as in the previous attachment), it fails 
across processes - the array is created, but gives a Python has 
generated errors window if the second process even attempts to read.


It can seem to work across processes with mmap.mmap() and tags (used 
in Windows)


def arrSharedMemory(shape, dtype, tag=PySharedMemory, access=None):
## Windows only !  share memory between different
## processes if same `tag` is used.
itemsize = N.dtype(dtype).itemsize
count = N.product(shape)
size =  count * itemsize
import mmap
sharedmem = mmap.mmap(0, size, tag, access)
a=N.frombuffer(sharedmem, dtype, count)
a.shape = shape
return a

I guess I'll use mmap unless someone can point out otherwise

Thanks,
Ray nFromAddress.py


##Numeric example, with address kludge
import Numeric, numpy, ctypes, subprocess
from time import clock, sleep

a = Numeric.zeros((4), Numeric.Int16)
nAddress = int(repr(.__copy__).split()[-1][:-1], 16)
tmp=(ctypes.c_long*1)(0)
ctypes.memmove(tmp, nAddress+8, 4)
nAddress = tmp[0]


a = numpy.zeros(4, numpy.int16)
nAddress = a.__array_interface__['data'][0]

print nAddress
pid = subprocess.Popen(
[r'C:\python24\python.exe', 
 ['nFromAddress2.py '+str(nAddress)]
]).pid

while clock()5:
sleep(.1)
if a[0]!=0: ## wait for a change...
print a0[0]

 nFromAddress.py


import numpy
import time, sys
def fromaddress(address, dtype, shape, strides=None):
 Create a numpy array from an integer address, a dtype 
or dtype string, a shape tuple, and possibly strides.

# Make sure our dtype is a dtype, not just f or whatever.
dtype = numpy.dtype(dtype)

class Dummy(object):
pass
d = Dummy()
d.__array_interface__ = dict(
data = (address, False),
typestr = dtype.str,
descr = dtype.descr,
shape = shape,
strides = strides,
version = 3,
)
return numpy.asarray(d)


nAddress = sys.argv[1]
print 'recvd addr', nAddress
a3 = fromaddress(nAddress, numpy.int16, (4,))

## any of the following cause a Python/Windows error on access
print a3
#a3[0] = 5___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] vectorizing loops

2007-10-29 Thread David Cournapeau
Timothy Hochberg wrote:


 On 10/29/07, *Christopher Barker* [EMAIL PROTECTED] 
 mailto:[EMAIL PROTECTED] wrote:

  (incidently, the kind of things 'we' are doing seem like the most
  simple things to JIT).

 Wouldn't a numpy-aware psyco be cool then?

 Oh well, I'm not going to write it!

 (though as I think about it, for the special case of a contiguous
 array,
 it would be awfully similar to an array.array --- hmmm.)


 Psyco is aware of array.array and can operate on array.array's quite 
 fast. [In fact, somewhere I have a ndarray-like class based on Psyco 
 that runs pretty fast for integers, but not so fast for floats]. The 
 problem that Psyco has for numeric purposes is that it has no concept 
 of floating point numbers. It can natively store only a few 
 different things: ints, pointers, and arrays of ints or pointers. To 
 put, for example, doubles, onto the Psyco stack or into one of it's 
 registers, you need to break the bits of the double up, and stuff them 
 into a couple of different int registers. Then to operate on them you 
 need to put them back together, since they may get separated. All of 
 this adds a fair amount of overhead.

 I've been hoping that the PyPy jit will address this, but I haven't 
 had time to follow that project closely enough to see if that's on the 
 agenda.
My impression is that it is hard to follow pypy when you are not 'part 
of it', but just from the ML, and a couple of questions of mine, my 
understanding is that it is on the agenda. They have a JIT (maybe does 
not work on float, though), there is work on numpy arrays at the rpython 
level (rpython being the typed subset of python pypy is using at their 
core).

cheers,

David



 -- 
 .  __
 .   |-\
 .
 .  [EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
 

 ___
 Numpy-discussion mailing list
 Numpy-discussion@scipy.org
 http://projects.scipy.org/mailman/listinfo/numpy-discussion
   

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion