Exemple #1
0
def annotated_plot(dm):

	"""
	desc:
		Plots mean pupil size separately for dark and bright trials, and
		annotates the plot with significance markers.

	arguments:
		dm:
			type: DataMatrix
	"""

	plot.new(size=(8,6))
	lm = trace_lmer_simple(dm)
	plt.axvline(RT, color='black', linestyle=':')
	x = np.arange(3000)
	for color, a in [
			('black', 1.96), # p < .05
			('red', 2.57), # p < .01
			('orange', 2.81), # p < .005
			('yellow', 3.29) # p < .001
			]:
		threshold = series.threshold(lm.t,
			lambda t: abs(t) > a, min_length=1)
		print('Alpha %.5f (%.2f)' % (a, series.reduce_(threshold)[1]))
		plot.threshold(threshold[1], color=color, linewidth=1)
	dm_dark = dm.type == 'ctrl'
	plot.trace(dm_dark.pupil, color=grey[3],
		label='Neutral (N=%d)' % len(dm_dark))
	pupil.brightness_plot(dm, subplot=True)
	plt.xticks(range(0, 3001, 500), np.linspace(0,3,7))
	plt.xlabel('Time since word onset (s)')
	plt.ylabel('Pupil size (normalized to word onset)')
	plot.save('annotated_plot')
Exemple #2
0
def preprocess(dm):

    """
	desc:
		Performs pupil preprocessing, and removes trials where pupil size was
		unrealistic.
	"""

    dm.pupil = series.blinkreconstruct(dm.ptrace_target)
    dm.pupil.depth = 3000
    dm.pupil = series.smooth(dm.pupil, winlen=51)
    dm.pupil = series.baseline(dm.pupil, dm.pupil, 0, 1)
    # Remove all trials where pupil size has unrealistic values
    dm.pupilmax = FloatColumn
    dm.pupilmin = FloatColumn
    for row in dm:
        row.pupilmin = min(row.pupil)
        row.pupilmax = max(row.pupil)
    plot.new()
    # plot.histogram(dm.pupilmin, bins=100, range=(0, 5), color=red[1], alpha=.5)
    # plot.histogram(dm.pupilmax, bins=100, range=(0, 5), color=green[1], alpha=.5)
    plt.hist(dm.pupilmin, bins=100, range=(0, 5), color=red[1], alpha=0.5)
    plt.hist(dm.pupilmax, bins=100, range=(0, 5), color=green[1], alpha=0.5)
    l0 = len(dm)
    dm = (dm.pupilmin > PUPILRANGE[0]) & (dm.pupilmax < PUPILRANGE[1])
    l1 = len(dm)
    s = "%d of %d unrealistic values" % (l0 - l1, l0)
    plt.title(s)
    plot.save("pupil-peaks")
    print(s)
    return dm
def filteredplot(dm):

	dm.bl = baseline.baseline(dm)
	dm.y2 = baseline.correct1(dm)
	dm.y4 = baseline.correct3(dm)
	dm = dm.bl >= MIN_BASELINE
	plot.new(size=(8, 3))
	plt.subplot(121)
	plotmean(dm, 'y2')
	plt.subplot(122)
	plotmean(dm, 'y4')
	plot.save('filteredplot')
Exemple #4
0
def plot_pupiltrace(dm, suffix='', interaction=False):

	"""
	desc:
		Create a plot of the pupil trace annotated with significant regions.

	arguments:
		dm:
			type: DataMatrix

	keywords:
		suffix:			A suffix for the plot.
		interaction:	Indicates whether the log_velocity by side interaction
						should be included.
	"""

	plot.new(size=(12, 6))
	# Plot the three velocities as separate lines
	for color, velocity in ( (orange[1], 30), (blue[1], 3), (green[1], .3) ):
		dm_ = dm.velocity == velocity
		plot.trace(dm_.pupil, label='%s cm/s (N=%d)' % (velocity, len(dm_)),
			color=color)
	# Run statistical model, and annotate the figure with three alpha levels:
	# .05, .01, and .005
	if interaction:
		model = 'pupil ~ log_velocity*side + (1+log_velocity*side|subject_nr)'
	else:
		model = 'pupil ~ log_velocity + (1+log_velocity|subject_nr)'
	lm = lme4.lmer_series(dm, formula=model, winlen=3,
		cacheid='lmer%s' % suffix)
	for y, color1, color2, alpha in [
		(.94, grey[2], blue[0], .05),
		(.942, grey[3], blue[1], .01),
		(.944, grey[4], blue[2], .005),
		(.946, grey[5], blue[2], .001)
		]:
		a = series.threshold(lm.p, lambda p: p > 0 and p < alpha,
			min_length=1)
		plot.threshold(a[1], y=y, color=color1, linewidth=4)
		if interaction:
			plot.threshold(a[3], y=y+.01, color=color2, linewidth=4)
	# Mark the baseline period
	plt.axvspan(50, 90, color='black', alpha=.2)
	# Mark the cue onset
	plt.axvline(90, color='black')
	# Adjust the x axis such that 0 is the cue onset, and the units are time in
	# seconds (assuming 33 ms per sample)
	plt.xticks(np.arange(40, 540, 50), np.arange(-50, 450, 50)*.033)
	plt.xlim(0, 540)
	plt.xlabel('Time since auditory cue (s)')
	plt.ylabel('Pupil size (relative to baseline)')
	plt.legend(frameon=False)
	plot.save('pupiltrace'+suffix)
def mainplots(dm):
		
	plot.new()
	dm.bl = baseline.baseline(dm)
	dm.y2 = baseline.correct1(dm)
	dm.y4 = baseline.correct3(dm)
	plotall(dm, 'y', 0, title='No baseline correction')
	plotall(dm, 'y2', 1, title='Divide by baseline')
	plotall(dm, 'y4', 2, title='Subtrace baseline')
	if '--sim' in sys.argv:
		plot.save('main-sim')
	elif '--real' in sys.argv:
		plot.save('main-real')
Exemple #6
0
def model_comparison(dm):

	"""TODO"""

	plot.new(size=(8,6))
	colors = brightcolors[:]
	for iv in ['rating_brightness', 'rating_valence', 'rating_intensity']:
		lm = trace_lmer_simple(dm, iv)
		plt.plot(np.abs(lm.t[1]), color=colors.pop(), label=iv)
	plt.legend()
	plt.xticks(range(0, 3001, 500), np.linspace(0,3,7))
	plt.xlabel('Time from word onset (s)')
	plt.ylabel('|t|')
	plot.save('model-comparison')
Exemple #7
0
def word_summary(dm):

    """
	desc:
		Plots the mean pupil size for dark and bright words as a bar plot. The
		time window is indicated by the PEAKWIN constant. This data is also
		written to a .csv file.

	arguments:
		dm:
			type: DataMatrix
	"""

    dm = (dm.type == "light") | (dm.type == "dark")
    x = np.arange(dm.pupil.depth)
    sm = DataMatrix(length=len(dm.word.unique))
    sm.word = 0
    sm.type = 0
    sm.pupil_win = FloatColumn
    sm.pupil_win_se = FloatColumn
    sm.pupil_full = FloatColumn
    sm.pupil_full_se = FloatColumn
    for i, w in enumerate(dm.word.unique):
        _dm = dm.word == w
        sm.word[i] = w
        sm.type[i] = (dm.word == w).type[0]
        sm.pupil_win[i], sm.pupil_win_se[i] = size_se(_dm, PEAKWIN[0], PEAKWIN[1])
        sm.pupil_full[i], sm.pupil_full_se[i] = size_se(_dm)
    sm = operations.sort(sm, sm.pupil_win)
    io.writetxt(sm, "%s/word_summary.csv" % OUTPUT_FOLDER)

    plot.new(size=(4, 3))
    dx = 0
    for color, type_ in ((orange[1], "light"), (blue[1], "dark")):
        sm_ = sm.type == type_
        x = np.arange(len(sm_))
        plt.plot(sm_.pupil_win, "o-", color=color)
        if type_ == "dark":
            yerr = (np.zeros(len(sm_)), sm_.pupil_win_se)
        else:
            yerr = (sm_.pupil_win_se, np.zeros(len(sm_)))
        plt.errorbar(x, sm_.pupil_win, yerr=yerr, linestyle="", color=color, capsize=0)
    plt.xlim(-1, 33)
    plt.ylabel("Pupil size (normalized)")
    plt.xlabel("Word")
    plt.xticks([])
    plot.save("word_summary")
def histogram(dm):

	
	dm.bl = baseline.baseline(dm)		
	plot.new(size=(8,4))
	y, x = np.histogram(dm.bl, bins=50, range=(0,4000))
	x =.5*x[1:]+.5*x[:-1]
	plt.subplot(121)
	plt.xlabel('Baseline pupil size')
	plt.ylabel('N')	
	plt.fill_between(x, y)
	plt.xlim(0, 3000)
	plt.axvline(MIN_BASELINE, color='black', linestyle=':')
	plt.subplot(122)
	plt.xlabel('Baseline pupil size')
	plt.ylabel('log10(N)')
	plt.fill_between(x, np.log10(y))
	plt.xlim(0, 3000)	
	plt.axvline(MIN_BASELINE, color='black', linestyle=':')
	plot.save('baseline-hist')
