def effect_se(dm, start=0, end=None): """ desc: Gets the standard error of the differencein pupil size between dark and bright trials during a time window. arguments: dm: type: DataMatrix keywords: start: The start time. end: The end time, or None for trace end. returns: type: ndarrray """ dm_bright = dm.type == "light" dm_dark = dm.type == "dark" bright = series.reduce_(series.window(dm_bright.pupil, start=start, end=end)) dark = series.reduce_(series.window(dm_dark.pupil, start=start, end=end)) diff = dark.mean - bright.mean se = ((bright.std ** 2 / len(bright)) + (dark.std ** 2 / len(dark))) ** 0.5 return diff, se
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 lmer_series(dm, formula, winlen=1): col = formula.split()[0] depth = dm[col].depth rm = None for i in range(0, depth, winlen): wm = dm[:] wm[col] = series.reduce_( series.window(wm[col], start=i, end=i+winlen)) lm = lmer(wm, formula) print('Sample %d' % i) print(lm) if rm is None: rm = DataMatrix(length=len(lm)) rm.effect = list(lm.effect) rm.p = SeriesColumn(depth=depth) rm.t = SeriesColumn(depth=depth) rm.est = SeriesColumn(depth=depth) rm.se = SeriesColumn(depth=depth) for lmrow, rmrow in zip(lm, rm): rmrow.p[i:i+winlen] = lmrow.p rmrow.t[i:i+winlen] = lmrow.t rmrow.est[i:i+winlen] = lmrow.est rmrow.se[i:i+winlen] = lmrow.se return rm
def glmer_series(dm, formula, family, winlen=1): col = formula.split()[0] depth = dm[col].depth rm = None for i in range(0, depth, winlen): wm = dm[:] wm[col] = series.reduce_( series.window(wm[col], start=i, end=i + winlen)) lm = glmer(wm, formula, family=family) print('Sample %d' % i) print(lm) if rm is None: rm = DataMatrix(length=len(lm)) rm.effect = list(lm.effect) rm.p = SeriesColumn(depth=depth) rm.z = SeriesColumn(depth=depth) rm.est = SeriesColumn(depth=depth) rm.se = SeriesColumn(depth=depth) for lmrow, rmrow in zip(lm, rm): rmrow.p[i:i + winlen] = lmrow.p rmrow.z[i:i + winlen] = lmrow.z rmrow.est[i:i + winlen] = lmrow.est rmrow.se[i:i + winlen] = lmrow.se return rm
def test_reduce_(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=3) dm.series[0] = 1, 2, 3 dm.series[1] = 2, 3, 4 dm.col = series.reduce_(dm.series) check_col(dm.col, [2, 3]) check_integrity(dm)
def descriptives(dm): """ desc: Provides basic descriptives of response times. These are printed directly to the stdout. arguments: dm: type: DataMatrix """ ops.keep_only(dm, cols=['type', 'rt']) gm = ops.group(dm, by=[dm.type]) gm.mean_rt = series.reduce_(gm.rt) gm.se_rt = series.reduce_(gm.rt, lambda x: np.nanstd(x)/np.sqrt(len(x))) gm.n = series.reduce_(gm.rt, lambda x: np.sum(~np.isnan(x))) del gm.rt print(gm)
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 size(dm, start=0, end=None): """ desc: Gets the mean pupil size during a time window. arguments: dm: type: DataMatrix keywords: start: The start time. end: The end time, or None for trace end. returns: type: ndarrray """ return series.reduce_(series.window(dm.pupil, start=start, end=end)).mean
def size_se(dm, start=0, end=None): """ desc: Gets the pupil-size standard error during a time window. arguments: dm: type: DataMatrix keywords: start: The start time. end: The end time, or None for trace end. returns: type: ndarrray """ s = series.reduce_(series.window(dm.pupil, start=start, end=end)) return s.mean, s.std / len(s) ** 0.5
def interactiontest(dm): dm.bl = baseline.baseline(dm) dm.test = srs.reduce_(srs.window(dm.y, start=-50, end=-1)) lmdata = DataMatrix(length=len(dm) * 2) lmdata.pupil = -1 lmdata.time = -1 lmdata.condition = -1 lmdata.trialid = -1 for i, row in enumerate(dm): lmdata.pupil[2 * i] = row.bl lmdata.time[2 * i] = 0 lmdata.condition[2 * i] = row.c lmdata.trialid[2 * i] = i lmdata.pupil[2 * i + 1] = row.test lmdata.time[2 * i + 1] = 1 lmdata.condition[2 * i + 1] = row.c lmdata.trialid[2 * i + 1] = i lm = lme4.lmer(lmdata, 'pupil ~ condition*time + (1|trialid)') print(lm.p[3]) return lm.p[3] < ALPHA
def ttest(dm, col): a1 = srs.reduce_(srs.window((dm.c == 1)[col], start=-50, end=-1)) b1 = srs.reduce_(srs.window((dm.c == 2)[col], start=-50, end=-1)) t, p = stats.ttest_ind(a1, b1) return p < ALPHA and t < 0, p < ALPHA and t > 0
def baseline(dm): return srs.reduce_(srs.window(dm.y, start=BASELINE[0], end=BASELINE[1]), operation=REDUCEFNC)