def new_eve(eve, epochs, eve_dir, rej_dir):
    print 'Running %s' % eve
    eve_d = readTable(eve)
    rej_types = ('blink',) # , 'grad', 'mag', 'eeg'
    type_rep = {'blink':'1000'}
    print 'epochs.keys listed in condCodes:',epochs.keys()
    k = filter(lambda y: y in eve, epochs.keys())  #given your eve file name, see if it contains any paradigm names
    paradigmKey = max(k, key=len)  #In case your filename contained more than one paradigm name, pick the longest one (to avoid substring problems)
    print 'using epoch key:', paradigmKey
    new_eve = eve_d[:]
    
    for type in rej_types:
        #get type from all_rej
        bname = op.basename(eve)
        base, _, _ = bname.rpartition('Mod.eve')
        search = op.join(rej_dir, '%s-Filtered_raw_%s.txt' % (base,type))
        #print 'Search:', search
        rej = glob(search)
        #print rej
        
        if len(rej) < 1:
           raise Exception('No rej of this type found!')
        print epochs[paradigmKey] ##print this line if you want to see what time samples are being used
        
        bad_dict = reject(type, epochs[paradigmKey], new_eve, readTable(rej[0]))
        #print bad_dict
        
        for code, r in bad_dict.items():
            f = lambda x:[x[0], x[1], x[2], str(int(type_rep[type])+int(code))] if x[3] == code and int(x[0]) in r else x
            new_eve[:] = map(f, new_eve)
        pass
    new_fname = op.join(eve_dir, '%s%s.eve' % (op.basename(eve).rpartition('.eve')[0], 'Rej'))
    print("Writing new eve to %s" % new_fname)
    writeTable(new_fname, new_eve)
    print
Example #2
0
def new_eve(eve):
    print 'Starting with %s' % eve
    eve_d = readTable(eve)
    rej_types = ('veog', 'heog')  # , 'grad', 'mag', 'eeg'
    k = filter(lambda y: y in eve, epochs.keys())[0]
    new_eve = eve_d[:]
    for type in rej_types:
        #get type from all_rej
        bname = op.basename(eve)
        base, _, _ = bname.rpartition('Mod.eve')
        search = op.join(temp_d, '%s_raw_%s.txt' % (base, type))
        rej = glob(search)
        if len(rej) < 1:
            raise Exception('No rej of this type found!')
        bad_dict = reject(type, epochs[k], new_eve, readTable(rej[0]))
        for code, r in bad_dict.items():
            f = lambda x: [
                x[0], x[1], x[2],
                str(int(type_rep[type]) + int(code))
            ] if x[3] == code and int(x[0]) in r else x
            new_eve[:] = map(f, new_eve)
        pass
    new_fname = op.join(
        eve_dir, '%s%s.eve' % (op.basename(eve).rpartition('.eve')[0], 'Rej'))
    print("Writing new eve to %s" % new_fname)
    writeTable(new_fname, new_eve)
Example #3
0
def compare(efile, rfile):
    print 'Starting comparison on %s and %s' % (efile, rfile)
    study = filter(lambda x: x in efile and x in rfile, cc.codes.keys())[0]
    eve = readTable(efile)
    rej = readTable(rfile)
    d = {}
    for code in cc.codes[study]:
        neve = len([e for e in eve if e[3] == code])
        nrej = len([r for r in rej if r[3] == code])
        print('Code %s\tEve# %d\t Rej# %d' % (code, neve, nrej))
        d[code] = [nrej, neve]
    return study, d
Example #4
0
def compare(efile, rfile):
    print 'Starting comparison on %s and %s' % (efile, rfile)
    study = filter(lambda x: x in efile and x in rfile, cc.codes.keys())[0]
    eve = readTable(efile)
    rej = readTable(rfile)
    d = {}
    for code in cc.codes[study]:
        neve = len([e for e in eve if e[3] == code])
        nrej = len([r for r in rej if r[3] == code])
        print('Code %s\tEve# %d\t Rej# %d' % (code, neve, nrej))
        d[code] = [nrej, neve]
    return study, d
def rej_rej2eve_projon(subjID):

    filePrefix = '/cluster/kuperberg/SemPrMM/MEG/data/' + subjID

    ##expList = ['ATLLoc','MaskedMM','BaleenLP','BaleenHP','AXCPT']
    expList = ['BaleenHP']

    runDict = {
        'ATLLoc': [''],
        'MaskedMM': ['Run1', 'Run2'],
        'BaleenLP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'BaleenHP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'AXCPT': ['Run1', 'Run2']
    }

    for exp in expList:
        print exp
        for run in runDict[exp]:
            inFile1 = filePrefix + '/ave_projon/logs/ssp_avelogs/' + subjID + '_' + exp + run + '_ecgeog1-ave.log'
            inFile2 = filePrefix + '/eve/' + subjID + '_' + exp + run + 'ModRej.eve'
            outFile = filePrefix + '/eve/' + subjID + '_' + exp + run + 'ModRej4projoff.eve'
            ##                        print inFile1
            ##                        print inFile2
            ##                        print('Step1 done')
            if os.path.exists(outFile):
                os.remove(outFile)

            if os.path.exists(inFile1) and os.path.exists(inFile2):
                data1 = readInput.readTable(inFile1)
                rowsin1 = len(data1)
                data2 = readInput.readTable(inFile2)
                rowsin2 = len(data2)
                firstLine = data2[0]
                firstTimept = int((firstLine[0]))
                ##                        print(firstTimept)
                timept1 = timept2 = []
                for i in range(0, len(data1)):
                    lineTemp1 = data1[i]
                    if len(lineTemp1) > 10:
                        timept1.append((lineTemp1[0]))
                print(timept1)

                for i in range(0, len(data2)):
                    lineTemp2 = data2[i]
                    timeN = int(lineTemp2[0])
                    timept2 = str(timeN - firstTimept)
                    if str(timept2) in timept1:
                        lineTemp2[3] = 4000 + int(lineTemp2[3])

                writeOutput.writeTable(outFile, data2)
def  rej_rej2eve_projon(subjID):
	
	filePrefix = '/cluster/kuperberg/SemPrMM/MEG/data/'+subjID
	
	##expList = ['ATLLoc','MaskedMM','BaleenLP','BaleenHP','AXCPT']
	expList = ['BaleenHP']
	
	runDict = {'ATLLoc':[''],'MaskedMM':['Run1','Run2'],'BaleenLP':['Run1','Run2','Run3','Run4'],'BaleenHP':['Run1','Run2','Run3','Run4'], 'AXCPT':['Run1','Run2']}
	

	for exp in expList:
                print exp
                for run in runDict[exp]:
                        inFile1 = filePrefix + '/ave_projon/logs/ssp_avelogs/' + subjID + '_' + exp + run + '_ecgeog1-ave.log'
                        inFile2 = filePrefix + '/eve/' + subjID + '_' + exp + run + 'ModRej.eve'
                        outFile = filePrefix + '/eve/' + subjID + '_' + exp + run + 'ModRej4projoff.eve'
