1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| def getPowerDataFrameTime(power_task, ch_pair): freqs = np.arange(2, 40) # frequencies from 2-35Hz # vmin, vmax = -1, 1.5 # set min and max ERDS values in plot baseline = (-7, -3) # baseline interval (in s) tmin, tmax = -7, 20 ''' ERSD: percent: 减去基线值的平均值,再除以基线值的平均值("百分比) ''' power_task.crop(tmin, tmax).apply_baseline(baseline, mode="percent") ''' 0. 字典形式 ''' df = power_task.to_data_frame(time_format=None, long_format=True) ''' 1. 选择感兴趣的通道 ''' new_categories = ch_pair df = df[df.channel.isin(new_categories)] df["channel"] = df["channel"].cat.remove_unused_categories() df["channel"] = df["channel"].cat.reorder_categories(new_categories, ordered=True)
''' 2. 选择感兴趣的频段 ''' freq_bounds = {"_": 0, "delta": 4, "theta": 8, "alpha": 13, "beta": 30, "gamma":40} df["band"] = pd.cut( df["freq"], list(freq_bounds.values()), labels=list(freq_bounds)[1:] ) freq_bands_of_interest = ["alpha","beta"] # "delta", "theta",, "beta", "gamma" df = df[df.band.isin(freq_bands_of_interest)] df["band"] = df["band"].cat.remove_unused_categories() return df
# 1. 选择感兴趣的通道 ch_pairs = ['C3', 'CZ', 'C4']
# 2. 获取统计的DataFrame task_power_time = getPowerDataFrameTime(task_power.copy(), ch_pairs)
# 3. 绘图 g = sns.FacetGrid(task_power_time, row="band", margin_titles=True) g.map(sns.lineplot, "time", "value", "channel", n_boot=10, linewidth=2) axline_kw = dict(color="black", linestyle="dashed", linewidth=0.5, alpha=0.5)
g.map(plt.axhline, y=0, **axline_kw) g.map(plt.axvline, x=0, **axline_kw) g.refline(x=0, color='red') g.refline(y=0, color='red') g.set(ylim=(-0.9, 0.9)) g.set_axis_labels("Time (s)", "ERDS") g.set_titles(col_template="{col_name}", row_template="{row_name}") g.add_legend(ncol=3, loc="lower center") g.fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.08)
|