def test_iter_axes_masked(): arr = seq_array((5,4,3,2)) a = np.ma.array(arr, mask=(arr % 2 == 0)) b = list(iter_axes(a, 3)) # All even numbers are masked assert_array_equal(a[:,:,:,0].mask, np.ones((5, 4, 3), dtype=bool)) assert_array_equal(a[:,:,:,1].mask, np.zeros((5, 4, 3), dtype=bool))
def test_iter_axes(): a = seq_array((5,4,3)) arrs = list(iter_axes(a, -1)) assert 3 == len(arrs) assert_array_equal(a[...,0], arrs[0]) assert_array_equal(a[...,1], arrs[1]) assert_array_equal(a[...,2], arrs[2])
def test_iter_axes_multiple(): a = seq_array((5,4,3,2)) arrs = list(iter_axes(a, [2,3])) assert 6 == len(arrs) assert_array_equal(a[:,:,0,0], arrs[0]) assert_array_equal(a[:,:,0,1], arrs[1]) assert_array_equal(a[:,:,1,0], arrs[2]) assert_array_equal(a[:,:,1,1], arrs[3]) assert_array_equal(a[:,:,2,0], arrs[4]) assert_array_equal(a[:,:,2,1], arrs[5])
def test_iter_axes_none(): a = np.ones((5,4)) assert 0 == len(list(iter_axes(a, [])))
def view(dicoms, groupby=tuple(), roi_filename=None, roi_tag=None, viewer=None, viewer_kws=None): """Display a dicomset with arrview Parameters ========== dicoms : iterable of dicoms An iterable of dicoms groupby Before displaying, group the dicoms (see data function) roi_filename : str Filename of rois to load. If not specified and the dicomset has rois, then those are loaded instead. roi_tag : str (default: '/') When using the rois from the dicomset, this is the roi tag to load. viewer : str Either arrview or matplotlib. viewer_kws : dict (default: {}) Keyword arguments to send to the viewer Returns ======= Displays the dicoms using arrview and returns the instance once the window closes. """ viewer = viewer or 'arrview' viewer_kws = viewer_kws or {} arr = data(dicoms, field='pixel_array', groupby=groupby) if viewer == 'matplotlib': assert 2 <= arr.ndim <= 4, 'matplotlib viewer can only handle 4 or less dims' import pylab from matplotlib import gridspec shape = arr.shape nrows = 1 if arr.ndim < 3 else shape[2] ncols = 1 if arr.ndim < 4 else shape[3] figure_kws = viewer_kws.get('figure_kws', {}) if 'figsize' not in figure_kws: aspect_ratio = ncols * shape[1] / float(nrows * shape[0]) height = 10 figure_kws['figsize'] = (height * aspect_ratio, height) fig = pylab.figure(**figure_kws) gs = gridspec.GridSpec(nrows, ncols, wspace=0, hspace=0) first_ax = None for spec, im in zip(iter(gs), iter_axes(arr, range(arr.ndim)[2:])): ax = pylab.subplot(spec, sharex=first_ax, sharey=first_ax) if first_ax is None: first_ax = ax ax.set_axis_off() ax.set_aspect('equal') ax.imshow(im, **viewer_kws.get('imshow_kws', {})) return fig elif viewer == 'arrview': import arrview dcm = dicoms.first if roi_filename is None and dcm.meta.get('roi_filename'): roi_filename = dcm.meta.roi_filename.get(roi_tag) else: roi_filename = os.path.dirname(dcm.filename) return arrview.view(arr, roi_filename=roi_filename, **viewer_kws) else: raise Exception('Unknown viewer %r' % viewer)