Re: [Numpy-discussion] converting list of int16 values to bitmask and back to bitmask and back to list of int32\float values
Hi again, I realize that my question was not clear enough, so I've refined it into one runnable function (attached below) My question is basically - is there a way to perform the same operation, but faster using NumPy (or even just by using Python better..) Thanks again and sorry for the unclearness.. Nissim. import struct def Convert(): Endian = '
On 08/10/17 09:12, Nissim Derdiger wrote:
Hi again, I realize that my question was not clear enough, so I've refined it into one runnable function (attached below) My question is basically - is there a way to perform the same operation, but faster using NumPy (or even just by using Python better..) Thanks again and sorry for the unclearness.. Nissim.
import struct
def Convert(): Endian = '
# pack every 2 registers, take only the first 2 bytes from each one, change their endianess than unpack them back to the Parameter format Result.append((struct.unpack(ParameterFormat,(struct.pack(Endian,RawDataList[(i*2)+1])[0:2] + struct.pack('
You can do this a little more elegantly (and probably faster) with
struct by putting it in a list comprehension:
[struct.unpack('f', struct.pack('
On 08/10/17 22:50, Thomas Jollans wrote:
On 08/10/17 09:12, Nissim Derdiger wrote:
Hi again, I realize that my question was not clear enough, so I've refined it into one runnable function (attached below) My question is basically - is there a way to perform the same operation, but faster using NumPy (or even just by using Python better..) Thanks again and sorry for the unclearness.. Nissim.
import struct
def Convert(): Endian = '
# pack every 2 registers, take only the first 2 bytes from each one, change their endianess than unpack them back to the Parameter format Result.append((struct.unpack(ParameterFormat,(struct.pack(Endian,RawDataList[(i*2)+1])[0:2] + struct.pack('
You can do this a little more elegantly (and probably faster) with struct by putting it in a list comprehension:
[struct.unpack('f', struct.pack('
Numpy can also do it. You can get your array of little-endian shorts with
le_shorts = np.array(raw_data, dtype='
and then reinterpret the bytes backing it as float32 with np.frombuffer:
np.frombuffer(le_shorts.data, dtype='f4')
For small lists like the one in your example, the two approaches are equally fast. For long ones, numpy is much faster:
*sigh* let's try that again:
In [82]: raw_data
Out[82]: [17252, 26334, 16141, 58057, 17252, 15478, 16144, 43257]
In [83]: raw_data2 = np.random.randint(0, 2**32, size=10**6, dtype='u4')
In [84]: %timeit np.frombuffer(np.array(raw_data, dtype=' In [82]: raw_data Out[82]: [17252, 26334, 16141, 58057, 17252, 15478,
16144, 43257] In [83]: raw_data2 = np.random.randint(0, 2**32,
size=10**6, dtype='u4') # 1 million random integers In [84]: %timeit
np.frombuffer(np.array(raw_data, dtype=' -- Thomas _______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion
participants (2)
-
Nissim Derdiger
-
Thomas Jollans