Date: 2010-12-12 07:40:33 +0000 (Sun, 12 Dec 2010)
Fix handling of float64 data.
The old code attempted to handle Python data as float32 (to save
memory). But contrary to the comment, it was coercing float64 numpy
arrays to float32. (Perhaps this is a difference between numpy and
Numeric?) That was never the intention.
The new version avoids this coersion, with the side-effect that Python
lists are converted to float64 numpy arrays.
Patch by: xaverxn (see tracker patch with ID 2671685)
> Gnuplot.py float64 handling is broken (gets downcast to float32, see
> gnuplot-py-users mailing list in the beginning of march 09). It is
> essentially my last proposal on the list, which wasn't discussed or
> commented on any further.
> This patch changes the file utils.py so it coverts python native types
> to numpy arrays and (up)casts everything but float64 to float32.
--- trunk/utils.py 2010-12-12 07:01:00 UTC (rev 315)
+++ trunk/utils.py 2010-12-12 07:40:33 UTC (rev 316)
@@ -18,29 +18,18 @@
"""Return the argument as a numpy array of type at least 'Float32'.
- Leave 'Float64' unchanged, but upcast all other types to
- 'Float32'. Allow also for the possibility that the argument is a
- python native type that can be converted to a numpy array using
- 'numpy.asarray()', but in that case don't worry about
- downcasting to single-precision float.
+ Convert input data to numpy array first (in case it is a python
+ native type), then return it if it is of dtype 'float64' or
+ 'float32'. Try to upcast everything else to float32.
- # Try Float32 (this will refuse to downcast)
- return numpy.asarray(m, numpy.float32)
- except TypeError:
- # That failure might have been because the input array was
- # of a wider data type than float32; try to convert to the
- # largest floating-point type available:
- # NOTE TBD: I'm not sure float_ is the best data-type for this...
- return numpy.asarray(m, numpy.float_)
- except TypeError:
- # TBD: Need better handling of this error!
- print "Fatal: array dimensions not equal!"
- return None
+ m = numpy.asarray(m)
+ if m.dtype in ('float64','float32'):
+ return m
+ return numpy.array(m,dtype=numpy.float32)
def write_array(f, set,
nest_prefix='', nest_suffix='\n', nest_sep=''):
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.