def descriptives(dm): """ Tabular analysis of the descriptive results. Arguments: dm -- A DataMatrix. Returns: A DataMatrix. """ dmc = dm.select('correct == 1') pm = PivotMatrix(dm, ['soa'], ['subject_nr'], dv='ctoa') pm._print('CTOA') pm.save('output/ctoa.csv') pm = PivotMatrix(dm, ['subject_nr'], ['subject_nr'], dv=lambda x: len(x)) pm._print('Cell count') pm.save('output/cellcount.csv') pm = PivotMatrix(dm, ['cueValidity', 'soa', 'cueLum'], ['subject_nr'], \ dv=lambda x: len(x)) pm._print('Cell count') pm.save('output/cellcount.cueValidity.soa.cueLum.csv') pm = PivotMatrix(dmc, ['cueValidity', 'soa'], ['subject_nr'], \ dv='response_time') pm.save('output/rt.csv') pm._print('Correct RT') pm.linePlot(show=show) pm = PivotMatrix(dm, ['cueValidity', 'soa'], ['subject_nr'], \ dv='correct') pm.save('output/acc.csv') pm._print('Accuracy') pm.linePlot(show=show)
def ovpOtherFactor(dm, factor, rt = 'rtFromLanding',\ nRows = 2, nCols = 4, nBin = 5, ws = True): """ """ # Determine whether or not to first split data per participant # (i.e. to make within-subjects bins): if ws: keys = ['file', factor] else: keys = None # Determine exp: exp = dm["exp"][0] # Add factors containing bins: dm = dm.addField("binned_endX1") dm = dm.calcPerc("endX1Norm", "binned_endX1", \ keys = keys, nBin = nBin) if exp == "004A": dm = dm.addField("binned_endX1Corr") dm = dm.calcPerc("endX1CorrNorm", "binned_endX1Corr", \ keys = keys, nBin = nBin) fig = plt.figure() plt.subplots_adjust(wspace = .3, hspace = .4) figName = "%s: OVP effects as a function of binned landing positions and %s - WS = %s" \ % (exp, factor, ws) plt.suptitle(figName) plotCount = 0 subjectID = ["file"] if exp == "004A": factorList = [[factor,"binned_endX1"], [factor,"binned_endX1Corr"]] else: factorList = [[factor,"binned_endX1"]] for factors in factorList: for dv in [rt, "durationFix1", "refixProb", "saccCount"]: plotCount +=1 _dm = dm.select("%s != ''"%dv) plt.subplot(nRows, nCols, plotCount) plt.title(dv) pm = PivotMatrix(_dm, factors, subjectID, dv, colsWithin = True) pm.linePlot(fig = fig) plt.savefig("%s.png"%figName)
def plotFullModel(dm, sacc): """ """ fig = plt.figure(figsize = (15, 5)) plt.suptitle("Sacc = %s" % sacc) ax1 = plt.subplot(141) lCols = ["blue", "red", "orange", "yellow", "green", "pink"] xVar = "sacc%s_ex" % sacc yVar = "sacc%s_ey" % sacc dm = dm.select("%s != ''" % xVar) dm = dm.select("%s != ''" % yVar) dm = dm.select("%s != -1000" % xVar) dm = dm.select("%s != -1000" % yVar) for a in dm.unique("realAngle"): _dm = dm.select("realAngle == %s" % a) col = lCols.pop() plt.scatter(_dm[xVar], _dm[yVar], color = col, marker = ".", label = a) plt.axvline(constants.xCen, color = gray[3], linestyle = '--') plt.axhline(constants.yCen, color = gray[3], linestyle = '--') ax2 = plt.subplot(142) pm = PivotMatrix(dm, ["stim_type", "realAngle"], ["file"], "xNorm1", colsWithin =True) pm.linePlot(fig = fig) pm.save("PM.csv") pm._print() plt.axhline(0, color = gray[3], linestyle = "--") ax3 = plt.subplot(143) plt.title("object") dmObj= dm.select("stim_type == 'object'") #slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(stimObj["ecc"], stimObj["xNorm1"]) x = dmObj["ecc"] y = dmObj["xNorm%s" % sacc] fit = scipy.polyfit(x,y,1) fit_fn = scipy.poly1d(fit) plt.plot(x,y, 'yo', x, fit_fn(x), '--k') ax4 = plt.subplot(144) plt.title("non-object") dmNo= dm.select("stim_type == 'non-object'") #slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(stimObj["ecc"], stimObj["xNorm1"]) x = dmNo["ecc"] y = dmNo["xNorm%s" % sacc] fit = scipy.polyfit(x,y,1) fit_fn = scipy.poly1d(fit) plt.plot(x,y, 'yo', x, fit_fn(x), '--k') plt.savefig("./plots/Full_model_004C_sacc%s.png" % sacc) plt.show()
def affCross(trim = True, exclY = False, dv = "RT"): """ Affordance effect as cross-over interaction between handle and response hand ACROSS saccades NOTE: on-object is difficult here, because we're not looking across saccades. """ fig = plt.figure() title = "Affordance effect per Exp - exclY = %s" % exclY plt.suptitle(title) plotNr = 1 for exp in ["004A", "004B"]: if exp != "004B": continue plt.subplot(1,2,plotNr) plt.title("Exp = %s" % exp) dm = getDM.getDM(exp) dm = onObject.onObject(dm, "1", exclY = exclY) if trim: trimmed_dm = dm.selectByStdDev(keys = \ ["handle_side", "response_hand", "file"], dv = dv) else: trimmed_dm = dm am = AnovaMatrix(trimmed_dm, ["handle_side", "response_hand"], dv = dv, \ subject = "file")._print(ret=True) print am # raw_input() # Get descriptive statistics: pm = PivotMatrix(trimmed_dm, ["comp"], ["file"], colsWithin = False, \ dv = dv, err = 'se') print pm raw_input() pm = PivotMatrix(trimmed_dm, ["handle_side", "response_hand"], \ ["file"], dv = dv, colsWithin = True) pm.linePlot(fig = fig) plotNr +=1 plt.savefig("%s.png"%title)
def lumBinAn(dm, nBin = 8): """ """ dm = latBins(dm) pm = PivotMatrix(dm, ['mask_side', 'saccLatBin'], ['file'], dv='endX', colsWithin=False) pm._print() xLabels = range(1,nBin+1) xLabel = "binned latencies" lLabel = "contrast manipulation" lLabels = ["control", "more contrast right", "more contrast left"] #lLabels = None yLabel = "landing position - negative = left, positive = right" pm.linePlot(show=True,xLabels = xLabels, xLabel = xLabel, yLabel = yLabel, legendTitle = lLabel, lLabels = lLabels) plt.axhline(0, color = "grey", linestyle = "--")
def affordanceEffect(dm, nRows = None, nCols = None, \ rowCount = None, fig = None): """ RT as a function of handle side and response handle Note: this two-way interaction should be identical to a main effect of compatibility. Arguments: dm Keyword arguments: nRows nCols rowCount fig """ # Select only handled objects: dm = dm.select("symm == 'asymm'") # Select only trials on which there was no mask applied: dm = dm.select("contrast_side == 'control'") #fig = plt.figure() nPlot = 0 factors = ["response_hand", "handle_side"] pp = ["file"] dv = "rtFromLanding" # Trim: _dm = dm.selectByStdDev(keys = factors + pp, dv = dv) am = AnovaMatrix(_dm, factors = factors, dv = dv, \ subject = pp[0])._print(maxLen=10, ret=True) plt.subplot2grid((nRows,nCols), (rowCount, 0)) pm = PivotMatrix(_dm, factors, pp, dv = dv, colsWithin = True) pm.linePlot(fig = fig, legendTitle = factors[0], xLabel = factors[1]) plt.subplot2grid((nRows,nCols), (rowCount, 1), colspan=2) plt.text(0.1,0.1,am, family='monospace')
def affordances(trim = True, exclY = True, dv = "RT"): """ """ fig = plt.figure() title = "Affordance effect per Exp" plt.suptitle(title) plotNr = 1 for exp in ["004A", "004B"]: plt.subplot(1,2,plotNr) plt.title("Exp = %s" % exp) dm = getDM.getDM(exp) trimmed_dm = dm.selectByStdDev(keys =["handle_side", "response_hand", "file"], dv = dv) for val in trimmed_dm.unique("comp"): print val print trimmed_dm["RT"][trimmed_dm.where("comp == '%s'" % val)].mean() pm = PivotMatrix(trimmed_dm, ["handle_side", "response_hand"], ["file"], dv = dv, colsWithin = True) pm.linePlot(fig = fig) plt.show() pm = PivotMatrix(trimmed_dm, ["comp"], ["file"], dv = dv, colsWithin = True) pm.plot(nLvl1 = 1, fig = fig) plt.show() plotNr +=1 ###am = AnovaMatrix(trimmed_dm, ["comp"], dv = dv, \ ###subject = "file")._print(ret=True) ###print am plt.show()
def contrastEffect(dm, saccCount): """ Plots dist hist of sacc latencies as a function of gap condition. Arguments: dm saccCount """ # Define keys: factors = ["contrast_side", "gap"] pp = ["file"] dv = "saccLat%s" % saccCount exp = dm["exp"][0] # Apply selections: dm = dm.select("%s != ''" % dv) dm = dm.selectByStdDev(keys = factors + pp, dv = dv) # Create figure: fig = plt.figure() plt.subplots_adjust(hspace = .4) figName = "%s: Latencies sacc %s as a function of Gap and Contrast" % (exp, saccCount) plt.suptitle(figName) plt.subplot2grid((2,2), (0, 0)) pm = PivotMatrix(dm, factors, pp, dv, colsWithin = True) pm.linePlot(fig = fig,xLabel = factors[-1], legendTitle = factors[0], lLabels = ["left", "contrast", "right"]) # NOTE why I have to give the lLabels as a parameter: the underscore of "_left" gives problems for plotting ## the legend.?? plt.ylabel(dv) am = AnovaMatrix(dm, factors = factors, dv = dv, \ subject = pp[0])._print(maxLen=10, ret=True) plt.subplot2grid((2,2), (1, 0), colspan=2) plt.text(0.1,0.1,am, family='monospace') plt.savefig("%s.png"%figName) return fig
def affOtherFactor(dm, factor2, nRows = None, nCols = None, \ rowCount = None, fig = None): """ RT as a function of compatibility and something else (e.g. category). Arguments: dm factor2 """ # Declare keys: factors = ["comp"] + [factor2] pp = ["file"] dv = "rtFromLanding" # Select only handled objects: dm = dm.select("symm == 'asymm'") # Trim: dm = dm.selectByStdDev(keys = factors + pp, dv = dv) # Create figure: #fig = plt.figure() nPlot = 0 # Stats: am = AnovaMatrix(dm, factors = factors, dv = dv, \ subject = pp[0])._print(maxLen=10, ret=True) # Plot: plt.subplot2grid((nRows, nCols), (rowCount, 0)) pm = PivotMatrix(dm, factors, pp, dv = dv, colsWithin = True) pm.linePlot(fig = fig, lLabels = None,legendTitle = factors[0], xLabel = factors[1]) plt.subplot2grid((nRows,nCols), (rowCount, 1), colspan=2) plt.text(0.1,0.1,am, family='monospace')
def gapEffect(dm, saccCount = "1"): """ Plots dist hist of sacc latencies as a function of gap condition. Arguments: dm Keyword arguments: saccCount --- Default = "1" """ # Define keys: factors = ["visual_field", "gap"] pp = ["file"] dv = "saccLat%s" % saccCount exp = dm["exp"][0] # Apply selections: dm = dm.select("%s != ''" % dv) dm = dm.selectByStdDev(keys = factors + pp, dv = dv) # Create figure: fig = plt.figure() plt.subplots_adjust(hspace = .4) figName = "%s: Latencies sacc %s as a function of Gap and Visual Field" % (exp, saccCount) plt.suptitle(figName) nPlots = 0 for vf in ["upper", "lower"]: vf_dm = dm.select("visual_field == '%s'" % vf) #plt.subplot(nRows, nCols, nPlots) plt.subplot2grid((3,2), (nPlots, 0)) for gap in ["zero", "overlap"]: if gap == "zero": col = "#ef2929" else: col = "#8ae234" plt.title("%s visual field" % vf) gap_dm = vf_dm.select("gap == '%s'" % gap) plt.hist(gap_dm[dv], bins = 30, color = col, alpha = .3) plt.xlim(0, 400) plt.ylabel('freq') plt.xlabel('initial saccade latencies') if vf == "lower": plt.legend(["zero", "overlap"], loc = 'best') nPlots +=1 plt.subplot2grid((3,2), (0, 1), rowspan=2) pm = PivotMatrix(dm, factors, pp, dv, colsWithin = True) pm.linePlot(fig = fig) plt.ylabel("saccade latency") am = AnovaMatrix(dm, factors = factors, dv = dv, \ subject = pp[0])._print(maxLen=10, ret=True) plt.subplot2grid((3,2), (2, 0), colspan=2) plt.text(0.1,0.1,am, family='monospace') plt.savefig("%s.png"%figName)
def twoWayHandle(dm, dv, factor2, onlyControl = True, addStats = True, figTitle = True): """ Plots landing positions as a function of gap condition. Arguments: dm dv --- dv, should be a landing variable factor2 -- second IV """ if onlyControl and factor2 == "contrast_side": print "You can't exclude contrast manipulation if one of the IV's is contrat_side" return if onlyControl: dm = dm.select("contrast_side == 'control'") # Define keys: factors = [factor2, "handle_side"] pp = ["file"] exp = dm["exp"][0] if addStats: nCols = nRows = 2 fig = plt.figure() else: nCols = nRows = 1 fig = plt.figure(figsize = (3,5)) # Apply selections: dm = dm.select("%s != ''" % dv) dm = dm.selectByStdDev(keys = factors + pp, dv = dv) # Create figure: plt.subplots_adjust(hspace = .4) figName = "%s: %s as a function of %s and Handle Side onlyControl = %s" % (exp, dv, factor2, onlyControl) if figTitle: plt.suptitle(figName) plt.subplot2grid((nRows,nCols), (0, 0)) pm = PivotMatrix(dm, factors, pp, dv, colsWithin = True) # Note why I have to give the lLabels as a parameter: the underscore of "_left" gives problems for plotting # the legend.?? if factor2 == "contrast_side": lLabels = ["left", "contrast", "right"] else: lLabels = None pm.linePlot(fig = fig,xLabel = factors[-1], legendTitle = factors[0], lLabels = lLabels) plt.ylabel(dv) plt.axhline(0, color = "#555753", linestyle = "--", linewidth = 2) if addStats: am = AnovaMatrix(dm, factors = factors, dv = dv, \ subject = pp[0])._print(maxLen=10, ret=True) plt.subplot2grid((nRows,nCols), (1, 0), colspan=2) plt.text(0.1,0.1,am, family='monospace') plt.savefig("%s.png"%figName) return fig
dm = dm.select("direction == 0") dm = dm.addField("congruency", dtype = str) dm = dm.addField("respHand", dtype = str) dm["respHand"][dm.where("correct_response == 1")] = "left" dm["respHand"][dm.where("correct_response == 2")] = "right" for i in dm.range(): handle = dm["flip"][i] respHand = dm["respHand"][i] if handle == respHand: dm["congruency"][i] = "congruent" print "congruent" else: dm["congruency"][i] = "incongruent" print "incongruent" print "flip = ", dm["flip"][i] print "correct response = ", dm["correct_response"][i] print "response hand = ", dm["respHand"][i] print "congruency = ", dm["congruency"][i] pm = PivotMatrix(dm, ["congruency", "stim_type"], ["file"], "response_time", colsWithin = True) pm.linePlot() plt.show()
def threeFactor(dm, dv, factors, subjectID = ["file"], trim = True, printStats = True, showFig=False, saveFig = True, figName = None, saveExt = ".jpg", legendTitle = None, lLabels = None, xLabel = None, xLabels = None, yLabel = None,exControlMask = False, exFiller = False, \ yLim = None): """ Performs and plots a three-factorial ANOVA dm --- Data matrix. dv --- Dependent variable. factors --- List containing three factor identifiers. Note that the first factor in the list is used to split the effects per subplot. Keyword arguments: subjectID --- Default = ["file"] showFig --- Boolean indicating whether or not to show the plots. Default = False. saveFig --- Boolean indicating whehter or not to save the plot. Default = True. printStats --- TODO Boolean indicating whether or not to print stats. figName --- Will be used as suptitle and save path, of provided. Default = None. saveExt --- legendTitle --- lLabels --- xLabel --- xLabels --- yLabel --- """ # Check whether 'factor' and 'dv' are column headers in the spread sheet: vNames = dm.asArray()[0] for factor in factors: if not factor in vNames: msg.userMsg.userMsg("The factor %s is not a column header."%factor,\ factor, __file__) return if not dv in vNames: msg.userMsg.userMsg("The dv %s is not a column header."%dv, __file__) return # Apply selection criteria: if exControlMask: dm = dm.select("mask_side != 'control'") if exFiller: dm = dm.select("symm == 'asymm'") driftCorr = dm["driftCorr"][0] if trim: dm = dm.selectByStdDev(keys = factors + subjectID, dv = dv) # Get anova matrix: am = AnovaMatrix(dm, factors = factors, dv = dv, subject = \ subjectID[0])._print(maxLen=8,ret=True) if printStats: print am # One of the factors is used to split the dm (for plotting purposes only, # the anova is carried out 'correctly'). splitFactor = factors[0] twoFactors = factors[1:] lvlList = np.unique(dm[splitFactor]) # Create the figure: fig = plt.figure() name = "%s by %s by %s effect on %s - Trim = %s exControlMask = %s exFiller = %s driftCorr = %s" %(factors[0], factors[1], \ factors[2], dv, trim, exControlMask, exFiller, driftCorr) plt.suptitle(name) nRows = 1 nCols = len(lvlList)+1 # 1 for the stats plotNr = 0 # Change the labels if one of the factors is 'mask_side': if "mask_side" == factors[1]: if "control" in np.unique(dm["mask_side"]): lLabels = ["control", "more contrast right", "more contrast left"] else: lLabels = ["more contrast right", "more contrast left"] if "mask_side" == factors[2]: if "control" in np.unique(dm["mask_side"]): xLabels = ["control", "more contrast right", "more contrast left"] else: xLabels = ["more contrast right", "more contrast left"] if "mask_side" == factors[0]: if "control" in np.unique(dm["mask_side"]): titles = ["control", "more contrast right", "more contrast left"] else: titles = ["more contrast right", "more contrast left"] else: titles = lvlList # Convert the list of titles to a list instead of a np.array: #titles = list(titles) # Reverse (for popping) popTitles = list(titles)[::-1] # Create the subplots: for lvl in lvlList: # Select one level: _dm = dm.select('%s == "%s"' % (splitFactor, lvl)) # Get pivot matrix: pm = PivotMatrix(_dm, twoFactors, subjectID, dv = dv, colsWithin=True) # Plot: plotNr +=1 if legendTitle == None: legendTitle = twoFactors[0] if xLabel == None: xLabel = twoFactors[1] if yLabel == None: yLabel = dv plt.subplot(nRows, nCols, plotNr) #title = popTitles.pop() #print title plt.title(popTitles.pop()) if lvl != lvlList[-1]: legendPos = None else: legendPos = 'best' pm.linePlot(fig = fig, xLabel = xLabel, xLabels = xLabels, \ legendTitle = legendTitle, lLabels = lLabels, yLabel = yLabel, \ legendPos = legendPos) plt.ylim(yLim) if dv in ["endX", "endXCorr", "endXCorrMask", "endXDegr","endXCorrDegr", "endXCorrMaskDegr"]: plt.axhline(0, linestyle = "--", color = "#555753") # Plot stats: statSubplot(am, nRows = nRows, nCols = nCols, plotNr = nCols) if showFig: plt.show() saveFig = False if saveFig: plt.savefig(os.path.join(outputFolder,"%s%s" % (name, saveExt)))
def twoFactor(dm, dv, factors, subjectID = ["file"], trim = True, printStats = True, showFig=False, saveFig = True, figName = None, saveExt = ".jpg", legendTitle = None, lLabels = None, xLabel = None, xLabels = None, yLabel = None,exControlMask = False, exFiller = False, \ mixedEffects = False, exMask = False, yLim = None, errBar = True): """ Performs and plots a two-factorial ANOVA. Arguments: dm --- Data matrix. dv --- Dependent variable. factors --- List containing two factor identifiers. Keyword arguments: subjectID --- Default = ["file"] showFig --- Boolean indicating whether or not to show the plots. Default = False. saveFig --- Boolean indicating whehter or not to save the plot. Default = True. printStats --- TODO Boolean indicating whether or not to print stats. figName --- Will be used as suptitle and save path, of provided. Default = None. saveExt --- legendTitle --- lLabels --- xLabel --- xLabels --- yLabel --- """ # Check whether 'factor' and 'dv' are column headers in the spread sheet: vNames = dm.asArray()[0] for factor in factors: if not factor in vNames: msg.userMsg.userMsg("The factor %s is not a column header."%factor,\ factor, __file__) return if not dv in vNames: msg.userMsg.userMsg("The dv %s is not a column header."%dv, __file__) return driftCorr = dm["driftCorr"][0] # If wanted, trim the data: if trim: dm = dm.selectByStdDev(keys = factors + subjectID, dv = dv) # Apply selection criteria: if exControlMask: dm = dm.select("mask_side != 'control'") if exMask: dm = dm.select("mask_side == 'control'") if exFiller: dm = dm.select("symm == 'asymm'") # Get pivot matrix: pm = PivotMatrix(dm, factors, subjectID, dv=dv, colsWithin=True) # Get anova matrix: am = AnovaMatrix(dm, factors = factors, dv = dv, \ subject = subjectID[0])._print(maxLen=10, ret=True) if mixedEffects: mem = MixedEffectsMatrix(dm, dv, factors, subjectID)._print(maxLen=10, ret=True) if printStats: if mixedEffects: print "MIXED EFFECTS:" print mem print "ANOVA:" print am # Create figure: fig = plt.figure() if figName == None: name = "%s by %s effect on %s - trim = %s exControlMask = %s exMask = %s exFiller = %s driftCorr = %s"%(\ factors[0], factors[1], dv, trim, exControlMask, exMask, exFiller, driftCorr) else: name = figName # Determine some plot properties: if yLabel == None: yLabel = dv if legendTitle == None: legendTitle = factors[0] if xLabel == None: xLabel = factors[1] # Change the labels if one of the factors is 'mask_side': if "mask_side" == factors[0]: if "control" in np.unique(dm["mask_side"]): lLabels = ["control", "more contrast right", "more contrast left"] else: lLabels = ["more contrast right", "more contrast left"] if "mask_side" == factors[1]: if "control" in np.unique(dm["mask_side"]): xLabels = ["control", "more contrast right", "more contrast left"] else: xLabels = ["more contrast right", "more contrast left"] # Plot effect: plt.subplot(121) plt.suptitle(name) pm.linePlot(fig = fig, legendTitle = legendTitle, xLabel = xLabel, \ lLabels = lLabels, xLabels = xLabels, yLabel = yLabel, yLim = yLim, \ errBar = errBar) if dv in ["endX", "endXCorr", "endXCorrMask", "endXDegr","endXCorrDegr", "endXCorrMaskDegr"]: plt.axhline(0, linestyle = "--", color = "#555753") # Print the stats: if mixedEffects: statSubplot(mem) else: statSubplot(am) if showFig: plt.show() saveFig = False if saveFig: plt.savefig(os.path.join(outputFolder,"%s%s"%(name,saveExt)))