[Numpy-discussion] Changes in PyArray_FromAny between 1.5.x and 1.6.x

2012-05-28 Thread Mike Hansen
Hello,

In trying to upgrade NumPy within Sage, we notices some differences in
behavior between 1.5 and 1.6.  In particular, in 1.5, we have

sage: f = 0.5
sage: f.__array_interface__
{'typestr': '=f8'}
sage: numpy.array(f)
array(0.5)
sage: numpy.array(float(f))
array(0.5)

In 1.6, we get the following,

sage: f = 0.5
sage: f.__array_interface__
{'typestr': '=f8'}
sage: numpy.array(f)
array(0.500, dtype=object)

This seems to be do to the changes in PyArray_FromAny introduced in
https://github.com/mwhansen/numpy/commit/2635398db3f26529ce2aaea4028a8118844f3c48
.  In particular, _array_find_type used to be used to query our
__array_interface__ attribute, and it no longer seems to work.  Is
there a way to get the old behavior with the current code?

--Mike
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] [enhancement] sum_angle() and sum_polar()

2012-05-28 Thread Ralf Gommers
On Mon, May 28, 2012 at 7:58 PM, Travis Oliphant tra...@continuum.iowrote:

 I didn't see anyone respond to this, but looking over his simple and
 elegant solution it seems like a useful addition to the 2-d functions
 available in NumPy as it works with any 2-d array (image or matrix) and
 does a transformation on the indices in order to organize the sum.

 It is not a general-purpose interpolating approach where the 2-d array is
 viewed as samples of an underlying continuous function.

 Are their other thoughts?

 This was discussed (not finished yet) on scipy-dev:
http://thread.gmane.org/gmane.comp.python.scientific.devel/16538/focus=16541.


Ralf



 -Travis



 On Mar 7, 2012, at 12:39 PM, Robert Jördens wrote:

  Hi everyone,
  I am proposing to add the the two following functions to
  numpy/lib/twodim_base.py:
 
  sum_angle() computes the sum of a 2-d array along an angled axis
  sum_polar() computes the sum of a 2-d array along radial lines or
  along azimuthal circles
 
  https://github.com/numpy/numpy/pull/230
 
  Comments?
 
  When I was looking for a solution to these problems of calculating
  special sums of 2-d arrays I could not find anything and it took me a
  while to figure out a (hopefully) useful and consistent algorithm.
  I can see how one would extend these to higher dimensions but that
  would preclude using bincount() to do the heavy lifting.
  Looking at some other functions, the doctests might need to be split
  into real examples and unittests.
 
  Best,
 
  --
  Robert Jordens.
  ___
  NumPy-Discussion mailing list
  NumPy-Discussion@scipy.org
  http://mail.scipy.org/mailman/listinfo/numpy-discussion

 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] [enhancement] sum_angle() and sum_polar()

2012-05-28 Thread Travis Oliphant

On May 28, 2012, at 1:02 PM, Ralf Gommers wrote:

 
 
 On Mon, May 28, 2012 at 7:58 PM, Travis Oliphant tra...@continuum.io wrote:
 I didn't see anyone respond to this, but looking over his simple and elegant 
 solution it seems like a useful addition to the 2-d functions available in 
 NumPy as it works with any 2-d array (image or matrix) and does a 
 transformation on the indices in order to organize the sum.
 
 It is not a general-purpose interpolating approach where the 2-d array is 
 viewed as samples of an underlying continuous function.
 
 Are their other thoughts?
 
 This was discussed (not finished yet) on scipy-dev: 
 http://thread.gmane.org/gmane.comp.python.scientific.devel/16538/focus=16541. 
 

That is a useful discussion, but the question about whether this function 
should just go into NumPy is also of interest.There are arguments that it 
could go into NumPy, SciPy, or sckitis-image.   I think going into 
scikits-image does not make sense because of their general applicability  for 
more than just images and the fact that in the context of image-processing 
these functions *just* do nearest neighbor interpolation. 

I could see these functions going into scipy.ndimage but again because they are 
not necessarily just image processing functions, and the fact that they are so 
simple, perhaps they are best put into NumPy itself. 