##                        print inFile1
##                        print inFile2
##                        print('Step1 done')
                        if os.path.exists(outFile):
                                os.remove(outFile)
                        
                        if os.path.exists(inFile1) and os.path.exists(inFile2):
                                data1 = readInput.readTable(inFile1)
                                rowsin1 = len(data1)
                                data2 = readInput.readTable(inFile2)
                                rowsin2 = len(data2)
                                firstLine = data2[0]
                                firstTimept = int((firstLine[0]))
                ##                        print(firstTimept)
                                timept1=timept2=[]
                                for i in range (0, len(data1)):
                                        lineTemp1 = data1[i]
                                        if len(lineTemp1) > 10:
                                                timept1.append((lineTemp1[0]))
                                print(timept1)

                                for i in range (0, len(data2)):
                                        lineTemp2 = data2[i]
                                        timeN = int(lineTemp2[0])
                                        timept2= str(timeN - firstTimept)
                                        if str(timept2) in timept1:
                                                lineTemp2[3] = 4000 + int(lineTemp2[3])

                                writeOutput.writeTable(outFile,data2)
def parse_dat(input):
    """
    This parses a .dat file found in the roi_summary folder, returning a dictionary
    keys are subjects, values are ROI values for that subject in this dat file
    """
    from readInput import readTable
    data = readTable(input)
    d = {}
    [d.__setitem__(x[0], x[1]) for x in data]
    return d    
Example #8
0
def parse_dat(input):
    from readInput import readTable
    data = readTable(input)
    d = {}
    [d.__setitem__(x[0], x[1]) for x in data]
    return d    
    
    
    
    
def equalizeTriggers(subjID):    

    os.chdir("/cluster/kuperberg/SemPrMM/MEG/data/"+subjID)
    
    expList = ['ATLLoc','MaskedMM','BaleenLP','BaleenHP','AXCPT']
    runDict = { 'ATLLoc':[''],'MaskedMM':['Run1','Run2'],'BaleenLP':['Run1','Run2','Run3','Run4'],'BaleenHP':['Run1','Run2','Run3','Run4'],'AXCPT':['Run1','Run2']}

    codeGroupDict = { 'AXCPT':['AXCPT_prime','AXCPT_target']}

    codeDict = { 'AXCPT_prime':['5','6'],'AXCPT_target':['1','2','3']}
    minDict = {}
    
    if subjID == 'ya3':
        runDict['AXCPT']=['Run1']
    if (subjID == 'ya1' or subjID == 'ya2' or subjID == 'ya4' or subjID == 'ya7' or subjID == 'ya8' or subjID == 'ya16'):
        runDict['AXCPT']=''
                
        
    #########################
    ##EQUALIZE TRIGGER COUNTS###
    print '----equalize triggers'
    for exp in expList:
        for codeGroup in codeGroupDict[exp]:
            evMin = 9999
            for code in codeDict[codeGroup]:
                    print int(code)-1
                    aveName = '/cluster/kuperberg/SemPrMM/MEG/data/%s/ave_projon/%s_%s_All-ave.fif' % (subjID, subjID, exp)
                    print aveName
                    evoked = fiff.Evoked(aveName,setno=int(code)-1) #needs revision
                    print evoked.nave
                    if int(evoked.nave) < evMin:
                        evMin = int(evoked.nave)
            minDict[codeGroup] = evMin
            print codeGroup, evMin, minDict[codeGroup]

            
        for run in runDict[exp]:
            inFile = 'eve/' + subjID + '_'+exp+run+'Mod.eve'
            outFile = 'eve/' + subjID + '_' + exp + run + 'Mod-testeq.eve' #when finalized, this should probably be ModRejEq.eve, and later scripts changed
            print inFile
            if os.path.exists(inFile):
                    data = readInput.readTable(inFile)
                    
                    firstRow = data[0]
                    firstSample = firstRow[0]
                    firstTime = firstRow[1]

                    flag = ''
                    
                    for row in data:
                        trigger = row[3]
                        time = row[1]


                    writeOutput.writeTable(outFile,data)
Example #10
0
def roistats2R(subj_gp, exp):

        tableLength=0
        dataTable=lineTemp=line=Con=dataLine=[]
        allData=[]
        Contrasts = []
        subj = []
        hem = ['lh', 'rh']
        roiName = []
        
        inFile = "/cluster/kuperberg/SemPrMM/MRI/results/roi_fsfast/roi_summary/" + subj_gp +"_" + exp +"FullTable.txt"
        dataTable = readInput.readTable(inFile)
        tableLength = len(dataTable)     
        
        ##parse the data to figure out number of contrasts, rois, and subjects
        for i in range(1, tableLength):   #skip the header row
            lineTemp = dataTable[i]
            Con = dataTable[0]
            if len(lineTemp)<6:
                   roiName.append(str(lineTemp[2]))
            elif len(lineTemp)>4:
                   subj.append(lineTemp[0])
                   
        subj_num = (len(subj)/len(roiName))
        
		##Get out the contrasts
        numContrasts = (len(Con)-1)/2   ##figure out how many contrasts there are
        for c in range(numContrasts):
        	conIndex = c*2+1
        	[Con[conIndex],x] = str.split(str(Con[conIndex]), '_')
        	Contrasts.append(Con[conIndex])
##        print Contrasts

        i = 0
        for i in range(1, tableLength):  #skip the header row
             line = dataTable[i]
             curr_subj = line[0]
             if len(line) < 4:  ##This is the beginning of a new label block so update current label
                          curr_label = line[2]
##                          print curr_label
             if len(line) > 4:
                          for c in range(numContrasts):
                                        for m in [0, 1]:
													curr_contrast = str(Contrasts[c])
													curr_hem = hem[m]
													curr_signal = line[c*2+1+m]  #for each contrast, this will find correct signal
##													print c*2+1+m
													dataLine = [curr_subj, curr_contrast, curr_label, curr_hem, curr_signal]
													allData.append(dataLine)
													dataLine = []
                                                        

        outFile = "/cluster/kuperberg/SemPrMM/MRI/results/roi_fsfast/R/" + subj_gp +"_" + exp + "_" + "roiTable.txt"                                              
        writeOutput.writeTable(outFile, allData)                                            
