Skip to content

Commit

Permalink
refactored ChannelDataArrayBlock.parseWith
Browse files Browse the repository at this point in the history
- reordered conditional to reduce indentation
- removed whitespace lint on `streamDtype` assignment
  • Loading branch information
CrepeGoat committed Jul 15, 2020
1 parent fcb067b commit 36d0e73
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions idelib/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 36d0e73

Please sign in to comment.