Example #1
0
def corrExample(dm, soaBehav, soaPupil, dv='correct', suffix=''):

	"""
	Plots an example of the correlation between behavior and pupil size at the
	most strongly correlating point.

	Arguments:
	dm			--	A DataMatrix.
	soaBehav	--	The SOA to analyze for the behavioral effect.
	soaPupil	--	The SOA to analyze for the pupil effect.

	Keyword arguments:
	dv			--	The dependent variable to use for the behavioral effect.
					(default='correct')
	suffix		--	A filename suffix.
	"""

	assert(soaPupil in dm.unique('soa'))
	assert(soaBehav in dm.unique('soa'))
	if stripCorr:
		suffix += '.stripCorr'
	a = corrTrace(dm, soaBehav, soaPupil, dv='correct', suffix='acc', \
		cacheId='corrTrace.correct.%d.%d%s' % (soaBehav, soaPupil, suffix))
	bestSample = np.argmax(a[:,0])
	dmBehav = dm.select('soa == %d' % soaBehav)
	dmPupil = dm.select('soa == %d' % soaPupil)
	xData = []
	yData = []
	print 'pp\tbehav\tpupil'
	for subject_nr in dm.unique('subject_nr'):
		ceb = cuingEffectBehav(dmBehav.select('subject_nr == %d' \
			% subject_nr, verbose=False), dv=dv)
		cep = cuingEffectPupil(dmPupil.select('subject_nr == %d' \
			% subject_nr, verbose=False), epoch=(bestSample, \
			bestSample+winSize))
		print '%.2d\t %.4f\t%.4f' % (subject_nr, ceb, cep)
		yData.append(100.*ceb)
		xData.append(cep)
	if stripCorr:
		index, xData, yData = stripStd(np.array(xData), np.array(yData))
	Plot.new(size=(3,3))
	plt.title('SOA: %d ms (behavior), %d ms (pupil)' % (soaBehav+55, \
		soaPupil+55))
	Plot.regress(xData, yData)
	plt.text(0.05, 0.90, 'Sample = %d' % bestSample, ha='left', \
			va='top', transform=plt.gca().transAxes)
	plt.axhline(0, linestyle='--', color='black')
	plt.axvline(0, linestyle='--', color='black')
	plt.plot(xData, yData, 'o', color='black')
	plt.ylabel('Behav. cuing effect (%)')
	plt.xlabel('Pupil cuing effect (norm.)')
	Plot.save('corrExample.%d.%d%s' % (soaBehav, soaPupil, suffix),
		'corrAnalysis', show=show)
Example #2
0
def prepFit(dm, suffix=''):

	"""
	Perform a linear-regression fit on only the first 220 ms.

	Arguments:
	dm		--	DataMatrix

	Keyword arguments:
	suffix	--	A suffix for the output files. (default='')
	"""

	fig = newFig(size=bigWide)
	plt.subplots_adjust(wspace=0, hspace=0)
	i = 0

	l = [['subjectNr', 'sConst', 'iConst', 'sOnset', 'iOnset', 'sSwap',
		'iSwap']]

	for dm in [dm] + dm.group('subject_nr'):

		print 'Subject %s' % i
		row = [i]

		# We use a 6 x 2 plot grid
		# XX X X X X
		# XX X X X X
		if i == 0:
			# Overall plot
			ax = plt.subplot2grid((2,6),(0,0), colspan=2, rowspan=2)
			linewidth = 1
			title = 'Full data (N=%d)' % len(dm.select('cond != "swap"'))
		else:
			# Individual plots
			ax = plt.subplot2grid((2,6),((i-1)/4, 2+(i-1)%4))
			linewidth = 1
			title = '%d (N=%d)' % (i, len(dm.select('cond != "swap"')))

		plt.text(0.9, 0.1, title, horizontalalignment='right', \
			verticalalignment='bottom', transform=ax.transAxes)

		if i == 0:
			plt.xticks([0, 50, 100, 150, 200])
		elif i > 0 and i < 5:
			plt.xticks([])
		else:
			plt.xticks([0, 100])
		if i > 0:
			plt.yticks([])
		else:
			plt.yticks([0, -.01, -.02])
		plt.ylim(-.025, .01)
		plt.axhline(0, color='black', linestyle=':')
		for cond in ('constant', 'onset', 'swap'):
			_dm = dm.select("cond == '%s'" % cond, verbose=False)
			dmWhite = _dm.select('saccCol == "white"', verbose=False)
			xAvg, yAvg, errAvg= TraceKit.getTraceAvg(_dm, signal='pupil', \
				phase='postSacc', traceLen=postTraceLen, baseline=baseline, \
				baselineLock=baselineLock)
			xWhite, yWhite, errWhite = TraceKit.getTraceAvg(dmWhite, \
				signal='pupil', phase='postSacc', traceLen=postTraceLen, \
				baseline=baseline, baselineLock=baselineLock)
			yWhite -= yAvg
			xWhite = xWhite[:prepWin]
			yWhite = yWhite[:prepWin]
			if cond == 'constant':
				col = constColor
				lineStyle = '-'
			elif cond == 'onset':
				col = onsetColor
				lineStyle = '--'
			else:
				col = swapColor
				lineStyle = '-.'
				yWhite *= -1
			opts = Plot.regress(xWhite, yWhite, lineColor=col, symbolColor=col,
				label=cond, annotate=False, symbol=lineStyle, linestyle=':')
			s = 1000.*opts[0]
			_i = 1000.*opts[1]
			print 's = %.4f, i = %.4f' % (s, _i)
			if i > 0:
				row += [s, _i]
			else:
				plt.legend(frameon=False, loc='upper right')
		if i > 0:
			l.append(row)
		i += 1
	saveFig('linFitPrepWin%s' % suffix, show=False)
	dm = DataMatrix(l)
	dm.save('output/%s/linFitPrepWin%s.csv' % (exp, suffix))

	# Summarize the data and perform ttests on the model parameters
	for dv in ['s', 'i']:
		print'\nAnalyzing %s' % dv
		aConst = dm['%sConst' % dv]
		aOnset = dm['%sOnset' % dv]
		aSwap = dm['%sSwap' % dv]
		print 'Constant: M = %f, SE = %f' % (aConst.mean(), \
			aConst.std() / np.sqrt(N))
		print 'Onset: M = %f, SE = %f' % (aOnset.mean(), \
			aOnset.std() / np.sqrt(N))
		print 'Swap: M = %f, SE = %f' % (aSwap.mean(), \
			aSwap.std() / np.sqrt(N))
		# Standard t-tests
		t, p = ttest_rel(aConst, aOnset)
		print 'Const vs onset, t = %.4f, p = %.4f' % (t, p)
		t, p = ttest_rel(aSwap, aOnset)
		print 'Swap vs onset, t = %.4f, p = %.4f' % (t, p)
		t, p = ttest_rel(aConst, aSwap)
		print 'Const vs swap, t = %.4f, p = %.4f' % (t, p)