Skip to content

Commit

Permalink
Merge pull request #948 from mantidproject/947_slice_save_incorrect
Browse files Browse the repository at this point in the history
Fix slice plot data save bug
  • Loading branch information
SilkeSchomann authored Oct 17, 2023
2 parents a8d0bef + 722550b commit 75511ca
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
18 changes: 3 additions & 15 deletions src/mslice/models/workspacemanager/workspace_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from mslice.models.axis import Axis
from mslice.util.mantid.algorithm_wrapper import add_to_ads
from mslice.models.workspacemanager.workspace_provider import get_workspace_handle, get_workspace_name, delete_workspace
from mslice.models.workspacemanager.workspace_provider import get_workspace_handle, delete_workspace
from mslice.util.mantid.mantid_algorithms import Load, MergeMD, MergeRuns, Scale, Minus, ConvertUnits, Rebose
from mslice.workspace.pixel_workspace import PixelWorkspace
from mslice.workspace.histogram_workspace import HistogramWorkspace
Expand Down Expand Up @@ -282,23 +282,11 @@ def export_workspace_to_ads(workspace):
def _save_single_ws(workspace, save_name, save_method, path, extension, slice_nonpsd):
save_as = save_name if save_name is not None else str(workspace) + extension
full_path = os.path.join(str(path), save_as)
workspace = get_workspace_handle(workspace)
if workspace.is_slice:
workspace = _get_slice_mdhisto(workspace, get_workspace_name(workspace))
if isinstance(workspace, string_types):
workspace = get_workspace_handle(workspace)
save_method(workspace, full_path)


def _get_slice_mdhisto(workspace, ws_name):
from mslice.models.slice.slice_functions import compute_slice
try:
return get_workspace_handle('__' + ws_name)
except KeyError:
x_axis = get_axis_from_dimension(workspace, 0)
y_axis = get_axis_from_dimension(workspace, 1)
compute_slice(ws_name, x_axis, y_axis, False)
return get_workspace_handle('__' + ws_name)


def get_axis_from_dimension(workspace, id):
dim = workspace.raw_ws.getDimension(id).name
min, max, step = workspace.limits[dim]
Expand Down
5 changes: 4 additions & 1 deletion src/mslice/plotting/plot_window/plot_figure_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ def save_plot(self):
if hasattr(self.plot_handler, 'ws_list'):
workspaces = self.plot_handler.ws_list
else:
workspaces = [self.plot_handler.ws_name]
if isinstance(self.plot_handler, SlicePlot):
workspaces = [self.plot_handler.get_cached_workspace()]
else:
workspaces = [self.plot_handler.ws_name]
try:
save_workspaces(workspaces,
file_path,
Expand Down
4 changes: 4 additions & 0 deletions src/mslice/plotting/plot_window/slice_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ def flip_icut(self):
def get_slice_cache(self):
return self._slice_plotter_presenter.get_slice_cache(self.ws_name)

def get_cached_workspace(self):
cached_slice = self.get_slice_cache()
return getattr(cached_slice, self.intensity_type.name.lower())

def update_workspaces(self):
self._slice_plotter_presenter.update_displayed_workspaces()

Expand Down
11 changes: 0 additions & 11 deletions src/mslice/presenters/slice_plotter_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from mslice.models.workspacemanager.workspace_provider import get_workspace_handle
from mslice.plotting.plot_window.overplot_interface import plot_overplot_line, remove_line
from mslice.presenters.presenter_utility import PresenterUtility
from mslice.plotting.pyplot import CATEGORY_SLICE
from mslice.util.intensity_correction import IntensityType, IntensityCache


class SlicePlotterPresenter(PresenterUtility):
Expand Down Expand Up @@ -114,12 +112,3 @@ def set_sample_temperature(self, workspace_name, temp):

def workspace_selection_changed(self):
pass

def _cache_intensity_correction_methods(self):
cat = CATEGORY_SLICE
IntensityCache.cache_method(cat, IntensityType.SCATTERING_FUNCTION, self.show_scattering_function)
IntensityCache.cache_method(cat, IntensityType.CHI, self.show_dynamical_susceptibility)
IntensityCache.cache_method(cat, IntensityType.CHI_MAGNETIC,
self.show_dynamical_susceptibility_magnetic)
IntensityCache.cache_method(cat, IntensityType.D2SIGMA, self.show_d2sigma)
IntensityCache.cache_method(cat, IntensityType.SYMMETRISED, self.show_symmetrised)
67 changes: 67 additions & 0 deletions tests/plot_figure_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from __future__ import (absolute_import, division, print_function)
import unittest
from unittest import mock
from unittest.mock import patch

from mslice.presenters.slice_plotter_presenter import SlicePlotterPresenter
from mslice.presenters.cut_plotter_presenter import CutPlotterPresenter
from mslice.util.intensity_correction import IntensityType

FORCE_METHOD_CALLS_TO_QAPP_THREAD = 'mslice.plotting.plot_window.plot_figure_manager.force_method_calls_to_qapp_thread'


class PlotFigureTest(unittest.TestCase):

def setUp(self):
self.mock_force_qapp = mock.patch(
FORCE_METHOD_CALLS_TO_QAPP_THREAD).start()
# make it a noop
self.mock_force_qapp.side_effect = lambda arg: arg
from mslice.plotting.plot_window.plot_figure_manager import new_plot_figure_manager
self.new_plot_figure_manager = new_plot_figure_manager
self.slice_plotter_presenter = SlicePlotterPresenter()
self.cut_plotter_presenter = CutPlotterPresenter()

def tearDown(self):
self.mock_force_qapp.stop()

def test_save_slice_nexus_sofqe(self):
gman = mock.Mock()
workspace = 'testworkspace'
file_name = ('', 'test.nxs', '.nxs')
fg = self.new_plot_figure_manager(num=1, global_manager=gman)
fg.add_slice_plot(self.slice_plotter_presenter, workspace=workspace)

with patch('mslice.plotting.plot_window.plot_figure_manager.get_save_directory') as get_save_dir, \
patch('mslice.models.workspacemanager.workspace_algorithms.save_nexus') as save_nexus, \
patch('mslice.models.workspacemanager.workspace_algorithms.get_workspace_handle') as get_handle, \
patch.object(SlicePlotterPresenter, 'get_slice_cache') as get_slice_cache:
get_save_dir.return_value = file_name
slice_cache = mock.Mock()
slice_cache.scattering_function = workspace
get_slice_cache.return_value = slice_cache
get_handle.return_value = workspace
fg.save_plot()
save_nexus.assert_called_once_with(workspace, file_name[1])
get_slice_cache.assert_called_once()

def test_save_slice_matlab_gdos(self):
gman = mock.Mock()
workspace = 'testworkspace'
file_name = ('', 'test.mat', '.mat')
fg = self.new_plot_figure_manager(num=1, global_manager=gman)
fg.add_slice_plot(self.slice_plotter_presenter, workspace=workspace)

with patch('mslice.plotting.plot_window.plot_figure_manager.get_save_directory') as get_save_dir, \
patch('mslice.models.workspacemanager.workspace_algorithms.save_matlab') as save_matlab, \
patch('mslice.models.workspacemanager.workspace_algorithms.get_workspace_handle') as get_handle, \
patch.object(SlicePlotterPresenter, 'get_slice_cache') as get_slice_cache:
get_save_dir.return_value = file_name
slice_cache = mock.Mock()
slice_cache.gdos = workspace
get_slice_cache.return_value = slice_cache
get_handle.return_value = workspace
fg.plot_handler.intensity_type = IntensityType.GDOS
fg.save_plot()
save_matlab.assert_called_once_with(workspace, file_name[1])
get_slice_cache.assert_called_once()

0 comments on commit 75511ca

Please sign in to comment.