diff --git a/idelib/parsers.py b/idelib/parsers.py index c8983dd7..19ca2fc0 100644 --- a/idelib/parsers.py +++ b/idelib/parsers.py @@ -905,59 +905,59 @@ def parseWith(self, parser, start=None, end=None, step=1, subchannel=None): # All numpy-compatible data streams use `struct.Struct` parsers isNumpyCompatibleFormat = isinstance(parser, struct.Struct) - if isNumpyCompatibleFormat: - - if parser is self._parser: - streamDtype = self._streamDtype - commonDtype = self._commonDtype + if not isNumpyCompatibleFormat: + # No parser format -> assume that data is from an old .ide file + # & should be handled safely using the parser object + blocks = list(ChannelDataBlock.parseWith( + self, parser, start, end, step, subchannel + )) + return np.array(blocks, dtype=np.float64).T + + if parser is self._parser: + streamDtype = self._streamDtype + commonDtype = self._commonDtype + else: + parser_format = parser.format + if parser_format[0] in ['<', '>']: + endian = parser_format[0] + parser_format = parser_format[1:] else: - parser_format = parser.format - if parser_format[0] in ['<', '>']: - endian = parser_format[0] - parser_format = parser_format[1:] - else: - endian = '>' + endian = '>' - streamDtype = np.dtype(','.join([endian+typeId - for typeId in parser_format])) - - isHomogeneous = len(set(parser_format)) == 1 - if isHomogeneous: - commonDtype = np.dtype(endian + parser_format[0]) - else: - commonDtype = None + streamDtype = np.dtype( + ','.join([endian+typeId for typeId in parser_format]) + ) - self._parser = parser - self._streamDtype = streamDtype - self._commonDtype = commonDtype + isHomogeneous = len(set(parser_format)) == 1 + if isHomogeneous: + commonDtype = np.dtype(endian + parser_format[0]) + else: + commonDtype = None - if commonDtype is not None: - rawData = np.frombuffer(self.payload, dtype=commonDtype) - rawData = rawData.reshape(-1, len(streamDtype) or 1)[start:end:step].T + self._parser = parser + self._streamDtype = streamDtype + self._commonDtype = commonDtype - if len(streamDtype) > 0 and subchannel is not None: - return rawData[[subchannel]] - return rawData + if commonDtype is not None: + rawData = np.frombuffer(self.payload, dtype=commonDtype) + rawData = rawData.reshape(-1, len(streamDtype) or 1)[start:end:step].T - rawData = np.frombuffer(self.payload, dtype=streamDtype)[start:end:step] + if len(streamDtype) > 0 and subchannel is not None: + return rawData[[subchannel]] + return rawData - # Special cases for single-channel outputs - if subchannel is not None: - return rawData[streamDtype.names[subchannel]][np.newaxis] + rawData = np.frombuffer(self.payload, dtype=streamDtype)[start:end:step] - data = np.empty((len(streamDtype),) + rawData.shape, - dtype=np.float64) - for i, chName in enumerate(streamDtype.names): - data[i] = rawData[chName] + # Special cases for single-channel outputs + if subchannel is not None: + return rawData[streamDtype.names[subchannel]][np.newaxis] - return data + data = np.empty((len(streamDtype),) + rawData.shape, + dtype=np.float64) + for i, chName in enumerate(streamDtype.names): + data[i] = rawData[chName] - # No parser format -> assume that data is from an old .ide file type - # & should be handled safely using the parser object - blocks = list(ChannelDataBlock.parseWith( - self, parser, start, end, step, subchannel - )) - return np.array(blocks, dtype=np.float64).T + return data def parseByIndexWith(self, parser, indices, subchannel=None): """ Parse an element's payload and get a specific set of samples. Used