-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path99.export_figs.py
221 lines (203 loc) · 6.63 KB
/
99.export_figs.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
export figures based on previous analysis
env: environments/generic.yml
"""
# %% imports and definition
import itertools as itt
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.transforms import blended_transform_factory
from tqdm.auto import tqdm
IN_PATH = "./intermediate/classify_cells"
INT_PATH = "./intermediate/export"
FIG_PATH = "./figs/export/"
PARAM_ZTHRES = 2
PARAM_EVTS = {
"baseline": (0, 400),
"tone": (0, 400),
"trace": (0, 400),
"shock": (0, 40),
"post-shock-1": (0, 400),
"post-shock-2": (0, 400),
"post-shock-3": (0, 400),
"post-shock-4": (0, 400),
"post-shock-5": (0, 400),
}
os.makedirs(FIG_PATH, exist_ok=True)
os.makedirs(INT_PATH, exist_ok=True)
def reset_uid(df):
uid_map = {u: i for i, u in enumerate(df["unit_id"].unique())}
df["uid"] = df["unit_id"].map(uid_map).astype("category")
return df.set_index("uid")
def combine_act_cell(act_df, cdf, by="resp"):
return (
act_df.merge(
cdf[["unit_id", by]], on="unit_id", how="right", validate="many_to_one"
)
.groupby(by, observed=True)
.apply(reset_uid, include_groups=False)
.reset_index()
.sort_values(["animal", "session"])
.sort_values(
"evt", key=lambda evts: [list(PARAM_EVTS.keys()).index(e) for e in evts]
)
.sort_values([by, "uid", "frame"])
)
def agg_by_trial_unit(act_df, cdf, by_col="resp"):
by_trial = []
by_unit = []
for by, by_df in cdf.groupby(by_col, observed=True):
dat_df = combine_act_cell(act_df, by_df)
assert len(dat_df) <= len(act_df)
by_unit.append(
dat_df.groupby(
[
"cls_var",
"group",
"animal",
"session",
"unit_id",
"uid",
"resp",
"evt",
"frame",
],
observed=True,
sort=False,
)["value"]
.mean()
.reset_index()
)
by_trial.append(
dat_df.groupby(
[
"cls_var",
"group",
"animal",
"session",
"trial",
"resp",
"evt",
"frame",
],
observed=True,
sort=False,
)["value"]
.mean()
.reset_index()
)
by_trial, by_unit = pd.concat(by_trial, ignore_index=True), pd.concat(
by_unit, ignore_index=True
)
return by_trial, by_unit
def plot_agg_curve(dat_df, fm_map, by="resp", hue="trial"):
g = sns.FacetGrid(
dat_df,
row=by,
margin_titles=True,
hue="group",
aspect=3.5,
sharex=True,
sharey=False,
)
g.map_dataframe(
concat_lineplot,
fm_map=fm_map,
estimator="mean",
errorbar="se",
lw=2,
err_kws={"alpha": 0.3},
)
g.add_legend()
g.set_axis_labels(x_var="frame", y_var="fluorescence")
return g.figure
def concat_lineplot(data, fm_map, color=None, **kwargs):
ax = plt.gca()
dat_plt = data.merge(fm_map, on=["evt", "frame"], how="left").dropna()
sns.lineplot(dat_plt, x="fm_plt", y="value", **kwargs, ax=ax)
for ievt, (evt, map_df) in enumerate(fm_map.groupby("evt", sort=False)):
fm0, fm1 = map_df["fm_plt"].min(), map_df["fm_plt"].max()
alpha = 0.1 if ievt % 2 == 0 else 0
ax.axvspan(fm0, fm1, facecolor="grey", alpha=alpha)
ax.text(
x=(fm0 + fm1) / 2,
y=1,
s=evt,
ha="center",
va="top",
rotation="vertical" if fm1 - fm0 < 100 else "horizontal",
transform=blended_transform_factory(ax.transData, ax.transAxes),
)
# %% aggregate data
cell_df = pd.read_feather(os.path.join(IN_PATH, "cell_df.feat"))
cell_cls_df = pd.read_feather(os.path.join(IN_PATH, "cell_cls_df.feat"))
act_zs = pd.read_feather(os.path.join(IN_PATH, "act_zs.feat"))
cell_df_plt = cell_df[cell_df["resp"] == "activated"].copy()
cell_df_plt["resp"] = (
cell_df_plt["evt"].astype(str) + "-" + cell_df_plt["resp"].astype(str)
).astype("category")
cell_df_plt = cell_df_plt.sort_values(
["cls_var", "animal", "session", "resp", "zval"]
).set_index(["cls_var", "animal", "session"])
cell_cls_df_plt = (
cell_cls_df[cell_cls_df["cls"] != "non-responsive"]
.sort_values(["cls_var", "animal", "session", "cls"])
.rename(columns={"cls": "resp"})
.set_index(["cls_var", "animal", "session"])
)
evt_dict = {
"single_evt": cell_df_plt,
"compound_evt": cell_cls_df_plt,
}
fig_path = os.path.join(FIG_PATH, "raster")
os.makedirs(fig_path, exist_ok=True)
for evt_type, evt_dat in evt_dict.items():
by_unit_df = []
by_trial_df = []
for (cls_var, grp, anm, ss), act_df in tqdm(
act_zs.groupby(["cls_var", "group", "animal", "session"], observed=True)
):
try:
cdf = evt_dat.loc[cls_var, anm, ss]
except KeyError:
print("missing events for {}, anm {} ss {}".format(cls_var, anm, ss))
continue
by_trial, by_unit = agg_by_trial_unit(act_df, cdf)
by_unit_df.append(by_unit)
by_trial_df.append(by_trial)
by_unit_df = pd.concat(by_unit_df, ignore_index=True, copy=False)
by_trial_df = pd.concat(by_trial_df, ignore_index=True, copy=False)
by_unit_df.to_feather(os.path.join(INT_PATH, "by_unit-{}.feat".format(evt_type)))
by_trial_df.to_feather(os.path.join(INT_PATH, "by_trial-{}.feat".format(evt_type)))
# %% plot agg lines
fig_path = os.path.join(FIG_PATH, "lines_agg")
os.makedirs(fig_path, exist_ok=True)
fm_map_df = None
for evt, evt_rg in PARAM_EVTS.items():
if fm_map_df is not None:
last_fm = fm_map_df["fm_plt"].max()
else:
last_fm = 0
map_new = pd.DataFrame(
{
"evt": evt,
"frame": np.arange(*evt_rg),
"fm_plt": np.arange(*evt_rg) + last_fm + 1,
}
)
fm_map_df = pd.concat([fm_map_df, map_new])
for evt_type, by_type in itt.product(
["single_evt", "compound_evt"], ["by_unit", "by_trial"]
):
dat_df = pd.read_feather(
os.path.join(INT_PATH, "{}-{}.feat".format(by_type, evt_type))
)
for cls_var, ddf in dat_df.groupby("cls_var", observed=True):
fig = plot_agg_curve(ddf, fm_map=fm_map_df)
fig.savefig(
os.path.join(fig_path, "{}-{}-{}.svg".format(evt_type, by_type, cls_var))
)
plt.close(fig)