[Numpy-discussion] Bug in numarray<->numpy interaction

Francesc Altet faltet at carabos.com
Mon Dec 25 06:15:57 EST 2006


A Diumenge 24 Desembre 2006 13:57, Norbert Nemec escrigué:
> The following snippet demonstrates a problem in the interaction of
> numarray 1.5.2 with numpy 1.0.1 (and older versions):
>
> -------------------
> #!/usr/bin/env python
>
> import numarray, numpy
>
> na = numarray.array(0.)
> np = numpy.array(0.)
>
> na[...] = np
> -------------------
>
> the last linec causes the error
>    "TypeError: NA_setFromPythonScalar: bad value type."
>
> The problem occured in a perfectly normal piece of code combining
> PyTables (internally based on numarray) with calculations done in NumPy.
> AFAICS, it should work but simply is a bug somewhere in numarray or numpy.

Well, I'm not sure whether we should take this as a bug of numarray
(although numpy is happily accepting rank-0 arrays from numarray or
Numeric, so we won't have this problem in forthcoming PyTables 2.0).
In any case, here you have a quick workaround for PyTables:

Index: tables/Array.py
===================================================================
--- tables/Array.py     (revision 2166)
+++ tables/Array.py     (revision 2167)
@@ -656,7 +656,10 @@

         # Assign the value to it
         try:
-            narr[...] = value
+            if hasattr(value, 'shape') and value.shape == ():  # Rank-0 case
+                narr[...] = value[()]
+            else:
+                narr[...] = value
         except Exception, exc:  #XXX
             raise ValueError, \
 """value parameter '%s' cannot be converted into an array object compliant 
with %s:

The above patch will accept rank-0 arrays from both NumPy or Numeric.
It has also been applied into std-1.4 branch:

http://www.pytables.org/trac/changeset/2168/branches

Cheers!

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"




More information about the NumPy-Discussion mailing list