Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-06 Thread Christopher Barker
Robert Kern wrote:
 Chris Barker wrote:
 What if your objects were nested sequences, and you wanted to partly 
 flatten them -- which would you flatten?
 
 I'm pretty sure that that is exactly the ambiguity that the depth option
 resolves. Can you give me an example where it's still ambiguous with the depth
 information provided?

I was imagining:

Say you have a bunch of nested sequences that, fully expanded, would 
yield a (i,j,k) rank-3 array. If you wanted a rank-2 array, you could 
have either:
a (i,j) array with each element being a length-k sequence
or:
a (i,k) array, with each element being a length-j sequence.

This is quite trivial with numpy n-d arrays.

However, while you could build either of those from nested sequences, 
there isn't, in fact, and object in there that is that length-j sequence.

I guess this really points to the power of n-d arrays!

Here's a simple example:

t = (((1,2),(3,4)),((5,6),(7,8)))

depth 1 -- (4,):
array([(1, 2), (3, 4), (5, 6), (7, 8)], dtype=object)
  a[0]
(1, 2)

depth 1 -- (2,)
  a
array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype=object)
  a[0]
(1, 2, 3, 4)

But since there was no object in the original sequence that actually had 
a 4-tuple, maybe no one would ever be looking for that.

-Chris







-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

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


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-06 Thread Robert Kern
Christopher Barker wrote:
 Robert Kern wrote:
 Chris Barker wrote:
 What if your objects were nested sequences, and you wanted to partly 
 flatten them -- which would you flatten?
 I'm pretty sure that that is exactly the ambiguity that the depth option
 resolves. Can you give me an example where it's still ambiguous with the 
 depth
 information provided?
 
 I was imagining:
 
 Say you have a bunch of nested sequences that, fully expanded, would 
 yield a (i,j,k) rank-3 array. If you wanted a rank-2 array, you could 
 have either:
 a (i,j) array with each element being a length-k sequence
 or:
 a (i,k) array, with each element being a length-j sequence.
 
 This is quite trivial with numpy n-d arrays.
 
 However, while you could build either of those from nested sequences, 
 there isn't, in fact, and object in there that is that length-j sequence.

And I think that's the key difference. I don't think array() should be 
responsible for *arbitrarily* flattening nested sequences in order to 
reinterpret structure. Instead, I think we really only need array() to be able 
to answer the question, is this object an atom or a sequence I need to descend 
into? Essentially, for all valid index-tuples (say, [i,j,k]) in the array:

   arr[i,j,k] == original[i,j,k]

 I guess this really points to the power of n-d arrays!

Yes. Since one can get the (i,k) array after getting the (i,j,k) array and 
reshaping, I don't think we need to support it in array().

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth.
   -- Umberto Eco
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-04 Thread Pearu Peterson
On Fri, January 4, 2008 8:00 pm, Pearu Peterson wrote:
 On Fri, January 4, 2008 7:33 pm, Travis E. Oliphant wrote:
 Pearu Peterson wrote:
 Hi,

 Say, one defines

 class A(tuple):
   def __repr__(self):
 return 'A(%s)' % (tuple.__repr__(self))

 and I'd like to create an array of A instances.

 So, create an empty object array and insert the entries the way you want
 them:

 a = np.empty(1,dtype=object)
 a[0] = A((1,2))

 Meantime I was reading arrayobject.c and it seems that
 before objects are checked for being sequences, their
 __array_interface__ is accessed (eg in Array_FromSequence,
 discover_depth).

 Would this provide a solution if the class A would define
 a property __array_interface__? I just don't know what
 the data field should be for an object.

Ok, I found a partial solution:


class A(tuple):
   def __repr__(self):
 return 'A(%s)' % (tuple.__repr__(self))
@property
def __array_interface__(self):
import numpy
obj = numpy.empty(1,dtype=object)
obj[0] = self
return obj.__array_interface__.copy()

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


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-04 Thread Pearu Peterson
On Fri, January 4, 2008 8:00 pm, Pearu Peterson wrote:
 On Fri, January 4, 2008 7:33 pm, Travis E. Oliphant wrote:
 Pearu Peterson wrote:
 Hi,

 Say, one defines

 class A(tuple):
   def __repr__(self):
 return 'A(%s)' % (tuple.__repr__(self))

 and I'd like to create an array of A instances.

 The array function was designed a long time ago to inspect sequences and
 flatten them.

 Arguably, there should be more intelligence when an object array is
 requested, but there is ambiguity about what the right thing to do is.

 Thus, the current situation is that if you are creating object arrays,
 the advice is to populate it after the fact.

 So, create an empty object array and insert the entries the way you want
 them:

 a = np.empty(1,dtype=object)
 a[0] = A((1,2))

 Meantime I was reading arrayobject.c and it seems that
 before objects are checked for being sequences, their
 __array_interface__ is accessed (eg in Array_FromSequence,
 discover_depth).

 Would this provide a solution if the class A would define
 a property __array_interface__? I just don't know what
 the data field should be for an object.