-Travis


 Ralf
 
  
 -Travis
 
 
 
 On Mar 7, 2012, at 12:39 PM, Robert Jördens wrote:
 
  Hi everyone,
  I am proposing to add the the two following functions to
  numpy/lib/twodim_base.py:
 
  sum_angle() computes the sum of a 2-d array along an angled axis
  sum_polar() computes the sum of a 2-d array along radial lines or
  along azimuthal circles
 
  https://github.com/numpy/numpy/pull/230
 
  Comments?
 
  When I was looking for a solution to these problems of calculating
  special sums of 2-d arrays I could not find anything and it took me a
  while to figure out a (hopefully) useful and consistent algorithm.
  I can see how one would extend these to higher dimensions but that
  would preclude using bincount() to do the heavy lifting.
  Looking at some other functions, the doctests might need to be split
  into real examples and unittests.
 
  Best,
 
  --
  Robert Jordens.
  ___
  NumPy-Discussion mailing list
  NumPy-Discussion@scipy.org
  http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] 1.6.2 no more unique for rows

2012-05-28 Thread josef . pktd
https://github.com/numpy/numpy/commit/74b9f5eef8fac643bf9012dbb2ac6b4b19f46892
broke return_inverse for structured arrays, because of the use of mergesort

I'm using structured dtypes to get uniques and return_inverse by rows

 groups = np.random.randint(0,4,size=(10,2))
 groups_ = groups.view([('',groups.dtype)]*groups.shape[1]).flatten()
 groups
array([[0, 2],
   [1, 2],
   [1, 1],
   [3, 1],
   [3, 1],
   [2, 1],
   [1, 0],
   [3, 3],
   [3, 2],
   [0, 0]])
 groups_
array([(0, 2), (1, 2), (1, 1), (3, 1), (3, 1), (2, 1), (1, 0), (3, 3),
   (3, 2), (0, 0)],
  dtype=[('f0', 'i4'), ('f1', 'i4')])

 np.argsort(groups_)
array([9, 0, 6, 2, 1, 5, 4, 3, 8, 7])

 np.argsort(groups_, kind='mergesort')
Traceback (most recent call last):
  File stdin, line 1, in module
  File C:\Python26\lib\site-packages\numpy\core\fromnumeric.py, line
679, in argsort
return argsort(axis, kind, order)
TypeError: requested sort not available for type

 uni, uni_idx, uni_inv = np.unique(groups_, return_index=True, 
 return_inverse=True)
 uni_inv
array([1, 4, 3, 6, 6, 5, 2, 8, 7, 0])

exception in numpy 1.6.2rc2 (as reported by Debian for statsmodels)

Josef
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters

2012-05-28 Thread Uri Laserson
I am trying to sample from a Dirichlet distribution, where some of the
shape parameters are very small.  To do so, the algorithm samples each
component individually from a Gamma(k,1) distribution where k is the shape
parameter for that component of the Dirichlet.  In principle, this should
always return a positive number (as the Dirichlet is defined).  However, if
k is very small, it will return zero:

In [157]: np.random.gamma(1e-1)
Out[157]: 4.863866491339177e-06

In [158]: np.random.gamma(1e-2)
Out[158]: 2.424451829710714e-57

In [159]: np.random.gamma(1e-3)
Out[159]: 5.1909861689757784e-197

In [160]: np.random.gamma(1e-4)
Out[160]: 0.0

In [161]: np.random.gamma(1e-5)
Out[161]: 0.0

What is the best way to deal with this?

Thanks!
Uri



...
Uri Laserson
Graduate Student, Biomedical Engineering
Harvard-MIT Division of Health Sciences and Technology
M +1 917 742 8019
laser...@mit.edu
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters

2012-05-28 Thread Val Kalatsky
You'll need some patience to get non-zeros, especially for k=1e-5

In [84]: np.sum(np.random.gamma(1e-5,size=100)!=0.0)
Out[84]: 7259
that's less than 1%. For k=1e-4 it's ~7%

Val


On Mon, May 28, 2012 at 10:33 PM, Uri Laserson uri.laser...@gmail.comwrote:

 I am trying to sample from a Dirichlet distribution, where some of the
 shape parameters are very small.  To do so, the algorithm samples each
 component individually from a Gamma(k,1) distribution where k is the shape
 parameter for that component of the Dirichlet.  In principle, this should
 always return a positive number (as the Dirichlet is defined).  However, if
 k is very small, it will return zero:

 In [157]: np.random.gamma(1e-1)
 Out[157]: 4.863866491339177e-06

 In [158]: np.random.gamma(1e-2)
 Out[158]: 2.424451829710714e-57

 In [159]: np.random.gamma(1e-3)
 Out[159]: 5.1909861689757784e-197

 In [160]: np.random.gamma(1e-4)
 Out[160]: 0.0

 In [161]: np.random.gamma(1e-5)
 Out[161]: 0.0

 What is the best way to deal with this?

 Thanks!
 Uri




 ...
 Uri Laserson
 Graduate Student, Biomedical Engineering
 Harvard-MIT Division of Health Sciences and Technology
 M +1 917 742 8019
 laser...@mit.edu

 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion