def test_get_matrix_2d_data_ragged_with_extent(self): x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_point_rag, False, histogram2D=True, extent=[2, 4, 1, 2], xbins=8, ybins=5) np.testing.assert_allclose( x, np.array([2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4])) np.testing.assert_allclose(y, np.array([1, 1.25, 1.5, 1.75, 2.0])) x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, False, histogram2D=True, extent=[2, 6, 5, 9], xbins=8, ybins=5) np.testing.assert_allclose( x, np.array([2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])) np.testing.assert_allclose(y, np.array([5, 6, 7, 8, 9])) x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, False, histogram2D=True, extent=[2, 6, -5, 9], xbins=8, ybins=5) np.testing.assert_allclose( x, np.array([2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])) np.testing.assert_allclose(y, np.array([-5, -1.5, 2, 5.5, 9])) # first z array is off the spectrum axis scale so should be masked np.testing.assert_array_equal(z[0].data, [np.nan] * 8) np.testing.assert_array_equal(z[0].mask, [1] * 8) np.testing.assert_array_equal(z[4].data, [2.0] * 8) np.testing.assert_array_equal(z[4].mask, [0] * 8)
def _resample_image(self, xbins=None, ybins=None): if self._resample_required: extent = self.get_extent() if xbins is None or ybins is None: xbins, ybins = self._calculate_bins_from_extent() x, y, data = get_matrix_2d_ragged(self.ws, self.normalize_by_bin_width, histogram2D=True, transpose=self.transpose, extent=extent, xbins=xbins, ybins=ybins, spec_info=self.spectrum_info, maxpooling=self._maxpooling) # Data is an MxN matrix. # If origin = upper extent is set as [xmin, xmax, ymax, ymin]. # Data[M,0] is the data at [xmin, ymin], which should be drawn at the top left corner, # whereas Data[0,0] is the data at [xmin, ymax], which should be drawn at the bottom left corner. # Origin upper starts drawing the data from top-left, which means we need to horizontally flip the matrix if self.origin == "upper": data = np.flip(data, 0) self.set_data(data) self._xbins = xbins self._ybins = ybins
def imshow(axes, workspace, *args, **kwargs): ''' Essentially the same as :meth:`matplotlib.axes.Axes.imshow`. :param axes: :class:`matplotlib.axes.Axes` object that will do the plotting :param workspace: :class:`mantid.api.MatrixWorkspace` or :class:`mantid.api.IMDHistoWorkspace` to extract the data from :param distribution: ``None`` (default) asks the workspace. ``False`` means divide by bin width. ``True`` means do not divide by bin width. Applies only when the the matrix workspace is a histogram. :param normalization: ``None`` (default) ask the workspace. Applies to MDHisto workspaces. It can override the value from displayNormalizationHisto. It checks only if the normalization is mantid.api.MDNormalization.NumEventsNormalization :param indices: Specify which slice of an MDHistoWorkspace to use when plotting. Needs to be a tuple and will be interpreted as a list of indices. You need to use ``slice(None)`` to select which dimensions to plot. *e.g.* to select the last two axes to plot from a 3D volume use ``indices=(5, slice(None), slice(None))`` where the 5 is the bin selected for the first axis. :param slicepoint: Specify which slice of an MDHistoWorkspace to use when plotting in the dimension units. You need to use ``None`` to select which dimension to plot. *e.g.* to select the last two axes to plot from a 3D volume use ``slicepoint=(1.0, None, None)`` where the 1.0 is the value of the dimension selected for the first axis. :param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable number of bins, the polygons will be aligned with the axes :param transpose: ``bool`` to transpose the x and y axes of the plotted dimensions of an MDHistoWorkspace ''' transpose = kwargs.pop('transpose', False) if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs) x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices, transpose) _setLabels2D(axes, workspace, indices, transpose) else: (aligned, kwargs) = check_resample_to_regular_grid(workspace, **kwargs) (normalize_by_bin_width, kwargs) = get_normalize_by_bin_width(workspace, axes, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs) if aligned: (x, y, z) = get_matrix_2d_ragged(workspace, normalize_by_bin_width, histogram2D=True, transpose=transpose) else: (x, y, z) = get_matrix_2d_data(workspace, distribution=distribution, histogram2D=True, transpose=transpose) _setLabels2D(axes, workspace, transpose=transpose, normalize_by_bin_width=normalize_by_bin_width) if 'extent' not in kwargs: if x.ndim == 2 and y.ndim == 2: kwargs['extent'] = [x[0, 0], x[0, -1], y[0, 0], y[-1, 0]] else: kwargs['extent'] = [x[0], x[-1], y[0], y[-1]] return mantid.plots.modest_image.imshow(axes, z, *args, **kwargs)
def test_get_matrix_2d_data_ragged_with_extent(self): x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_point_rag, False, histogram2D=True, extent=[2, 4, 1, 2], xbins=8, ybins=5) np.testing.assert_allclose( x, np.array([2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4])) np.testing.assert_allclose(y, np.array([1, 1.25, 1.5, 1.75, 2.0])) x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, False, histogram2D=True, extent=[2, 6, 5, 9], xbins=8, ybins=5) np.testing.assert_allclose( x, np.array([2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])) np.testing.assert_allclose(y, np.array([5, 6, 7, 8, 9]))
def test_get_matrix2d_ragged_without_maxpooling(self): x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_high_counting_detector, False, histogram2D=True, extent=[1, 4, 1, 1000], xbins=4, ybins=20, maxpooling=False) # 12th spectra is high counting but will skipped if we don't use maxpooling np.testing.assert_allclose(z[0], self.ws2d_high_counting_detector.readY(0))
def test_get_matrix2d_ragged_with_maxpooling(self): x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_high_counting_detector, False, histogram2D=True, extent=[1, 4, 1, 1000], xbins=4, ybins=20, maxpooling=True) # 12th spectra is high counting and will be the first entry in the data when we are using maxpooling np.testing.assert_allclose(z[0], self.ws2d_high_counting_detector.readY(12))
def test_get_matrix_2d_ragged_when_transpose_is_true(self): x, y, z_transposed = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, False, histogram2D=True, extent=[5, 9, 2, 6], xbins=8, ybins=5, transpose=True) np.testing.assert_allclose(x, np.array([5, 6, 7, 8, 9])) np.testing.assert_allclose( y, np.array([2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])) _, _, z = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, False, histogram2D=True, extent=[2, 6, 5, 9], xbins=8, ybins=5, transpose=False) np.testing.assert_allclose(z_transposed, z.T)
def test_get_matrix_2d_data_ragged(self): # contour from ragged point data x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_point_rag, True, histogram2D=False) np.testing.assert_allclose(x, np.array([1., 2., 3., 4., 5., 6., 7., 8.])) np.testing.assert_allclose(y, np.array([0.5, 1.5, 2.5])) # contour from ragged histo data x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, True, histogram2D=False) np.testing.assert_allclose( x, np.array( [1.5, 2.4375, 3.375, 4.3125, 5.25, 6.1875, 7.125, 8.0625, 9.])) np.testing.assert_allclose(y, np.array([4., 6., 8., 10.])) # mesh from ragged point data x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_point_rag, True, histogram2D=True) np.testing.assert_allclose( x, np.array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5])) np.testing.assert_allclose(y, np.array([0.5, 1.5, 2.5])) # mesh from ragged histo data x, y, z = funcs.get_matrix_2d_ragged(self.ws2d_histo_rag, True, histogram2D=True) np.testing.assert_allclose( x, np.array([ 1.03125, 1.96875, 2.90625, 3.84375, 4.78125, 5.71875, 6.65625, 7.59375, 8.53125, 9.46875 ])) np.testing.assert_allclose(y, np.array([4., 6, 8., 10.])) # check that fails for uneven data self.assertRaises(ValueError, funcs.get_matrix_2d_data, self.ws2d_point_uneven, True)