In some earlier code (at least one of) the following worked fine. I just want to get a new type that is a byteswap of, say, float64 because I want to memmap an array with a non-native byte order. Any suggestions? Thanks, Jonathan ------------------------------------------ Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import numpy numpy.__version__ '0.9.9.2716' d=numpy.float64 swapped=d.newbyteorder('big') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: descriptor 'newbyteorder' requires a 'genericscalar' object but received a 'str' swapped=d.newbyteorder('>') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: descriptor 'newbyteorder' requires a 'genericscalar' object but received a 'str'
-- ------------------------------------------------------------------------ Jonathan Taylor Tel: 650.723.9230 Dept. of Statistics Fax: 650.725.8977 Sequoia Hall, 137 www-stat.stanford.edu/~jtaylo 390 Serra Mall Stanford, CA 94305
Jonathan Taylor wrote:
In some earlier code (at least one of) the following worked fine. I just want to get a new type that is a byteswap of, say, float64 because I want to memmap an array with a non-native byte order.
Any suggestions?
Last year the array scalars (like float64) were confused with the
data-type objects dtype('=i4'). This was fortunately changed many
months ago so the two are now separate concepts. This may be why your
old code worked.
You want to get a data-type object itself:
d = numpy.dtype(numpy.float64)
d = numpy.float64(1).dtype # you have to instantiate a float64 object
to access it's data-type.
Then
d.newbyteorder('>') or d.newbyteorder('big') will work.
But, probably easier and clearer is just to use:
dlittle = numpy.dtype('
import numpy as N d = N.dtype(N.float64) d.newbyteorder('big')
D = d.newbyteorder('big')
x = N.zeros((10,10), D) N.nan_to_num(x) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/home/jtaylo/python/lib/python2.4/site-packages/numpy/lib/type_check.py",
Thanks. The byteswap works for me now, but I have another problem... -------------------------------------------------------------------- dtype('>f8') line 127, in nan_to_num y[are_inf] = maxf SystemError: error return without exception set
Here is what maxf, minf are: ---------------------------------------------------------------- SystemError: error return without exception set /home/jtaylo/python/lib/python2.4/site-packages/numpy/lib/type_check.py in nan_to_num() 126 y[are_nan] = 0 --> 127 y[are_inf] = maxf 128 y[are_neg_inf] = minf ipdb> maxf array(1.7976931348623157e+308) ipdb> minf -1.7976931348623157e+308 ----------------------------------------------------------------------- ----------------------------------------------------------- -- Jonathan -- ------------------------------------------------------------------------ Jonathan Taylor Tel: 650.723.9230 Dept. of Statistics Fax: 650.725.8977 Sequoia Hall, 137 www-stat.stanford.edu/~jtaylo 390 Serra Mall Stanford, CA 94305
participants (2)
-
Jonathan Taylor
-
Travis Oliphant