Example #11
0
def countBadChan(inFile, par):

    trialCount = 0
    rtrialCount = 0
    badTrials = []
    badchans = []  #initialise separately
    items = []
    dataTable = []

    dataTable = readInput.readTable(inFile)
    trialCount = len(dataTable)
    #print trialCount

    # read through the output logfile from the MNE averaging and look for longer lines indicating rejection
    # lines that have more than 9 columns are epochs that were rejected
    # if you count over 8 from the right, you will get the channel number that caused the rejection
    for row in dataTable:
        if len(row) > 10:
            badTrials.append(
                row[len(row) - 8]
            )  #append the last but 8th item in the list-lineTemp to chans
            rtrialCount += 1
    badChanList = list(
        set(badTrials))  #finds the items in a list(first occurances)
    #print(list(badTrials))

    ##Create critical data table of bad channels and the number of trials they caused rejection
    for chan in badChanList:
        badchans.append([chan, badTrials.count((chan))])

#print(badchans)

    name1 = str.split(str(inFile), '_')
    name2 = str(name1[0]) + '_MEEGArtReject_' + str(par)
    outFile1 = str(name2)
    myFile2 = open(outFile1, "a")
    myFile2.write("\n")
    myFile2.write(str(trialCount))
    myFile2.write("\t")
    myFile2.write(str(rtrialCount))
    myFile2.close()

    name3 = str(name1[0]) + '_MEEGArtReject-BadChan_' + str(par)
    outFile2 = str(name3)
    myFile3 = open(outFile2, "a")
    for row in badchans:
        myFile3.write("\n")
        myFile3.write(row[0])
        myFile3.write("\t")
        myFile3.write(str(row[1]))
        myFile3.write("\n")
    myFile3.close()
Example #12
0
def new_eve(eve):
    print 'Starting with %s' % eve
    eve_d = readTable(eve)
    rej_types = ('veog', 'heog') # , 'grad', 'mag', 'eeg'
    k = filter(lambda y: y in eve, epochs.keys())[0]
    new_eve = eve_d[:]
    for type in rej_types:
        #get type from all_rej
        bname = op.basename(eve)
        base, _, _ = bname.rpartition('Mod.eve')
        search = op.join(temp_d, '%s_raw_%s.txt' % (base,type))
        rej = glob(search)
        if len(rej) < 1:
            raise Exception('No rej of this type found!')
        bad_dict = reject(type, epochs[k], new_eve, readTable(rej[0]))
        for code, r in bad_dict.items():
            f = lambda x:[x[0], x[1], x[2], str(int(type_rep[type])+int(code))] if x[3] == code and int(x[0]) in r else x
            new_eve[:] = map(f, new_eve)
        pass
    new_fname = op.join(eve_dir, '%s%s.eve' % (op.basename(eve).rpartition('.eve')[0], 'Rej'))
    print("Writing new eve to %s" % new_fname)
    writeTable(new_fname, new_eve)
def countBadChan(inFile, par):
        
        trialCount= 0
        rtrialCount = 0
        badTrials=[]
        badchans=[] #initialise separately
        items=[]
        dataTable = []
        
        dataTable = readInput.readTable(inFile)
        trialCount=len(dataTable)
        #print trialCount
        
        # read through the output logfile from the MNE averaging and look for longer lines indicating rejection
        # lines that have more than 9 columns are epochs that were rejected
        # if you count over 8 from the right, you will get the channel number that caused the rejection
       	for row in dataTable:
             if len(row) > 10:
                     badTrials.append(row[len(row) - 8]) #append the last but 8th item in the list-lineTemp to chans
                     rtrialCount+=1
    	badChanList=list(set(badTrials)) #finds the items in a list(first occurances)
    	#print(list(badTrials))
    	
    	##Create critical data table of bad channels and the number of trials they caused rejection
    	for chan in badChanList:
    		badchans.append([chan, badTrials.count((chan))])
    	#print(badchans)	
                                    
        name1=str.split(str(inFile), '_')
        name2=str(name1[0]) + '_MEEGArtReject_' + str(par)
        outFile1=str(name2)
        myFile2 = open(outFile1, "a")
        myFile2.write("\n")
        myFile2.write(str(trialCount))
        myFile2.write("\t")
        myFile2.write(str(rtrialCount))
        myFile2.close()

        name3=str(name1[0]) + '_MEEGArtReject-BadChan_' + str(par)
        outFile2=str(name3)
        myFile3 = open(outFile2, "a")
        for row in badchans:
            myFile3.write("\n")
            myFile3.write(row[0])
            myFile3.write("\t")
            myFile3.write(str(row[1]))
            myFile3.write("\n")
        myFile3.close()
Example #14
0
def parse_eve(eve, study):
    mask_maxrt = 2.0
    bal_maxrt = 1.5
    ax_maxrt = 2.0
    lines = readTable(eve)
    results = dict()
    response_ind = [
        i for i, x in enumerate(lines) if x[3] == "16" or x[3] == "32"
        or x[3] == "64" or x[3] == "128" or x[3] == "4294934528"
    ]
    for code in codes[study]:
        code_lines = [line for line in lines if line[3] == code]
        if len(code_lines) > 0:  #code present at all
            cr = dict({})
            cr["c"] = 0
            cr["t"] = 0
            cr["rts"] = []
            for cl in code_lines:
                ct = float(cl[1])
                #using Mod.eve files, the task code always comes before the response
                after_response_ind = [
                    i for i in response_ind if i > lines.index(cl)
                ]
                if len(after_response_ind) > 0:
                    rt = round(float(lines[after_response_ind[0]][1]) - ct, 3)
                else:
                    rt = -1
                task = codes[study][code][1]
                if study == "MaskedMM":
                    good_rt = 0 < rt < mask_maxrt
                elif study == "Baleen":
                    good_rt = 0 < rt < bal_maxrt
                elif study == "AXCPT":
                    good_rt = 0 < rt < ax_maxrt
                    print good_rt
                if (task and good_rt) or (not task and not good_rt):
                    cr["c"] += 1
                    if task:
                        cr["rts"].append(rt)
                cr["t"] += 1
            results[code] = cr
    if study == "AXCPT":
        for code in ["41", "42", "43", "44"]:
            actual_code = code[1]
            num_misses = len([miss for miss in lines if miss[3] == code])
            results[actual_code]['t'] += num_misses
    return results
def binary_conversion(exp, subjID, paradigm):

	maxCount = 16777215  #2^24-1, because we use 24 good triggers (162-187, excluding 176 and 177; could go to 188 but more than 2^24 screws MNE conversion)
	filePath = '/Volumes/CUTTLEFISH/MEG_Experiments/'+exp+'/data/' #Lawrence added 'Experiments' 4.23.13
	
	trigSeqDict = {1:162,2:163,3:164,4:165,5:166,6:167,7:168,8:169,9:170,10:171,11:172,12:173,13:174,14:175,15:178,16:179,17:180,18:181,19:182,20:183,21:184,22:185,23:186,24:187}
	
	binaryDict = {maxCount-2**0:1,maxCount-2**1:2,maxCount-2**2:3,maxCount-2**3:4,maxCount-2**4:5,maxCount-2**5:6,maxCount-2**6:7,maxCount-2**7:8,maxCount-2**8:9,maxCount-2**9:10,maxCount-2**10:11,maxCount-2**11:12,maxCount-2**12:13,maxCount-2**13:14,maxCount-2**14:15,maxCount-2**15:16,maxCount-2**16:17,maxCount-2**17:18,maxCount-2**18:19,maxCount-2**19:20,maxCount-2**20:21,maxCount-2**21:22,maxCount-2**22:23,maxCount-2**23:24}
	
	count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	#print binaryDict
	inFile = filePath + subjID + '/eve/' + subjID + '_' + paradigm + '.eve'
	outFile = filePath + subjID + '/eve/' + subjID + '_' + paradigm + 'Mod.eve'

	eveData = readInput.readTable(inFile)
	
	for row in eveData:
		#print row
		if (not row[3] == str(maxCount)) and (not row[3] == '0'):
			#print row
			code = binaryDict[int(row[3])]
			#print trigSeqDict[code]
			count[code-1] = count[code-1]+1
			row[3] = trigSeqDict[code]
			row[2] = 0
	
	for row in eveData:
		if row[3] == str(maxCount):
			#print 'catch'
			eveData.remove(row)
			
	writeOutput.writeTable(outFile,eveData)		
			
	print count
	i=0
	for item in count:
		i = i+1
		print "trigger_"+str(i)+" = "+str(item)
Example #16
0
def fixTriggersAllUnrelated(subjID):

    os.chdir("/cluster/kuperberg/SemPrMM/MEG/data/" + subjID)

    expList = ['BaleenLP', 'BaleenHP']

    runDict = {
        'BaleenLP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'BaleenHP': ['Run1', 'Run2', 'Run3', 'Run4']
    }

    ###############################################
    #BALEEN Change all codes to Unrelated

    for exp in expList:
        for run in runDict[exp]:
            inFile = 'eve/' + subjID + '_' + exp + run + 'ModRej.eve'
            outFile = 'eve/' + subjID + '_' + exp + run + 'ModRejAllU.eve'
            if os.path.exists(inFile):
                data = readInput.readTable(inFile)

                rowCount = 0
                flag2 = 0
                for row in data:
                    trigger = row[3]

                    #BaleenLP
                    if trigger == '2':
                        row[3] = '4'

                    #BaleenHP

                    if trigger == '7':
                        row[3] = '9'

                    rowCount += 1

                writeOutput.writeTable(outFile, data)
def fixTriggersAllUnrelated(subjID):    

    os.chdir("/cluster/kuperberg/SemPrMM/MEG/data/"+subjID)
    
    expList = ['BaleenLP','BaleenHP']
    
    runDict = {'BaleenLP':['Run1','Run2','Run3','Run4'],'BaleenHP':['Run1','Run2','Run3','Run4']}
    
        
    ###############################################
    #BALEEN Change all codes to Unrelated 
    
    for exp in expList:
        for run in runDict[exp]:
			inFile = 'eve/'+subjID+'_'+exp+run+'ModRej.eve'
			outFile = 'eve/'+subjID+'_'+exp+run+'ModRejAllU.eve'
			if os.path.exists(inFile):
				data = readInput.readTable(inFile)
				
				rowCount = 0
				flag2 = 0
				for row in data:
					trigger = row[3]
					
					#BaleenLP
					if trigger == '2':
						row[3] = '4'
					
					#BaleenHP

					if trigger == '7':
						row[3] = '9'
						
					rowCount +=1
				
				writeOutput.writeTable(outFile, data)
Example #18
0
def equalizeTriggers(subjID):

    os.chdir("/cluster/kuperberg/SemPrMM/MEG/data/" + subjID)

    expList = ['ATLLoc', 'MaskedMM', 'BaleenLP', 'BaleenHP', 'AXCPT']
    runDict = {
        'ATLLoc': [''],
        'MaskedMM': ['Run1', 'Run2'],
        'BaleenLP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'BaleenHP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'AXCPT': ['Run1', 'Run2']
    }

    codeGroupDict = {'AXCPT': ['AXCPT_prime', 'AXCPT_target']}

    codeDict = {'AXCPT_prime': ['5', '6'], 'AXCPT_target': ['1', '2', '3']}
    minDict = {}

    if subjID == 'ya3':
        runDict['AXCPT'] = ['Run1']
    if (subjID == 'ya1' or subjID == 'ya2' or subjID == 'ya4'
            or subjID == 'ya7' or subjID == 'ya8' or subjID == 'ya16'):
        runDict['AXCPT'] = ''

    #########################
    ##EQUALIZE TRIGGER COUNTS###
    print '----equalize triggers'
    for exp in expList:
        for codeGroup in codeGroupDict[exp]:
            evMin = 9999
            for code in codeDict[codeGroup]:
                print int(code) - 1
                aveName = '/cluster/kuperberg/SemPrMM/MEG/data/%s/ave_projon/%s_%s_All-ave.fif' % (
                    subjID, subjID, exp)
                print aveName
                evoked = fiff.Evoked(aveName,
                                     setno=int(code) - 1)  #needs revision
                print evoked.nave
                if int(evoked.nave) < evMin:
                    evMin = int(evoked.nave)
            minDict[codeGroup] = evMin
            print codeGroup, evMin, minDict[codeGroup]

        for run in runDict[exp]:
            inFile = 'eve/' + subjID + '_' + exp + run + 'Mod.eve'
            outFile = 'eve/' + subjID + '_' + exp + run + 'Mod-testeq.eve'  #when finalized, this should probably be ModRejEq.eve, and later scripts changed
            print inFile
            if os.path.exists(inFile):
                data = readInput.readTable(inFile)

                firstRow = data[0]
                firstSample = firstRow[0]
                firstTime = firstRow[1]

                flag = ''

                for row in data:
                    trigger = row[3]
                    time = row[1]

                writeOutput.writeTable(outFile, data)