Exemple #9
0
def subject_diff_traces(dm):

    """
	desc:
		Plots the difference in pupil size for dark and bright trials over time,
		separately for each participant.

	arguments:
		dm:
			type: DataMatrix
	"""

    plot.new()
    x = np.arange(dm.pupil.depth)
    for i, s in enumerate(dm.subject_nr.unique):
        print(s)
        _dm = dm.subject_nr == s
        plt.subplot(6, 5, i + 1)
        plt.title("Subject %d" % i)
        brightness_plot(_dm, subplot=True)
    plot.save("subject_diff_trace")
Exemple #10
0
def brightness_plot(dm, subplot=False):

    """
	desc:
		Plots mean pupil size separately for dark and bright trials over time.

	arguments:
		dm:
			type: DataMatrix

	keywords:
		subplot:	Indicates whether a new plot should be created, or not.
	"""

    if not subplot:
        plot.new()
    dm_bright = dm.type == "light"
    dm_dark = dm.type == "dark"
    plot.trace(dm_bright.pupil, color=orange[1], label="Bright (N=%d)" % len(dm_bright))
    plot.trace(dm_dark.pupil, color=blue[1], label="Dark (N=%d)" % len(dm_dark))
    plt.legend(frameon=False)
    if not subplot:
        plot.save("brightness_plot")
Exemple #11
0
def brightness_intensity(dm):

	"""TODO"""

	lm = lme4.lmer_series((dm.type == 'light') | (dm.type == 'dark'),
		'pupil ~ type + rating_intensity + (1+type|subject_nr)',
		winlen=WINLEN, cacheid='lmer_series_type_intensity')
		
	threshold = series.threshold(lm.t, lambda t: abs(t) > 1.96, min_length=200)
	print('threshold', threshold > 0)
		
	plot.new(size=(8,6))
	plt.plot(threshold)
	plt.plot(lm.t[1], label='Type')
	plt.plot(lm.t[2], label='Intensity')
	plot.save('model-type-intensity')
	
	lm = lme4.lmer_series(dm.type != 'animal',
		'pupil ~ rating_brightness + rating_intensity + (1+rating_brightness|subject_nr)',
		winlen=WINLEN, cacheid='lmer_series_brightness_intensity')
	plot.new(size=(8,6))
	plt.plot(lm.t[1], label='Brightness')
	plt.plot(lm.t[2], label='Intensity')
	plot.save('model-brightness-intensity')
Exemple #12
0
def subject_summary(dm):

    """
	desc:
		Plots the mean difference in pupil size between dark and bright trials
		for each participant as a bar plot. The time window is indicated by the
		PEAKWIN constant. This data is also written to a .csv file.

	arguments:
		dm:
			type: DataMatrix
	"""

    x = np.arange(len(dm.subject_nr.unique))
    sm = DataMatrix(length=len(dm.subject_nr.unique))
    sm.subject_nr = 0
    sm.effect_win = FloatColumn
    sm.effect_win_se = FloatColumn
    sm.effect_full = FloatColumn
    sm.effect_full_se = FloatColumn
    for i, s in enumerate(dm.subject_nr.unique):
        _dm = dm.subject_nr == s
        sm.subject_nr[i] = s
        sm.effect_win[i], sm.effect_win_se[i] = effect_se(_dm, PEAKWIN[0], PEAKWIN[1])
        sm.effect_full[i], sm.effect_full_se[i] = effect_se(_dm)
    sm = operations.sort(sm, by=sm.effect_win)
    plot.new(size=(4, 3))
    plt.axhline(0, color="black")
    plt.plot(sm.effect_win, "o-", color=green[-1])
    plt.errorbar(x, sm.effect_win, yerr=sm.effect_win_se, linestyle="", color=green[-1], capsize=0)
    plt.xlim(-1, 30)
    plt.ylabel("Pupil-size difference (normalized)")
    plt.xlabel("Participant")
    plt.xticks([])
    plot.save("subject_summary")
    io.writetxt(sm, "%s/subject_summary.csv" % OUTPUT_FOLDER)
Exemple #13
0
def valence_plot(dm, subplot=False):

    """
	desc:
		Plots mean pupil size separately for positive and negative trials over
		time.

	arguments:
		dm:
			type: DataMatrix

	keywords:
		subplot:	Indicates whether a new plot should be created, or not.
	"""

    if not subplot:
        plot.new()
    dm_pos = dm.category == "positive"
    dm_neg = dm.category == "negative"
    plot.trace(dm_pos.pupil, color=green[1], label="Positive (N=%d)" % len(dm_pos))
    plot.trace(dm_neg.pupil, color=red[1], label="Negative (N=%d)" % len(dm_neg))
    plt.legend(frameon=False)
    if not subplot:
        plot.save("valence_plot")
