Skip to content

Commit 10c4edf

Browse files
committed
Add a few enhancements to morphometry plotting
- Allow for a custom colormap - Allow for custom min/max - Plot bivariate measures (curv and sulc) with the midpoint at 0 - Compute the min and max of the colormap robustly - Allow morphometry colorbar to be optional
1 parent b27896e commit 10c4edf

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

surfer/viz.py

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

11651165
def add_morphometry(self, measure, grayscale=False, hemi=None,
1166+
colormap=None, min=None, max=None, colorbar=True,
11661167
remove_existing=True):
11671168
"""Add a morphometry overlay to the image.
11681169
@@ -1176,6 +1177,8 @@ def add_morphometry(self, measure, grayscale=False, hemi=None,
11761177
If None, it is assumed to belong to the hemipshere being
11771178
shown. If two hemispheres are being shown, data must exist
11781179
for both hemispheres.
1180+
colormap : str
1181+
Mayavi colormap name, or None to use a sensible default.
11791182
remove_existing : bool
11801183
If True (default), remove old annotations.
11811184
"""
@@ -1195,16 +1198,23 @@ def add_morphometry(self, measure, grayscale=False, hemi=None,
11951198
# Get rid of any old overlays
11961199
for m in self.morphometry_list:
11971200
m['surface'].remove()
1198-
m['colorbar'].visible = False
1201+
if m["colorbar"] is not None:
1202+
m['colorbar'].visible = False
11991203
self.morphometry_list = []
12001204
ml = self.morphometry_list
1205+
12011206
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")
1207+
1208+
if colormap is None:
1209+
# Preset colormaps
1210+
if grayscale:
1211+
colormap = "gray"
1212+
else:
1213+
colormap = dict(area="pink",
1214+
curv="RdBu",
1215+
jacobian_white="pink",
1216+
sulc="RdBu",
1217+
thickness="pink")[measure]
12081218

12091219
# Read in the morphometric data
12101220
morph_data = nib.freesurfer.read_morph_data(morph_file)
@@ -1214,24 +1224,27 @@ def add_morphometry(self, measure, grayscale=False, hemi=None,
12141224
ctx_idx = self.geo[hemi].labels["cortex"]
12151225

12161226
# 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]
1227+
min_default, max_default = np.percentile(morph_data[ctx_idx],
1228+
[2, 98])
1229+
if min is None:
1230+
min = min_default
1231+
if max is None:
1232+
max = max_default
1233+
1234+
# Use appropriate values for bivariate measures
1235+
if measure in ["curv", "sulc"]:
1236+
lim = np.max([abs(min), abs(max)])
1237+
min, max = -lim, lim
12211238

12221239
# Set up the Mayavi pipeline
12231240
morph_data = _prepare_data(morph_data)
12241241

1225-
if grayscale:
1226-
colormap = "gray"
1227-
else:
1228-
colormap = cmap_dict[measure]
1229-
12301242
for brain in self._brain_list:
12311243
if brain['hemi'] == hemi:
12321244
ml.append(brain['brain'].add_morphometry(morph_data,
12331245
colormap, measure,
1234-
min, max))
1246+
min, max,
1247+
colorbar))
12351248
self.morphometry_list = ml
12361249
self._toggle_render(True, views)
12371250

@@ -2469,7 +2482,8 @@ def add_label(self, label, label_name, color, alpha):
24692482
surf.module_manager.scalar_lut_manager.lut.table = cmap
24702483
return surf
24712484

2472-
def add_morphometry(self, morph_data, colormap, measure, min, max):
2485+
def add_morphometry(self, morph_data, colormap, measure,
2486+
min, max, colorbar):
24732487
"""Add a morphometry overlay to the image"""
24742488
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
24752489
self._geo.y,
@@ -2485,9 +2499,12 @@ def add_morphometry(self, morph_data, colormap, measure, min, max):
24852499
name=measure, figure=self._f)
24862500

24872501
# Get the colorbar
2488-
bar = mlab.scalarbar(surf)
2489-
self._format_cbar_text(bar)
2490-
bar.scalar_bar_representation.position2 = .8, 0.09
2502+
if colorbar:
2503+
bar = mlab.scalarbar(surf)
2504+
self._format_cbar_text(bar)
2505+
bar.scalar_bar_representation.position2 = .8, 0.09
2506+
else:
2507+
bar = None
24912508

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

0 commit comments

Comments
 (0)