-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_10_updrs_boxplot_medoffvson.py
146 lines (132 loc) · 4.42 KB
/
task_10_updrs_boxplot_medoffvson.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
"""Plot UPDRS Med. OFF vs ON"""
from __future__ import annotations
import csv
from enum import Enum
from pathlib import Path
from typing import Annotated
import numpy as np
import pandas as pd
import pte_decode
import scipy.stats
from pytask import Product
import motor_intention.plotting_settings
import motor_intention.project_constants as constants
import motor_intention.stats_helpers
PLOT_PATH = constants.PLOTS / "updrs"
PLOT_PATH.mkdir(parents=True, exist_ok=True)
PART_INFO = constants.DATA / "participant_info.csv"
BASENAME = "UPDRS_boxplot_medoffvson"
FNAME_PLOT = PLOT_PATH / (BASENAME + ".svg")
FNAME_STATS = PLOT_PATH / (BASENAME + "_stats.csv")
class Cond(Enum):
OFF_THERAPY = "OFF Therapy"
ON_LEVODOPA = "ON Levodopa"
ON_STN_DBS = "ON STN-DBS"
def task_plot_updrs_medoffvson(
part_info: Path = PART_INFO,
outpath_plot: Annotated[Path, Product] = FNAME_PLOT,
outpath_stats: Annotated[Path, Product] = FNAME_STATS,
) -> None:
motor_intention.plotting_settings.activate()
motor_intention.plotting_settings.medoffvson()
x = "Medication"
y = "UPDRS-III"
order = [Cond.OFF_THERAPY, Cond.ON_LEVODOPA]
data_raw = pd.read_csv(part_info).rename(
columns={
"ID": "Subject",
f"{y} recordingMedOFF": Cond.OFF_THERAPY.value,
f"{y} recordingMedON": Cond.ON_LEVODOPA.value,
}
)
data = (
pd.melt(
data_raw,
value_vars=[cond.value for cond in order],
id_vars=["Subject"],
var_name="Medication",
value_name=y,
)
.dropna(axis="index", how="any")
.query(f"Subject in {[sub.strip('sub-') for sub in constants.MED_PAIRED]}")
.sort_values("Subject")
)
print(data.head())
figsize = (1.0, 1.3)
fig = pte_decode.boxplot_updrs(
data=data,
outpath=None,
x=x,
y=y,
add_lines="Subject",
order=[cond.value for cond in order],
title=None,
figsize=figsize,
show=False,
)
ax = fig.axes[0]
print(ax.get_ylim())
ylims = (5, 50)
ax.set_ylim(ylims[0], ylims[-1])
ax.set_yticks([ylims[0], ylims[-1]])
ax.set_xticklabels(ax.get_xticklabels(), weight="bold")
motor_intention.plotting_settings.save_fig(fig, outpath_plot)
outpath_stats.unlink(missing_ok=True)
with outpath_stats.open(mode="w", encoding="utf-8", newline="") as file:
writer = csv.writer(file)
writer.writerow(["description", "mean", "std", "statistic", "P"])
statistic = np.mean
for cond in order:
description = cond.value
print(f"{description = }")
data_cond = data.query(f"{x} == '{cond.value}'")[y].to_numpy()
test = scipy.stats.permutation_test(
(data_cond - 0.0,),
statistic,
vectorized=True,
n_resamples=int(1e6),
permutation_type="samples",
)
print(f"statistic = {test.statistic}, P = {test.pvalue}")
writer.writerow(
[
description,
statistic(data_cond),
np.std(data_cond),
test.statistic,
test.pvalue,
]
)
for cond_a, cond_b in ((Cond.OFF_THERAPY, Cond.ON_LEVODOPA),):
description = f"{cond_a.value} vs {cond_b.value}"
print(f"{description = }")
data_a = (
data.query(f"{x} == '{cond_a.value}'")
.sort_values("Subject")[y]
.to_numpy()
)
data_b = (
data.query(f"{x} == '{cond_b.value}'")
.sort_values("Subject")[y]
.to_numpy()
)
test = scipy.stats.permutation_test(
(data_a - data_b,),
statistic := np.mean,
vectorized=True,
n_resamples=int(1e6),
permutation_type="samples",
)
print(f"statistic = {test.statistic}, P = {test.pvalue}")
writer.writerow(
[
description,
statistic(data_a - data_b),
np.std(data_a - data_b),
test.statistic,
test.pvalue,
]
)
if __name__ == "__main__":
task_plot_updrs_medoffvson()
# plt.show(block=True)