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 getEpochInfos(filePath, sampRate): import numpy as np from xml.dom.minidom import parse import os.path from mff.mff_micros2Sample import mff_micros2Sample ##------------------------------------------------------------------------------ epochfile = filePath+'/epochs.xml' epochList = parse(epochfile) epochs = epochList.getElementsByTagName('epoch') numEpochs = epochs.length epochBeginSamps = np.zeros((numEpochs), dtype='i8') epochNumSamps = np.zeros((numEpochs), dtype='i8') epochFirstBlocks = np.zeros((numEpochs), dtype='i8') epochLastBlocks = np.zeros((numEpochs), dtype='i8') epochTime0 = np.zeros((numEpochs), dtype='i8') epochLabels = [None]*numEpochs #np.zeros((numEpochs), dtype='S50') epochSubjects = [] epochFilenames = [] epochSegStatus = [None]*numEpochs #np.zeros((numEpochs), dtype='S50') multiSubj = False for p in range(numEpochs): anEpoch = epochs[p] epochBegin = int(anEpoch.getElementsByTagName('beginTime')[0].firstChild.data) epochEnd = int(anEpoch.getElementsByTagName('endTime')[0].firstChild.data) epochBeginSamps[p] = mff_micros2Sample(epochBegin, sampRate)[0] epochTime0[p] = epochBeginSamps[p] epochNumSamps[p] = mff_micros2Sample(epochEnd, sampRate)[0]- epochBeginSamps[p] epochFirstBlocks[p] = int(anEpoch.getElementsByTagName('firstBlock')[0].firstChild.data) epochLastBlocks[p] = int(anEpoch.getElementsByTagName('lastBlock')[0].firstChild.data) epochLabels[p] = 'epoch' epochType = 'cnt' totalNumSegs = 0 ##------------------------------------------------------------------------------------ categfile = filePath+'/categories.xml' if os.path.isfile(categfile): epochType = 'seg' categList = parse(categfile) cats = categList.getElementsByTagName('cat') numCategs = cats.length multiSubj = False if os.path.exists(filePath+'/subjects'): multiSubj = True for p in range(numCategs): aCateg = cats[p] segList = aCateg.getElementsByTagName('segments') numSegs = segList.length for q in range(numSegs): aSeg = segList[q] keyListArray = aSeg.getElementsByTagName('keyCode') numKeys = keyListArray.length numSubjs = 0 for r in range(numKeys): aKey = keyListArray[r] if aKey.firstChild.data.encode() == 'subj': numSubjs = numSubjs + 1 if multiSubj: epochSubjects = [None]*numEpochs #np.zeros((numEpochs), dtype='S50') epochFilenames = [None]*numEpochs #np.zeros((numEpochs), dtype='S50') for p in range(numCategs): aCateg = cats[p] categLabel = aCateg.getElementsByTagName('name')[0].firstChild.data #.encode() segList = aCateg.getElementsByTagName('seg') numSegs = segList.length totalNumSegs = totalNumSegs + numSegs for q in range(numSegs): aSeg = segList[q] segBegin = int(aSeg.getElementsByTagName('beginTime')[0].firstChild.data) segBeginSamp = int(mff_micros2Sample(segBegin, sampRate)[0]) segInd = np.where(epochBeginSamps == segBeginSamp)[0] epochSegStatus[segInd[0]] = aSeg.getAttribute('status') epochLabels[segInd[0]] = categLabel time0 = int(aSeg.getElementsByTagName('evtBegin')[0].firstChild.data) time0Samp = mff_micros2Sample(time0, sampRate)[0] time0Samp = time0Samp - segBeginSamp epochTime0[segInd[0]] = time0Samp if multiSubj: keyListArray = aSeg.getElementsByTagName('keyCode') numKeys = keyListArray.length dataListArray = aSeg.getElementsByTagName('data') for r in range(numKeys): aKey = keyListArray[r] aData = dataListArray[r] subject = None filename = None if aData.firstChild != None: if aKey.firstChild.data.encode() =='subj': subject = aData.firstChild.data #.encode() elif aKey.firstChild.data.encode() == 'FILE': filename = aData.firstChild.data.encode() epochSubjects[segInd[0]] = subject epochFilenames[segInd[0]] = filename if (multiSubj and len(set(epochFilenames))==1 and len(set(epochSubjects)) == 1): epochSubjects = [] epochFilenames = [] multiSubj = False ##------------------------------------------------------------------------------------ epochInfo = {'epochType':epochType, 'epochBeginSamps':epochBeginSamps, 'epochNumSamps':epochNumSamps, 'epochFirstBlocks':epochFirstBlocks, 'epochLastBlocks':epochLastBlocks, 'epochLabels':epochLabels, 'epochTime0':epochTime0, 'multiSubj':multiSubj, 'epochSubjects':epochSubjects, 'epochFilenames':epochFilenames, 'epochSegStatus':epochSegStatus} return epochInfo
def getEpochInfos(filePath, sampRate): import numpy as np from xml.dom.minidom import parse import os.path from mff.mff_micros2Sample import mff_micros2Sample ##------------------------------------------------------------------------------ epochfile = filePath + '/epochs.xml' epochList = parse(epochfile) epochs = epochList.getElementsByTagName('epoch') numEpochs = epochs.length epochBeginSamps = np.zeros((numEpochs), dtype='i8') epochNumSamps = np.zeros((numEpochs), dtype='i8') epochFirstBlocks = np.zeros((numEpochs), dtype='i8') epochLastBlocks = np.zeros((numEpochs), dtype='i8') epochTime0 = np.zeros((numEpochs), dtype='i8') epochLabels = [None] * numEpochs #np.zeros((numEpochs), dtype='S50') epochSubjects = [] epochFilenames = [] epochSegStatus = [None] * numEpochs #np.zeros((numEpochs), dtype='S50') multiSubj = False for p in range(numEpochs): anEpoch = epochs[p] epochBegin = int( anEpoch.getElementsByTagName('beginTime')[0].firstChild.data) epochEnd = int( anEpoch.getElementsByTagName('endTime')[0].firstChild.data) epochBeginSamps[p] = mff_micros2Sample(epochBegin, sampRate)[0] epochTime0[p] = epochBeginSamps[p] epochNumSamps[p] = mff_micros2Sample(epochEnd, sampRate)[0] - epochBeginSamps[p] epochFirstBlocks[p] = int( anEpoch.getElementsByTagName('firstBlock')[0].firstChild.data) epochLastBlocks[p] = int( anEpoch.getElementsByTagName('lastBlock')[0].firstChild.data) epochLabels[p] = 'epoch' epochType = 'cnt' totalNumSegs = 0 ##------------------------------------------------------------------------------------ categfile = filePath + '/categories.xml' if os.path.isfile(categfile): epochType = 'seg' categList = parse(categfile) cats = categList.getElementsByTagName('cat') numCategs = cats.length multiSubj = False if os.path.exists(filePath + '/subjects'): multiSubj = True for p in range(numCategs): aCateg = cats[p] segList = aCateg.getElementsByTagName('segments') numSegs = segList.length for q in range(numSegs): aSeg = segList[q] keyListArray = aSeg.getElementsByTagName('keyCode') numKeys = keyListArray.length numSubjs = 0 for r in range(numKeys): aKey = keyListArray[r] if aKey.firstChild.data.encode() == 'subj': numSubjs = numSubjs + 1 if multiSubj: epochSubjects = [None ] * numEpochs #np.zeros((numEpochs), dtype='S50') epochFilenames = [ None ] * numEpochs #np.zeros((numEpochs), dtype='S50') for p in range(numCategs): aCateg = cats[p] categLabel = aCateg.getElementsByTagName( 'name')[0].firstChild.data #.encode() segList = aCateg.getElementsByTagName('seg') numSegs = segList.length totalNumSegs = totalNumSegs + numSegs for q in range(numSegs): aSeg = segList[q] segBegin = int( aSeg.getElementsByTagName('beginTime')[0].firstChild.data) segBeginSamp = int(mff_micros2Sample(segBegin, sampRate)[0]) segInd = np.where(epochBeginSamps == segBeginSamp)[0] epochSegStatus[segInd[0]] = aSeg.getAttribute('status') epochLabels[segInd[0]] = categLabel time0 = int( aSeg.getElementsByTagName('evtBegin')[0].firstChild.data) time0Samp = mff_micros2Sample(time0, sampRate)[0] time0Samp = time0Samp - segBeginSamp epochTime0[segInd[0]] = time0Samp if multiSubj: keyListArray = aSeg.getElementsByTagName('keyCode') numKeys = keyListArray.length dataListArray = aSeg.getElementsByTagName('data') for r in range(numKeys): aKey = keyListArray[r] aData = dataListArray[r] subject = None filename = None if aData.firstChild != None: if aKey.firstChild.data.encode() == 'subj': subject = aData.firstChild.data #.encode() elif aKey.firstChild.data.encode() == 'FILE': filename = aData.firstChild.data.encode() epochSubjects[segInd[0]] = subject epochFilenames[segInd[0]] = filename if (multiSubj and len(set(epochFilenames)) == 1 and len(set(epochSubjects)) == 1): epochSubjects = [] epochFilenames = [] multiSubj = False ##------------------------------------------------------------------------------------ epochInfo = { 'epochType': epochType, 'epochBeginSamps': epochBeginSamps, 'epochNumSamps': epochNumSamps, 'epochFirstBlocks': epochFirstBlocks, 'epochLastBlocks': epochLastBlocks, 'epochLabels': epochLabels, 'epochTime0': epochTime0, 'multiSubj': multiSubj, 'epochSubjects': epochSubjects, 'epochFilenames': epochFilenames, 'epochSegStatus': epochSegStatus } return epochInfo