use numpy for faster de/serialization

This commit is contained in:
Jan Petykiewicz 2019-12-12 00:50:12 -08:00
parent 5a4c7db6b4
commit e1b5d95b43

View File

@ -27,6 +27,7 @@ from . import exceptions, tags, types
from datetime import datetime from datetime import datetime
import math import math
import struct import struct
import numpy
__all__ = [ __all__ = [
'Record', 'Record',
@ -76,7 +77,7 @@ def _parse_int2(data):
data_len = len(data) data_len = len(data)
if not data_len or (data_len % 2): if not data_len or (data_len % 2):
raise exceptions.IncorrectDataSize('INT2') raise exceptions.IncorrectDataSize('INT2')
return struct.unpack('>%dh' % (data_len//2), data) return numpy.frombuffer(data, dtype='>i2', count=data_len // 2)
def _parse_int4(data): def _parse_int4(data):
""" """
@ -96,7 +97,7 @@ def _parse_int4(data):
data_len = len(data) data_len = len(data)
if not data_len or (data_len % 4): if not data_len or (data_len % 4):
raise exceptions.IncorrectDataSize('INT4') raise exceptions.IncorrectDataSize('INT4')
return struct.unpack('>%dl' % (data_len//4), data) return numpy.frombuffer(data, dtype='>i4', count=data_len // 4)
def _int_to_real(num): def _int_to_real(num):
""" """
@ -208,7 +209,7 @@ def _pack_int2(data):
6 6
""" """
size = len(data) size = len(data)
return struct.pack('>{0}h'.format(size), *data) return numpy.array(data).astype('>i2').tobytes()
def _pack_int4(data): def _pack_int4(data):
""" """
@ -223,7 +224,7 @@ def _pack_int4(data):
12 12
""" """
size = len(data) size = len(data)
return struct.pack('>{0}l'.format(size), *data) return numpy.array(data).astype('>i4').tobytes()
def _real_to_int(fnum): def _real_to_int(fnum):
""" """
@ -344,12 +345,7 @@ class Record(object):
if data is not None: if data is not None:
self.data = data self.data = data
elif points is not None: elif points is not None:
new_data = [] self.data = numpy.ravel(points)
# TODO make it faster
for point in points:
new_data.append(point[0])
new_data.append(point[1])
self.data = new_data
elif times is not None: elif times is not None:
mod_time = times[0] mod_time = times[0]
acc_time = times[1] acc_time = times[1]
@ -503,7 +499,7 @@ class Record(object):
data_size = len(self.data) data_size = len(self.data)
if not data_size or (data_size % 2): if not data_size or (data_size % 2):
raise exceptions.DataSizeError(self.tag) raise exceptions.DataSizeError(self.tag)
return [(self.data[i], self.data[i+1]) for i in range(0, data_size, 2)] return numpy.array(self.data).reshape(-1, 2)
@property @property
def times(self): def times(self):