Skip to content

Commit

Permalink
Correct bug if channelist is given, can now run mdfconverter
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric.rateau@gmail.com committed May 9, 2014
1 parent 5ba8a52 commit 28a5d55
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
36 changes: 19 additions & 17 deletions mdf4reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ def __init__(self, fid, pointer, numpyDataRecordFormat, numberOfRecords , dataR
# block header
self.loadHeader(fid, pointer)
if self['id'] in ('##DT', '##RD', b'##DT', b'##RD'): # normal data block
if channelList==None: # reads all blocks
self['data']=numpy.core.records.fromfile( fid, dtype = numpyDataRecordFormat, shape = numberOfRecords , names=dataRecordName)
else: # channelList defined
# reads only the channels using offset functions, channel by channel
buf={}
# order offsets and names based on offset value
channelList=OrderedDict(sorted(channelList.items(), key=lambda t: t[1]))
for record in range(numberOfRecords):
for name in dataRecordName:
if name in channelList.keys():
fid.seek()
buf.append(numpy.core.records.fromfile( fid, dtype = numpyDataRecordFormat, shape = 1 , names=name, offset=None ))
self['data']=buf
# if channelList==None: # reads all blocks
self['data']=numpy.core.records.fromfile( fid, dtype = numpyDataRecordFormat, shape = numberOfRecords , names=dataRecordName)
# else: # channelList defined
# # reads only the channels using offset functions, channel by channel
# buf={}
# # order offsets and names based on offset value
# channelList=OrderedDict(sorted(channelList.items(), key=lambda t: t[1]))
# for record in range(numberOfRecords):
# for name in dataRecordName:
# if name in channelList.keys():
# fid.seek()
# buf.append(numpy.core.records.fromfile( fid, dtype = numpyDataRecordFormat, shape = 1 , names=name, offset=None ))
# self['data']=buf

elif self['id'] in ('##SD', b'##SD'): # Signal Data Block
unpack('uint32', fid.read(4)) # length of data
Expand Down Expand Up @@ -211,7 +211,7 @@ def read4( self, fileName=None, info = None, multiProc = False, channelList=None
if info['CGBlock'][dataGroup][channelGroup]['cg_cn_first']==0 or info['CGBlock'][dataGroup][channelGroup]['cg_flags']&1:
print('VLSD ChannelGroup, not really implemented yet')
numberOfRecords=info['CGBlock'][dataGroup][channelGroup]['cg_cycle_count']
channelListByte=[]
#channelListByte=[]
#dataBytes=info['CGBlock'][dataGroup][channelGroup]['cg_dataBytes']
#invalidBytes=info['CGBlock'][dataGroup][channelGroup]['cg_invalid_bytes']
#invalidBit=info['CGBlock'][dataGroup][channelGroup]['cg_invalid_bit_pos']
Expand All @@ -220,13 +220,13 @@ def read4( self, fileName=None, info = None, multiProc = False, channelList=None
signalDataType = info['CNBlock'][dataGroup][channelGroup][channel]['cn_data_type']
# Corresponding number of bits for this format
numberOfBits = info['CNBlock'][dataGroup][channelGroup][channel]['cn_bit_count']
channelByteOffset= info['CNBlock'][dataGroup][channelGroup][channel]['cn_byte_offset']+info['DGBlock'][dataGroup]['dg_rec_id_size']
#channelByteOffset= info['CNBlock'][dataGroup][channelGroup][channel]['cn_byte_offset']+info['DGBlock'][dataGroup]['dg_rec_id_size']
channelName=info['CNBlock'][dataGroup][channelGroup][channel]['name']
# is it a master channel for the group
if info['CNBlock'][dataGroup][channelGroup][channel]['cn_type'] in (2, 3, 4): # master channel
masterDataGroup[dataGroup]=channelName
if channelList is not None:
channelListByte[channelName]=channelByteOffset
#if channelList is not None:
# channelListByte[channelName]=channelByteOffset

if numberOfBits < 8: # adding bit format, ubit1 or ubit2
if precedingNumberOfBits == 8: # 8 bit make a byte
Expand Down Expand Up @@ -273,6 +273,8 @@ def read4( self, fileName=None, info = None, multiProc = False, channelList=None
self.masterChannelList[masterDataGroup[dataGroup]].append(channelName)
self[channelName] = {}
self[channelName]['master'] = masterDataGroup[dataGroup]
# sync_types : 0 data, 1 time (sec), 2 angle (radians), 4 index
self[channelName]['masterType'] = info['CNBlock'][dataGroup][channelGroup][channel]['cn_sync_type']
if 'unit' in list(info['CCBlock'][dataGroup][channelGroup][channel].keys()):
self[channelName]['unit'] = info['CCBlock'][dataGroup][channelGroup][channel]['unit']
elif 'unit' in list(info['CNBlock'][dataGroup][channelGroup][channel].keys()):
Expand Down
7 changes: 5 additions & 2 deletions mdfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,18 @@ def resample( self, samplingTime = 0.1, masterChannelName=None ):
masterChannelName='master'
self[masterChannelName] = {}
unit = ''
masterType = 1 # time by default
for master in list(self.masterChannelList.keys()):
if master in self and len( self[master]['data'] ) > 5: # consider groups having minimum size
minTime.append( self[master]['data'][0] )
maxTime.append( self[master]['data'][len( self[master]['data'] ) - 1] )
if self[master]['unit'] != '':
if len(self[master]['unit'])>1 :
unit = self[master]['unit']
masterType = self[master]['masterType']
self[masterChannelName]['data'] = numpy.arange( min( minTime ),max( maxTime ),samplingTime )
self[masterChannelName]['unit'] = unit
self[masterChannelName]['description'] = 'Unique master channel'
self[masterChannelName]['masterType'] = masterType

# Interpolate channels
timevect=[]
Expand All @@ -283,7 +286,7 @@ def resample( self, samplingTime = 0.1, masterChannelName=None ):
for ind in list(self.masterChannelList.keys()):
del self[ind]
self.masterChannelList = {} # empty dict
self.masterChannelList[masterChannelName] = list(self.keys())
self.masterChannelList[masterChannelName] = list(self.keys())
else:
pass

Expand Down
2 changes: 1 addition & 1 deletion mdfreaderui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from os import path
PythonVersion=version_info
PythonVersion=PythonVersion[0]
MultiProc=True # multiprocess switch, for debug purpose
MultiProc=True # multiprocess switch, for debug purpose put False

class MainWindow(QMainWindow, Ui_MainWindow, QFileDialog):
"""
Expand Down

0 comments on commit 28a5d55

Please sign in to comment.