-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting_settings.py
196 lines (161 loc) · 6.61 KB
/
plotting_settings.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
"""Initialize settings for plotting."""
from __future__ import annotations
import pathlib
import re
from enum import Enum
import cycler
import matplotlib as mpl
import mne
import numpy as np
import seaborn as sns
from matplotlib import figure
from matplotlib import pyplot as plt
PALETTE_WJN_2022 = np.array(
(
(55 / 255, 110 / 255, 180 / 255), # blue - Med. OFF Stim. OFF
(113 / 255, 188 / 255, 173 / 255), # green - Med. ON Stim. OFF
(223 / 255, 74 / 255, 74 / 255), # red - Med. OFF Stim. ON
(125 / 255, 125 / 255, 125 / 255), # medium grey - STN
(150 / 255, 150 / 255, 150 / 255), # medium grey - Parietal Cortex
(215 / 255, 178 / 255, 66 / 255), # orange - Sensory Cortex
(100 / 255, 100 / 255, 100 / 255), # dark grey - Motor Cortex
(215 / 255, 213 / 255, 203 / 255), # medium/light grey
(234 / 255, 234 / 255, 234 / 255), # light grey
# (64 / 255, 64 / 255, 64 / 255),
)
)
class Color(Enum):
ECOG_MEDOFF = (55 / 255, 110 / 255, 180 / 255) # blue - Med. OFF Stim. OFF
ECOG_MEDON = (113 / 255, 188 / 255, 173 / 255) # green - Med. ON Stim. OFF
ECOG_STIMON = (223 / 255, 74 / 255, 74 / 255) # red - Med. OFF Stim. ON
STN = (125 / 255, 125 / 255, 125 / 255) # medium grey - STN
PARIETAL = (
150 / 255,
150 / 255,
150 / 255,
) # medium grey - Parietal Cortex
SENSORY = (215 / 255, 178 / 255, 66 / 255) # orange - Sensory Cortex
MOTOR = (
215 / 255,
213 / 255,
203 / 255,
) # medium/light grey - Motor Cortex
def set_mne_backends() -> None:
if mne.viz.get_3d_backend() != "pyvistaqt":
# mne.viz.set_3d_backend("pyvistaqt")
mne.viz.set_3d_backend("notebook")
if mne.viz.get_browser_backend() != "qt":
mne.viz.set_browser_backend("qt")
mne.set_config("MNE_BROWSER_BACKEND", "qt")
def activate() -> None:
mpl.rcParams["font.family"] = "Arial"
mpl.rcParams["font.size"] = 7
mpl.rcParams["pdf.fonttype"] = "TrueType"
mpl.rcParams["svg.fonttype"] = "none"
mpl.rcParams["savefig.dpi"] = 300
mpl.rcParams["savefig.bbox"] = "tight"
mpl.rcParams["savefig.pad_inches"] = 0
mpl.rcParams["savefig.transparent"] = True
mpl.rcParams["figure.figsize"] = [6.4, 4.8]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=PALETTE_WJN_2022)
mpl.rcParams["axes.xmargin"] = 0
mpl.rcParams["legend.title_fontsize"] = mpl.rcParams["legend.fontsize"]
mpl.rcParams["lines.linewidth"] = 1
def ecogvsstn_medoff(show: bool = False) -> None:
colors = [Color.ECOG_MEDOFF.value, Color.STN.value]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def ecogvsstn_medon(show: bool = False) -> None:
colors = [Color.ECOG_MEDON.value, Color.STN.value]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def ecogvsstn_stimon(show: bool = False) -> None:
colors = [Color.ECOG_STIMON.value, Color.STN.value]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def medoffvson(show: bool = False) -> None:
colors = [Color.ECOG_MEDOFF.value, Color.ECOG_MEDON.value]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def stimoffvson(show: bool = False) -> None:
colors = [Color.ECOG_MEDOFF.value, Color.ECOG_STIMON.value]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def medoff_medon_stimon(show: bool = False) -> None:
colors = [
Color.ECOG_MEDOFF.value,
Color.ECOG_MEDON.value,
Color.ECOG_STIMON.value,
]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def cortical_region(show: bool = False) -> None:
colors = [Color.PARIETAL.value, Color.SENSORY.value, Color.MOTOR.value]
mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)
if show:
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)
def save_fig(fig: figure.Figure, outpath: pathlib.Path) -> None:
fig.savefig(str(outpath))
if outpath.suffix == ".svg":
with outpath.open(mode="r", encoding="utf-8") as f:
svg_text = f.read()
patched_svg = patch_affinity_svg(svg_text)
with outpath.open(mode="w", encoding="utf-8") as f:
f.write(patched_svg)
def patch_affinity_svg(svg_text: str) -> str:
"""Patch Matplotlib SVG so that it can be read by Affinity Designer."""
matches = list(
re.finditer(
'font:( [0-9][0-9]?[0-9]?[0-9]?)? ([0-9.]+)px ([^;"]+)[";]',
svg_text,
)
)
if not matches:
return svg_text
svg_pieces = [svg_text[: matches[0].start()]]
for i, match in enumerate(matches):
# Change "font" style property to separate "font-size" and
# "font-family" properties because Affinity ignores "font".
group = match.groups()
if len(group) == 2:
font_weight, font_size_px, font_family = match.groups()
new_font_style = (
f"font-size: {float(font_size_px):.1f}px; "
f"font-family: {font_family}"
)
else:
font_weight, font_size_px, font_family = match.groups()
if font_weight is not None:
new_font_style = (
f"font-weight: {font_weight}; "
f"font-size: {float(font_size_px):.1f}px; "
f"font-family: {font_family}"
)
else:
new_font_style = (
f"font-size: {float(font_size_px):.1f}px; "
f"font-family: {font_family}"
)
svg_pieces.append(new_font_style)
if i < len(matches) - 1:
svg_pieces.append(svg_text[match.end() - 1 : matches[i + 1].start()])
else:
svg_pieces.append(svg_text[match.end() - 1 :])
return "".join(svg_pieces)
if __name__ == "__main__":
activate()
sns.palplot(mpl.rcParams["axes.prop_cycle"].by_key()["color"])
plt.show(block=True)