diff --git a/idelib/importer.py b/idelib/importer.py index d6f74679..c4c41d64 100644 --- a/idelib/importer.py +++ b/idelib/importer.py @@ -603,7 +603,7 @@ def getExitCondition(recording, bytesRead=1000): if idx <= len(data): result = data[idx] except (IOError, IndexError, ValueError) as e: - logger.warn(e) + logger.warning(e) recording.seek(offset) return result diff --git a/idelib/parsers.py b/idelib/parsers.py index 612eb4cc..e063b3bb 100644 --- a/idelib/parsers.py +++ b/idelib/parsers.py @@ -889,7 +889,6 @@ def getHeader(self): class ChannelDataArrayBlock(ChannelDataBlock): - def __init__(self, element): super(ChannelDataArrayBlock, self).__init__(element) self._payload = None @@ -905,6 +904,34 @@ def payload(self): self._payloadEl.gc() return self._payload + # Define standard mapping from struct to numpy typestring + # (conversions taken from struct & numpy docs:) + # https://docs.python.org/3/library/struct.html#format-characters + # https://numpy.org/doc/stable/reference/arrays.dtypes.html#specifying-and-constructing-data-types + TO_NP_TYPESTR = { + # 'x': '', + 'c': 'b', + 'b': 'b', + 'B': 'B', + '?': '?', + 'h': 'i2', + 'H': 'u2', + 'i': 'i4', + 'I': 'u4', + 'l': 'i4', + 'L': 'u4', + 'q': 'i8', + 'Q': 'u8', + # 'n': '', + # 'N': '', + # 'e': 'f2', unsupported in Python3.5 + 'f': 'f4', + 'd': 'f8', + # 's': '', + # 'p': '', + # 'P': '', + } + def parseWith(self, parser, start=None, end=None, step=1, subchannel=None): """ Parse an element's payload. Use this instead of directly using `parser.parse()` for consistency's sake. @@ -941,13 +968,13 @@ def parseWith(self, parser, start=None, end=None, step=1, subchannel=None): else: endian = '>' - streamDtype = np.dtype( - ','.join([endian+typeId for typeId in parser_format]) - ) + streamDtype = np.dtype(','.join([ + endian + self.TO_NP_TYPESTR[typeId] for typeId in parser_format + ])) isHomogeneous = len(set(parser_format)) == 1 if isHomogeneous: - commonDtype = np.dtype(endian + parser_format[0]) + commonDtype = np.dtype(endian + self.TO_NP_TYPESTR[parser_format[0]]) else: commonDtype = None diff --git a/testing/test3.IDE b/testing/test3.IDE new file mode 100644 index 00000000..bf879e3f Binary files /dev/null and b/testing/test3.IDE differ diff --git a/testing/test_integration.py b/testing/test_integration.py new file mode 100644 index 00000000..e63624fd --- /dev/null +++ b/testing/test_integration.py @@ -0,0 +1,42 @@ +import os.path + +import pytest # type: ignore +import idelib + + +@pytest.mark.parametrize('filename', [ + os.path.join('testing', 'test3.IDE'), +]) +def test_integ_channels(filename): + with idelib.importFile(filename) as ds: + assert {i for i in ds.channels.keys()} == {8, 36, 59, 70, 80, 84} + + accel100g_ch = ds.channels[8] + data = accel100g_ch.getSession().arraySlice() + assert data.shape[0] == 5 # t, x, y, z, mic + assert data.size > 0 + + accel40g_ch = ds.channels[80] + data = accel40g_ch.getSession().arraySlice() + assert data.shape[0] == 4 # t, x, y, z + assert data.size > 0 + + rot_ch = ds.channels[84] + data = rot_ch.getSession().arraySlice() + assert data.shape[0] == 4 # t, x, y, z + assert data.size > 0 + + orient_ch = ds.channels[70] + data = orient_ch.getSession().arraySlice() + assert data.shape[0] == 5 # t, x, y, z, w + assert data.size > 0 + + pt_ch = ds.channels[36] + data = pt_ch.getSession().arraySlice() + assert data.shape[0] == 3 # t, press, temp + assert data.size > 0 + + pt_ctrl_ch = ds.channels[59] + data = pt_ctrl_ch.getSession().arraySlice() + assert data.shape[0] == 3 # t, press, temp + assert data.size > 0 diff --git a/testing/test_parsers.py b/testing/test_parsers.py index f9b90e0c..fc999622 100644 --- a/testing/test_parsers.py +++ b/testing/test_parsers.py @@ -1,5 +1,6 @@ import unittest import mock +import struct from ebmlite.core import * # type: ignore import numpy as np # type: ignore @@ -107,6 +108,14 @@ def testParseMinMeanMax(self): def testGetHeader(self): self.assertEqual(self.block.getHeader(), (211, 32)) + def testToNpTypestr(self): + for stype, nptype in ChannelDataArrayBlock.TO_NP_TYPESTR.items(): + for endian in ('<', '>'): + assert ( + struct.calcsize(endian+stype) + == np.dtype(endian+nptype).itemsize + ) + def testParseWith(self): parser = self.doc.channels[32].parser