Skip to content

Commit dfb82b5

Browse files
committed
FIX: Better unlinking
1 parent 263820c commit dfb82b5

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

nibabel/spatialimages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,4 +730,5 @@ def plot(self):
730730
consider using viewer.show() (equivalently plt.show()) to show
731731
the figure.
732732
"""
733-
return OrthoSlicer3D(self.get_data(), self.get_affine())
733+
return OrthoSlicer3D(self.get_data(), self.get_affine(),
734+
title=self.get_filename())

nibabel/tests/test_viewers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from numpy.testing.decorators import skipif
1818
from numpy.testing import assert_array_equal
1919

20-
from nose.tools import assert_raises
20+
from nose.tools import assert_raises, assert_true
2121

2222
matplotlib, has_mpl = optional_package('matplotlib')[:2]
2323
needs_mpl = skipif(not has_mpl, 'These tests need matplotlib')
@@ -35,6 +35,7 @@ def test_viewer():
3535
data = data * np.array([1., 2.]) # give it a # of volumes > 1
3636
v = OrthoSlicer3D(data)
3737
assert_array_equal(v.position, (0, 0, 0))
38+
assert_true('OrthoSlicer3D' in repr(v))
3839

3940
# fake some events, inside and outside axes
4041
v._on_scroll(nt('event', 'button inaxes key')('up', None, None))
@@ -49,6 +50,7 @@ def test_viewer():
4950
v.set_volume_idx(1)
5051
v.set_volume_idx(1) # should just pass
5152
v.close()
53+
v._draw() # should be safe
5254

5355
# non-multi-volume
5456
v = OrthoSlicer3D(data[:, :, :, 0])

nibabel/viewers.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class OrthoSlicer3D(object):
3232
"""
3333
# Skip doctest above b/c not all systems have mpl installed
3434
def __init__(self, data, affine=None, axes=None, cmap='gray',
35-
pcnt_range=(1., 99.), figsize=(8, 8)):
35+
pcnt_range=(1., 99.), figsize=(8, 8), title=None):
3636
"""
3737
Parameters
3838
----------
@@ -60,6 +60,8 @@ def __init__(self, data, affine=None, axes=None, cmap='gray',
6060
plt, _, _ = optional_package('matplotlib.pyplot')
6161
mpl_img, _, _ = optional_package('matplotlib.image')
6262
mpl_patch, _, _ = optional_package('matplotlib.patches')
63+
self._title = title
64+
self._closed = False
6365

6466
data = np.asanyarray(data)
6567
if data.ndim < 3:
@@ -107,6 +109,8 @@ def __init__(self, data, affine=None, axes=None, cmap='gray',
107109
if self.n_volumes <= 1:
108110
fig.delaxes(self._axes[3])
109111
self._axes.pop(-1)
112+
if self._title is not None:
113+
fig.canvas.set_window_title(str(title))
110114
else:
111115
self._axes = [axes[0], axes[1], axes[2]]
112116
if len(axes) > 3:
@@ -196,6 +200,14 @@ def __init__(self, data, affine=None, axes=None, cmap='gray',
196200
self._set_position(0., 0., 0.)
197201
self._draw()
198202

203+
def __repr__(self):
204+
title = '' if self._title is None else ('%s ' % self._title)
205+
vol = '' if self.n_volumes <= 1 else (', %s' % self.n_volumes)
206+
r = ('<%s: %s(%s, %s, %s%s)>'
207+
% (self.__class__.__name__, title, self._sizes[0], self._sizes[1],
208+
self._sizes[2], vol))
209+
return r
210+
199211
# User-level functions ###################################################
200212
def show(self):
201213
"""Show the slicer in blocking mode; convenience for ``plt.show()``
@@ -213,8 +225,9 @@ def close(self):
213225

214226
def _cleanup(self):
215227
"""Clean up before closing"""
216-
for link in self._links:
217-
link()._unlink(self)
228+
self._closed = True
229+
for link in list(self._links): # make a copy before iterating
230+
self._unlink(link())
218231

219232
@property
220233
def n_volumes(self):
@@ -423,6 +436,8 @@ def _on_keypress(self, event):
423436

424437
def _draw(self):
425438
"""Update all four (or three) plots"""
439+
if self._closed: # make sure we don't draw when we shouldn't
440+
return
426441
for ii in range(3):
427442
ax = self._axes[ii]
428443
ax.draw_artist(self._ims[ii])

0 commit comments

Comments
 (0)