Re: [Numpy-discussion] sort method raises unexpected error with axis=None

2008-02-13 Thread Matthew Brett
Ah,

To answer my own question:

 Suggestion 1:
 Wrap the .sort method call in a tiny python wrapper of the form:

 def sort(self, axis=-1, kind='quicksort', order=None):
 if axis=None:
_c_sort(self.ravel(), axis, kind, order)
else:
   _c_sort(self, axis, kind, order)

I guess this is not good because self.ravel might return a copy, in
situations I don't think I fully grasp?  Guessing that there is no
other way to do a guaranteed inplace sort for axis=None, I guess that
making that clear in the method docstring is the best way to go?

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


Re: [Numpy-discussion] sort method raises unexpected error with axis=None

2008-02-13 Thread Matthew Brett
Hi,

 Is it possible, in fact, to do an inplace sort on an array with
 axis=None (ie flat sort)?

 Should the sort method have its docstring changed to reflect the fact
 that axis=None is not valid?

Sorry to press on, but it would be good to resolve this somehow.

Is there some reason not to:

Suggestion 1:
Wrap the .sort method call in a tiny python wrapper of the form:

def sort(self, axis=-1, kind='quicksort', order=None):
if axis=None:
   _c_sort(self.ravel(), axis, kind, order)
   else:
  _c_sort(self, axis, kind, order)

or 2:
Modify the method docstring to remove axis=None as valid option.

I'm happy to do either.

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


Re: [Numpy-discussion] sort method raises unexpected error with axis=None

2008-02-13 Thread Charles R Harris
On Feb 13, 2008 1:52 PM, Matthew Brett [EMAIL PROTECTED] wrote:

 Ah,

 To answer my own question:

  Suggestion 1:
  Wrap the .sort method call in a tiny python wrapper of the form:
 
  def sort(self, axis=-1, kind='quicksort', order=None):
  if axis=None:
 _c_sort(self.ravel(), axis, kind, order)
 else:
_c_sort(self, axis, kind, order)

 I guess this is not good because self.ravel might return a copy, in
 situations I don't think I fully grasp?  Guessing that there is no
 other way to do a guaranteed inplace sort for axis=None, I guess that
 making that clear in the method docstring is the best way to go?


I think it is possible to make sort work with the None keyword, So I think
the question is whether or not we want it to. If we do, then the current
lack is a bug, if we don't, then the documentation needs to be fixed.

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


Re: [Numpy-discussion] sort method raises unexpected error with axis=None

2008-02-12 Thread Anne Archibald
On 12/02/2008, Matthew Brett [EMAIL PROTECTED] wrote:

 Is it possible, in fact, to do an inplace sort on an array with
 axis=None (ie flat sort)?

It is, sometimes; just make an array object to point to the flattened
version and sort that:

In [16]: b = a[:]

In [17]: b.shape = (16,)

In [18]: b.sort()

This is not always possible, depending on the arrangement of a in memory.

An efficient way to handle in-place (or out-of-place, come to think of
it) median along multiple axes is actually to take medians along all
axes in succession. That saves you some sorting effort, and some
programming effort, and doesn't require in-place multidimensional
sorting:

In [24]: def all_axes_median(a):
   : if len(a.shape)1:
   : return all_axes_median(N.median(a))
   : else:
   : return N.median(a)
   :
   :

In [26]: all_axes_median(N.reshape(N.arange(32),(2,4,2,-1)))
Out[26]: 15.5

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


Re: [Numpy-discussion] sort method raises unexpected error with axis=None

2008-02-12 Thread Anne Archibald
On 12/02/2008, Anne Archibald [EMAIL PROTECTED] wrote:
 An efficient way to handle in-place (or out-of-place, come to think of
 it) median along multiple axes is actually to take medians along all
 axes in succession. That saves you some sorting effort, and some
 programming effort, and doesn't require in-place multidimensional
 sorting:

Aargh. Sorry. No, that doesn't work:

In [28]: all_axes_median(N.reshape([1,5,6,7],(2,2)))
Out[28]: 4.75

Oops.

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


Re: [Numpy-discussion] sort method raises unexpected error with axis=None

2008-02-12 Thread Matthew Brett
Hi,

To rephrase:

Is it possible, in fact, to do an inplace sort on an array with
axis=None (ie flat sort)?

Should the sort method have its docstring changed to reflect the fact
that axis=None is not valid?

Matthew

On Feb 10, 2008 7:50 PM, Matthew Brett [EMAIL PROTECTED] wrote:
 Hi,

 I just noticed this:

 From the sort method docstring:

 axis : integer
 Axis to be sorted along. None indicates that the flattened array
 should be used. Default is -1.

 In [40]: import numpy as N

 In [41]: a = N.arange(10)

 In [42]: N.sort(a, None)
 Out[42]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

 In [43]: a.sort(None)
 ---
 TypeError Traceback (most recent call last)

 /home/mb312/ipython console in module()

 TypeError: an integer is required


 Perhaps the sort method is calling the c code directly, and this is
 not checking for axis=None?

 Matthew

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