Example #19
0
def fixTriggers(subjID):

    os.chdir("/cluster/kuperberg/SemPrMM/MEG/data/" + subjID)

    expList = ['ATLLoc', 'MaskedMM', 'BaleenLP', 'BaleenHP', 'AXCPT']

    runDict = {
        'ATLLoc': [''],
        'MaskedMM': ['Run1', 'Run2'],
        'BaleenLP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'BaleenHP': ['Run1', 'Run2', 'Run3', 'Run4'],
        'AXCPT': ['Run1', 'Run2']
    }

    if subjID == 'ya3':
        runDict['AXCPT'] = ['Run1']

    if (subjID == 'ya1' or subjID == 'ya2' or subjID == 'ya4'
            or subjID == 'ya7' or subjID == 'ya8' or subjID == 'ya16'):
        runDict['AXCPT'] = ''

    print '----subject specific fixes'

    #########################
    ##FIX RANDOM YA19 ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    inFile = 'eve/ya19_BaleenLPRun2.eve'
    outFile = 'eve/ya19_BaleenLPRun2.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        for row in data:
            trigger = row[3]
            time = row[1]
            if (time == '162.257' and trigger == '2'):
                print row
                row[3] = 14
                print row
        writeOutput.writeTable(outFile, data)

###########################FIX RANDOM YA22 ERROR
##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    inFile = 'eve/ya22_BaleenHPRun3.eve'
    outFile = 'eve/ya22_BaleenHPRun3.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        for row in data:
            trigger = row[3]
            time = row[1]
            if (time == '71.100' and trigger == '6'):
                print row
                row[3] = 14
                print row
        writeOutput.writeTable(outFile, data)

    #########################
    ##FIX RANDOM YA6 ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    ##NOt clear what this code was originally so changing to 600
    inFile = 'eve/ya6_BaleenHPRun3.eve'
    outFile = 'eve/ya6_BaleenHPRun3.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        for row in data:
            trigger = row[3]
            time = row[1]
            if (time == '163.827' and trigger == '6'):
                print row
                row[3] = 600
                print row
            writeOutput.writeTable(outFile, data)

    #########################
    ##FIX RANDOM YA16 MASKEDMM ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    inFile = 'eve/ya16_MaskedMMRun1.eve'
    outFile = 'eve/ya16_MaskedMMRun1.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        for row in data:
            trigger = row[3]
            time = row[1]
            if (time == '232.349' and trigger == '2'):
                print row
                row[3] = 3
                print row
        writeOutput.writeTable(outFile, data)

##########################
##FIX UNKNOWN YA3 LPRUN3 ERROR
#Although no documentation to explain, vtsd logs and raw datafiles show run3 restarted after one minute (54 events), so responses to the stimuli that had already been shown should not be counted.

    inFile = 'eve/ya3_BaleenLPRun3.eve'
    outFile = 'eve/ya3_BaleenLPRun3.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        for i in range(1, 54):
            row = data[i]
            row[3] = int(row[3]) + 900
        writeOutput.writeTable(outFile, data)

