def read_mff_data(filePath, indType, startInd, lastInd, hdr): import numpy as np from mff.mff_getSummaryInfo import mff_getSummaryInfo from mff.blockSample2BlockAndSample import blockSample2BlockAndSample from mff.read_signalN import read_signalN if hdr != None: summaryInfo = hdr['orig'] else: summaryInfo = mff_getSummaryInfo(filePath) ##------------------------------------------------------------------------------ blockNumSamps = summaryInfo['blockNumSamps'] if indType == 'sample': [startBlockNum, startSamp] = blockSample2BlockAndSample(startInd-1, blockNumSamps) [lastBlockNum, lastSamp] = blockSample2BlockAndSample(lastInd, blockNumSamps) blocksInEpoch = range(startBlockNum, lastBlockNum+1) elif indType == 'epoch': blocksInEpoch = [] for i in range(startInd-1, lastInd): blocksInd = range(summaryInfo['epochFirstBlocks'][i]-1,summaryInfo['epochLastBlocks'][i]) blocksInEpoch.extend(blocksInd) else: print("Error: indType must to be either 'epoch' or 'sample'") ##-------------------------------------------------------------------------------- dataeeg = read_signalN(filePath, 'signal1.bin', blocksInEpoch) if summaryInfo['pibFilename'] != []: datapib = read_signalN(filePath, summaryInfo['pibFilename'], blocksInEpoch) datalist = [] for i in range(len(dataeeg)): dataI = np.concatenate((dataeeg[i], datapib[i]), axis=0) datalist.append(dataI) else: datalist = dataeeg ##-------------------------------------------------------------------------------- if indType == 'sample': if startBlockNum == lastBlockNum : data = datalist[0][:, startSamp:lastSamp] elif lastBlockNum-startBlockNum == 1: datalistS = datalist[0][:,startSamp:] datalistL = datalist[1][:,:lastSamp] data = np.concatenate((datalistS, datalistL), axis=1) else: datalistS = datalist[0][:,startSamp:] for j in range(1, len(datalist)-1): datalistJ = datalist[j] datalistS = np.concatenate((datalistS, datalistJ), axis=1) datalistL = datalist[-1][:,:lastSamp] data = np.concatenate((datalistS, datalistL), axis=1) elif indType == 'epoch': if len(datalist) > 1: if len(set(blockNumSamps)) > 1 : data = datalist[0] for j in range(1,len(datalist)): datalistj = datalist[j] data = np.concatenate((data, datalistj), axis=1) else : data = np.zeros((datalist[0].shape[0], datalist[0].shape[1], len(datalist))) for j in range(len(datalist)): data[:,:,j] = datalist[j] else: data = datalist[0] else: print("Error: indType must to be either 'epoch' or 'sample'") return data
def read_mff_header(filePath): # import numpy as np from xml.dom.minidom import parse from mff.mff_getSummaryInfo import mff_getSummaryInfo ##--------------------------------------------------------------------------- summaryInfo = mff_getSummaryInfo(filePath) ##--------------------------------------------------------------------------- # Pull header info from the summary info. nSamplesPre = 0 if summaryInfo['epochType'] =='seg': nSamples = summaryInfo['epochNumSamps'][0]; nTrials = len(summaryInfo['epochNumSamps']) # nTrials = summaryInfo['blocks'] # if Time0 is the same for all segments... if len(set(summaryInfo['epochTime0'])) == 1 : nSamplesPre = summaryInfo['epochTime0'][0] else : nSamples = sum(summaryInfo['epochNumSamps']) nTrials = 1 ##--------------------------------------------------------------------------- # Add the sensor info. sensorLayoutfile = filePath+'/sensorLayout.xml' sensorLayoutObj = parse(sensorLayoutfile) sensors = sensorLayoutObj.getElementsByTagName('sensor') label = [] chantype = [] chanunit = [] tmpLabel = [] nChans = 0 for sensor in sensors: sensortype = int(sensor.getElementsByTagName('type')[0].firstChild.data) if sensortype == 0 or sensortype == 1 : if sensor.getElementsByTagName('name')[0].firstChild == None: sn = sensor.getElementsByTagName('number')[0].firstChild.data.encode() tmpLabel = 'E'+ sn.decode() # tmpLabel = 'E'+ sensor.getElementsByTagName('number')[0].firstChild.data.encode() else: sn = sensor.getElementsByTagName('name')[0].firstChild.data.encode() tmpLabel = sn.decode() label.append(tmpLabel) chantype.append('eeg') chanunit.append('uV') nChans = nChans + 1 if nChans != summaryInfo['nChans']: print("Error. Should never occur.") ##---------------------------------------------------------------------------- if summaryInfo['pibNChans'] > 0 : pnsSetfile = filePath + '/pnsSet.xml' pnsSetObj = parse(pnsSetfile) pnsSensors = pnsSetObj.getElementsByTagName('sensor') for p in range(summaryInfo['pibNChans']): tmpLabel = 'pib' + str(p+1) label.append(tmpLabel) pnsSensorObj = pnsSensors[p] chantype.append(pnsSensorObj.getElementsByTagName('name')[0].firstChild.data.encode()) chanunit.append(pnsSensorObj.getElementsByTagName('unit')[0].firstChild.data.encode()) nChans = nChans + summaryInfo['pibNChans'] ##------------------------------------------------------------------------------- header = {'Fs':summaryInfo['sampRate'], 'nChans':nChans, 'nSamplesPre':nSamplesPre, 'nSamples':nSamples,'nTrials':nTrials,'label':label,'chantype':chantype, 'chanunit':chanunit, 'orig':summaryInfo } return header
def read_mff_event(filePath, hdr): import numpy as np from xml.dom.minidom import parse import os.path from mff.mff_micros2Sample import mff_micros2Sample import glob import os from mff.mff_getSummaryInfo import mff_getSummaryInfo from mff.ns2pyTime import ns2pyTime from mff.mff_micros2Sample import mff_micros2Sample from mff.samples2EpochSample import samples2EpochSample if hdr == None : summaryInfo = mff_getSummaryInfo(filePath) else: summaryInfo = hdr['orig'] # Pull the information about the epochs out of the summary info epochBeginSamps = summaryInfo['epochBeginSamps'] epochNumSamps = summaryInfo['epochNumSamps'] epochFirstBlocks = summaryInfo['epochFirstBlocks'] epochLastBlocks = summaryInfo['epochLastBlocks'] infofile = filePath+'/info.xml' infotime = parse(infofile) beginTime = infotime.getElementsByTagName('recordTime')[0].firstChild.data.encode() bTime = ns2pyTime(beginTime) numEpochs = len(epochBeginSamps) events = [] eventInds = np.zeros((numEpochs,2), dtype='i') eventInd = 0 for p in range(numEpochs): origp = [0, 0, 'metadata',{}] eventp = ['break '+ summaryInfo['epochType'], summaryInfo['epochBeginSamps'][p], summaryInfo['epochLabels'][p], [], summaryInfo['epochNumSamps'][p], [], origp ] events.append(eventp) eventInds[eventInd,:] = [eventp[1], eventInd] eventInd = eventInd+1 for file in glob.glob(filePath+"/Events_*.xml"): eventsfile = file eventsListObj = parse(eventsfile) trackname = eventsListObj.getElementsByTagName('name')[0].firstChild.data.encode() eventsList = eventsListObj.getElementsByTagName('event') numEvents = eventsList.length for p in range(numEvents): theEvent= eventsList[p] eventTime = theEvent.getElementsByTagName('beginTime')[0].firstChild.data.encode() eTime = ns2pyTime(eventTime) eventTimeInMicros = (eTime-bTime).microseconds out = mff_micros2Sample(eventTimeInMicros,summaryInfo['sampRate']) eventTimeInSamples = int(out[0]) sampleRemainder = int(out[1]) eventTimeInEpochSamples= samples2EpochSample(eventTimeInSamples, epochBeginSamps, epochNumSamps) if eventTimeInEpochSamples >= 0: keylist = theEvent.getElementsByTagName('key') eventkeycount = len(keylist) keyp = [] for q in range(eventkeycount): theKey = keylist[q] theKeyCode = theKey.getElementsByTagName('keyCode')[0].firstChild.data.encode() theKeyData = theKey.getElementsByTagName('data')[0].firstChild.data.encode() theKeyData1 = theKey.getElementsByTagName('data') theKeyData0 = theKeyData1[0] # print theKeyData0.toxml() a = theKeyData0.attributes["dataType"] theKeyDatatype = a.value.encode() theKeyq = [theKeyCode, int(theKeyData), theKeyDatatype,''] keyp.append(theKeyq) duration = int(theEvent.getElementsByTagName('duration')[0].firstChild.data.encode()) out = mff_micros2Sample(duration, summaryInfo['sampRate']) origp =[sampleRemainder, int(out[1]), trackname, keyp] eventp=[theEvent.getElementsByTagName('code')[0].firstChild.data.encode(),eventTimeInEpochSamples,0, [], int(out[0]), [], origp] events.append(eventp) eventIndp = np.array([eventp[1], eventInd], dtype='i').reshape((1,2)) eventInds = np.concatenate((eventInds,eventIndp), axis=0) eventInd = eventInd+1 eventInds0 = eventInds[eventInds[:,0].argsort()] sortedEvents = events for p in range(eventInd): nextEventInd = eventInds0[p,1] sortedEvents[p] = events[nextEventInd] events = sortedEvents return events
def read_mff_event(filePath, hdr): import numpy as np from xml.dom.minidom import parse import os.path from mff.mff_micros2Sample import mff_micros2Sample import glob import os from mff.mff_getSummaryInfo import mff_getSummaryInfo from mff.ns2pyTime import ns2pyTime from mff.mff_micros2Sample import mff_micros2Sample from mff.samples2EpochSample import samples2EpochSample if hdr == None: summaryInfo = mff_getSummaryInfo(filePath) else: summaryInfo = hdr['orig'] # Pull the information about the epochs out of the summary info epochBeginSamps = summaryInfo['epochBeginSamps'] epochNumSamps = summaryInfo['epochNumSamps'] epochFirstBlocks = summaryInfo['epochFirstBlocks'] epochLastBlocks = summaryInfo['epochLastBlocks'] infofile = filePath + '/info.xml' infotime = parse(infofile) beginTime = infotime.getElementsByTagName( 'recordTime')[0].firstChild.data.encode() bTime = ns2pyTime(beginTime) numEpochs = len(epochBeginSamps) events = [] eventInds = np.zeros((numEpochs, 2), dtype='i') eventInd = 0 for p in range(numEpochs): origp = [0, 0, 'metadata', {}] eventp = [ 'break ' + summaryInfo['epochType'], summaryInfo['epochBeginSamps'][p], summaryInfo['epochLabels'][p], [], summaryInfo['epochNumSamps'][p], [], origp ] events.append(eventp) eventInds[eventInd, :] = [eventp[1], eventInd] eventInd = eventInd + 1 for file in glob.glob(filePath + "/Events_*.xml"): eventsfile = file eventsListObj = parse(eventsfile) trackname = eventsListObj.getElementsByTagName( 'name')[0].firstChild.data.encode() eventsList = eventsListObj.getElementsByTagName('event') numEvents = eventsList.length for p in range(numEvents): theEvent = eventsList[p] eventTime = theEvent.getElementsByTagName( 'beginTime')[0].firstChild.data.encode() eTime = ns2pyTime(eventTime) eventTimeInMicros = (eTime - bTime).microseconds out = mff_micros2Sample(eventTimeInMicros, summaryInfo['sampRate']) eventTimeInSamples = int(out[0]) sampleRemainder = int(out[1]) eventTimeInEpochSamples = samples2EpochSample(eventTimeInSamples, epochBeginSamps, epochNumSamps) if eventTimeInEpochSamples >= 0: keylist = theEvent.getElementsByTagName('key') eventkeycount = len(keylist) keyp = [] for q in range(eventkeycount): theKey = keylist[q] theKeyCode = theKey.getElementsByTagName( 'keyCode')[0].firstChild.data.encode() theKeyData = theKey.getElementsByTagName( 'data')[0].firstChild.data.encode() theKeyData1 = theKey.getElementsByTagName('data') theKeyData0 = theKeyData1[0] # print theKeyData0.toxml() a = theKeyData0.attributes["dataType"] theKeyDatatype = a.value.encode() theKeyq = [theKeyCode, int(theKeyData), theKeyDatatype, ''] keyp.append(theKeyq) duration = int( theEvent.getElementsByTagName('duration') [0].firstChild.data.encode()) out = mff_micros2Sample(duration, summaryInfo['sampRate']) origp = [sampleRemainder, int(out[1]), trackname, keyp] eventp = [ theEvent.getElementsByTagName('code') [0].firstChild.data.encode(), eventTimeInEpochSamples, 0, [], int(out[0]), [], origp ] events.append(eventp) eventIndp = np.array([eventp[1], eventInd], dtype='i').reshape( (1, 2)) eventInds = np.concatenate((eventInds, eventIndp), axis=0) eventInd = eventInd + 1 eventInds0 = eventInds[eventInds[:, 0].argsort()] sortedEvents = events for p in range(eventInd): nextEventInd = eventInds0[p, 1] sortedEvents[p] = events[nextEventInd] events = sortedEvents return events
def read_mff_data(filePath, indType, startInd, lastInd, hdr): import numpy as np from mff.mff_getSummaryInfo import mff_getSummaryInfo from mff.blockSample2BlockAndSample import blockSample2BlockAndSample from mff.read_signalN import read_signalN if hdr != None: summaryInfo = hdr['orig'] else: summaryInfo = mff_getSummaryInfo(filePath) ##------------------------------------------------------------------------------ blockNumSamps = summaryInfo['blockNumSamps'] if indType == 'sample': [startBlockNum, startSamp] = blockSample2BlockAndSample(startInd - 1, blockNumSamps) [lastBlockNum, lastSamp] = blockSample2BlockAndSample(lastInd, blockNumSamps) blocksInEpoch = range(startBlockNum, lastBlockNum + 1) elif indType == 'epoch': blocksInEpoch = [] for i in range(startInd - 1, lastInd): blocksInd = range(summaryInfo['epochFirstBlocks'][i] - 1, summaryInfo['epochLastBlocks'][i]) blocksInEpoch.extend(blocksInd) else: print("Error: indType must to be either 'epoch' or 'sample'") ##-------------------------------------------------------------------------------- dataeeg = read_signalN(filePath, 'signal1.bin', blocksInEpoch) if summaryInfo['pibFilename'] != []: datapib = read_signalN(filePath, summaryInfo['pibFilename'], blocksInEpoch) datalist = [] for i in range(len(dataeeg)): dataI = np.concatenate((dataeeg[i], datapib[i]), axis=0) datalist.append(dataI) else: datalist = dataeeg ##-------------------------------------------------------------------------------- if indType == 'sample': if startBlockNum == lastBlockNum: data = datalist[0][:, startSamp:lastSamp] elif lastBlockNum - startBlockNum == 1: datalistS = datalist[0][:, startSamp:] datalistL = datalist[1][:, :lastSamp] data = np.concatenate((datalistS, datalistL), axis=1) else: datalistS = datalist[0][:, startSamp:] for j in range(1, len(datalist) - 1): datalistJ = datalist[j] datalistS = np.concatenate((datalistS, datalistJ), axis=1) datalistL = datalist[-1][:, :lastSamp] data = np.concatenate((datalistS, datalistL), axis=1) elif indType == 'epoch': if len(datalist) > 1: if len(set(blockNumSamps)) > 1: data = datalist[0] for j in range(1, len(datalist)): datalistj = datalist[j] data = np.concatenate((data, datalistj), axis=1) else: data = np.zeros((datalist[0].shape[0], datalist[0].shape[1], len(datalist))) for j in range(len(datalist)): data[:, :, j] = datalist[j] else: data = datalist[0] else: print("Error: indType must to be either 'epoch' or 'sample'") return data