def anova(self, model1, model2, anovaVar="aov1"): """ Performs a model comparison using the R `anova()` function. The models should be fitted and given a name first, using `RBridge.lmer()`. Arguments: model1 -- The name of the first model. model2 -- The name of the first model. Keyword arguments: anovaVar -- The R variable to store the `anova()` output in. (default='aov1') Returns: A DataMatrix with the output for the model comparison. """ if os.path.exists(".rbridge-anova.csv"): os.remove(".rbridge-anova.csv") self.write("%s <- anova(%s, %s)" % (anovaVar, model1, model2)) self.call('write.csv(%s, ".rbridge-anova.csv")' % anovaVar) while not os.path.exists(".rbridge-anova.csv"): time.sleep(0.1) # Try this a few times, because sometimes the csv hasn't been written # yet for i in range(10): try: dm = CsvReader(".rbridge-anova.csv").dataMatrix() break except: time.sleep(1) dm.rename("f0", "model") return dm
def plotRegr(exp): """ Plots data for one experiment. Arguments: exp --- {"004A", "004B"} """ src = 'selected_dm_%s_WITH_drift_corr_onlyControl_False.csv' % exp dm = CsvReader(src).dataMatrix() R = RBridge() fig = plt.figure(figsize = (8,3)) plt.subplots_adjust(left=.2, bottom=.15) # Plot hline: plt.axhline(0, color = "black", linestyle = "--") legend = True for sacc in [1, 2]: colList = [green[1], orange[1], blue[1]] for contrast in dm.unique("contrast_side"): print "contrast side = ", contrast print "sacc = ", sacc _dm = dm.select("contrast_side == '%s'" % contrast) lmeRegr(R, _dm, sacc=sacc, corr=False, \ color = colList.pop(),stats=False, legend = contrast.strip("_")) if legend: plt.legend(frameon = False, loc='best') legend = False plt.savefig("Timecourse_contrast_per_cond%s.png" % exp)
def __init__(self, path='data', ext='.csv', delimiter=',', quote='"', maxN=None): """ desc: Constructor. Reads all csv files from a specific folder. keywords: path: desc: The folder containing the csv files. type: [str, unicode] ext: desc: The extension of the csv files, or None to parse all files. type: [str, unicode, NoneType] delimiter: desc: The column delimiter. type: [str, unicode] quote: desc: The character used for quoting columns. type: [str, unicode, NoneType] maxN: desc: The maximum number of files to process, or `None` to process all. type: [int, NoneType] """ print 'Scanning \'%s\'' % path self.dm = None l = sorted(os.listdir(path)) if maxN != None: l = l[:maxN] for fname in l: if ext == None or os.path.splitext(fname)[1] == ext: print 'Reading %s ...' % fname, try: cr = CsvReader(os.path.join(path, fname), delimiter=delimiter, quote=quote) except: warnings.warn('Failed to read %s' % fname) continue dm = cr.dataMatrix() while '__src__' not in dm.columns(): try: # Apparently this can go wrong sometimes, presumably # due to a low-level bug. It appears to be safe to just # try again. dm = dm.addField('__src__', dtype=str) except: print 'Trying again ...' dm['__src__'] = fname if self.dm == None: self.dm = dm else: self.dm += dm print '(%d rows)' % len(dm)
def crossExpDescriptives(dm): for exp in ['exp1', 'exp2', 'exp3']: dm = CsvReader('data/%s.data.csv' % exp).dataMatrix() if exp == 'exp1': dm = dm.select('trialType == "control"') elif exp == 'exp3': print dm.collapse(['cond'], 'rt') stats.R.load(dm) lm = stats.R.lmer('rt ~ cond + (1+cond|subject_nr)') print lm rt = dm['rt'] print 'Exp = %s' % exp print 'N = %d' % len(rt) print 'RT = %.2f ms (%.2f)' % (rt.mean(), rt.std()) a = np.loadtxt('data/%s.fixdur.csv' % exp) print 'Fixdur = %.2f ms (%.2f)' % (a.mean(), a.std())
def addCog(dm): """ Use NEW CoG calculation. """ # Get cog dictionary: f = "compare cogs/cog_per_stim_004C.csv" cogDm = CsvReader(f).dataMatrix() dm = dm.addField("xCog", default = -1000) for i in dm.range(): stimName = dm["object"][i] flip = dm["handle_side"][i] symm = dm["symm"][i] if symm == "symm": continue xCogUnrot = cogDm.select("name == '%s'" % stimName, verbose = False)["xCog"][0] if flip == "left": xCog = xCogUnrot * -1 elif flip == "right": xCog = xCogUnrot dm["xCog"][i] = xCog #print "object = ", stimName #print "symm = ", symm #print "flip = ", flip #print "cog = ", xCog return dm
# Constants: fName = "/home/lotje/Documents/PhD Marseille/Studies/004 - Single-object experiment - Handle-orientation effect/analysis 004/simulation/data.csv" heavyHandle = ["chisel", "screwdriver", "sharpeningsteel"] printSum = False if __name__ == "__main__": # Get dictionary containing w and h of imaginary boxes around the stimuli: boxDict = drawBox.boxDict() # OpenSesame output file is opened as dm. The fixation values will # be added to this dm. dm = CsvReader(fName, delimiter = ",").dataMatrix() # The varialbe 'corrDirection' is determined in ascParser, # and doesn't exist for the simulation data. # Therefore we add it manually: dm = dm.addField("corrDirection", dtype = str) # Make new column headers: for sacc in range(1,4): # Landing position: dm = dm.addField('endX%s' % sacc, dtype=str) dm = dm.addField('endY%s' % sacc, dtype = str) # Normalised landing position: dm = dm.addField('endX%sNorm' % sacc, dtype = str)
the analyses of experiment 1 and 2. """ # Import modules: from exparser.CsvReader import CsvReader # Constants: xc = 1024/2 yc = 768/2 ythr = 100 if __name__ == "__main__": dm = CsvReader('data.csv').dataMatrix() dm = dm.select('mask_side == "control"') dm = dm.select('symm == "asymm"') #dm = dm.select('gap == "overlap"') #dm = dm.select('handle_side == "right"') #dm = dm.select('object == "screwdriver"') # X coordinates, where positive values indicate a deviation towards the handle dm = dm.addField('refix1', dtype=float) dm = dm.addField('refix2', dtype=float) dm = dm.addField('refix3', dtype=float) # Timestamps for each fixation dm = dm.addField('tfix1', dtype=float) dm = dm.addField('tfix2', dtype=float)
def refixLaunch(exp,corr=False, trim=True, bins=8): """ """ src = 'selected_dm_%s_WITH_drift_corr_onlyControl_True.csv' % exp dm = CsvReader(src).dataMatrix() lat1 = "saccLat1" lat2 = "saccLat2" if corr: fix1 = "endX1CorrNormToHandle" fix2 = "endX2CorrNormToHandle" else: fix1 = "endX1NormToHandle" fix2 = "endX2NormToHandle" for var in [lat1, lat2, fix1, fix2]: dm = dm.select("%s != ''" % var) # Trim the data if trim: dm = dm.selectByStdDev(["file"],lat2, verbose=False) dm = dm.removeField("__dummyCond__") dm = dm.removeField("__stdOutlier__") dm = dm.selectByStdDev(["file"], fix2, verbose=False) dm = dm.addField('saccLat2_perc', dtype=float) dm = dm.calcPerc(lat2, 'saccLat2_perc', nBin=bins) cmLat1 = dm.collapse(['saccLat2_perc'], lat1) cmX = cmLat2 = dm.collapse(['saccLat2_perc'], lat2) cmFix1 = dm.collapse(['saccLat2_perc'], fix1) cmFix2 = dm.collapse(['saccLat2_perc'], fix2) fig = plt.figure(figsize = (3,6)) plt.subplots_adjust(left=.2, bottom=.15) colList = [orange[1], blue[1]] nRows = 3 nCols = 1 nPlot = 0 lTitles = ["Landing pos 2", "Landing pos 1"] lTitles.reverse() for cmY in [cmFix2, cmFix1]:#, cmLat1]: #nPlot +=1 #plt.subplot(nRows, nCols, nPlot) color = colList.pop() plt.plot(cmX['mean'], cmY['mean'], marker = 'o', color=color, \ markerfacecolor='white', markeredgecolor=color, \ markeredgewidth=1) plt.xlabel("Sacc lat 2") plt.legend(lTitles, frameon=False, loc='best') plt.axhline(0, linestyle = "--", color = gray[3]) plt.savefig("Launch_site_refixations_%s_corr_%s.png" % (exp, corr))
def plotContrast(exp = "004B", trim=True, inclSim=True,stats=True,norm=False): """ Plots landing positions as a function of contrats manipulation per saccade, for Exp 1 and Exp 2, relative to CoG. Keyword arguments: trim --- (default=True) """ # Get dm: src = 'selected_dm_%s_WITH_drift_corr_onlyControl_False.csv' % exp dm = CsvReader(src).dataMatrix() colList = ["#f57900", "#3465a4"] lLegend = ["Saccade 1", "Saccade 2"] fig = plt.figure(figsize = (3,4)) plt.subplots_adjust(left=.2, bottom=.15) yLim = [-.07, .12] for sacc in ["1", "2"]: dv = "endX%sNorm" % sacc saccVar = "saccLat%s" % sacc # Get dm: # Only on-object: dm = onObject.onObject(dm, sacc,verbose=False) if trim: dm = dm.removeField("__dummyCond__") dm = dm.removeField("__stdOutlier__") dm = dm.selectByStdDev(keys = ["file"], \ dv = dv,verbose=False) dm = dm.removeField("__dummyCond__") dm = dm.removeField("__stdOutlier__") dm = dm.selectByStdDev(keys = ["file"], dv = saccVar,\ verbose=False) # Collect mean and error bar per saccade: lM = [] lErr = [] if norm: # Normalize across handle side: dm = dm.removeField("normDV") dm= dm.addField("normDV", dtype = float) dm= dm.withinize(dv, "normDV", \ ["handle_side"], whiten=False) dv = "normDV" for contrast in dm.unique("contrast_side"): contrast_dm = dm.select("contrast_side == '%s'" % contrast,\ verbose = False) cm = contrast_dm.collapse(["file"], dv) M = cm["mean"].mean() SE = cm['mean'].std() / np.sqrt(len(cm)) CI = SE * critVal lM.append(M) lErr.append(CI) if stats: # Run a full LME print "Exp = ", exp print "DV = ", dv print "trim = ", trim print "norm = ", norm print "saccVar = ", saccVar lmeContrast(R, dm, dv, saccVar, exp=exp) #raw_input() xData = range(len(lM)) yData = lM yErr = lErr col = colList.pop() plt.errorbar(xData, yData, yerr=yErr, fmt='o-', marker = "o", \ color = col, markerfacecolor='white', markeredgecolor=col, \ markeredgewidth=1) plt.axhline(0, linestyle = "--") plt.ylim(yLim) plt.ylabel(yTitle) #ax.yaxis.set_ticklabels([]) plt.legend(lLegend, frameon = False) plt.axhline(0, color = "black", linestyle = "--") plt.xlabel("High-contrast side") spacing = 0.5 xTicks = range(0,3) xLabels = ["Left", "Control", "Right"] plt.xticks(xTicks, xLabels, rotation = .5) plt.xlim(min(xTicks)-spacing, max(xTicks)+spacing) plt.savefig(os.path.join(dst,"Contrast_Effect_%s_trim_%s_norm_%s.png") \ % (exp, trim, norm)) plt.show()