def test_get_wksp_index_dist_and_label(self): # fail case self.assertRaises(RuntimeError, funcs.get_wksp_index_dist_and_label, self.ws2d_histo) # get info from a 2d workspace index, dist, kwargs = funcs.get_wksp_index_dist_and_label(self.ws2d_histo, specNum=2) self.assertEqual(index, 1) self.assertTrue(dist) self.assertEqual(kwargs['label'], 'ws2d_histo: spec 2') # get info from default spectrum in the 1d case index, dist, kwargs = funcs.get_wksp_index_dist_and_label(self.ws1d_point) self.assertEqual(index, 0) self.assertFalse(dist) self.assertEqual(kwargs['label'], 'ws1d_point: spec 1')
def plot(axes, workspace, *args, **kwargs): ''' 3D plots - line plots :param axes: class:`matplotlib.axes.Axes3D` object that will do the plotting :param workspace: :class:`mantid.api.MatrixWorkspace` or :class:`mantid.api.IMDHistoWorkspace` to extract the data from :param zdir: Which direction to use as z ('x', 'y' or 'z') when plotting a 2D set. :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 dimension to plot. *e.g.* to select the second axis to plot from a 3D volume use ``indices=(5, slice(None), 10)`` where the 5/10 are the bins selected for the other 2 axes. :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 second axis to plot from a 3D volume use ``slicepoint=(1.0, None, 2.0)`` where the 1.0/2.0 are the dimension selected for the other 2 axes. ''' if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs) (x, y, z) = get_md_data1d(workspace, normalization, indices) else: (wksp_index, distribution, kwargs) = get_wksp_index_dist_and_label(workspace, **kwargs) (x, z, _, _) = get_spectrum(workspace, wksp_index, distribution, withDy=False, withDx=False) y_val = workspace.getAxis(1).extractValues()[wksp_index] y = [y_val for _ in range(len(x))] # fill x size array with y value _set_labels_3d(axes, workspace) return axes.plot(x, y, z, *args, **kwargs)
def scatter(axes, workspace, *args, **kwargs): ''' Unpack mantid workspace and render it with matplotlib. ``args`` and ``kwargs`` are passed to :py:meth:`matplotlib.axes.Axes.scatter` after special keyword arguments are removed. This will automatically label the line according to the spectrum number unless specified otherwise. :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 specNum: spectrum number to plot if MatrixWorkspace :param wkspIndex: workspace index to plot if MatrixWorkspace :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 workspace is a MatrixWorkspace 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 For matrix workspaces with more than one spectra, either ``specNum`` or ``wkspIndex`` needs to be specified. Giving both will generate a :class:`RuntimeError`. There is no similar keyword for MDHistoWorkspaces. These type of workspaces have to have exactly one non integrated dimension ''' if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) (x, y, _) = get_md_data1d(workspace, normalization) else: (wkspIndex, distribution, kwargs) = get_wksp_index_dist_and_label(workspace, **kwargs) (x, y, _, _) = get_spectrum(workspace, wkspIndex, distribution) _setLabels1D(axes, workspace) return axes.scatter(x, y, *args, **kwargs)
def _get_data_for_plot(axes, workspace, kwargs, with_dy=False, with_dx=False): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs) (x, y, dy) = get_md_data1d(workspace, normalization, indices) dx = None axis = None else: axis = MantidAxType(kwargs.pop("axis", MantidAxType.SPECTRUM)) normalize_by_bin_width, kwargs = get_normalize_by_bin_width( workspace, axes, **kwargs) workspace_index, distribution, kwargs = get_wksp_index_dist_and_label( workspace, axis, **kwargs) if axis == MantidAxType.BIN: # Overwrite any user specified xlabel axes.set_xlabel("Spectrum") x, y, dy, dx = get_bins(workspace, workspace_index, with_dy) elif axis == MantidAxType.SPECTRUM: x, y, dy, dx = get_spectrum(workspace, workspace_index, normalize_by_bin_width, with_dy, with_dx) else: raise ValueError( "Axis {} is not a valid axis number.".format(axis)) indices = None return x, y, dy, dx, indices, axis, kwargs
def test_legend_uses_label_if_first_axis_is_numeric_axis(self): ws = CreateWorkspace([1], [1], NSpec=1) axis = mantid.api.NumericAxis.create(1) ws.replaceAxis(1, axis) ws.getAxis(1).setValue(0, 50) index, dist, kwargs = funcs.get_wksp_index_dist_and_label(ws, specNum=1) self.assertEqual(kwargs['label'], 'ws: 50')
def test_get_wksp_index_dist_and_label_for_bins(self): """ Tests that the workspace index, distribution and label are correctly retrieved. The label changes depending on the axis. """ ws = CreateSampleWorkspace() axis = MantidAxType.BIN res_workspace_index, res_distribution, res_kwargs = funcs.get_wksp_index_dist_and_label(ws, axis, wkspIndex=1) self.assertEqual(1, res_workspace_index) self.assertEqual(False, res_distribution) self.assertEqual(res_kwargs['label'], 'ws: bin 1') res_workspace_index, res_distribution, res_kwargs = funcs.get_wksp_index_dist_and_label(ws, axis, wkspIndex=0) self.assertEqual(0, res_workspace_index) self.assertEqual(False, res_distribution) self.assertEqual(res_kwargs['label'], 'ws: bin 0')
def scatter(axes, workspace, *args, **kwargs): ''' Unpack mantid workspace and render it with matplotlib. ``args`` and ``kwargs`` are passed to :py:meth:`matplotlib.axes.Axes.scatter` after special keyword arguments are removed. This will automatically label the line according to the spectrum number unless specified otherwise. :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 specNum: spectrum number to plot if MatrixWorkspace :param wkspIndex: workspace index to plot if MatrixWorkspace :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 workspace is a MatrixWorkspace 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 dimension to plot. *e.g.* to select the second axis to plot from a 3D volume use ``indices=(5, slice(None), 10)`` where the 5/10 are the bins selected for the other 2 axes. :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 second axis to plot from a 3D volume use ``slicepoint=(1.0, None, 2.0)`` where the 1.0/2.0 are the dimension selected for the other 2 axes. For matrix workspaces with more than one spectra, either ``specNum`` or ``wkspIndex`` needs to be specified. Giving both will generate a :class:`RuntimeError`. There is no similar keyword for MDHistoWorkspaces. These type of workspaces have to have exactly one non integrated dimension ''' if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs) (x, y, _) = get_md_data1d(workspace, normalization, indices) _setLabels1D(axes, workspace, indices) else: (wkspIndex, distribution, kwargs) = get_wksp_index_dist_and_label(workspace, **kwargs) (x, y, _, _) = get_spectrum(workspace, wkspIndex, distribution) _setLabels1D(axes, workspace) return axes.scatter(x, y, *args, **kwargs)
def plot(axes, workspace, *args, **kwargs): ''' 3D plots - line plots :param axes: class:`matplotlib.axes.Axes3D` object that will do the plotting :param workspace: :class:`mantid.api.MatrixWorkspace` or :class:`mantid.api.IMDHistoWorkspace` to extract the data from :param zdir: Which direction to use as z ('x', 'y' or 'z') when plotting a 2D set. ''' if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) (x, y, z) = get_md_data1d(workspace, normalization) else: (wksp_index, distribution, kwargs) = get_wksp_index_dist_and_label(workspace, **kwargs) (x, z, _, _) = get_spectrum(workspace, wksp_index, distribution, withDy=False, withDx=False) y_val = workspace.getAxis(1).extractValues()[wksp_index] y = [y_val for _ in range(len(x))] # fill x size array with y value _set_labels_3d(axes, workspace) return axes.plot(x, y, z, *args, **kwargs)
def _get_data_for_plot(axes, workspace, kwargs, with_dy=False, with_dx=False): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): (normalization, kwargs) = get_normalization(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs) (x, y, dy) = get_md_data1d(workspace, normalization, indices) dx = None else: axis = kwargs.pop("axis", MantidAxType.SPECTRUM) workspace_index, distribution, kwargs = get_wksp_index_dist_and_label(workspace, axis, **kwargs) if axis == MantidAxType.BIN: # Overwrite any user specified xlabel axes.set_xlabel("Spectrum") x, y, dy, dx = get_bins(workspace, workspace_index, with_dy) elif axis == MantidAxType.SPECTRUM: x, y, dy, dx = get_spectrum(workspace, workspace_index, distribution, with_dy, with_dx) else: raise ValueError("Axis {} is not a valid axis number.".format(axis)) indices = None return x, y, dy, dx, indices, kwargs