Skip to content
Snippets Groups Projects
Unverified Commit c79a3f58 authored by Gagik Vardanyan's avatar Gagik Vardanyan Committed by GitHub
Browse files

Merge pull request #25655 from rosswhitfield/sliceviewer_transpose

Allow transposing dimensions of 2D plots in mantid.plots
parents 1584522c f6f1cdc0
No related branches found
No related tags found
No related merge requests found
...@@ -315,7 +315,7 @@ def get_bins(workspace, wkspIndex, withDy=False): ...@@ -315,7 +315,7 @@ def get_bins(workspace, wkspIndex, withDy=False):
return x, y, dy, dx return x, y, dy, dx
def get_md_data2d_bin_bounds(workspace, normalization, indices=None): def get_md_data2d_bin_bounds(workspace, normalization, indices=None, transpose=False):
""" """
Function to transform data in an MDHisto workspace with exactly Function to transform data in an MDHisto workspace with exactly
two non-integrated dimension into arrays of bin boundaries in each two non-integrated dimension into arrays of bin boundaries in each
...@@ -325,10 +325,13 @@ def get_md_data2d_bin_bounds(workspace, normalization, indices=None): ...@@ -325,10 +325,13 @@ def get_md_data2d_bin_bounds(workspace, normalization, indices=None):
""" """
coordinate, data, _ = get_md_data(workspace, normalization, indices, withError=False) coordinate, data, _ = get_md_data(workspace, normalization, indices, withError=False)
assert len(coordinate) == 2, 'The workspace is not 2D' assert len(coordinate) == 2, 'The workspace is not 2D'
return coordinate[0], coordinate[1], data if transpose:
return coordinate[1], coordinate[0], data.T
else:
return coordinate[0], coordinate[1], data
def get_md_data2d_bin_centers(workspace, normalization, indices=None): def get_md_data2d_bin_centers(workspace, normalization, indices=None, transpose=False):
""" """
Function to transform data in an MDHisto workspace with exactly Function to transform data in an MDHisto workspace with exactly
two non-integrated dimension into arrays of bin centers in each two non-integrated dimension into arrays of bin centers in each
...@@ -337,7 +340,7 @@ def get_md_data2d_bin_centers(workspace, normalization, indices=None): ...@@ -337,7 +340,7 @@ def get_md_data2d_bin_centers(workspace, normalization, indices=None):
Note: return coordinates are 1d vectors. Use numpy.meshgrid to generate 2d versions Note: return coordinates are 1d vectors. Use numpy.meshgrid to generate 2d versions
""" """
x, y, data = get_md_data2d_bin_bounds(workspace, normalization, indices) x, y, data = get_md_data2d_bin_bounds(workspace, normalization, indices, transpose)
x = points_from_boundaries(x) x = points_from_boundaries(x)
y = points_from_boundaries(y) y = points_from_boundaries(y)
return x, y, data return x, y, data
...@@ -366,7 +369,7 @@ def common_x(arr): ...@@ -366,7 +369,7 @@ def common_x(arr):
return numpy.all(arr == arr[0, :], axis=(1, 0)) return numpy.all(arr == arr[0, :], axis=(1, 0))
def get_matrix_2d_ragged(workspace, distribution, histogram2D=False): def get_matrix_2d_ragged(workspace, distribution, histogram2D=False, transpose=False):
num_hist = workspace.getNumberHistograms() num_hist = workspace.getNumberHistograms()
delta = numpy.finfo(numpy.float64).max delta = numpy.finfo(numpy.float64).max
min_value = numpy.finfo(numpy.float64).max min_value = numpy.finfo(numpy.float64).max
...@@ -395,10 +398,13 @@ def get_matrix_2d_ragged(workspace, distribution, histogram2D=False): ...@@ -395,10 +398,13 @@ def get_matrix_2d_ragged(workspace, distribution, histogram2D=False):
x = mantid.plots.helperfunctions.boundaries_from_points(x_centers) x = mantid.plots.helperfunctions.boundaries_from_points(x_centers)
else: else:
x = x_centers x = x_centers
return x,y,z if transpose:
return y.T,x.T,z.T
else:
return x,y,z
def get_matrix_2d_data(workspace, distribution, histogram2D=False): def get_matrix_2d_data(workspace, distribution, histogram2D=False, transpose=False):
''' '''
Get all data from a Matrix workspace that has the same number of bins Get all data from a Matrix workspace that has the same number of bins
in every spectrum. It is used for 2D plots in every spectrum. It is used for 2D plots
...@@ -446,7 +452,10 @@ def get_matrix_2d_data(workspace, distribution, histogram2D=False): ...@@ -446,7 +452,10 @@ def get_matrix_2d_data(workspace, distribution, histogram2D=False):
y = points_from_boundaries(y) y = points_from_boundaries(y)
y = numpy.tile(y, x.shape[1]).reshape(x.shape[1], x.shape[0]).transpose() y = numpy.tile(y, x.shape[1]).reshape(x.shape[1], x.shape[0]).transpose()
z = numpy.ma.masked_invalid(z) z = numpy.ma.masked_invalid(z)
return x, y, z if transpose:
return y.T,x.T,z.T
else:
return x,y,z
def get_uneven_data(workspace, distribution): def get_uneven_data(workspace, distribution):
......
...@@ -46,13 +46,17 @@ def _setLabels1D(axes, workspace, indices=None): ...@@ -46,13 +46,17 @@ def _setLabels1D(axes, workspace, indices=None):
axes.set_ylabel(labels[0]) axes.set_ylabel(labels[0])
def _setLabels2D(axes, workspace, indices=None): def _setLabels2D(axes, workspace, indices=None, transpose=False):
''' '''
helper function to automatically set axes labels for 2D plots helper function to automatically set axes labels for 2D plots
''' '''
labels = get_axes_labels(workspace, indices) labels = get_axes_labels(workspace, indices)
axes.set_xlabel(labels[1]) if transpose:
axes.set_ylabel(labels[2]) axes.set_xlabel(labels[2])
axes.set_ylabel(labels[1])
else:
axes.set_xlabel(labels[1])
axes.set_ylabel(labels[2])
axes.set_title(labels[-1]) axes.set_title(labels[-1])
...@@ -143,7 +147,6 @@ def plot(axes, workspace, *args, **kwargs): ...@@ -143,7 +147,6 @@ def plot(axes, workspace, *args, **kwargs):
axis to plot from a 3D volume use ``slicepoint=(1.0, None, 2.0)`` where the 1.0/2.0 are 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. the dimension selected for the other 2 axes.
For matrix workspaces with more than one spectra, either ``specNum`` or ``wkspIndex`` 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 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 keyword for MDHistoWorkspaces. These type of workspaces have to have exactly one non integrated
...@@ -264,16 +267,18 @@ def contour(axes, workspace, *args, **kwargs): ...@@ -264,16 +267,18 @@ def contour(axes, workspace, *args, **kwargs):
You need to use ``None`` to select which dimension to plot. *e.g.* to select the last 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 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. the value of the dimension selected for the first axis.
: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): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x, y, z = get_md_data2d_bin_centers(workspace, normalization, indices) x, y, z = get_md_data2d_bin_centers(workspace, normalization, indices, transpose)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose=transpose)
return axes.contour(x, y, z, *args, **kwargs) return axes.contour(x, y, z, *args, **kwargs)
...@@ -301,16 +306,18 @@ def contourf(axes, workspace, *args, **kwargs): ...@@ -301,16 +306,18 @@ def contourf(axes, workspace, *args, **kwargs):
You need to use ``None`` to select which dimension to plot. *e.g.* to select the last 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 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. the value of the dimension selected for the first axis.
: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): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x, y, z = get_md_data2d_bin_centers(workspace, normalization, indices) x, y, z = get_md_data2d_bin_centers(workspace, normalization, indices, transpose)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose=transpose)
return axes.contourf(x, y, z, *args, **kwargs) return axes.contourf(x, y, z, *args, **kwargs)
...@@ -385,12 +392,14 @@ def pcolor(axes, workspace, *args, **kwargs): ...@@ -385,12 +392,14 @@ def pcolor(axes, workspace, *args, **kwargs):
the value of the dimension selected for the first axis. the value of the dimension selected for the first axis.
:param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable :param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable
number of bins, the polygons will be aligned with the axes 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): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices) x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices, transpose)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(aligned, kwargs) = get_data_uneven_flag(workspace, **kwargs) (aligned, kwargs) = get_data_uneven_flag(workspace, **kwargs)
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
...@@ -398,8 +407,8 @@ def pcolor(axes, workspace, *args, **kwargs): ...@@ -398,8 +407,8 @@ def pcolor(axes, workspace, *args, **kwargs):
kwargs['pcolortype'] = '' kwargs['pcolortype'] = ''
return _pcolorpieces(axes, workspace, distribution, *args, **kwargs) return _pcolorpieces(axes, workspace, distribution, *args, **kwargs)
else: else:
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
return axes.pcolor(x, y, z, *args, **kwargs) return axes.pcolor(x, y, z, *args, **kwargs)
...@@ -427,12 +436,14 @@ def pcolorfast(axes, workspace, *args, **kwargs): ...@@ -427,12 +436,14 @@ def pcolorfast(axes, workspace, *args, **kwargs):
the value of the dimension selected for the first axis. the value of the dimension selected for the first axis.
:param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable :param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable
number of bins, the polygons will be aligned with the axes 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): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x, y, z, = get_md_data2d_bin_bounds(workspace, normalization, indices) x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices, transpose)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(aligned, kwargs) = get_data_uneven_flag(workspace, **kwargs) (aligned, kwargs) = get_data_uneven_flag(workspace, **kwargs)
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
...@@ -440,8 +451,8 @@ def pcolorfast(axes, workspace, *args, **kwargs): ...@@ -440,8 +451,8 @@ def pcolorfast(axes, workspace, *args, **kwargs):
kwargs['pcolortype'] = 'fast' kwargs['pcolortype'] = 'fast'
return _pcolorpieces(axes, workspace, distribution, *args, **kwargs) return _pcolorpieces(axes, workspace, distribution, *args, **kwargs)
else: else:
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
return axes.pcolorfast(x, y, z, *args, **kwargs) return axes.pcolorfast(x, y, z, *args, **kwargs)
...@@ -469,12 +480,14 @@ def pcolormesh(axes, workspace, *args, **kwargs): ...@@ -469,12 +480,14 @@ def pcolormesh(axes, workspace, *args, **kwargs):
the value of the dimension selected for the first axis. the value of the dimension selected for the first axis.
:param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable :param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable
number of bins, the polygons will be aligned with the axes 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): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices) x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices, transpose)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(aligned, kwargs) = get_data_uneven_flag(workspace, **kwargs) (aligned, kwargs) = get_data_uneven_flag(workspace, **kwargs)
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
...@@ -482,8 +495,8 @@ def pcolormesh(axes, workspace, *args, **kwargs): ...@@ -482,8 +495,8 @@ def pcolormesh(axes, workspace, *args, **kwargs):
kwargs['pcolortype'] = 'mesh' kwargs['pcolortype'] = 'mesh'
return _pcolorpieces(axes, workspace, distribution, *args, **kwargs) return _pcolorpieces(axes, workspace, distribution, *args, **kwargs)
else: else:
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
return axes.pcolormesh(x, y, z, *args, **kwargs) return axes.pcolormesh(x, y, z, *args, **kwargs)
...@@ -511,20 +524,22 @@ def imshow(axes, workspace, *args, **kwargs): ...@@ -511,20 +524,22 @@ def imshow(axes, workspace, *args, **kwargs):
the value of the dimension selected for the first axis. the value of the dimension selected for the first axis.
:param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable :param axisaligned: ``False`` (default). If ``True``, or if the workspace has a variable
number of bins, the polygons will be aligned with the axes 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): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x, y, z, = get_md_data2d_bin_bounds(workspace, normalization, indices) x, y, z = get_md_data2d_bin_bounds(workspace, normalization, indices, transpose)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(uneven_bins, kwargs) = get_data_uneven_flag(workspace, **kwargs) (uneven_bins, kwargs) = get_data_uneven_flag(workspace, **kwargs)
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
if check_resample_to_regular_grid(workspace): if check_resample_to_regular_grid(workspace):
(x, y, z) = get_matrix_2d_ragged(workspace, distribution, histogram2D=True) (x, y, z) = get_matrix_2d_ragged(workspace, distribution, histogram2D=True, transpose=transpose)
else: else:
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=True, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
if 'extent' not in kwargs: if 'extent' not in kwargs:
if x.ndim == 2 and y.ndim == 2: if x.ndim == 2 and y.ndim == 2:
kwargs['extent'] = [x[0, 0], x[0, -1], y[0, 0], y[-1, 0]] kwargs['extent'] = [x[0, 0], x[0, -1], y[0, 0], y[-1, 0]]
...@@ -557,19 +572,21 @@ def tripcolor(axes, workspace, *args, **kwargs): ...@@ -557,19 +572,21 @@ def tripcolor(axes, workspace, *args, **kwargs):
:param normalization: ``None`` (default) ask the workspace. Applies to MDHisto workspaces. It can override :param normalization: ``None`` (default) ask the workspace. Applies to MDHisto workspaces. It can override
the value from displayNormalizationHisto. It checks only if the value from displayNormalizationHisto. It checks only if
the normalization is mantid.api.MDNormalization.NumEventsNormalization the normalization is mantid.api.MDNormalization.NumEventsNormalization
:param transpose: ``bool`` to transpose the x and y axes of the plotted dimensions of an MDHistoWorkspace
See :meth:`matplotlib.axes.Axes.tripcolor` for more information. See :meth:`matplotlib.axes.Axes.tripcolor` for more information.
''' '''
transpose = kwargs.pop('transpose', False)
if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
x_temp, y_temp, z = get_md_data2d_bin_centers(workspace, normalization, indices) x_temp, y_temp, z = get_md_data2d_bin_centers(workspace, normalization, indices, transpose)
x, y = numpy.meshgrid(x_temp, y_temp) x, y = numpy.meshgrid(x_temp, y_temp)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
return axes.tripcolor(x.ravel(), y.ravel(), z.ravel(), *args, **kwargs) return axes.tripcolor(x.ravel(), y.ravel(), z.ravel(), *args, **kwargs)
...@@ -598,19 +615,21 @@ def tricontour(axes, workspace, *args, **kwargs): ...@@ -598,19 +615,21 @@ def tricontour(axes, workspace, *args, **kwargs):
:param normalization: ``None`` (default) ask the workspace. Applies to MDHisto workspaces. It can override :param normalization: ``None`` (default) ask the workspace. Applies to MDHisto workspaces. It can override
the value from displayNormalizationHisto. It checks only if the value from displayNormalizationHisto. It checks only if
the normalization is mantid.api.MDNormalization.NumEventsNormalization the normalization is mantid.api.MDNormalization.NumEventsNormalization
:param transpose: ``bool`` to transpose the x and y axes of the plotted dimensions of an MDHistoWorkspace
See :meth:`matplotlib.axes.Axes.tricontour` for more information. See :meth:`matplotlib.axes.Axes.tricontour` for more information.
''' '''
transpose = kwargs.pop('transpose', False)
if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
(x_temp, y_temp, z) = get_md_data2d_bin_centers(workspace, normalization, indices) x_temp, y_temp, z = get_md_data2d_bin_centers(workspace, normalization, indices, transpose)
(x, y) = numpy.meshgrid(x_temp, y_temp) x, y = numpy.meshgrid(x_temp, y_temp)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
# tricontour segfaults if many z values are not finite # tricontour segfaults if many z values are not finite
# https://github.com/matplotlib/matplotlib/issues/10167 # https://github.com/matplotlib/matplotlib/issues/10167
x = x.ravel() x = x.ravel()
...@@ -648,19 +667,21 @@ def tricontourf(axes, workspace, *args, **kwargs): ...@@ -648,19 +667,21 @@ def tricontourf(axes, workspace, *args, **kwargs):
You need to use ``None`` to select which dimension to plot. *e.g.* to select the last 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 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. the value of the dimension selected for the first axis.
:param transpose: ``bool`` to transpose the x and y axes of the plotted dimensions of an MDHistoWorkspace
See :meth:`matplotlib.axes.Axes.tricontourf` for more information. See :meth:`matplotlib.axes.Axes.tricontourf` for more information.
''' '''
transpose = kwargs.pop('transpose', False)
if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace): if isinstance(workspace, mantid.dataobjects.MDHistoWorkspace):
(normalization, kwargs) = get_normalization(workspace, **kwargs) (normalization, kwargs) = get_normalization(workspace, **kwargs)
indices, kwargs = get_indices(workspace, **kwargs) indices, kwargs = get_indices(workspace, **kwargs)
(x_temp, y_temp, z) = get_md_data2d_bin_centers(workspace, normalization, indices) x_temp, y_temp, z = get_md_data2d_bin_centers(workspace, normalization, indices, transpose)
(x, y) = numpy.meshgrid(x_temp, y_temp) x, y = numpy.meshgrid(x_temp, y_temp)
_setLabels2D(axes, workspace, indices) _setLabels2D(axes, workspace, indices, transpose)
else: else:
(distribution, kwargs) = get_distribution(workspace, **kwargs) (distribution, kwargs) = get_distribution(workspace, **kwargs)
(x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False) (x, y, z) = get_matrix_2d_data(workspace, distribution, histogram2D=False, transpose=transpose)
_setLabels2D(axes, workspace) _setLabels2D(axes, workspace, transpose)
# tricontourf segfaults if many z values are not finite # tricontourf segfaults if many z values are not finite
# https://github.com/matplotlib/matplotlib/issues/10167 # https://github.com/matplotlib/matplotlib/issues/10167
x = x.ravel() x = x.ravel()
......
...@@ -226,6 +226,15 @@ class HelperFunctionsTest(unittest.TestCase): ...@@ -226,6 +226,15 @@ class HelperFunctionsTest(unittest.TestCase):
np.testing.assert_allclose(y, np.array([-8, -4, 0, 4, 8]), atol=1e-10) np.testing.assert_allclose(y, np.array([-8, -4, 0, 4, 8]), atol=1e-10)
np.testing.assert_allclose(data, np.arange(25).reshape(5, 5) * 0.1, atol=1e-10) np.testing.assert_allclose(data, np.arange(25).reshape(5, 5) * 0.1, atol=1e-10)
def test_get_md_data2d_bin_centers_transpose(self):
"""
Same as the test above but should be the transpose
"""
x, y, data = funcs.get_md_data2d_bin_centers(self.ws_MD_2d, mantid.api.MDNormalization.NumEventsNormalization, transpose=True)
np.testing.assert_allclose(x, np.array([-8, -4, 0, 4, 8]), atol=1e-10)
np.testing.assert_allclose(y, np.array([-2.4, -1.2, 0, 1.2, 2.4]), atol=1e-10)
np.testing.assert_allclose(data, np.arange(25).reshape(5, 5).T * 0.1, atol=1e-10)
def test_get_md_data1d(self): def test_get_md_data1d(self):
coords, data, err = funcs.get_md_data1d(self.ws_MD_1d, mantid.api.MDNormalization.NumEventsNormalization) coords, data, err = funcs.get_md_data1d(self.ws_MD_1d, mantid.api.MDNormalization.NumEventsNormalization)
np.testing.assert_allclose(coords, np.array([-8, -4, 0, 4, 8]), atol=1e-10) np.testing.assert_allclose(coords, np.array([-8, -4, 0, 4, 8]), atol=1e-10)
...@@ -252,6 +261,29 @@ class HelperFunctionsTest(unittest.TestCase): ...@@ -252,6 +261,29 @@ class HelperFunctionsTest(unittest.TestCase):
np.testing.assert_allclose(x, np.array([[10, 20, 30], [10, 20, 30], [10, 20, 30]])) np.testing.assert_allclose(x, np.array([[10, 20, 30], [10, 20, 30], [10, 20, 30]]))
np.testing.assert_allclose(y, np.array([[4, 4, 4], [6, 6, 6], [8, 8, 8]])) np.testing.assert_allclose(y, np.array([[4, 4, 4], [6, 6, 6], [8, 8, 8]]))
def test_get_matrix_2d_data_rect_transpose(self):
# same as the test above bur should be the transpose
# contour from aligned point data
x, y, z = funcs.get_matrix_2d_data(self.ws2d_point, True, histogram2D=False, transpose=True)
np.testing.assert_allclose(y, np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]).T)
np.testing.assert_allclose(x, np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]).T)
# mesh from aligned point data
x, y, z = funcs.get_matrix_2d_data(self.ws2d_point, True, histogram2D=True, transpose=True)
np.testing.assert_allclose(y, np.array(
[[0.5, 1.5, 2.5, 3.5, 4.5], [0.5, 1.5, 2.5, 3.5, 4.5], [0.5, 1.5, 2.5, 3.5, 4.5],
[0.5, 1.5, 2.5, 3.5, 4.5]]).T)
np.testing.assert_allclose(x, np.array(
[[0.5, 0.5, 0.5, 0.5, 0.5], [1.5, 1.5, 1.5, 1.5, 1.5], [2.5, 2.5, 2.5, 2.5, 2.5],
[3.5, 3.5, 3.5, 3.5, 3.5]]).T)
# contour from aligned histo data
x, y, z = funcs.get_matrix_2d_data(self.ws2d_histo, True, histogram2D=False, transpose=True)
np.testing.assert_allclose(y, np.array([[15, 25], [15, 25]]).T)
np.testing.assert_allclose(x, np.array([[5, 5], [7, 7]]).T)
# mesh from aligned histo data
x, y, z = funcs.get_matrix_2d_data(self.ws2d_histo, True, histogram2D=True, transpose=True)
np.testing.assert_allclose(y, np.array([[10, 20, 30], [10, 20, 30], [10, 20, 30]]).T)
np.testing.assert_allclose(x, np.array([[4, 4, 4], [6, 6, 6], [8, 8, 8]]).T)
def test_get_matrix_2d_data_rag(self): def test_get_matrix_2d_data_rag(self):
# contour from ragged point data # contour from ragged point data
x, y, z = funcs.get_matrix_2d_data(self.ws2d_point_rag, True, histogram2D=False) x, y, z = funcs.get_matrix_2d_data(self.ws2d_point_rag, True, histogram2D=False)
...@@ -594,5 +626,6 @@ class HelperFunctionsTest(unittest.TestCase): ...@@ -594,5 +626,6 @@ class HelperFunctionsTest(unittest.TestCase):
self.assertIn('label', kwargs) self.assertIn('label', kwargs)
self.assertEqual(kwargs['label'], 'ws_MD_2d: Dim1=-1.2') self.assertEqual(kwargs['label'], 'ws_MD_2d: Dim1=-1.2')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -27,7 +27,7 @@ Improvements ...@@ -27,7 +27,7 @@ Improvements
- :ref:`GenerateEventsFilter <algm-GenerateEventsFilter>` is able to accept any `MatrixWorkspace`, as long as it has run objects loaded from `LoadNexusLogs <algm-LoadNexusLogs>`, other than `EventWorkspace`. - :ref:`GenerateEventsFilter <algm-GenerateEventsFilter>` is able to accept any `MatrixWorkspace`, as long as it has run objects loaded from `LoadNexusLogs <algm-LoadNexusLogs>`, other than `EventWorkspace`.
- :ref:`AbsorptionCorrection <algm-AbsorptionCorrection>` has a new property `ScatterFrom` which allows for calculating the correction for the other components (i.e. container and environment) - :ref:`AbsorptionCorrection <algm-AbsorptionCorrection>` has a new property `ScatterFrom` which allows for calculating the correction for the other components (i.e. container and environment)
- Prevent an error due to the locale settings which may appear when reading, for instance, the incident energy Ei value from the logs in :ref:`ConvertUnits <algm-ConvertUnits>` and many other algorithms. - Prevent an error due to the locale settings which may appear when reading, for instance, the incident energy Ei value from the logs in :ref:`ConvertUnits <algm-ConvertUnits>` and many other algorithms.
- :code:`indices` and :code:`slicepoint` options have been added to :ref:`mantid.plots <mantid.plots>` to allow selection of which plane to plot from an MDHistoWorkspace - :code:`indices` and :code:`slicepoint` options have been added to :ref:`mantid.plots <mantid.plots>` to allow selection of which plane to plot from an MDHistoWorkspace. :code:`transpose` has also been added to transpose the axes of any 2D plot.
- :ref:`Pseudo-Voigt <func-PseudoVoigt>` has been modified to be more in line with FULLPROF and GSAS. One of its basic parameter, Height, is changed to Intensity. - :ref:`Pseudo-Voigt <func-PseudoVoigt>` has been modified to be more in line with FULLPROF and GSAS. One of its basic parameter, Height, is changed to Intensity.
Removed Removed
......
...@@ -52,6 +52,7 @@ class DimensionWidget(QWidget): ...@@ -52,6 +52,7 @@ class DimensionWidget(QWidget):
self.layout.addWidget(widget) self.layout.addWidget(widget)
self.set_initial_states() self.set_initial_states()
self.transpose = False
def change_dims(self, number): def change_dims(self, number):
states = [d.get_state() for n, d in enumerate(self.dims)] states = [d.get_state() for n, d in enumerate(self.dims)]
...@@ -73,8 +74,17 @@ class DimensionWidget(QWidget): ...@@ -73,8 +74,17 @@ class DimensionWidget(QWidget):
if n != number and d.get_state() == State.Y: if n != number and d.get_state() == State.Y:
d.set_state(State.NONE) d.set_state(State.NONE)
self.check_transpose()
self.dimensionsChanged.emit() self.dimensionsChanged.emit()
def check_transpose(self):
for d in reversed(self.dims):
if d.get_state() == State.X:
self.transpose = False
elif d.get_state() == State.Y:
self.transpose = True
def set_initial_states(self): def set_initial_states(self):
# set first 2 State.NONE dimensions as x and y # set first 2 State.NONE dimensions as x and y
none_state_dims = [d for d in self.dims if d.state==State.NONE] none_state_dims = [d for d in self.dims if d.state==State.NONE]
......
...@@ -26,9 +26,12 @@ class SliceViewerModel(object): ...@@ -26,9 +26,12 @@ class SliceViewerModel(object):
def get_ws(self): def get_ws(self):
return self._ws return self._ws
def get_data(self, slicepoint): def get_data(self, slicepoint, transpose=False):
indices, _ = get_indices(self.get_ws(), slicepoint=slicepoint) indices, _ = get_indices(self.get_ws(), slicepoint=slicepoint)
return np.ma.masked_invalid(self.get_ws().getSignalArray()[indices]) if transpose:
return np.ma.masked_invalid(self.get_ws().getSignalArray()[indices]).T
else:
return np.ma.masked_invalid(self.get_ws().getSignalArray()[indices])
def get_dim_info(self, n): def get_dim_info(self, n):
""" """
......
...@@ -24,4 +24,4 @@ class SliceViewer(object): ...@@ -24,4 +24,4 @@ class SliceViewer(object):
self.view.plot(self.model.get_ws(), slicepoint=self.view.dimensions.get_slicepoint()) self.view.plot(self.model.get_ws(), slicepoint=self.view.dimensions.get_slicepoint())
def update_plot_data(self): def update_plot_data(self):
self.view.update_plot_data(self.model.get_data(self.view.dimensions.get_slicepoint())) self.view.update_plot_data(self.model.get_data(self.view.dimensions.get_slicepoint(), self.view.dimensions.transpose))
...@@ -60,6 +60,7 @@ class SliceViewerView(QWidget): ...@@ -60,6 +60,7 @@ class SliceViewerView(QWidget):
""" """
self.ax.clear() self.ax.clear()
self.im = self.ax.imshow(ws, origin='lower', aspect='auto', self.im = self.ax.imshow(ws, origin='lower', aspect='auto',
transpose=self.dimensions.transpose,
norm=self.colorbar.get_norm(), **kwargs) norm=self.colorbar.get_norm(), **kwargs)
self.ax.set_title('') self.ax.set_title('')
self.colorbar.set_mappable(self.im) self.colorbar.set_mappable(self.im)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment