-
Notifications
You must be signed in to change notification settings - Fork 0
/
osc_counting_subj.py
55 lines (50 loc) · 1.87 KB
/
osc_counting_subj.py
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
48
49
50
51
52
53
54
55
import mne
import numpy as np
import pandas as pd
from mne.time_frequency import read_tfrs
from os.path import isdir
import seaborn as sns
import matplotlib.pyplot as plt
plt.ion()
if isdir("/home/jev"):
root_dir = "/home/jev/hdd/sfb/"
elif isdir("/home/jeff"):
root_dir = "/home/jeff/hdd/jeff/sfb/"
elif isdir("/home/jeffhanna/"):
root_dir = "/scratch/jeffhanna/sfb/"
proc_dir = root_dir+"proc/"
baseline = "zscore"
plot_conds = ["sham30s","fix30s","eig30s","sham2m","fix2m","eig2m","sham5m","fix5m","eig5m"]
tfr = read_tfrs("{}grand_central_{}-tfr.h5".format(proc_dir, baseline))[0]
tfr = tfr["OscType=='SO' and PrePost=='Post'"]
subjs = list(tfr.metadata["Subj"].unique())
dfs = {"Subj":[], "df":[], "var":[]}
for subj in subjs:
this_tfr = tfr["Subj=='{}'".format(subj)]
these_conds = list(this_tfr.metadata["Cond"].unique())
counts = this_tfr.metadata.value_counts(subset=["Cond"])
count_df = {"Cond":[], "Count":[]}
for pc in plot_conds:
count_df["Cond"].append(pc)
if pc in these_conds:
count_df["Count"].append(counts[pc].values[0])
else:
count_df["Count"].append(0)
count_df = pd.DataFrame.from_dict(count_df)
dfs["Subj"].append(subj)
dfs["df"].append(count_df)
vals = count_df["Count"].values.copy()
nz_mean = vals[vals!=0].mean() # only compute mean on non-zero values
vals[vals>nz_mean] = nz_mean # don't penalise variance above the mean
var = np.sqrt(np.sum((vals - np.ones_like(vals)*nz_mean)**2))
dfs["var"].append(var)
dfs = pd.DataFrame.from_dict(dfs)
dfs = dfs.sort_values("var")
fig, axes = plt.subplots(6,6)
axes = [ax for axe in axes for ax in axe]
subjs = list(dfs["Subj"].values)
for idx, subj in enumerate(subjs):
df = dfs.iloc[idx]["df"]
sns.barplot(data=df, x="Cond", y="Count", order=plot_conds, ax=axes[idx])
axes[idx].set_title(subj)
plt.tight_layout()