Ok, I found a partial solution:

class A(tuple):
def __repr__(self):
return 'A('+tuple.__repr__(self)+')'
@property
def __array_interface__(self):
import numpy
obj = numpy.empty(1,dtype=object)
obj[0] = self
return obj.__array_interface__.copy()

 from numpy import *
 array([A((1,2))])
array([[1, 2]], dtype=object)

but

 array(A((1,2)))
array([None], dtype=object)

Pearu
PS: sorry about previous mail, Send was pressed accidentaly.


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


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-04 Thread Travis E. Oliphant
Charles R Harris wrote:


 On Jan 4, 2008 11:13 AM, Pearu Peterson [EMAIL PROTECTED] 
 mailto:[EMAIL PROTECTED] wrote:

 On Fri, January 4, 2008 7:33 pm, Travis E. Oliphant wrote:

  So, create an empty object array and insert the entries the way
 you want
  them:
 
  a = np.empty(1,dtype=object)
  a[0] = A((1,2))

 Actually this is still an option if to put the above
 to a A.as_array method something like

 class A(tuple):
def as_array(self):
import numpy
obj = numpy.empty(1,dtype=object)
obj[0] = self
return obj

 but it would be nice if the __array__ method also worked.
 Could this be something that should be put to issues or
 would it just get a WontFix flag? 

  
 IIRC the previous discussions, there simply isn't enough information 
 in the array interface to decide what needs to be done with regards to 
 object arrays. I think a general solution would require a specialized 
 creation function with more arguments, say, a depth argument which 
 would create objects at a certain depth of nesting. In your case that 
 would be at level 0(1?). I think that would solve most problems, and I 
 suppose it could be added to the current array interface with a 
 default value implying current behavior, i.e., as deep as possible.

I like the depth option, and that could be used with the current 
interface without too much difficulty, I think.

This question does come up quite often and should probably be addressed.

-Travis


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


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-04 Thread Pearu Peterson

Just ignore this solution. It was not quite working
and I was able to get a segfault from it.

Pearu

On Fri, January 4, 2008 8:58 pm, Pearu Peterson wrote:
 On Fri, January 4, 2008 8:00 pm, Pearu Peterson wrote:
 On Fri, January 4, 2008 7:33 pm, Travis E. Oliphant wrote:
 Pearu Peterson wrote:
 Hi,

 Say, one defines

 class A(tuple):
   def __repr__(self):
 return 'A(%s)' % (tuple.__repr__(self))

 and I'd like to create an array of A instances.

 So, create an empty object array and insert the entries the way you
 want
 them:

 a = np.empty(1,dtype=object)
 a[0] = A((1,2))

 Meantime I was reading arrayobject.c and it seems that
 before objects are checked for being sequences, their
 __array_interface__ is accessed (eg in Array_FromSequence,
 discover_depth).

 Would this provide a solution if the class A would define
 a property __array_interface__? I just don't know what
 the data field should be for an object.

 Ok, I found a partial solution:


 class A(tuple):
def __repr__(self):
  return 'A(%s)' % (tuple.__repr__(self))
 @property
 def __array_interface__(self):
 import numpy
 obj = numpy.empty(1,dtype=object)
 obj[0] = self
 return obj.__array_interface__.copy()

 ___
 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


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-04 Thread Chris Barker
Travis E. Oliphant wrote:

 I like the depth option, and that could be used with the current 
 interface without too much difficulty, I think.
 
 This question does come up quite often and should probably be addressed.

Yes, it has, but the depth option was discussed too, and while it might 
solve some problems, it would still be ambiguous:

What if your objects were nested sequences, and you wanted to partly 
flatten them -- which would you flatten? Maybe that's a weird enough 
case that the depth option would work most of the time, but I'm not 
sure. I think a shape option might cover this better.

It seems the case at hand is how to make a custom object that can tell 
numpy how it should be put into an object array -- that may be an even 
rarer use case, but it seems like a good idea to support it.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

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


Re: [Numpy-discussion] how to create an array of objects that are sequences?

2008-01-04 Thread Robert Kern
Chris Barker wrote:
 Travis E. Oliphant wrote:
 
 I like the depth option, and that could be used with the current 
 interface without too much difficulty, I think.

 This question does come up quite often and should probably be addressed.
 
 Yes, it has, but the depth option was discussed too, and while it might 
 solve some problems, it would still be ambiguous:
 
 What if your objects were nested sequences, and you wanted to partly 
 flatten them -- which would you flatten?

I'm pretty sure that that is exactly the ambiguity that the depth option
resolves. Can you give me an example where it's still ambiguous with the depth
information provided?

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion