def test_threshold(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=4) dm.series[0] = range(4) dm.series[1] = range(1, 5) dm.t1 = series.threshold(dm.series, lambda v: v > 1) dm.t2 = series.threshold(dm.series, lambda v: v > 1 and v < 3) dm.t3 = series.threshold(dm.series, lambda v: v < 3, min_length=3) check_series(dm.t1, [[0, 0, 1, 1], [0, 1, 1, 1]]) check_series(dm.t2, [[0, 0, 1, 0], [0, 1, 0, 0]]) check_series(dm.t3, [[1, 1, 1, 0], [0, 0, 0, 0]]) check_integrity(dm)
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')
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 trace_lmer_ratings(dm, dv='rating_brightness'): """TODO""" dm = dm.type != 'animal' print(dm.type.unique, dv) lm = lme4.lmer_series(dm, 'pupil ~ (%(dv)s) + (1+%(dv)s|subject_nr)' \ % {'dv' : dv}, winlen=WINLEN, cacheid='lmer_series_ratings.%s' % dv) threshold = series.threshold(lm.p, lambda p: p > 0 and p<.05, min_length=200) print('Alpha .05 (%.2f)' % series.reduce_(threshold)[1]) plt.plot(lm.t[1]) plot.threshold(threshold[1], color=blue[1], linewidth=1) plt.show() return lm
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')