Skip to content

Commit 65a22bf

Browse files
christianbrodbecklarsoner
authored andcommitted
FIX: Reset zoom (#242)
* FIX Brain: don't reset zoom * TEST * DOC: Brain.show_view() parameters and return values * also fix Brain.add_contour_overlay() * TEST: fix for test backend * TEST: view persistence more generally
1 parent baca853 commit 65a22bf

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

surfer/tests/test_viz.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ def _set_backend(backend=None):
5151
mlab.options.backend = backend
5252

5353

54+
def get_view(brain):
55+
"""Setup for view persistence test"""
56+
fig = brain._figures[0][0]
57+
if mlab.options.backend == 'test':
58+
return
59+
fig.scene.camera.parallel_scale = 50
60+
assert fig.scene.camera.parallel_scale == 50
61+
view, roll = brain.show_view()
62+
return fig.scene.camera.parallel_scale, view, roll
63+
64+
65+
def check_view(brain, view):
66+
"""Test view persistence"""
67+
fig = brain._figures[0][0]
68+
if mlab.options.backend == 'test':
69+
return
70+
parallel_scale, view, roll = view
71+
assert fig.scene.camera.parallel_scale == parallel_scale
72+
view_now, roll_now = brain.show_view()
73+
assert view_now[:3] == view[:3]
74+
assert_array_equal(view_now[3], view[3])
75+
assert roll_now == roll
76+
77+
5478
@requires_fsaverage
5579
def test_offscreen():
5680
"""Test offscreen rendering."""
@@ -146,10 +170,15 @@ def test_annot():
146170
borders = [True, False, 2]
147171
alphas = [1, 0.5]
148172
brain = Brain(*std_args)
173+
view = get_view(brain)
174+
149175
for a, b, p in zip(annots, borders, alphas):
150176
brain.add_annotation(a, b, p)
177+
check_view(brain, view)
178+
151179
brain.set_surf('white')
152-
assert_raises(ValueError, brain.add_annotation, 'aparc', borders=-1)
180+
with pytest.raises(ValueError):
181+
brain.add_annotation('aparc', borders=-1)
153182

154183
subj_dir = utils._get_subjects_dir()
155184
annot_path = pjoin(subj_dir, subject_id, 'label', 'lh.aparc.a2009s.annot')
@@ -164,12 +193,16 @@ def test_contour():
164193
"""Test plotting of contour overlay."""
165194
_set_backend()
166195
brain = Brain(*std_args)
196+
view = get_view(brain)
197+
167198
overlay_file = pjoin(data_dir, "lh.sig.nii.gz")
168199
brain.add_contour_overlay(overlay_file)
169200
brain.add_contour_overlay(overlay_file, max=20, n_contours=9,
170201
line_width=2)
171202
brain.contour['surface'].actor.property.line_width = 1
172203
brain.contour['surface'].contour.number_of_contours = 10
204+
205+
check_view(brain, view)
173206
brain.close()
174207

175208

@@ -228,7 +261,10 @@ def test_label():
228261
hemi = "lh"
229262
surf = "inflated"
230263
brain = Brain(subject_id, hemi, surf)
264+
view = get_view(brain)
265+
231266
brain.add_label("BA1")
267+
check_view(brain, view)
232268
brain.add_label("BA1", color="blue", scalar_thresh=.5)
233269
subj_dir = utils._get_subjects_dir()
234270
label_file = pjoin(subj_dir, subject_id,

surfer/viz.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,11 +1768,6 @@ def show_view(self, view=None, roll=None, distance=None, row=-1, col=-1):
17681768
brain surface to view (one of 'lateral', 'medial', 'rostral',
17691769
'caudal', 'dorsal', 'ventral', 'frontal', 'parietal') or kwargs to
17701770
pass to :func:`mayavi.mlab.view()`.
1771-
1772-
Returns
1773-
-------
1774-
view : tuple
1775-
tuple returned from mlab.view
17761771
roll : float
17771772
camera roll
17781773
distance : float | 'auto' | None
@@ -1781,6 +1776,13 @@ def show_view(self, view=None, roll=None, distance=None, row=-1, col=-1):
17811776
Row index of which brain to use
17821777
col : int
17831778
Column index of which brain to use
1779+
1780+
Returns
1781+
-------
1782+
view : tuple
1783+
tuple returned from mlab.view
1784+
roll : float
1785+
camera roll returned from mlab.roll
17841786
"""
17851787
return self.brain_matrix[row][col].show_view(view, roll, distance)
17861788

@@ -3125,7 +3127,8 @@ def add_overlay(self, old):
31253127
pos_thresh = threshold_filter(mesh, low=old.pos_lims[0])
31263128
pos = mlab.pipeline.surface(
31273129
pos_thresh, colormap="YlOrRd", figure=self._f,
3128-
vmin=old.pos_lims[1], vmax=old.pos_lims[2])
3130+
vmin=old.pos_lims[1], vmax=old.pos_lims[2],
3131+
reset_zoom=False)
31293132
pos.actor.property.backface_culling = False
31303133
pos_bar = mlab.scalarbar(pos, nb_labels=5)
31313134
pos_bar.reverse_lut = True
@@ -3140,7 +3143,8 @@ def add_overlay(self, old):
31403143
neg_thresh = threshold_filter(mesh, up=old.neg_lims[0])
31413144
neg = mlab.pipeline.surface(
31423145
neg_thresh, colormap="PuBu", figure=self._f,
3143-
vmin=old.neg_lims[1], vmax=old.neg_lims[2])
3146+
vmin=old.neg_lims[1], vmax=old.neg_lims[2],
3147+
reset_zoom=False)
31443148
neg.actor.property.backface_culling = False
31453149
neg_bar = mlab.scalarbar(neg, nb_labels=5)
31463150
neg_bar.scalar_bar_representation.position = (0.05, 0.01)
@@ -3196,7 +3200,7 @@ def add_data(self, array, fmin, fmid, fmax, thresh, lut, colormap, alpha,
31963200
with warnings.catch_warnings(record=True):
31973201
surf = mlab.pipeline.surface(
31983202
pipe, colormap=colormap, vmin=fmin, vmax=fmax,
3199-
opacity=float(alpha), figure=self._f)
3203+
opacity=float(alpha), figure=self._f, reset_zoom=False)
32003204
surf.actor.property.backface_culling = False
32013205

32023206
# apply look up table if given
@@ -3226,7 +3230,8 @@ def add_annotation(self, annot, ids, cmap):
32263230
# Add scalar values to dataset
32273231
array_id, pipe = self._add_scalar_data(ids)
32283232
with warnings.catch_warnings(record=True):
3229-
surf = mlab.pipeline.surface(pipe, name=annot, figure=self._f)
3233+
surf = mlab.pipeline.surface(pipe, name=annot, figure=self._f,
3234+
reset_zoom=False)
32303235
surf.actor.property.backface_culling = False
32313236

32323237
# Set the color table
@@ -3242,7 +3247,8 @@ def add_label(self, label, label_name, color, alpha):
32423247
from matplotlib.colors import colorConverter
32433248
array_id, pipe = self._add_scalar_data(label)
32443249
with warnings.catch_warnings(record=True):
3245-
surf = mlab.pipeline.surface(pipe, name=label_name, figure=self._f)
3250+
surf = mlab.pipeline.surface(pipe, name=label_name, figure=self._f,
3251+
reset_zoom=False)
32463252
surf.actor.property.backface_culling = False
32473253
color = colorConverter.to_rgba(color, alpha)
32483254
cmap = np.array([(0, 0, 0, 0,), color])
@@ -3258,9 +3264,9 @@ def add_morphometry(self, morph_data, colormap, measure,
32583264
"""Add a morphometry overlay to the image"""
32593265
array_id, pipe = self._add_scalar_data(morph_data)
32603266
with warnings.catch_warnings(record=True):
3261-
surf = mlab.pipeline.surface(pipe, colormap=colormap,
3262-
vmin=min, vmax=max,
3263-
name=measure, figure=self._f)
3267+
surf = mlab.pipeline.surface(
3268+
pipe, colormap=colormap, vmin=min, vmax=max, name=measure,
3269+
figure=self._f, reset_zoom=False)
32643270

32653271
# Get the colorbar
32663272
if colorbar:
@@ -3291,8 +3297,9 @@ def add_contour_overlay(self, scalar_data, min=None, max=None,
32913297
array_id, pipe = self._add_scalar_data(scalar_data)
32923298
with warnings.catch_warnings(record=True):
32933299
thresh = threshold_filter(pipe, low=min)
3294-
surf = mlab.pipeline.contour_surface(thresh, contours=n_contours,
3295-
line_width=line_width)
3300+
surf = mlab.pipeline.contour_surface(
3301+
thresh, contours=n_contours, line_width=line_width,
3302+
reset_zoom=False)
32963303
if lut is not None:
32973304
l_m = surf.module_manager.scalar_lut_manager
32983305
l_m.load_lut_from_list(lut / 255.)

0 commit comments

Comments
 (0)