11
11
12
12
plt , _ , _ = optional_package ('matplotlib.pyplot' )
13
13
mpl_img , _ , _ = optional_package ('matplotlib.image' )
14
+ mpl_patch , _ , _ = optional_package ('matplotlib.patches' )
14
15
15
16
16
17
class OrthoSlicer3D (object ):
@@ -61,7 +62,7 @@ def __init__(self, data, axes=None, aspect_ratio=(1, 1, 1), cmap='gray',
61
62
aspect_ratio = dict (x = ar [0 ], y = ar [1 ], z = ar [2 ])
62
63
data = np .asanyarray (data )
63
64
if data .ndim < 3 :
64
- raise RuntimeError ('data must have at least 3 dimensions' )
65
+ raise ValueError ('data must have at least 3 dimensions' )
65
66
self ._volume_dims = data .shape [3 :]
66
67
self ._current_vol_data = data [:, :, :, 0 ] if data .ndim > 3 else data
67
68
self ._data = data
@@ -147,20 +148,23 @@ def __init__(self, data, axes=None, aspect_ratio=(1, 1, 1), cmap='gray',
147
148
ax .axes .get_xaxis ().set_visible (False )
148
149
149
150
# Set up volumes axis
150
- if self .multi_volume :
151
+ if self .multi_volume and 'v' in self . _axes :
151
152
ax = self ._axes ['v' ]
152
153
ax .set_axis_bgcolor ('k' )
153
154
ax .set_title ('Volumes' )
154
155
n_vols = np .prod (self ._volume_dims )
155
- print (n_vols )
156
156
y = np .mean (np .mean (np .mean (self ._data , 0 ), 0 ), 0 ).ravel ()
157
157
y = np .concatenate ((y , [y [- 1 ]]))
158
158
x = np .arange (n_vols + 1 ) - 0.5
159
159
step = ax .step (x , y , where = 'post' , color = 'y' )[0 ]
160
160
ax .set_xticks (np .unique (np .linspace (0 , n_vols - 1 , 5 ).astype (int )))
161
161
ax .set_xlim (x [0 ], x [- 1 ])
162
- line = ax .plot ([0 , 0 ], ax .get_ylim (), color = (0 , 1 , 0 ))[0 ]
163
- self ._time_lines = [line , step ]
162
+ lims = ax .get_ylim ()
163
+ patch = mpl_patch .Rectangle ([- 0.5 , lims [0 ]], 1. , np .diff (lims )[0 ],
164
+ fill = True , facecolor = (0 , 1 , 0 ),
165
+ edgecolor = (0 , 1 , 0 ), alpha = 0.25 )
166
+ ax .add_patch (patch )
167
+ self ._time_lines = [patch , step ]
164
168
165
169
# setup pairwise connections between the slice dimensions
166
170
self ._click_update_keys = dict (x = 'yz' , y = 'xz' , z = 'xy' )
@@ -180,11 +184,9 @@ def __init__(self, data, axes=None, aspect_ratio=(1, 1, 1), cmap='gray',
180
184
fig .canvas .mpl_connect ('motion_notify_event' , self ._on_mousemove )
181
185
fig .canvas .mpl_connect ('button_press_event' , self ._on_mousemove )
182
186
fig .canvas .mpl_connect ('key_press_event' , self ._on_keypress )
183
- plt .draw ()
184
- self ._draw ()
185
187
186
188
def show (self ):
187
- """ Show the slicer; convenience for ``plt.show()``
189
+ """ Show the slicer in blocking mode ; convenience for ``plt.show()``
188
190
"""
189
191
plt .show ()
190
192
@@ -220,8 +222,8 @@ def set_indices(self, x=None, y=None, z=None, v=None):
220
222
draw = False
221
223
if v is not None :
222
224
if not self .multi_volume :
223
- raise RuntimeError ('cannot change volume index of '
224
- 'single-volume image' )
225
+ raise ValueError ('cannot change volume index of single-volume '
226
+ ' image' )
225
227
self ._set_vol_idx (v , draw = False ) # delay draw
226
228
draw = True
227
229
for key , val in zip ('zyx' , (z , y , x )):
@@ -239,7 +241,7 @@ def _set_vol_idx(self, idx, draw=True):
239
241
self ._current_vol_data = self ._data [:, :, :, self ._idx ['v' ]]
240
242
for key in 'xyz' :
241
243
self ._ims [key ].set_data (self ._get_slice (key ))
242
- self ._time_lines [0 ].set_xdata ([ self ._idx ['v' ]] * 2 )
244
+ self ._time_lines [0 ].set_x ( self ._idx ['v' ] - 0.5 )
243
245
if draw :
244
246
self ._draw ()
245
247
@@ -262,7 +264,6 @@ def _in_axis(self, event):
262
264
for key , ax in self ._axes .items ():
263
265
if event .inaxes is ax :
264
266
return key
265
- return None
266
267
267
268
def _on_scroll (self , event ):
268
269
assert event .button in ('up' , 'down' )
@@ -309,7 +310,7 @@ def _draw(self):
309
310
ax .figure .canvas .blit (ax .bbox )
310
311
for t in im .texts :
311
312
ax .draw_artist (t )
312
- if self .multi_volume :
313
+ if self .multi_volume and 'v' in self . _axes : # user might only pass 3
313
314
ax = self ._axes ['v' ]
314
315
ax .draw_artist (ax .patch )
315
316
for artist in self ._time_lines :
0 commit comments