##########################
##FIX YA24 MASKEDMMRUN1 LOSS OF DISPLAY ERROR
#Scan log notes that MaskedMMRun1 was ended early because display got disconnected. This probably happened several seconds before presentation was ended. The last moment at which the subject responded was 316.707, so to be conservative we are removing triggers in the 16 subsequent seconds, in which the subject did not respond to several probe trials

    inFile = 'eve/ya24_MaskedMMRun1.eve'
    outFile = 'eve/ya24_MaskedMMRun1.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        for row in data:
            trigger = row[3]
            time = row[1]
            if (float(time) > 316.707):
                row[3] = int(row[3]) + 900
                print row
        writeOutput.writeTable(outFile, data)

    #########################
    ##FIX TIMING IN ALL SCRIPTS###
    print '----fix Timing'
    for exp in expList:
        for run in runDict[exp]:
            inFile = 'eve/' + subjID + '_' + exp + run + '.eve'
            outFile = 'eve/' + subjID + '_' + exp + run + 'Mod.eve'
            print inFile
            if os.path.exists(inFile):
                data = readInput.readTable(inFile)

                firstRow = data[0]
                firstSample = firstRow[0]
                firstTime = firstRow[1]

                wordCount = 0  ##for ATLLoc
                flag = ''

                for row in data:
                    trigger = row[3]
                    time = row[1]
                    sampleRate = float(row[0]) / float(row[1])
                    trueSample = float(
                        row[0]
                    ) + 19  ## compensate for the time lag between projector and trigger
                    trueTime = trueSample / sampleRate
                    row[0] = str(int(round(trueSample, 0)))
                    row[1] = str(round(trueTime, 3))
                    finalRow = row

                ##Undo the timing change for the first row in file because this row indicates the beginning of the scan, not a visual event, so it shouldn't be changed

                firstRow[0] = firstSample
                firstRow[1] = firstTime

                ###add extra trigger to get around MNE bug that ignores last row
                extraRow = [
                    str(int(finalRow[0]) + 1),
                    str(round(float(finalRow[1]) + 1, 3)), '0', '99'
                ]
                data.append(extraRow)
                writeOutput.writeTable(outFile, data)

    print '-----fix codes'

    ###############################
    ###CHANGE CODES IN ATLLOC
    inFile = 'eve/' + subjID + '_ATLLocMod.eve'
    outFile = 'eve/' + subjID + '_ATLLocMod.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        print inFile
        wordCount = 0
        flag = ''

        for row in data:
            trigger = row[3]

            if trigger == '1' or trigger == '2' or trigger == '3':
                flag = trigger
                wordCount = 0
            else:
                wordCount += 1

            if trigger == '4' and wordCount <= 9:
                row[3] = trigger + flag

        writeOutput.writeTable(outFile, data)

    ###############################################
    #MASKEDMM

    for x in runDict['MaskedMM']:
        inFile = 'eve/' + subjID + '_MaskedMM' + x + 'Mod.eve'
        outFile = 'eve/' + subjID + '_MaskedMM' + x + 'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile

            ########################
            ##Change trigger for incorrect button presses ('go' responses)
            rowCount = 0
            for row in data:
                trigger = row[3]
                if (trigger == '1' or trigger == '2' or trigger == '3'):
                    nextRow = data[rowCount + 1]
                    nextTrigger = nextRow[3]
                    #print 'hello', trigger, row, nextRow
                    if nextTrigger == '16' or nextTrigger == '32' or nextTrigger == '64' or nextTrigger == '128':
                        print "false positive: ", row, nextRow
                        row[3] = '500' + row[3]
                rowCount += 1

    ###############################################
    #BALEENLP

    for x in runDict['BaleenLP']:
        inFile = 'eve/' + subjID + '_BaleenLP' + x + 'Mod.eve'
        outFile = 'eve/' + subjID + '_BaleenLP' + x + 'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile
            rowCount = 0
            for row in data:
                trigger = row[3]

                ##This part fixes the coding for the probe primes. Originally the trigger 11 was sent for the target when the prime was a probe. This recodes the target as 111 and codes the prime itself as 11
                if trigger == '11':  ##change the target to '111'
                    row[3] = '1' + trigger
                    #print row

                    for i in [
                            1, 2, 3
                    ]:  ## look through the previous three events to try to find the prime
                        compRow = data[rowCount - i]
                        #print 'Comparison', compRow

                        compTrigger = compRow[3]
                        compSOA = float(row[1]) - float(compRow[1])
                        if compTrigger == '14' and (
                                compSOA > .580 and compSOA < .620
                        ):  ##check for a prime with correct SOA
                            compRow[3] = trigger
                            #print 'new ', compRow, row
                            break

                rowCount += 1

            ########################
            ##Change trigger for incorrect button presses ('go' responses)
            rowCount = 0
            for row in data:
                trigger = row[3]
                if (trigger == '1' or trigger == '2' or trigger == '4'):
                    nextRow = data[rowCount + 1]
                    nextTrigger = nextRow[3]
                    #print row, nextRow
                    if nextTrigger == '16' or nextTrigger == '32' or nextTrigger == '64' or nextTrigger == '128':
                        print "false positive: ", row, nextRow
                        row[3] = '500' + row[3]
                rowCount += 1

            writeOutput.writeTable(outFile, data)

    ###############################################
    #BALEENHP

    for x in runDict['BaleenHP']:
        inFile = 'eve/' + subjID + '_BaleenHP' + x + 'Mod.eve'
        outFile = 'eve/' + subjID + '_BaleenHP' + x + 'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile
            rowCount = 0
            flag2 = 0
            for row in data:
                trigger = row[3]

                if trigger == '12':  ##change the target to '112'
                    row[3] = '1' + trigger
                    #print row

                    for i in [
                            1, 2, 3
                    ]:  ## look through the previous three events to try to find the prime
                        compRow = data[rowCount - i]
                        #print 'Comparison', compRow

                        compTrigger = compRow[3]
                        compSOA = float(row[1]) - float(compRow[1])
                        if compTrigger == '14' and (
                                compSOA > .580 and compSOA < .620
                        ):  ##check for a prime with correct SOA
                            compRow[3] = trigger
                            #print 'new ', compRow, row
                            break

                ##Flip half of the related filler triggers to '18' to get equal number of related and unrelated
                if trigger == '8':
                    if flag2 == 1:
                        row[3] = '18'
                        flag2 = 0
                    elif flag2 == 0:
                        flag2 = 1

                rowCount += 1

            ########################
            ##Change trigger for incorrect button presses ('go' responses)
            rowCount = 0
            for row in data:
                trigger = row[3]

                if (trigger == '6' or trigger == '7' or trigger == '8'
                        or trigger == '9' or trigger == '18'):
                    nextRow = data[rowCount + 1]
                    nextTrigger = nextRow[3]
                    #print row, nextRow
                    if nextTrigger == '16' or nextTrigger == '32' or nextTrigger == '64' or nextTrigger == '128':
                        print "false positive: ", row, nextRow
                        row[3] = '500' + row[3]
                rowCount += 1

            ##########################################write it all out
            writeOutput.writeTable(outFile, data)

    #########################
    ###AXCPT

    for x in runDict['AXCPT']:
        x
        inFile = 'eve/' + subjID + '_AXCPT' + x + 'Mod.eve'
        outFile = 'eve/' + subjID + '_AXCPT' + x + 'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile
            ###############################################################
            if subjID == 'ya6':  ####Fix error in triggers for this subject
                logFile = '../../vtsd_logs/ya6/AXCPT_ya6_List101_' + x + '.vtsd_log'
                print logFile
                logData = readInput.readTable(logFile)
                firstPrimeRow = data[2]
                firstPrimeTime = firstPrimeRow[1]
                count = 0

                ##Fix timing due to error in trigger coding
                for row in data:
                    trueTime = round(float(row[1]) - float(firstPrimeTime))
                    if row[3] == '8':
                        for logRow in logData:
                            logTime = round(float(logRow[5]) + 1)
                            if logTime == trueTime:
                                #print logRow[5], logTime, trueTime, row[3], logRow[9]
                                row[3] = logRow[9]
                                #print logRow
            ################################################################

            rowCount = 0
            flag3 = 0
            for row in data:
                trigger = row[3]
                if len(data) > rowCount + 1:
                    nextRow = data[rowCount + 1]
                    nextTrigger = nextRow[3]
                else:
                    break

                #############################################
                ##change blinks triggered as 6 to 7s

                if trigger == '6':
                    if nextTrigger == '7':
                        row[3] = '7'


##                #################################################
##                ##Flip 3/4 of the 'A' triggers to 15, to match number of As and Bs
##                if trigger == '5':
##                    if flag3 == 3:
##                        flag3 = 0
##                    elif flag3 != 3:
##                        flag3 = flag3 + 1
##                        row[3] = '15'
##
###########################################
##change triggers for incorrect trials#####

##AX case
                if trigger == '4':
                    if len(nextTrigger
                           ) < 2:  ###Test for response (16, 32, 64 or 128)
                        row[3] = '9' + trigger
                        print 'miss:', row

                ##BX, BY, AY case
                if (trigger == '1' or trigger == '2' or trigger == '3'):

                    if len(nextTrigger
                           ) > 1:  ###Test for response (16, 32, 64 or 128)
                        row[3] = '9' + trigger
                        print 'false positive:', row

                rowCount += 1

            writeOutput.writeTable(outFile, data)
