Skip to content

Commit 4e0165b

Browse files
committed
Merge pull request #118 from mwaskom/morph_colormaps
Add a few enhancements to morphometry plotting
2 parents a64f6b2 + 14ebea0 commit 4e0165b

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

surfer/viz.py

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,8 @@ def remove_labels(self, labels=None, hemi=None):
11631163
ll.remove()
11641164

11651165
def add_morphometry(self, measure, grayscale=False, hemi=None,
1166-
remove_existing=True):
1166+
remove_existing=True, colormap=None,
1167+
min=None, max=None, colorbar=True):
11671168
"""Add a morphometry overlay to the image.
11681169
11691170
Parameters
@@ -1178,6 +1179,14 @@ def add_morphometry(self, measure, grayscale=False, hemi=None,
11781179
for both hemispheres.
11791180
remove_existing : bool
11801181
If True (default), remove old annotations.
1182+
colormap : str
1183+
Mayavi colormap name, or None to use a sensible default.
1184+
min, max : floats
1185+
Endpoints for the colormap; if not provided the robust range
1186+
of the data is used.
1187+
colorbar : bool
1188+
If True, show a colorbar corresponding to the overlay data.
1189+
11811190
"""
11821191
hemis = self._check_hemis(hemi)
11831192
morph_files = []
@@ -1195,16 +1204,23 @@ def add_morphometry(self, measure, grayscale=False, hemi=None,
11951204
# Get rid of any old overlays
11961205
for m in self.morphometry_list:
11971206
m['surface'].remove()
1198-
m['colorbar'].visible = False
1207+
if m["colorbar"] is not None:
1208+
m['colorbar'].visible = False
11991209
self.morphometry_list = []
12001210
ml = self.morphometry_list
1211+
12011212
for hemi, morph_file in zip(hemis, morph_files):
1202-
# Preset colormaps
1203-
cmap_dict = dict(area="pink",
1204-
curv="RdBu",
1205-
jacobian_white="pink",
1206-
sulc="RdBu",
1207-
thickness="pink")
1213+
1214+
if colormap is None:
1215+
# Preset colormaps
1216+
if grayscale:
1217+
colormap = "gray"
1218+
else:
1219+
colormap = dict(area="pink",
1220+
curv="RdBu",
1221+
jacobian_white="pink",
1222+
sulc="RdBu",
1223+
thickness="pink")[measure]
12081224

12091225
# Read in the morphometric data
12101226
morph_data = nib.freesurfer.read_morph_data(morph_file)
@@ -1214,24 +1230,27 @@ def add_morphometry(self, measure, grayscale=False, hemi=None,
12141230
ctx_idx = self.geo[hemi].labels["cortex"]
12151231

12161232
# Get the display range
1217-
if measure == "thickness":
1218-
min, max = 1, 4
1219-
else:
1220-
min, max = stats.describe(morph_data[ctx_idx])[1]
1233+
min_default, max_default = np.percentile(morph_data[ctx_idx],
1234+
[2, 98])
1235+
if min is None:
1236+
min = min_default
1237+
if max is None:
1238+
max = max_default
1239+
1240+
# Use appropriate values for bivariate measures
1241+
if measure in ["curv", "sulc"]:
1242+
lim = np.max([abs(min), abs(max)])
1243+
min, max = -lim, lim
12211244

12221245
# Set up the Mayavi pipeline
12231246
morph_data = _prepare_data(morph_data)
12241247

1225-
if grayscale:
1226-
colormap = "gray"
1227-
else:
1228-
colormap = cmap_dict[measure]
1229-
12301248
for brain in self._brain_list:
12311249
if brain['hemi'] == hemi:
12321250
ml.append(brain['brain'].add_morphometry(morph_data,
12331251
colormap, measure,
1234-
min, max))
1252+
min, max,
1253+
colorbar))
12351254
self.morphometry_list = ml
12361255
self._toggle_render(True, views)
12371256

@@ -2469,7 +2488,8 @@ def add_label(self, label, label_name, color, alpha):
24692488
surf.module_manager.scalar_lut_manager.lut.table = cmap
24702489
return surf
24712490

2472-
def add_morphometry(self, morph_data, colormap, measure, min, max):
2491+
def add_morphometry(self, morph_data, colormap, measure,
2492+
min, max, colorbar):
24732493
"""Add a morphometry overlay to the image"""
24742494
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
24752495
self._geo.y,
@@ -2485,9 +2505,12 @@ def add_morphometry(self, morph_data, colormap, measure, min, max):
24852505
name=measure, figure=self._f)
24862506

24872507
# Get the colorbar
2488-
bar = mlab.scalarbar(surf)
2489-
self._format_cbar_text(bar)
2490-
bar.scalar_bar_representation.position2 = .8, 0.09
2508+
if colorbar:
2509+
bar = mlab.scalarbar(surf)
2510+
self._format_cbar_text(bar)
2511+
bar.scalar_bar_representation.position2 = .8, 0.09
2512+
else:
2513+
bar = None
24912514

24922515
# Fil in the morphometry dict
24932516
return dict(surface=surf, colorbar=bar, measure=measure)

0 commit comments

Comments
 (0)