Skip to content

Commit 706335b

Browse files
committed
Support multiple data resolution.
1 parent d40c47e commit 706335b

17 files changed

Lines changed: 101 additions & 5 deletions

src/brainplotlib/__init__.py

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,111 @@
33
from matplotlib import cm
44

55

6-
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
7-
mapping = np.load(os.path.join(DIR_PATH, 'data', 'icoorder3_mapping.npy'))
6+
DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
87

8+
PLOT_MAPPING = np.load(os.path.join(DATA_DIR, 'fsaverage_to_image.npy'))
99

10-
def brain_plot(values, vmax, vmin, cmap=None):
10+
GUESS_SEPARATE = {
11+
## masked data
12+
# (588, 587): ('fsaverage5', 3, True),
13+
# (588, 587): ('fsaverage6', 3, True),
14+
(588, 587): ('fsaverage', 3, True),
15+
16+
(2341, 2346): ('fsaverage5', 4, True),
17+
(2343, 2347): ('fsaverage6', 4, True),
18+
(2343, 2344): ('fsaverage', 4, True),
19+
20+
(9354, 9361): ('fsaverage5', 5, True),
21+
(9372, 9369): ('fsaverage6', 5, True),
22+
(9372, 9370): ('fsaverage', 5, True),
23+
24+
(37476, 37471): ('fsaverage6', 6, True),
25+
(37487, 37482): ('fsaverage', 6, True),
26+
27+
(149955, 149926): ('fsaverage', 7, True),
28+
29+
## unmasked data
30+
(642, 642): ('fsaverage', 3, False),
31+
(2562, 2562): ('fsaverage', 4, False),
32+
(10242, 10242): ('fsaverage', 5, False),
33+
(40962, 40962): ('fsaverage', 6, False),
34+
(163842, 163842): ('fsaverage', 7, False),
35+
}
36+
37+
GUESS_COMBINED = {
38+
## masked data
39+
# 1175: ('fsaverage5', 3, True, [588]),
40+
# 1175: ('fsaverage6', 3, True, [588]),
41+
1175: ('fsaverage', 3, True, [588]),
42+
43+
# 4687: ('fsaverage5', 4, True, [2341]),
44+
4690: ('fsaverage6', 4, True, [2343]),
45+
4687: ('fsaverage', 4, True, [2343]),
46+
47+
18715: ('fsaverage5', 5, True, [9354]),
48+
18741: ('fsaverage6', 5, True, [9372]),
49+
18742: ('fsaverage', 5, True, [9372]),
50+
51+
74947: ('fsaverage6', 6, True, [37476]),
52+
74969: ('fsaverage', 6, True, [37487]),
53+
54+
299881: ('fsaverage', 7, True, [149955]),
55+
56+
## unmasked data
57+
1284: ('fsaverage', 3, False, [642]),
58+
5124: ('fsaverage', 4, False, [2562]),
59+
20484: ('fsaverage', 5, False, [10242]),
60+
81924: ('fsaverage', 6, False, [40962]),
61+
327684: ('fsaverage', 7, False, [163842]),
62+
}
63+
64+
65+
def unmask_and_upsample(lh, rh, space, icoorder, masked):
66+
nv = 4**icoorder * 10 + 2
67+
new_values = []
68+
for v, lr in zip([lh, rh], 'lr'):
69+
if masked:
70+
mask = np.load(os.path.join(DATA_DIR, f'mask_{space}_{lr}h.npy'))[:nv]
71+
vv = np.full((nv, ) + v.shape[1:], np.nan)
72+
vv[mask] = v
73+
else:
74+
vv = v
75+
76+
voronoi = np.load(os.path.join(DATA_DIR, f'voronoi_fsaverage_{lr}h_icoorder{icoorder}.npy'))
77+
vv = vv[voronoi]
78+
new_values.append(vv)
79+
new_values = np.concatenate(new_values, axis=0)
80+
return new_values
81+
82+
83+
def prepare_data(*values):
84+
while isinstance(values, (tuple, list)) and len(values) == 1:
85+
values = values[0]
86+
87+
if isinstance(values, (tuple, list)) and len(values) == 2:
88+
## separate left and right hemisphere
89+
lh, rh = values
90+
shapes = (lh.shape[0], rh.shape[0])
91+
space, icoorder, masked = GUESS_SEPARATE[shapes]
92+
new_values = unmask_and_upsample(lh, rh, space, icoorder, masked)
93+
94+
else:
95+
## combined hemispheres
96+
space, icoorder, masked, sections = GUESS_COMBINED[values.shape[0]]
97+
lh, rh = np.array_split(values, sections, axis=0)
98+
new_values = unmask_and_upsample(lh, rh, space, icoorder, masked)
99+
100+
return new_values
101+
102+
103+
def brain_plot(*values, vmax, vmin, cmap=None, medial_wall_color=[0.8, 0.8, 0.8, 1.0], background_color=[1.0, 1.0, 1.0, 0.0]):
104+
values = prepare_data(*values)
105+
nan_mask = np.isnan(values)
11106
r = (vmax - values) / (vmax - vmin)
12107
r = np.clip(r, 0.0, 1.0)
13108
cmap = cm.get_cmap(cmap)
14109
c = cmap(r)
15-
c = np.concatenate([c, [[0.8] * c.shape[1], [1.0] * c.shape[1]]], axis=0)
16-
img = c[mapping]
110+
c[nan_mask] = medial_wall_color
111+
c = np.concatenate([c, [_[:c.shape[1]] for _ in [medial_wall_color, background_color]]], axis=0)
112+
img = c[PLOT_MAPPING]
17113
return img
20.6 MB
Binary file not shown.
-5.14 MB
Binary file not shown.
10.1 KB
Binary file not shown.
10.1 KB
Binary file not shown.
40.1 KB
Binary file not shown.
40.1 KB
Binary file not shown.
160 KB
Binary file not shown.
160 KB
Binary file not shown.
1.25 MB
Binary file not shown.

0 commit comments

Comments
 (0)