Example #20
0
def fixTriggers(subjID):    

    os.chdir("/cluster/kuperberg/SemPrMM/MEG/data/"+subjID)
    
    expList = ['ATLLoc','MaskedMM','BaleenLP','BaleenHP','AXCPT']
    
    runDict = { 'ATLLoc':[''],'MaskedMM':['Run1','Run2'],'BaleenLP':['Run1','Run2','Run3','Run4'],'BaleenHP':['Run1','Run2','Run3','Run4'],'AXCPT':['Run1','Run2']}
    
    if subjID == 'ya3':
        runDict['AXCPT']=['Run1']

        
    if (subjID == 'ya1' or subjID == 'ya2' or subjID == 'ya4' or subjID == 'ya7' or subjID == 'ya8' or subjID == 'ya16'):
        runDict['AXCPT']=''
    
    print '----subject specific fixes'
    
        
        
    #########################
    ##FIX RANDOM YA19 ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    inFile= 'eve/ya19_BaleenLPRun2.eve'
    outFile = 'eve/ya19_BaleenLPRun2.eve'
    if os.path.exists(inFile):
    	data = readInput.readTable(inFile)
    	for row in data:
    		trigger = row[3]
    		time = row[1]
    		if (time == '162.257' and trigger == '2'):
    			print row
    			row[3] = 14
    			print row
    	writeOutput.writeTable(outFile,data)		

 
	###########################FIX RANDOM YA22 ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    inFile= 'eve/ya22_BaleenHPRun3.eve'
    outFile = 'eve/ya22_BaleenHPRun3.eve'
    if os.path.exists(inFile):
    	data = readInput.readTable(inFile)
    	for row in data:
    		trigger = row[3]
    		time = row[1]
    		if (time == '71.100' and trigger == '6'):
    			print row
    			row[3] = 14
    			print row
    	writeOutput.writeTable(outFile,data)		

    #########################
    ##FIX RANDOM YA6 ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    ##NOt clear what this code was originally so changing to 600
    inFile= 'eve/ya6_BaleenHPRun3.eve'
    outFile = 'eve/ya6_BaleenHPRun3.eve'
    if os.path.exists(inFile):
    	data = readInput.readTable(inFile)
    	for row in data:
    		trigger = row[3]
    		time = row[1]
    		if (time == '163.827' and trigger == '6'):
    			print row
    			row[3] = 600
    			print row
    		writeOutput.writeTable(outFile,data)
    		
    #########################
    ##FIX RANDOM YA16 MASKEDMM ERROR
    ##This is only case where original .eve files gets changed, because somehow incorrect trigger got sent on a single trial (how could this happen? spooky)
    inFile= 'eve/ya16_MaskedMMRun1.eve'
    outFile = 'eve/ya16_MaskedMMRun1.eve'
    if os.path.exists(inFile):
    	data = readInput.readTable(inFile)
    	for row in data:
    		trigger = row[3]
    		time = row[1]
    		if (time == '232.349' and trigger == '2'):
    			print row
    			row[3] = 3
    			print row
    	writeOutput.writeTable(outFile,data)	
    
	##########################
	##FIX UNKNOWN YA3 LPRUN3 ERROR
	#Although no documentation to explain, vtsd logs and raw datafiles show run3 restarted after one minute (54 events), so responses to the stimuli that had already been shown should not be counted. 
	
    inFile = 'eve/ya3_BaleenLPRun3.eve'
    outFile = 'eve/ya3_BaleenLPRun3.eve'
    if os.path.exists(inFile):
    	data = readInput.readTable(inFile)
    	for i in range(1,54):
    		row = data[i]
    		row[3] = int(row[3])+900
    	writeOutput.writeTable(outFile,data)	
    	
    	
	##########################
	##FIX YA24 MASKEDMMRUN1 LOSS OF DISPLAY ERROR
	#Scan log notes that MaskedMMRun1 was ended early because display got disconnected. This probably happened several seconds before presentation was ended. The last moment at which the subject responded was 316.707, so to be conservative we are removing triggers in the 16 subsequent seconds, in which the subject did not respond to several probe trials
	
    inFile= 'eve/ya24_MaskedMMRun1.eve'
    outFile = 'eve/ya24_MaskedMMRun1.eve'
    if os.path.exists(inFile):
    	data = readInput.readTable(inFile)
    	for row in data:
    		trigger = row[3]
    		time = row[1]
    		if (float(time) > 316.707):
    			row[3] = int(row[3])+900
    			print row
    	writeOutput.writeTable(outFile,data)	
	
    	
    	
    	
    	
    	
    	
    #########################
    ##FIX TIMING IN ALL SCRIPTS###
    print '----fix Timing'
    for exp in expList:
        for run in runDict[exp]:
                inFile = 'eve/' + subjID + '_'+exp+run+'.eve'
                outFile = 'eve/' + subjID + '_' + exp + run + 'Mod.eve'
                print inFile
                if os.path.exists(inFile):
                    data = readInput.readTable(inFile)
                    
                    firstRow = data[0]
                    firstSample = firstRow[0]
                    firstTime = firstRow[1]
                    
                    wordCount = 0   ##for ATLLoc
                    flag = ''
                    
                    for row in data:
                        trigger = row[3]
                        time = row[1]   
                        sampleRate = float(row[0])/float(row[1])
                        trueSample = float(row[0]) + 19 ## compensate for the time lag between projector and trigger
                        trueTime = trueSample/sampleRate
                        row[0] = str(int(round(trueSample,0)))
                        row[1] = str(round(trueTime,3))
                        finalRow = row
                
                    ##Undo the timing change for the first row in file because this row indicates the beginning of the scan, not a visual event, so it shouldn't be changed
                        
                    firstRow[0] = firstSample
                    firstRow[1] = firstTime
    
                    ###add extra trigger to get around MNE bug that ignores last row
                    extraRow = [str(int(finalRow[0])+1),str(round(float(finalRow[1])+1,3)),'0','99']
                    data.append(extraRow)
                    writeOutput.writeTable(outFile,data)
        
        
    print '-----fix codes'    
    
        
        
    ###############################
    ###CHANGE CODES IN ATLLOC
    inFile = 'eve/' + subjID + '_ATLLocMod.eve'
    outFile = 'eve/' + subjID + '_ATLLocMod.eve'
    if os.path.exists(inFile):
        data = readInput.readTable(inFile)
        print inFile
        wordCount = 0
        flag = ''
        
        for row in data:
            trigger = row[3]
    
            if trigger == '1' or trigger == '2' or trigger == '3':
                flag = trigger
                wordCount = 0
            else: wordCount +=1
        
            if trigger == '4' and wordCount <= 9:
                row[3] = trigger+flag
        
        writeOutput.writeTable(outFile,data)
        
    ###############################################
    #MASKEDMM
    
    for x in runDict['MaskedMM']:
    	inFile = 'eve/'+subjID+'_MaskedMM'+x+'Mod.eve'
    	outFile = 'eve/'+subjID+'_MaskedMM'+x+'Mod.eve'
    	if os.path.exists(inFile):
    		data = readInput.readTable(inFile)
    		print inFile
    		
    		########################
    		##Change trigger for incorrect button presses ('go' responses)
    		rowCount = 0
    		for row in data:
    			trigger = row[3]
    			if (trigger == '1' or trigger == '2' or trigger == '3'):
    				nextRow = data[rowCount+1]
    				nextTrigger = nextRow[3]
    				#print 'hello', trigger, row, nextRow
    				if nextTrigger == '16' or nextTrigger == '32' or nextTrigger == '64' or nextTrigger == '128':
    					print "false positive: ", row, nextRow
    					row[3] = '500' + row[3]
    			rowCount +=1

    
    
    ###############################################
    #BALEENLP
    
    for x in runDict['BaleenLP']:
        inFile = 'eve/'+subjID+'_BaleenLP'+x+'Mod.eve'
        outFile = 'eve/'+subjID+'_BaleenLP'+x+'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile
            rowCount = 0
            for row in data:
                trigger = row[3]
                
                ##This part fixes the coding for the probe primes. Originally the trigger 11 was sent for the target when the prime was a probe. This recodes the target as 111 and codes the prime itself as 11
                if trigger == '11':   ##change the target to '111'
                    row[3] = '1'+trigger
                    #print row
                    
                    for i in [1,2,3]:                 ## look through the previous three events to try to find the prime
                        compRow = data[rowCount-i]
                        #print 'Comparison', compRow
        
                        compTrigger = compRow[3]
                        compSOA = float(row[1])-float(compRow[1])
                        if compTrigger == '14' and ( compSOA > .580 and compSOA < .620 ):  ##check for a prime with correct SOA
                            compRow[3] = trigger
                            #print 'new ', compRow, row
                            break
                    
                rowCount +=1
                
            ########################
            ##Change trigger for incorrect button presses ('go' responses)
            rowCount = 0
            for row in data:
                trigger = row[3]
                if (trigger == '1' or trigger == '2' or trigger == '4'):
                	nextRow = data[rowCount+1]
                	nextTrigger = nextRow[3]
                	#print row, nextRow
                	if nextTrigger == '16' or nextTrigger == '32' or nextTrigger == '64' or nextTrigger == '128':
                		print "false positive: ", row, nextRow
                		row[3] = '500' + row[3]
                rowCount +=1

            writeOutput.writeTable(outFile, data)

    ###############################################
    #BALEENHP
    
    for x in runDict['BaleenHP']:
        inFile = 'eve/'+subjID+'_BaleenHP'+x+'Mod.eve'
        outFile = 'eve/'+subjID+'_BaleenHP'+x+'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile
            rowCount = 0
            flag2 = 0
            for row in data:
                trigger = row[3]
                
                if trigger == '12':   ##change the target to '112'
                    row[3] = '1'+trigger
                    #print row
                    
                    for i in [1,2,3]:                 ## look through the previous three events to try to find the prime
                        compRow = data[rowCount-i]
                        #print 'Comparison', compRow
        
                        compTrigger = compRow[3]
                        compSOA = float(row[1])-float(compRow[1])
                        if compTrigger == '14' and ( compSOA > .580 and compSOA < .620 ):  ##check for a prime with correct SOA
                            compRow[3] = trigger
                            #print 'new ', compRow, row
                            break
                    
                ##Flip half of the related filler triggers to '18' to get equal number of related and unrelated
                if trigger == '8':
                    if flag2 == 1:
                        row[3] = '18'
                        flag2 = 0
                    elif flag2 == 0:
                        flag2 = 1   
                                            
                rowCount +=1          
            
            ########################
            ##Change trigger for incorrect button presses ('go' responses)
            rowCount = 0
            for row in data:
                trigger = row[3]

                if (trigger == '6' or trigger == '7' or trigger == '8' or trigger == '9' or trigger == '18'):
                	nextRow = data[rowCount+1]
                	nextTrigger = nextRow[3]
                	#print row, nextRow
                	if nextTrigger == '16' or nextTrigger == '32' or nextTrigger == '64' or nextTrigger == '128':
                		print "false positive: ", row, nextRow
                		row[3] = '500' + row[3]
                rowCount +=1
                		
                            
            ##########################################write it all out    
            writeOutput.writeTable(outFile, data)


    #########################   
    ###AXCPT
            
    for x in runDict['AXCPT']:
        x
        inFile = 'eve/'+subjID+'_AXCPT'+x+'Mod.eve'
        outFile = 'eve/'+subjID+'_AXCPT'+x+'Mod.eve'
        if os.path.exists(inFile):
            data = readInput.readTable(inFile)
            print inFile
            ###############################################################
            if subjID == 'ya6':  ####Fix error in triggers for this subject
                logFile = '../../vtsd_logs/ya6/AXCPT_ya6_List101_'+x+'.vtsd_log'
                print logFile
                logData = readInput.readTable(logFile)
                firstPrimeRow = data[2]
                firstPrimeTime = firstPrimeRow[1]       
                count = 0
                
                ##Fix timing due to error in trigger coding
                for row in data:
                    trueTime =  round(float(row[1]) - float(firstPrimeTime))
                    if row[3] == '8':
                        for logRow in logData:
                            logTime = round(float(logRow[5])+1)
                            if logTime == trueTime:
                                #print logRow[5], logTime, trueTime, row[3], logRow[9]
                                row[3] = logRow[9]
                                #print logRow
            ################################################################
    
            rowCount = 0
            flag3 = 0
            for row in data:
                trigger = row[3]
                if len(data) > rowCount +1:
                    nextRow = data[rowCount+1]
                    nextTrigger = nextRow[3]
                else:
                    break
        
                #############################################
                ##change blinks triggered as 6 to 7s
                
                if trigger == '6':   
                    if nextTrigger == '7':  
                            row[3] = '7'
                
##                #################################################
##                ##Flip 3/4 of the 'A' triggers to 15, to match number of As and Bs
##                if trigger == '5':
##                    if flag3 == 3:
##                        flag3 = 0
##                    elif flag3 != 3:
##                        flag3 = flag3 + 1   
##                        row[3] = '15'
##                                    
                ###########################################
                ##change triggers for incorrect trials#####
                
                ##AX case
                if trigger == '4': 
                     if len(nextTrigger) < 2:  ###Test for response (16, 32, 64 or 128)
                         row[3] = '9' + trigger
                         print 'miss:',row
                        
                ##BX, BY, AY case
                if (trigger == '1' or trigger == '2' or trigger == '3'):
                     
                     if len(nextTrigger) > 1:  ###Test for response (16, 32, 64 or 128)
                         row[3] = '9' + trigger
                         print 'false positive:',row
                             
                rowCount +=1
            
            
            writeOutput.writeTable(outFile,data)