rm = DataMatrix(length=101)
rm.word = ''
rm._type = ''
rm.rating_brightness = FloatColumn
rm.rating_valence = FloatColumn
rm.rating_intensity = FloatColumn
for row, word in zip(rm, dm.word.unique):
	dm_ = dm.word == word
	dme = dm_.rating_type == 'emotion'
	dmb = dm_.rating_type == 'brightness'
	# Pénombre was accidentally rated twice.
	assert(len(dmb)==30 or word == 'pénombre')
	assert(len(dme)==30 or word == 'pénombre')
	row.word = word
	row._type = dme.type[0]
	row.rating_brightness = dmb.rating.mean
	row.rating_valence = dme.rating.mean
	# The intensity is just the deviation from the middle score (2). In the
	# initial analysis, the deviation from the mean valence was taken. But
	# taking the per-trial deviation from the middle score, and then averaging
	# that seems to make more sense.
	row.rating_intensity = np.abs(dme.rating-2).mean()
io.writetxt(rm, 'ratings.csv')

# Determine the correlations
print(plot.regress(rm.rating_brightness, rm.rating_valence))
plot.save('regress.brightness.valence')
print(plot.regress(rm.rating_brightness, rm.rating_intensity))
plot.save('regress.brightness.intensity')
def powersummary(dummy):

    plot.new()
    for i, blinksinbaseline in enumerate((True, False)):
        l1 = []
        l2 = []
        l4 = []
        ls1 = []
        ls2 = []
        ls4 = []
        effectsizes = range(0, 501, 50)
        for effectsize in effectsizes:
            kwargs = {
                'effectsize': effectsize,
                'blfilter': False,
                'blinksinbaseline': blinksinbaseline
            }
            cid = 'power-%(effectsize)s-%(blfilter)s-%(blinksinbaseline)s' % kwargs
            p1, p2, p4, s1, s2, s4 = power(None, cacheid=cid, **kwargs)
            l1.append(p1)
            l2.append(p2)
            l4.append(p4)
            ls1.append(s1)
            ls2.append(s2)
            ls4.append(s4)
        plt.subplot(2, 2, i * 2 + 1)
        if i == 0:
            plt.title('With blinks during baseline')
        else:
            plt.title('No blinks during baseline')
        plt.axhline(.025, color='black', linestyle=':')
        plt.xlim(effectsizes[0] - 10, effectsizes[-1] + 10)
        plt.ylim(-.05, 1.05)
        plt.xlabel('True effect size')
        plt.ylabel('Proportion correct detections')
        plt.plot(effectsizes,
                 l1,
                 'o-',
                 label='No baseline correction',
                 color=COLORNOCORRECT)
        plt.plot(effectsizes,
                 l2,
                 'o-',
                 label='Divide by baseline',
                 color=COLORDIVIDE)
        plt.plot(effectsizes,
                 l4,
                 'o-',
                 label='Subtract baseline',
                 color=COLORSUBTRACT)
        plt.subplot(2, 2, i * 2 + 2)
        plt.title('Spurious')
        plt.axhline(.025, color='black', linestyle=':')
        plt.xlim(effectsizes[0] - 10, effectsizes[-1] + 10)
        plt.ylim(-.05, 1.05)
        plt.xlabel('True effect size')
        plt.ylabel('Proportion spurious detections')
        plt.plot(effectsizes,
                 ls1,
                 'o-',
                 label='No baseline correction',
                 color=COLORNOCORRECT)
        plt.plot(effectsizes,
                 ls2,
                 'o-',
                 label='Divide by baseline',
                 color=COLORDIVIDE)
        plt.plot(effectsizes,
                 ls4,
                 'o-',
                 label='Subtract baseline',
                 color=COLORSUBTRACT)
        if i == 3:
            plt.legend(frameon=False, loc='lower right')
    plot.save('powersummary')
Exemple #16
0

plot.new(size=(16, 5))
plt.subplot(1, 3, 1)
plt.xlim(-5, 5)
plt.title('a) VC ~ pupil size')
roi_hist(ldm, 't_bold_pupil')
plt.subplot(1, 3, 2)
plt.xlim(-5, 5)
plt.title('b) VC ~ luminance')
roi_hist(ldm, 't_bold_luminance')
plt.subplot(1, 3, 3)
plt.xlim(-10, 10)
plt.title('c) VC ~ visual change')
roi_hist(ldm, 't_bold_change')
plot.save('histograms', show=True)
# </codecell>

# <markdowncell>
"""
## Correlations between pupil size, luminance, and visual change

__Conclusion:__

- Pupil size and luminance are strongly negatively correlated
- Pupil size and visual change are negatively correlated
- Visual change and luminance are positively correlated
"""

# </markdowncell>