Consider conversion of a slice using the astype() method:
a = array([[1,2],[3,4]]) b = a[1, ...] print b [3 4]
Conversion using the astype() method goes wrong:
print b.astype(Float32) [ 1. 2.]
This fixes it:
print b.copy().astype(Float32) [ 3. 4.]
A bug? Cheers, Peter -- Dr. Peter J. Verveer Cell Biology and Cell Biophysics Programme EMBL Meyerhofstrasse 1 D-69117 Heidelberg Germany Tel. : +49 6221 387245 Fax : +49 6221 387242 Email: Peter.Verveer@embl-heidelberg.de
Hi Peter, What you're demonstrating below looks like a bug in numarray which was just recently solved: https://sourceforge.net/tracker/index.php?func=detail&aid=784866&group_id=1369&atid=450446 As you've shown, numarray's astype() fails for some slices, notably those which are offset from the base of the array and therefore have a non-zero _byteoffset. This is fixed in CVS now and will be "officially released" soon. On Mon, 2003-08-18 at 08:01, Peter Verveer wrote:
Consider conversion of a slice using the astype() method:
a = array([[1,2],[3,4]]) b = a[1, ...] print b [3 4]
Conversion using the astype() method goes wrong:
print b.astype(Float32) [ 1. 2.]
This fixes it:
print b.copy().astype(Float32) [ 3. 4.]
A bug?
Cheers, Peter -- Todd Miller <jmiller@stsci.edu>
# ta.py to check Int8, Int16 import numarray as _n import numarray.numerictypes as _nt b= _n.arange(4, type= _nt.Int8) print 'b, b._type:', b, b._type c= b*1001 # Grief here - type not updated print 'c, c._type:', c, c._type e= _n.array([1, -2, 3000, 4.6], type= _nt.Int8) # undocumented feature fraction discarded # and lowest eight bits retained as a signed # integer. print 'e, e._type:', e, e._type f= _n.array([1, 2, 3, 4.6], type= _nt.Int8) * 9.6 print 'f, f._type:', f, f._type g= (f.copy()*2000).astype(_nt.Int16) # undocumented - see e above print 'g, g._type:', g, g._type -------------------------------------------------------------------- PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32. Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
b, b._type: [0 1 2 3] Int8 c, c._type: [ 0 -23 -46 -69] Int8 e, e._type: [ 1 -2 -72 4] Int8 f, f._type: [ 9.6 19.2 28.8 38.4] Float64 g, g._type: [ 19200 -27136 -7937 11264] Int16
Colin W.
These problems are (partially) addressed in numarray-0.7 with the addition of a check_overflow parameter to numarray.fromlist(). The checked fromlist is now used in the implementation of ufuncs (e.g. multiply), preventing the silent truncation of scalar values to the type of the array. Problem c: solved -- 1001 scalar was silently truncated to Int8 before multiplication. Problem e: solved -- 3000 was silently truncated to Int8 during fromlist. Use fromlist() rather than array() and add check_overflow=1 parameter. Problem f: rejected -- working as designed, won't change. Floating point numbers passed into array() are silently truncated if the type is not a floating point type. Operating on an integer array and a floating point scalar returns a floating point array by design; likewise, operating on an integer array with an integer scalar returns an integer array. This is all expected behavior. Problem g: unaddressed -- working as designed, open to discussion. The silent truncation of g to Int16 is documented in the description of astype(). It's possible to add a check_overflow flag to astype() Thanks for the feedback, Todd On Mon, 2003-08-18 at 09:20, Colin J. Williams wrote:
# ta.py to check Int8, Int16 import numarray as _n import numarray.numerictypes as _nt b= _n.arange(4, type= _nt.Int8) print 'b, b._type:', b, b._type c= b*1001 # Grief here - type not updated print 'c, c._type:', c, c._type e= _n.array([1, -2, 3000, 4.6], type= _nt.Int8) # undocumented feature fraction discarded # and lowest eight bits retained as a signed # integer. print 'e, e._type:', e, e._type f= _n.array([1, 2, 3, 4.6], type= _nt.Int8) * 9.6 print 'f, f._type:', f, f._type g= (f.copy()*2000).astype(_nt.Int16) # undocumented - see e above print 'g, g._type:', g, g._type
-------------------------------------------------------------------- PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32. Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
b, b._type: [0 1 2 3] Int8 c, c._type: [ 0 -23 -46 -69] Int8 e, e._type: [ 1 -2 -72 4] Int8 f, f._type: [ 9.6 19.2 28.8 38.4] Float64 g, g._type: [ 19200 -27136 -7937 11264] Int16
Colin W.
------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion -- Todd Miller jmiller@stsci.edu STSCI / ESS / SSB
participants (3)
-
Colin J. Williams
-
Peter Verveer
-
Todd Miller