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 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')
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 trace_lmer_simple(dm, iv='type'): """ desc: Runs the main LME for each 10 ms window separately. Here the word type (bright or dark) is a fixed, pupil size is a dependent measure, and the model contains random by-participant slopes and intercepts. arguments: dm: type: DataMatrix keywords: iv: The independent variable. returns: desc: A DataMatrix with statistical results. """ dm = (dm.type == 'light') | (dm.type == 'dark') lm = lme4.lmer_series(dm, 'pupil ~ (%(iv)s) + (1+%(iv)s|subject_nr)' \ % {'iv' : iv}, winlen=WINLEN, cacheid='lmer_series_simple.%s' % iv) return lm
def analyse(dm, model): '''Analyses the parsed data''' lm = lme4.lmer_series(dm, model) return lm