From ab02ddfbd7376e074fbedd5bcb8fdcab8aeeb43c Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Tue, 10 Oct 2023 16:15:42 +0100 Subject: [PATCH 01/18] Introduced class that appends to suffixes to workspaces The new class WorkspaceNameAppender returns the changed workspace name for 4 operations: Sum, Subtract, Scale and Rebose. This implementation makes it easier to change the naming conventions if needed. Altered tests to import this class, so that tests get updated if naming convention changes. --- .../workspacemanager/workspace_algorithms.py | 18 ++++++++++-------- src/mslice/workspace/helperfunctions.py | 17 +++++++++++++++++ tests/workspace_provider_test.py | 7 ++++--- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/mslice/models/workspacemanager/workspace_algorithms.py b/src/mslice/models/workspacemanager/workspace_algorithms.py index 682ed4603..c1ea010ea 100644 --- a/src/mslice/models/workspacemanager/workspace_algorithms.py +++ b/src/mslice/models/workspacemanager/workspace_algorithms.py @@ -25,6 +25,7 @@ from mslice.workspace.pixel_workspace import PixelWorkspace from mslice.workspace.histogram_workspace import HistogramWorkspace from mslice.workspace.workspace import Workspace +from mslice.workspace.helperfunctions import WorkspaceNameAppender from .file_io import save_ascii, save_matlab, save_nexus, save_nxspe @@ -198,18 +199,18 @@ def combine_workspace(selected_workspaces, new_name): def add_workspace_runs(selected_ws): - out_ws_name = selected_ws[0] + '_sum' - sum_ws = MergeRuns(OutputWorkspace=out_ws_name, InputWorkspaces=selected_ws) + sum_ws = MergeRuns(OutputWorkspace=WorkspaceNameAppender.sum(selected_ws[0]), + InputWorkspaces=selected_ws) propagate_properties(get_workspace_handle(selected_ws[0]), sum_ws) def subtract(workspaces, background_ws, ssf): - scaled_bg_ws = Scale(OutputWorkspace=str(background_ws) + '_scaled', + scaled_bg_ws = Scale(OutputWorkspace=WorkspaceNameAppender.scale(str(background_ws), ssf), InputWorkspace=str(background_ws), Factor=ssf, store=False) try: for ws_name in workspaces: - result = Minus(OutputWorkspace=ws_name + f'_subtracted_by_{ssf:.2f}', LHSWorkspace=ws_name, - RHSWorkspace=scaled_bg_ws) + result = Minus(OutputWorkspace=WorkspaceNameAppender.subtract(ws_name, ssf), + LHSWorkspace=ws_name, RHSWorkspace=scaled_bg_ws) propagate_properties(get_workspace_handle(ws_name), result) except ValueError as e: raise ValueError(e) @@ -217,8 +218,8 @@ def subtract(workspaces, background_ws, ssf): def rebose_single(ws, from_temp, to_temp): ws = get_workspace_handle(ws) - results = Rebose(InputWorkspace=ws, CurrentTemperature=from_temp, TargetTemperature=to_temp, - OutputWorkspace=ws.name+'_bosed') + results = Rebose(OutputWorkspace=WorkspaceNameAppender.rebose(ws.name), + InputWorkspace=ws, CurrentTemperature=from_temp, TargetTemperature=to_temp) propagate_properties(ws, results) return results @@ -232,7 +233,8 @@ def scale_workspaces(workspaces, scale_factor=None, from_temp=None, to_temp=None else: for ws_name in workspaces: ws = get_workspace_handle(ws_name) - result = Scale(InputWorkspace=ws.raw_ws, Factor=scale_factor, OutputWorkspace=ws_name+'_scaled') + result = Scale(OutputWorkspace=WorkspaceNameAppender.scale(ws_name, scale_factor), + InputWorkspace=ws.raw_ws, Factor=scale_factor) propagate_properties(ws, result) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index 9bfbd056d..139247b89 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -116,3 +116,20 @@ def __exit__(self, exc_type, exc_val, exc_tb): if self.workspace: self.workspace.remove_saved_attributes() return True + + +class WorkspaceNameAppender: + + def scale(ws_name: str, scaling_factor: float) -> str: + return ws_name + f"_ssf_{scaling_factor:.2f}" + # return ws_name + "_scaled" + + def subtract(ws_name: str, scaling_factor: float) -> str: + return ws_name + f"_minus_ssf_{scaling_factor:.2f}" + # return ws_name + "_subtracted" + + def sum(ws_name: str) -> str: + return ws_name + "_sum" + + def rebose(ws_name: str) -> str: + return ws_name + "_bosed" diff --git a/tests/workspace_provider_test.py b/tests/workspace_provider_test.py index 98e41ceac..ecbe3252a 100644 --- a/tests/workspace_provider_test.py +++ b/tests/workspace_provider_test.py @@ -13,6 +13,7 @@ from mslice.widgets.workspacemanager.command import Command from mantid.simpleapi import AddSampleLog from mslice.views.interfaces.workspace_view import WorkspaceView +from mslice.workspace.helperfunctions import WorkspaceNameAppender class MantidWorkspaceProviderTest(unittest.TestCase): @@ -44,16 +45,16 @@ def test_delete_workspace(self): def test_subtract_workspace(self): subtract(['test_ws_2d'], 'test_ws_2d', 0.95) - result = get_workspace_handle('test_ws_2d_subtracted') + result = get_workspace_handle(WorkspaceNameAppender.subtract('test_ws_2d', 0.95)) np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [0.05] * 20) np.testing.assert_array_almost_equal(self.test_ws_2d.raw_ws.dataY(0), [1] * 20) - self.assertFalse('test_ws_2d_scaled' in get_visible_workspace_names()) + self.assertFalse(WorkspaceNameAppender.scale('test_ws_2d', 0.95) in get_visible_workspace_names()) self.assertRaises(ValueError, subtract, ['test_ws_2d'], 'test_ws_md', 1.0) def test_add_workspace(self): original_data = self.test_ws_2d.raw_ws.dataY(0) add_workspace_runs(['test_ws_2d', 'test_ws_2d']) - result = get_workspace_handle('test_ws_2d_sum') + result = get_workspace_handle(WorkspaceNameAppender.sum('test_ws_2d')) np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [2.0] * 20) np.testing.assert_array_almost_equal(original_data, [1] * 20) From 29ded707656143ec27f1e7af8ca06ae2e779bf97 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Tue, 10 Oct 2023 16:32:42 +0100 Subject: [PATCH 02/18] Removed commented out sections --- src/mslice/workspace/helperfunctions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index 139247b89..982e1365d 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -122,11 +122,9 @@ class WorkspaceNameAppender: def scale(ws_name: str, scaling_factor: float) -> str: return ws_name + f"_ssf_{scaling_factor:.2f}" - # return ws_name + "_scaled" def subtract(ws_name: str, scaling_factor: float) -> str: return ws_name + f"_minus_ssf_{scaling_factor:.2f}" - # return ws_name + "_subtracted" def sum(ws_name: str) -> str: return ws_name + "_sum" From 1719bff7b1bf8a9e7a280d916e1335e30fb52d2b Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Tue, 7 Nov 2023 15:18:17 +0000 Subject: [PATCH 03/18] Changed main ws operations and started looking into hidden ws - Altered class for ws name handling of ws operatiosn - Started looking into other sections of code where names of ws are altered - Changed some parts of algorithm wrapper and scripting files - Currently it's a mess because I don't understand what these routines are doing yet --- .../workspacemanager/workspace_algorithms.py | 14 ++++---- src/mslice/scripting/__init__.py | 11 +++--- src/mslice/util/mantid/algorithm_wrapper.py | 5 ++- src/mslice/workspace/helperfunctions.py | 36 +++++++++++++------ 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/mslice/models/workspacemanager/workspace_algorithms.py b/src/mslice/models/workspacemanager/workspace_algorithms.py index c1ea010ea..c1f744bcd 100644 --- a/src/mslice/models/workspacemanager/workspace_algorithms.py +++ b/src/mslice/models/workspacemanager/workspace_algorithms.py @@ -20,12 +20,12 @@ 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, get_workspace_name, delete_workspace, add_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 from mslice.workspace.workspace import Workspace -from mslice.workspace.helperfunctions import WorkspaceNameAppender +from mslice.workspace.helperfunctions import WorkspaceNameHandler from .file_io import save_ascii, save_matlab, save_nexus, save_nxspe @@ -199,17 +199,17 @@ def combine_workspace(selected_workspaces, new_name): def add_workspace_runs(selected_ws): - sum_ws = MergeRuns(OutputWorkspace=WorkspaceNameAppender.sum(selected_ws[0]), + sum_ws = MergeRuns(OutputWorkspace=WorkspaceNameHandler(selected_ws[0]).summed(), InputWorkspaces=selected_ws) propagate_properties(get_workspace_handle(selected_ws[0]), sum_ws) def subtract(workspaces, background_ws, ssf): - scaled_bg_ws = Scale(OutputWorkspace=WorkspaceNameAppender.scale(str(background_ws), ssf), + scaled_bg_ws = Scale(OutputWorkspace=WorkspaceNameHandler(background_ws).scaled(ssf), InputWorkspace=str(background_ws), Factor=ssf, store=False) try: for ws_name in workspaces: - result = Minus(OutputWorkspace=WorkspaceNameAppender.subtract(ws_name, ssf), + result = Minus(OutputWorkspace=WorkspaceNameHandler(ws_name).subtracted(ssf), LHSWorkspace=ws_name, RHSWorkspace=scaled_bg_ws) propagate_properties(get_workspace_handle(ws_name), result) except ValueError as e: @@ -218,7 +218,7 @@ def subtract(workspaces, background_ws, ssf): def rebose_single(ws, from_temp, to_temp): ws = get_workspace_handle(ws) - results = Rebose(OutputWorkspace=WorkspaceNameAppender.rebose(ws.name), + results = Rebose(OutputWorkspace=WorkspaceNameHandler(ws.name).rebosed(), InputWorkspace=ws, CurrentTemperature=from_temp, TargetTemperature=to_temp) propagate_properties(ws, results) return results @@ -233,7 +233,7 @@ def scale_workspaces(workspaces, scale_factor=None, from_temp=None, to_temp=None else: for ws_name in workspaces: ws = get_workspace_handle(ws_name) - result = Scale(OutputWorkspace=WorkspaceNameAppender.scale(ws_name, scale_factor), + result = Scale(OutputWorkspace=WorkspaceNameHandler(ws_name).scaled(scale_factor), InputWorkspace=ws.raw_ws, Factor=scale_factor) propagate_properties(ws, result) diff --git a/src/mslice/scripting/__init__.py b/src/mslice/scripting/__init__.py index e02594295..1b8e08b4a 100644 --- a/src/mslice/scripting/__init__.py +++ b/src/mslice/scripting/__init__.py @@ -2,6 +2,7 @@ from mslice.models.workspacemanager.workspace_provider import get_workspace_handle from mslice.scripting.helperfunctions import add_plot_statements, replace_ws_special_chars from mslice.app.presenters import get_cut_plotter_presenter +from mslice.workspace.helperfunctions import WorkspaceNameHandler from six import string_types @@ -76,12 +77,14 @@ def _parse_prop(prop): pname = prop.name() hidden = False output_ws = None - if isinstance(pval, string_types): - pval = pval.replace("__MSL", "").replace("_HIDDEN", "") + if prop.name() == "OutputWorkspace": output_ws = replace_ws_special_chars(pval) - if "_HIDDEN" in prop.value(): - hidden = True + hidden = WorkspaceNameHandler(pval).is_hidden() + + if isinstance(pval, string_types): + pval = pval.replace("__MSL", "").replace("_HIDDEN", "") + return pname, pval, output_ws, hidden diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 4ab12ffde..cf8508286 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -10,6 +10,8 @@ from mslice.workspace.base import WorkspaceBase as MsliceWorkspace from mslice.workspace.workspace import Workspace as MsliceWorkspace2D +from mslice.workspace.helperfunctions import WorkspaceNameHandler + def _parse_ws_names(args, kwargs): input_workspace = kwargs.get('InputWorkspace', None) @@ -56,7 +58,8 @@ def alg_wrapper(*args, **kwargs): ads_name = '__MSL' + output_name if output_name else '__MSLTMP' + str(uuid4())[:8] store = kwargs.pop('store', True) if not store: - ads_name += '_HIDDEN' + # ads_name += '_HIDDEN' + ads_name = WorkspaceNameHandler(ads_name).make_hidden() result = algorithm(*args, OutputWorkspace=ads_name, **kwargs) else: result = algorithm(*args, **kwargs) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index 982e1365d..fb7ecadfe 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -85,7 +85,8 @@ def attribute_to_log(attrdict, raw_ws, append=False): def delete_workspace(workspace, ws): try: - if hasattr(workspace, str(ws)) and ws is not None and ws.name().endswith('_HIDDEN'): + # if hasattr(workspace, str(ws)) and ws is not None and ws.name().endswith('_HIDDEN'): + if hasattr(workspace, str(ws)) and ws is not None and WorkspaceNameHandler(ws.name()).is_hidden(): DeleteWorkspace(ws) ws = None except RuntimeError: @@ -118,16 +119,31 @@ def __exit__(self, exc_type, exc_val, exc_tb): return True -class WorkspaceNameAppender: +class WorkspaceNameHandler: - def scale(ws_name: str, scaling_factor: float) -> str: - return ws_name + f"_ssf_{scaling_factor:.2f}" + def __init__(self, ws_name: str): + self.ws_name = ws_name - def subtract(ws_name: str, scaling_factor: float) -> str: - return ws_name + f"_minus_ssf_{scaling_factor:.2f}" + def add_prefix(self, prefix) -> str: + return prefix + self.ws_name - def sum(ws_name: str) -> str: - return ws_name + "_sum" + def add_sufix(self, sufix) -> str: + return self.ws_name + sufix - def rebose(ws_name: str) -> str: - return ws_name + "_bosed" + def scaled(self, scaling_factor: float) -> str: + return self.add_sufix(f"_ssf_{scaling_factor:.2f}") + + def subtracted(self, scaling_factor: float) -> str: + return self.add_sufix(f"_minus_ssf_{scaling_factor:.2f}") + + def summed(self) -> str: + return self.add_sufix("_sum") + + def rebosed(self) -> str: + return self.add_sufix('_bosed') + + def is_hidden(self) -> bool: + return self.ws_name.endswith('_HIDDEN') + + def make_hidden(self) -> str: + return self.add_sufix('_HIDDEN') From 0f687d3dfc10b007b620800b1428b10155bbd9b2 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Thu, 9 Nov 2023 17:06:50 +0000 Subject: [PATCH 04/18] Changed methods to suitable variable names - Named methods in WorkspaceNameHandler according to their function - replaced several parts of code with new methods --- src/mslice/models/cut/cut_algorithm.py | 7 ++-- .../models/intensity_correction_algs.py | 3 +- src/mslice/models/slice/slice_functions.py | 3 +- .../workspacemanager/workspace_algorithms.py | 4 +-- .../presenters/slice_plotter_presenter.py | 7 ++-- src/mslice/scripting/__init__.py | 8 ++--- src/mslice/util/mantid/_workspace_ops.py | 3 +- src/mslice/util/mantid/algorithm_wrapper.py | 13 +++----- src/mslice/workspace/helperfunctions.py | 32 ++++++++++++++++--- 9 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/mslice/models/cut/cut_algorithm.py b/src/mslice/models/cut/cut_algorithm.py index 84883c83d..6b3707175 100644 --- a/src/mslice/models/cut/cut_algorithm.py +++ b/src/mslice/models/cut/cut_algorithm.py @@ -13,6 +13,7 @@ from mslice.workspace.helperfunctions import attribute_to_log from .cut_normalisation import normalize_workspace +from mslice.workspace.helperfunctions import WorkspaceNameHandler class Cut(PythonAlgorithm): @@ -87,17 +88,17 @@ def _compute_cut_nonPSD(selected_workspace, cut_axis, integration_axis, emode, a integration_axis.end_meV))) idx = 0 if 'Rebin' in algo else 1 unit = 'DeltaE' - name = '__MSL_EnergyTransfer' + name = WorkspaceNameHandler('_EnergyTransfer').hideMslInAds() if is_momentum(cut_axis.units): ws_out = _cut_nonPSD_momentum(cut_binning, int_binning, emode, selected_workspace, algo) idx = 1 unit = 'MomentumTransfer' - name = '__MSL_|Q|' + name = WorkspaceNameHandler('_|Q|').hideMslInAds() elif is_twotheta(cut_axis.units): ws_out = _cut_nonPSD_theta(int_binning, cut_binning, selected_workspace, algo) idx = 1 if 'Rebin' in algo else 0 unit = 'Degrees' - name = '__MSL_Theta' + name = WorkspaceNameHandler('_Theta').hideMslInAds() elif integration_axis.units == '|Q|': ws_out = _cut_nonPSD_momentum(int_binning, cut_binning, emode, selected_workspace, algo) else: diff --git a/src/mslice/models/intensity_correction_algs.py b/src/mslice/models/intensity_correction_algs.py index a0dc837e1..6d989c206 100644 --- a/src/mslice/models/intensity_correction_algs.py +++ b/src/mslice/models/intensity_correction_algs.py @@ -15,6 +15,7 @@ from mslice.models.cut.cut_algorithm import _cut_nonPSD_general from math import trunc, ceil +from mslice.workspace.helperfunctions import WorkspaceNameHandler KB_MEV = constants.value('Boltzmann constant in eV/K') * 1000 E_TO_K = np.sqrt(2 * constants.neutron_mass * constants.elementary_charge / 1000) / constants.hbar @@ -141,7 +142,7 @@ def _cut_compute_gdos(scattering_data, sample_temp, q_axis, e_axis, rotated, nor def _cut_compute_gdos_pixel(scattering_data, sample_temp, q_axis, e_axis, rotated, norm_to_one, algorithm, is_icut): pixel_ws = get_workspace_handle(scattering_data.parent) if is_icut: - slice_ws = get_workspace_handle("__" + scattering_data.parent) + slice_ws = get_workspace_handle(WorkspaceNameHandler(scattering_data.parent).hideInAds()) slice_rotated = not _is_momentum_or_two_theta(slice_ws.raw_ws.getXDimension().getUnits()) # fn arg rotated refers to cut. else: slice_rotated = not _is_momentum_or_two_theta(pixel_ws.raw_ws.getXDimension().getUnits()) # no pre existing slice, use pixel ws. diff --git a/src/mslice/models/slice/slice_functions.py b/src/mslice/models/slice/slice_functions.py index 4e375a3f1..68fa69a52 100644 --- a/src/mslice/models/slice/slice_functions.py +++ b/src/mslice/models/slice/slice_functions.py @@ -11,11 +11,12 @@ from mslice.workspace.pixel_workspace import PixelWorkspace from mslice.workspace.workspace import Workspace +from mslice.workspace.helperfunctions import WorkspaceNameHandler def compute_slice(selected_workspace, x_axis, y_axis, norm_to_one, store_in_ADS=True): workspace = get_workspace_handle(selected_workspace) - slice = mantid_algorithms.Slice(OutputWorkspace='__' + workspace.name, InputWorkspace=workspace, + slice = mantid_algorithms.Slice(OutputWorkspace=WorkspaceNameHandler(workspace.name).hideInAds(), InputWorkspace=workspace, XAxis=x_axis.to_dict(), YAxis=y_axis.to_dict(), PSD=workspace.is_PSD, EMode=workspace.e_mode, NormToOne=norm_to_one, StoreInADS=store_in_ADS) propagate_properties(workspace, slice) diff --git a/src/mslice/models/workspacemanager/workspace_algorithms.py b/src/mslice/models/workspacemanager/workspace_algorithms.py index c1f744bcd..15ef5aa47 100644 --- a/src/mslice/models/workspacemanager/workspace_algorithms.py +++ b/src/mslice/models/workspacemanager/workspace_algorithms.py @@ -293,12 +293,12 @@ def _save_single_ws(workspace, save_name, save_method, path, extension, slice_no def _get_slice_mdhisto(workspace, ws_name): from mslice.models.slice.slice_functions import compute_slice try: - return get_workspace_handle('__' + ws_name) + return get_workspace_handle(WorkspaceNameHandler(ws_name).hideInAds()) 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) + return get_workspace_handle(WorkspaceNameHandler(ws_name).hideInAds()) def get_axis_from_dimension(workspace, id): diff --git a/src/mslice/presenters/slice_plotter_presenter.py b/src/mslice/presenters/slice_plotter_presenter.py index 118338244..e8c7d7aa3 100644 --- a/src/mslice/presenters/slice_plotter_presenter.py +++ b/src/mslice/presenters/slice_plotter_presenter.py @@ -12,6 +12,7 @@ from mslice.presenters.presenter_utility import PresenterUtility from mslice.plotting.pyplot import CATEGORY_SLICE from mslice.util.intensity_correction import IntensityType, IntensityCache +from mslice.workspace.helperfunctions import WorkspaceNameHandler class SlicePlotterPresenter(PresenterUtility): @@ -33,19 +34,19 @@ def create_slice(self, selected_ws, x_axis, y_axis, intensity_start, intensity_e return slice def plot_from_cache(self, workspace): - ws_name = workspace.name.lstrip('__') + ws_name = WorkspaceNameHandler(workspace.name).makeVisibleInAds() create_slice_figure(ws_name, self) self.show_scattering_function(ws_name) def change_intensity(self, workspace_name, intensity_start, intensity_end): - workspace_name = workspace_name.lstrip('__') + workspace_name = WorkspaceNameHandler(workspace_name).makeVisibleInAds() intensity_start, intensity_end = self.validate_intensity(intensity_start, intensity_end) norm = Normalize(vmin=intensity_start, vmax=intensity_end) self._slice_cache[workspace_name].norm = norm def change_colourmap(self, workspace_name, colourmap): if colourmap in ALLOWED_CMAPS: - workspace_name = workspace_name.lstrip('__') + workspace_name = WorkspaceNameHandler(workspace_name).makeVisibleInAds() self._slice_cache[workspace_name].colourmap = colourmap else: raise ValueError('colourmap not recognised') diff --git a/src/mslice/scripting/__init__.py b/src/mslice/scripting/__init__.py index 1b8e08b4a..658dd98d1 100644 --- a/src/mslice/scripting/__init__.py +++ b/src/mslice/scripting/__init__.py @@ -78,12 +78,12 @@ def _parse_prop(prop): hidden = False output_ws = None + if isinstance(pval, string_types): + pval = WorkspaceNameHandler(pval).removeHideFlags() + if prop.name() == "OutputWorkspace": output_ws = replace_ws_special_chars(pval) - hidden = WorkspaceNameHandler(pval).is_hidden() - - if isinstance(pval, string_types): - pval = pval.replace("__MSL", "").replace("_HIDDEN", "") + hidden = WorkspaceNameHandler(prop.value()).isHiddenFromMsl() return pname, pval, output_ws, hidden diff --git a/src/mslice/util/mantid/_workspace_ops.py b/src/mslice/util/mantid/_workspace_ops.py index 3a2b09175..ac0406848 100644 --- a/src/mslice/util/mantid/_workspace_ops.py +++ b/src/mslice/util/mantid/_workspace_ops.py @@ -4,6 +4,7 @@ from mantid.api._workspaceops import _do_binary_operation from mantid.kernel.funcinspect import lhs_info from mslice.workspace.workspace import WorkspaceOperatorMixin +from mslice.workspace.helperfunctions import WorkspaceNameHandler _binary_operator_map = { "Plus": operator.add, @@ -65,7 +66,7 @@ def op_wrapper(self, other): result_info = lhs_info() # Replace output workspace name with a unique temporary name hidden in ADS if result_info[0] > 0: - result_info = (result_info[0], ('__MSLTMP' + str(uuid4())[:8],) + result_info[1][1:]) + result_info = (result_info[0], (WorkspaceNameHandler(str(uuid4())[:8]).hideTmpMslInAds(),) + result_info[1][1:]) return _binary_op(self, other, algorithm, result_info, inplace, reverse) op_wrapper.__name__ = attr diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index cf8508286..65980d1f3 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -22,9 +22,7 @@ def _parse_ws_names(args, kwargs): input_workspace = get_workspace_handle(args[0]) args = (_name_or_wrapper_to_workspace(args[0]),) + args[1:] - output_name = '' - if 'OutputWorkspace' in kwargs: - output_name = kwargs.pop('OutputWorkspace') + output_name = kwargs.pop('OutputWorkspace', '') for key in kwargs.keys(): if input_workspace is None and 'LHS' in key: @@ -51,15 +49,14 @@ def alg_wrapper(*args, **kwargs): kwargs['InputWorkspaces'] = [_name_or_wrapper_to_workspace(arg) for arg in kwargs['InputWorkspaces']] for ky in [k for k in kwargs.keys() if 'Workspace' in k]: - if isinstance(kwargs[ky], string_types) and '__MSL' not in kwargs[ky]: + if isinstance(kwargs[ky], string_types) and not WorkspaceNameHandler(kwargs[ky]).isMslHiddenInAds(): kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) if _alg_has_outputws(algorithm): - ads_name = '__MSL' + output_name if output_name else '__MSLTMP' + str(uuid4())[:8] + ads_name = WorkspaceNameHandler(output_name).hideMslInAds() if output_name else WorkspaceNameHandler(str(uuid4())[:8]).hideTmpMslInAds() store = kwargs.pop('store', True) if not store: - # ads_name += '_HIDDEN' - ads_name = WorkspaceNameHandler(ads_name).make_hidden() + ads_name = WorkspaceNameHandler(ads_name).hideFromMsl() result = algorithm(*args, OutputWorkspace=ads_name, **kwargs) else: result = algorithm(*args, **kwargs) @@ -96,5 +93,5 @@ def add_to_ads(workspaces): except TypeError: workspaces = [workspaces] for workspace in workspaces: - startid = (5 if workspace.name.startswith('__mat') else 2) if workspace.name.startswith('__') else 0 + startid = (5 if workspace.name.startswith('__mat') else 2) if WorkspaceNameHandler(workspace.name).isHiddenInAds() else 0 AnalysisDataService.Instance().addOrReplace(workspace.name[startid:], workspace.raw_ws) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index fb7ecadfe..6cea9acf4 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -1,4 +1,5 @@ import pickle +from uuid import uuid4 import codecs from mantid.simpleapi import DeleteWorkspace, RenameWorkspace @@ -86,7 +87,7 @@ def attribute_to_log(attrdict, raw_ws, append=False): def delete_workspace(workspace, ws): try: # if hasattr(workspace, str(ws)) and ws is not None and ws.name().endswith('_HIDDEN'): - if hasattr(workspace, str(ws)) and ws is not None and WorkspaceNameHandler(ws.name()).is_hidden(): + if hasattr(workspace, str(ws)) and ws is not None and WorkspaceNameHandler(ws.name()).isHiddenFromMsl(): DeleteWorkspace(ws) ws = None except RuntimeError: @@ -141,9 +142,30 @@ def summed(self) -> str: def rebosed(self) -> str: return self.add_sufix('_bosed') + + def isHiddenFromMsl(self) -> bool: + return '_HIDDEN' in self.ws_name - def is_hidden(self) -> bool: - return self.ws_name.endswith('_HIDDEN') - - def make_hidden(self) -> str: + def hideFromMsl(self) -> str: return self.add_sufix('_HIDDEN') + + def removeHideFlags(self): + return self.ws_name.replace('__MSL', '').replace('_HIDDEN', '') + + def hideMslInAds(self): + return self.add_prefix('__MSL') + + def isMslHiddenInAds(self): + return '__MSL' in self.ws_name + + def hideTmpMslInAds(self): + return self.add_prefix('__MSLTMP') + + def hideInAds(self): + return self.add_prefix('__') + + def isHiddenInAds(self): + return self.ws_name.startswith('__') + + def makeVisibleInAds(self): + return self.ws_name.lstrip('__') From a6b9d1b2a3948eec70c3993b1568508209f6eae6 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Thu, 9 Nov 2023 17:13:49 +0000 Subject: [PATCH 05/18] Removed artifact comment --- src/mslice/workspace/helperfunctions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index 6cea9acf4..b31d2b331 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -86,7 +86,6 @@ def attribute_to_log(attrdict, raw_ws, append=False): def delete_workspace(workspace, ws): try: - # if hasattr(workspace, str(ws)) and ws is not None and ws.name().endswith('_HIDDEN'): if hasattr(workspace, str(ws)) and ws is not None and WorkspaceNameHandler(ws.name()).isHiddenFromMsl(): DeleteWorkspace(ws) ws = None From 5a703cad07fa07d08e0ca30b454722e4f2b89114 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Fri, 10 Nov 2023 16:36:50 +0000 Subject: [PATCH 06/18] Changed naming of scaling ws and added two more methods - Removed '.' from workspaces names because might cause trouble with mantid - Added two methods for appending sufixes to workspaces --- src/mslice/cli/_mslice_commands.py | 3 ++- .../presenters/data_loader_presenter.py | 3 ++- .../presenters/workspace_manager_presenter.py | 3 ++- src/mslice/workspace/helperfunctions.py | 24 ++++++++++++------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/mslice/cli/_mslice_commands.py b/src/mslice/cli/_mslice_commands.py index 030ee5cd8..6e369dd97 100644 --- a/src/mslice/cli/_mslice_commands.py +++ b/src/mslice/cli/_mslice_commands.py @@ -16,6 +16,7 @@ from mslice.cli.helperfunctions import (_string_to_integration_axis, _process_axis, _check_workspace_name, _check_workspace_type, _correct_intensity) from mslice.workspace.pixel_workspace import PixelWorkspace +from mslice.workspace.helperfunctions import WorkspaceNameHandler from mslice.util.qt.qapp import QAppThreadCall, mainloop from six import string_types from mslice.workspace.histogram_workspace import HistogramWorkspace @@ -87,7 +88,7 @@ def Load(Filename, OutputWorkspace=None): if OutputWorkspace is not None: old_name = ospath.splitext(ospath.basename(Filename))[0] if merge: - old_name = old_name + '_merged' + old_name = WorkspaceNameHandler(old_name).merged() name = rename_workspace(workspace=old_name, new_name=OutputWorkspace).name return get_workspace_handle(name) diff --git a/src/mslice/presenters/data_loader_presenter.py b/src/mslice/presenters/data_loader_presenter.py index d1e1a15f8..6b9c68301 100644 --- a/src/mslice/presenters/data_loader_presenter.py +++ b/src/mslice/presenters/data_loader_presenter.py @@ -8,6 +8,7 @@ from mslice.presenters.interfaces.data_loader_presenter import DataLoaderPresenterInterface from mslice.presenters.presenter_utility import PresenterUtility from mslice.models.workspacemanager.file_io import load_from_ascii +from mslice.workspace.helperfunctions import WorkspaceNameHandler def apply_fixed_final_energy_to_a_valid_workspace(workspace_name, fixed_final_energy: float): @@ -39,7 +40,7 @@ def load_workspace(self, file_paths, merge=False, force_overwrite=False): if not self.file_types_match(file_paths): self._view.error_merge_different_file_formats() return - ws_names = [ws_names[0] + '_merged'] + ws_names = [WorkspaceNameHandler(ws_names[0]).merged()] file_paths = ['+'.join(file_paths)] self._load_ws(file_paths, ws_names, force_overwrite) diff --git a/src/mslice/presenters/workspace_manager_presenter.py b/src/mslice/presenters/workspace_manager_presenter.py index 89951f89d..7d3e3a9fb 100644 --- a/src/mslice/presenters/workspace_manager_presenter.py +++ b/src/mslice/presenters/workspace_manager_presenter.py @@ -2,6 +2,7 @@ from six import string_types from .busy import show_busy +from mslice.workspace.helperfunctions import WorkspaceNameHandler from mslice.widgets.workspacemanager.command import Command from mslice.widgets.workspacemanager import TAB_2D, TAB_NONPSD from mslice.models.workspacemanager.file_io import get_save_directory @@ -141,7 +142,7 @@ def _combine_workspace(self): return elif len(selected_workspaces) == 1: selected_workspaces.append(str(self._workspace_manager_view.add_workspace_dialog())) - new_workspace = selected_workspaces[0] + '_combined' + new_workspace = WorkspaceNameHandler(selected_workspaces[0]).combined() if all([is_pixel_workspace(workspace) for workspace in selected_workspaces]): combine_workspace(selected_workspaces, new_workspace) else: diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index b31d2b331..4105c1e3f 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -131,16 +131,22 @@ def add_sufix(self, sufix) -> str: return self.ws_name + sufix def scaled(self, scaling_factor: float) -> str: - return self.add_sufix(f"_ssf_{scaling_factor:.2f}") + return self.add_sufix(f"_ssf_{str(scaling_factor).replace('.', '_')}") def subtracted(self, scaling_factor: float) -> str: - return self.add_sufix(f"_minus_ssf_{scaling_factor:.2f}") + return self.add_sufix(f"_minus_ssf_{str(scaling_factor).replace('.', '_')}") def summed(self) -> str: return self.add_sufix("_sum") def rebosed(self) -> str: return self.add_sufix('_bosed') + + def combined(self) -> str: + return self.add_sufix('_combined') + + def merged(self) -> str: + return self.add_sufix('_merged') def isHiddenFromMsl(self) -> bool: return '_HIDDEN' in self.ws_name @@ -148,23 +154,23 @@ def isHiddenFromMsl(self) -> bool: def hideFromMsl(self) -> str: return self.add_sufix('_HIDDEN') - def removeHideFlags(self): + def removeHideFlags(self) -> str: return self.ws_name.replace('__MSL', '').replace('_HIDDEN', '') - def hideMslInAds(self): + def hideMslInAds(self) -> str: return self.add_prefix('__MSL') - def isMslHiddenInAds(self): + def isMslHiddenInAds(self) -> bool: return '__MSL' in self.ws_name - def hideTmpMslInAds(self): + def hideTmpMslInAds(self) -> str: return self.add_prefix('__MSLTMP') - def hideInAds(self): + def hideInAds(self) -> str: return self.add_prefix('__') - def isHiddenInAds(self): + def isHiddenInAds(self) -> bool: return self.ws_name.startswith('__') - def makeVisibleInAds(self): + def makeVisibleInAds(self) -> str: return self.ws_name.lstrip('__') From b9b4a0cd698f1cd8112651cbb15a2d3539f97574 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Fri, 10 Nov 2023 17:11:27 +0000 Subject: [PATCH 07/18] Updated unit tests - Checked that changing the suffixes in my code makes tests fail - The prefixes '__MSL', '__MSLTMP' and suffix '_HIDDEN' are not covered by the unit tests --- tests/workspace_provider_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/workspace_provider_test.py b/tests/workspace_provider_test.py index ecbe3252a..3944009c3 100644 --- a/tests/workspace_provider_test.py +++ b/tests/workspace_provider_test.py @@ -13,7 +13,7 @@ from mslice.widgets.workspacemanager.command import Command from mantid.simpleapi import AddSampleLog from mslice.views.interfaces.workspace_view import WorkspaceView -from mslice.workspace.helperfunctions import WorkspaceNameAppender +from mslice.workspace.helperfunctions import WorkspaceNameHandler class MantidWorkspaceProviderTest(unittest.TestCase): @@ -45,16 +45,16 @@ def test_delete_workspace(self): def test_subtract_workspace(self): subtract(['test_ws_2d'], 'test_ws_2d', 0.95) - result = get_workspace_handle(WorkspaceNameAppender.subtract('test_ws_2d', 0.95)) + result = get_workspace_handle(WorkspaceNameHandler('test_ws_2d').subtracted(0.95)) np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [0.05] * 20) np.testing.assert_array_almost_equal(self.test_ws_2d.raw_ws.dataY(0), [1] * 20) - self.assertFalse(WorkspaceNameAppender.scale('test_ws_2d', 0.95) in get_visible_workspace_names()) + self.assertFalse(WorkspaceNameHandler('test_ws_2d').scaled(0.95) in get_visible_workspace_names()) self.assertRaises(ValueError, subtract, ['test_ws_2d'], 'test_ws_md', 1.0) def test_add_workspace(self): original_data = self.test_ws_2d.raw_ws.dataY(0) add_workspace_runs(['test_ws_2d', 'test_ws_2d']) - result = get_workspace_handle(WorkspaceNameAppender.sum('test_ws_2d')) + result = get_workspace_handle(WorkspaceNameHandler('test_ws_2d').summed()) np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [2.0] * 20) np.testing.assert_array_almost_equal(original_data, [1] * 20) From 37beb373be45bad91b49a00690f586cf602cca62 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Mon, 13 Nov 2023 10:37:54 +0000 Subject: [PATCH 08/18] Fixed ws names for scaling - Prevented scaling factor in workspace names from having more than 2 decimal places --- src/mslice/workspace/helperfunctions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index 4105c1e3f..cca23e886 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -131,10 +131,10 @@ def add_sufix(self, sufix) -> str: return self.ws_name + sufix def scaled(self, scaling_factor: float) -> str: - return self.add_sufix(f"_ssf_{str(scaling_factor).replace('.', '_')}") + return self.add_sufix("_ssf_" + f"{scaling_factor:.2f}".replace('.', '_')) def subtracted(self, scaling_factor: float) -> str: - return self.add_sufix(f"_minus_ssf_{str(scaling_factor).replace('.', '_')}") + return self.add_sufix("_minus_ssf_" + f"{scaling_factor:.2f}".replace('.', '_')) def summed(self) -> str: return self.add_sufix("_sum") From fda525445d459ffe762e85b0848f4c47ed2d893b Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Mon, 13 Nov 2023 16:15:15 +0000 Subject: [PATCH 09/18] Fixed artifacts from merge --- src/mslice/presenters/slice_plotter_presenter.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mslice/presenters/slice_plotter_presenter.py b/src/mslice/presenters/slice_plotter_presenter.py index 3aa5f2998..0949f257b 100644 --- a/src/mslice/presenters/slice_plotter_presenter.py +++ b/src/mslice/presenters/slice_plotter_presenter.py @@ -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 from mslice.workspace.helperfunctions import WorkspaceNameHandler From 83ba1b222e2ae849ea881062e611f663a7204d79 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Mon, 13 Nov 2023 16:19:16 +0000 Subject: [PATCH 10/18] Fixed long line and removed unused import --- src/mslice/util/mantid/algorithm_wrapper.py | 6 +++++- src/mslice/workspace/helperfunctions.py | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 65980d1f3..10647cb76 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -53,7 +53,11 @@ def alg_wrapper(*args, **kwargs): kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) if _alg_has_outputws(algorithm): - ads_name = WorkspaceNameHandler(output_name).hideMslInAds() if output_name else WorkspaceNameHandler(str(uuid4())[:8]).hideTmpMslInAds() + if output_name: + ads_name = WorkspaceNameHandler(output_name).hideMslInAds() + else: + ads_name = WorkspaceNameHandler(str(uuid4())[:8]).hideTmpMslInAds() + store = kwargs.pop('store', True) if not store: ads_name = WorkspaceNameHandler(ads_name).hideFromMsl() diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index cca23e886..e377a892a 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -1,7 +1,5 @@ import pickle -from uuid import uuid4 import codecs - from mantid.simpleapi import DeleteWorkspace, RenameWorkspace From f2b2527a3fbaf9e8e1aff477638e73b3d628a8b5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:20:30 +0000 Subject: [PATCH 11/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/mslice/workspace/helperfunctions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index e377a892a..b3844705b 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -145,7 +145,7 @@ def combined(self) -> str: def merged(self) -> str: return self.add_sufix('_merged') - + def isHiddenFromMsl(self) -> bool: return '_HIDDEN' in self.ws_name From abdb3802f75c0146280f4535ad704f15d66322de Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Mon, 13 Nov 2023 16:26:33 +0000 Subject: [PATCH 12/18] Fixed missing blank line --- src/mslice/models/cut/cut_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mslice/models/cut/cut_algorithm.py b/src/mslice/models/cut/cut_algorithm.py index 6b3707175..8f073b508 100644 --- a/src/mslice/models/cut/cut_algorithm.py +++ b/src/mslice/models/cut/cut_algorithm.py @@ -12,9 +12,9 @@ from mslice.models.units import EnergyUnits from mslice.workspace.helperfunctions import attribute_to_log from .cut_normalisation import normalize_workspace - from mslice.workspace.helperfunctions import WorkspaceNameHandler + class Cut(PythonAlgorithm): def PyInit(self): From 4e214dcf95b46b68041248637819dbae706fe744 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Wed, 10 Jan 2024 16:56:42 +0000 Subject: [PATCH 13/18] Updated class for clearer usability --- src/mslice/cli/_mslice_commands.py | 2 +- src/mslice/models/cut/cut_algorithm.py | 6 +- .../models/intensity_correction_algs.py | 2 +- src/mslice/models/slice/slice_functions.py | 3 +- .../workspacemanager/workspace_algorithms.py | 10 +- .../presenters/data_loader_presenter.py | 2 +- .../presenters/slice_plotter_presenter.py | 6 +- .../presenters/workspace_manager_presenter.py | 2 +- src/mslice/scripting/__init__.py | 4 +- src/mslice/util/mantid/_workspace_ops.py | 3 +- src/mslice/util/mantid/algorithm_wrapper.py | 11 +- src/mslice/workspace/helperfunctions.py | 103 ++++++++++++------ tests/workspace_provider_test.py | 6 +- 13 files changed, 98 insertions(+), 62 deletions(-) diff --git a/src/mslice/cli/_mslice_commands.py b/src/mslice/cli/_mslice_commands.py index 702cd5add..53e7ad21a 100644 --- a/src/mslice/cli/_mslice_commands.py +++ b/src/mslice/cli/_mslice_commands.py @@ -88,7 +88,7 @@ def Load(Filename, OutputWorkspace=None): if OutputWorkspace is not None: old_name = ospath.splitext(ospath.basename(Filename))[0] if merge: - old_name = WorkspaceNameHandler(old_name).merged() + old_name = WorkspaceNameHandler(old_name).get_name(merged=True) name = rename_workspace(workspace=old_name, new_name=OutputWorkspace).name return get_workspace_handle(name) diff --git a/src/mslice/models/cut/cut_algorithm.py b/src/mslice/models/cut/cut_algorithm.py index 8f073b508..39b70197b 100644 --- a/src/mslice/models/cut/cut_algorithm.py +++ b/src/mslice/models/cut/cut_algorithm.py @@ -88,17 +88,17 @@ def _compute_cut_nonPSD(selected_workspace, cut_axis, integration_axis, emode, a integration_axis.end_meV))) idx = 0 if 'Rebin' in algo else 1 unit = 'DeltaE' - name = WorkspaceNameHandler('_EnergyTransfer').hideMslInAds() + name = WorkspaceNameHandler('_EnergyTransfer').get_name(hide_from_ADS=True, mslice_signature=True) if is_momentum(cut_axis.units): ws_out = _cut_nonPSD_momentum(cut_binning, int_binning, emode, selected_workspace, algo) idx = 1 unit = 'MomentumTransfer' - name = WorkspaceNameHandler('_|Q|').hideMslInAds() + name = WorkspaceNameHandler('_|Q|').get_name(hide_from_ADS=True, mslice_signature=True) elif is_twotheta(cut_axis.units): ws_out = _cut_nonPSD_theta(int_binning, cut_binning, selected_workspace, algo) idx = 1 if 'Rebin' in algo else 0 unit = 'Degrees' - name = WorkspaceNameHandler('_Theta').hideMslInAds() + name = WorkspaceNameHandler('_Theta').get_name(hide_from_ADS=True, mslice_signature=True) elif integration_axis.units == '|Q|': ws_out = _cut_nonPSD_momentum(int_binning, cut_binning, emode, selected_workspace, algo) else: diff --git a/src/mslice/models/intensity_correction_algs.py b/src/mslice/models/intensity_correction_algs.py index 6d989c206..7b35ed7e8 100644 --- a/src/mslice/models/intensity_correction_algs.py +++ b/src/mslice/models/intensity_correction_algs.py @@ -142,7 +142,7 @@ def _cut_compute_gdos(scattering_data, sample_temp, q_axis, e_axis, rotated, nor def _cut_compute_gdos_pixel(scattering_data, sample_temp, q_axis, e_axis, rotated, norm_to_one, algorithm, is_icut): pixel_ws = get_workspace_handle(scattering_data.parent) if is_icut: - slice_ws = get_workspace_handle(WorkspaceNameHandler(scattering_data.parent).hideInAds()) + slice_ws = get_workspace_handle(WorkspaceNameHandler(scattering_data.parent).get_name(hide_from_ADS=True)) slice_rotated = not _is_momentum_or_two_theta(slice_ws.raw_ws.getXDimension().getUnits()) # fn arg rotated refers to cut. else: slice_rotated = not _is_momentum_or_two_theta(pixel_ws.raw_ws.getXDimension().getUnits()) # no pre existing slice, use pixel ws. diff --git a/src/mslice/models/slice/slice_functions.py b/src/mslice/models/slice/slice_functions.py index 68fa69a52..d854bb8e5 100644 --- a/src/mslice/models/slice/slice_functions.py +++ b/src/mslice/models/slice/slice_functions.py @@ -16,7 +16,8 @@ def compute_slice(selected_workspace, x_axis, y_axis, norm_to_one, store_in_ADS=True): workspace = get_workspace_handle(selected_workspace) - slice = mantid_algorithms.Slice(OutputWorkspace=WorkspaceNameHandler(workspace.name).hideInAds(), InputWorkspace=workspace, + output_name = WorkspaceNameHandler(workspace.name).get_name(hide_from_ADS=True) + slice = mantid_algorithms.Slice(OutputWorkspace=output_name, InputWorkspace=workspace, XAxis=x_axis.to_dict(), YAxis=y_axis.to_dict(), PSD=workspace.is_PSD, EMode=workspace.e_mode, NormToOne=norm_to_one, StoreInADS=store_in_ADS) propagate_properties(workspace, slice) diff --git a/src/mslice/models/workspacemanager/workspace_algorithms.py b/src/mslice/models/workspacemanager/workspace_algorithms.py index 8681e5621..481a68399 100644 --- a/src/mslice/models/workspacemanager/workspace_algorithms.py +++ b/src/mslice/models/workspacemanager/workspace_algorithms.py @@ -199,17 +199,17 @@ def combine_workspace(selected_workspaces, new_name): def add_workspace_runs(selected_ws): - sum_ws = MergeRuns(OutputWorkspace=WorkspaceNameHandler(selected_ws[0]).summed(), + sum_ws = MergeRuns(OutputWorkspace=WorkspaceNameHandler(selected_ws[0]).get_name(summed=True), InputWorkspaces=selected_ws) propagate_properties(get_workspace_handle(selected_ws[0]), sum_ws) def subtract(workspaces, background_ws, ssf): - scaled_bg_ws = Scale(OutputWorkspace=WorkspaceNameHandler(background_ws).scaled(ssf), + scaled_bg_ws = Scale(OutputWorkspace=WorkspaceNameHandler(background_ws).get_name(scaling_factor=ssf, scaled=True), InputWorkspace=str(background_ws), Factor=ssf, store=False) try: for ws_name in workspaces: - result = Minus(OutputWorkspace=WorkspaceNameHandler(ws_name).subtracted(ssf), + result = Minus(OutputWorkspace=WorkspaceNameHandler(ws_name).get_name(scaling_factor=ssf, subtracted=True), LHSWorkspace=ws_name, RHSWorkspace=scaled_bg_ws) propagate_properties(get_workspace_handle(ws_name), result) except ValueError as e: @@ -218,7 +218,7 @@ def subtract(workspaces, background_ws, ssf): def rebose_single(ws, from_temp, to_temp): ws = get_workspace_handle(ws) - results = Rebose(OutputWorkspace=WorkspaceNameHandler(ws.name).rebosed(), + results = Rebose(OutputWorkspace=WorkspaceNameHandler(ws.name).get_name(rebosed=True), InputWorkspace=ws, CurrentTemperature=from_temp, TargetTemperature=to_temp) propagate_properties(ws, results) return results @@ -233,7 +233,7 @@ def scale_workspaces(workspaces, scale_factor=None, from_temp=None, to_temp=None else: for ws_name in workspaces: ws = get_workspace_handle(ws_name) - result = Scale(OutputWorkspace=WorkspaceNameHandler(ws_name).scaled(scale_factor), + result = Scale(OutputWorkspace=WorkspaceNameHandler(ws_name).get_name(scaling_factor=scale_factor, scaled=True), InputWorkspace=ws.raw_ws, Factor=scale_factor) propagate_properties(ws, result) diff --git a/src/mslice/presenters/data_loader_presenter.py b/src/mslice/presenters/data_loader_presenter.py index 6b9c68301..80ecbda37 100644 --- a/src/mslice/presenters/data_loader_presenter.py +++ b/src/mslice/presenters/data_loader_presenter.py @@ -40,7 +40,7 @@ def load_workspace(self, file_paths, merge=False, force_overwrite=False): if not self.file_types_match(file_paths): self._view.error_merge_different_file_formats() return - ws_names = [WorkspaceNameHandler(ws_names[0]).merged()] + ws_names = [WorkspaceNameHandler(ws_names[0]).get_name(merged=True)] file_paths = ['+'.join(file_paths)] self._load_ws(file_paths, ws_names, force_overwrite) diff --git a/src/mslice/presenters/slice_plotter_presenter.py b/src/mslice/presenters/slice_plotter_presenter.py index 0949f257b..d7c4d8f5a 100644 --- a/src/mslice/presenters/slice_plotter_presenter.py +++ b/src/mslice/presenters/slice_plotter_presenter.py @@ -32,19 +32,19 @@ def create_slice(self, selected_ws, x_axis, y_axis, intensity_start, intensity_e return slice def plot_from_cache(self, workspace): - ws_name = WorkspaceNameHandler(workspace.name).makeVisibleInAds() + ws_name = WorkspaceNameHandler(workspace.name).get_name(make_ws_visible_in_ADS=True) create_slice_figure(ws_name, self) self.show_scattering_function(ws_name) def change_intensity(self, workspace_name, intensity_start, intensity_end): - workspace_name = WorkspaceNameHandler(workspace_name).makeVisibleInAds() + workspace_name = WorkspaceNameHandler(workspace_name).get_name(make_ws_visible_in_ADS=True) intensity_start, intensity_end = self.validate_intensity(intensity_start, intensity_end) norm = Normalize(vmin=intensity_start, vmax=intensity_end) self._slice_cache[workspace_name].norm = norm def change_colourmap(self, workspace_name, colourmap): if colourmap in ALLOWED_CMAPS: - workspace_name = WorkspaceNameHandler(workspace_name).makeVisibleInAds() + workspace_name = WorkspaceNameHandler(workspace_name).get_name(make_ws_visible_in_ADS=True) self._slice_cache[workspace_name].colourmap = colourmap else: raise ValueError('colourmap not recognised') diff --git a/src/mslice/presenters/workspace_manager_presenter.py b/src/mslice/presenters/workspace_manager_presenter.py index 7d3e3a9fb..f7c58f966 100644 --- a/src/mslice/presenters/workspace_manager_presenter.py +++ b/src/mslice/presenters/workspace_manager_presenter.py @@ -142,7 +142,7 @@ def _combine_workspace(self): return elif len(selected_workspaces) == 1: selected_workspaces.append(str(self._workspace_manager_view.add_workspace_dialog())) - new_workspace = WorkspaceNameHandler(selected_workspaces[0]).combined() + new_workspace = WorkspaceNameHandler(selected_workspaces[0]).get_name(combined=True) if all([is_pixel_workspace(workspace) for workspace in selected_workspaces]): combine_workspace(selected_workspaces, new_workspace) else: diff --git a/src/mslice/scripting/__init__.py b/src/mslice/scripting/__init__.py index e4a63f7b9..b078a59cd 100644 --- a/src/mslice/scripting/__init__.py +++ b/src/mslice/scripting/__init__.py @@ -79,11 +79,11 @@ def _parse_prop(prop): output_ws = None if isinstance(pval, string_types): - pval = WorkspaceNameHandler(pval).removeHideFlags() + pval = WorkspaceNameHandler(pval).get_name(make_ws_visible_in_ADS=True, make_ws_visible_in_mslice=True) if prop.name() == "OutputWorkspace": output_ws = replace_ws_special_chars(pval) - hidden = WorkspaceNameHandler(prop.value()).isHiddenFromMsl() + hidden = WorkspaceNameHandler(prop.value()).assert_name(is_hidden_from_mslice=True) return pname, pval, output_ws, hidden diff --git a/src/mslice/util/mantid/_workspace_ops.py b/src/mslice/util/mantid/_workspace_ops.py index ac0406848..367055469 100644 --- a/src/mslice/util/mantid/_workspace_ops.py +++ b/src/mslice/util/mantid/_workspace_ops.py @@ -66,7 +66,8 @@ def op_wrapper(self, other): result_info = lhs_info() # Replace output workspace name with a unique temporary name hidden in ADS if result_info[0] > 0: - result_info = (result_info[0], (WorkspaceNameHandler(str(uuid4())[:8]).hideTmpMslInAds(),) + result_info[1][1:]) + temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) + result_info = (result_info[0], (temp_name,) + result_info[1][1:]) return _binary_op(self, other, algorithm, result_info, inplace, reverse) op_wrapper.__name__ = attr diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 10647cb76..4555ea910 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -49,18 +49,19 @@ def alg_wrapper(*args, **kwargs): kwargs['InputWorkspaces'] = [_name_or_wrapper_to_workspace(arg) for arg in kwargs['InputWorkspaces']] for ky in [k for k in kwargs.keys() if 'Workspace' in k]: - if isinstance(kwargs[ky], string_types) and not WorkspaceNameHandler(kwargs[ky]).isMslHiddenInAds(): + if isinstance(kwargs[ky], string_types) and not WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True): kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) if _alg_has_outputws(algorithm): if output_name: - ads_name = WorkspaceNameHandler(output_name).hideMslInAds() + ads_name = WorkspaceNameHandler(output_name).get_name(hide_from_ADS=True, mslice_signature=True) else: - ads_name = WorkspaceNameHandler(str(uuid4())[:8]).hideTmpMslInAds() + print("Missing output workspace!") + ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) store = kwargs.pop('store', True) if not store: - ads_name = WorkspaceNameHandler(ads_name).hideFromMsl() + ads_name = WorkspaceNameHandler(ads_name).get_name(hide_from_mslice=True) result = algorithm(*args, OutputWorkspace=ads_name, **kwargs) else: result = algorithm(*args, **kwargs) @@ -97,5 +98,5 @@ def add_to_ads(workspaces): except TypeError: workspaces = [workspaces] for workspace in workspaces: - startid = (5 if workspace.name.startswith('__mat') else 2) if WorkspaceNameHandler(workspace.name).isHiddenInAds() else 0 + startid = (5 if workspace.name.startswith('__mat') else 2) if WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) else 0 AnalysisDataService.Instance().addOrReplace(workspace.name[startid:], workspace.raw_ws) diff --git a/src/mslice/workspace/helperfunctions.py b/src/mslice/workspace/helperfunctions.py index b3844705b..fb95823fc 100644 --- a/src/mslice/workspace/helperfunctions.py +++ b/src/mslice/workspace/helperfunctions.py @@ -84,7 +84,7 @@ def attribute_to_log(attrdict, raw_ws, append=False): def delete_workspace(workspace, ws): try: - if hasattr(workspace, str(ws)) and ws is not None and WorkspaceNameHandler(ws.name()).isHiddenFromMsl(): + if hasattr(workspace, str(ws)) and ws is not None and WorkspaceNameHandler(ws.name()).assert_name(is_hidden_from_mslice=True): DeleteWorkspace(ws) ws = None except RuntimeError: @@ -122,53 +122,86 @@ class WorkspaceNameHandler: def __init__(self, ws_name: str): self.ws_name = ws_name - def add_prefix(self, prefix) -> str: - return prefix + self.ws_name + def _add_prefix(self, prefix) -> str: + self.ws_name = prefix + self.ws_name - def add_sufix(self, sufix) -> str: - return self.ws_name + sufix + def _add_sufix(self, sufix) -> str: + self.ws_name = self.ws_name + sufix - def scaled(self, scaling_factor: float) -> str: - return self.add_sufix("_ssf_" + f"{scaling_factor:.2f}".replace('.', '_')) + def get_name( + self, + scaling_factor=None, + scaled=False, + subtracted=False, + summed=False, + rebosed=False, + combined=False, + merged=False, + hide_from_mslice=False, + hide_from_ADS=False, + mslice_signature=False, + temporary_signature=False, + make_ws_visible_in_ADS=False, + make_ws_visible_in_mslice=False): - def subtracted(self, scaling_factor: float) -> str: - return self.add_sufix("_minus_ssf_" + f"{scaling_factor:.2f}".replace('.', '_')) + singular_arguments = [scaled, subtracted, summed, rebosed, combined, merged] + assert sum(singular_arguments) <= 1, "Two or more incompatible arguments were set to True." - def summed(self) -> str: - return self.add_sufix("_sum") + if scaled: + assert scaling_factor is not None, "Scaling factor should be provided to build name of workspace" + self._add_sufix("_ssf_" + f"{scaling_factor:.2f}".replace('.', '_')) - def rebosed(self) -> str: - return self.add_sufix('_bosed') + if subtracted: + assert scaling_factor is not None, "Scaling factor should be provided to build name of workspace" + self._add_sufix("_minus_ssf_" + f"{scaling_factor:.2f}".replace('.', '_')) - def combined(self) -> str: - return self.add_sufix('_combined') + if summed: + self._add_sufix("_sum") - def merged(self) -> str: - return self.add_sufix('_merged') + if rebosed: + self._add_sufix("_bosed") - def isHiddenFromMsl(self) -> bool: - return '_HIDDEN' in self.ws_name + if combined: + self._add_sufix("_combined") - def hideFromMsl(self) -> str: - return self.add_sufix('_HIDDEN') + if merged: + self._add_sufix("_merged") - def removeHideFlags(self) -> str: - return self.ws_name.replace('__MSL', '').replace('_HIDDEN', '') + if hide_from_mslice: + self._add_sufix("_HIDDEN") - def hideMslInAds(self) -> str: - return self.add_prefix('__MSL') + if temporary_signature: + self._add_prefix("TMP") - def isMslHiddenInAds(self) -> bool: - return '__MSL' in self.ws_name + if mslice_signature: + self._add_prefix("MSL") - def hideTmpMslInAds(self) -> str: - return self.add_prefix('__MSLTMP') + if hide_from_ADS: + self._add_prefix("__") - def hideInAds(self) -> str: - return self.add_prefix('__') + if make_ws_visible_in_ADS: + self.ws_name = self.ws_name.replace('__MSL', '').replace('__', '') - def isHiddenInAds(self) -> bool: - return self.ws_name.startswith('__') + if make_ws_visible_in_mslice: + self.ws_name = self.ws_name.replace('_HIDDEN', '') - def makeVisibleInAds(self) -> str: - return self.ws_name.lstrip('__') + return self.ws_name + + def assert_name( + self, + is_hidden_from_mslice=False, + is_hidden_from_ADS=False, + has_mslice_signature=False): + + result_flag = True + + if is_hidden_from_mslice: + result_flag = result_flag and (self.ws_name.endswith("_HIDDEN")) + + if is_hidden_from_ADS: + result_flag = result_flag and (self.ws_name.startswith("__")) + + if has_mslice_signature: + result_flag = result_flag and (self.ws_name.startswith("__MSL") or self.ws_name.startswith("MSL")) + + return result_flag diff --git a/tests/workspace_provider_test.py b/tests/workspace_provider_test.py index 3944009c3..c14aab65d 100644 --- a/tests/workspace_provider_test.py +++ b/tests/workspace_provider_test.py @@ -45,16 +45,16 @@ def test_delete_workspace(self): def test_subtract_workspace(self): subtract(['test_ws_2d'], 'test_ws_2d', 0.95) - result = get_workspace_handle(WorkspaceNameHandler('test_ws_2d').subtracted(0.95)) + result = get_workspace_handle(WorkspaceNameHandler('test_ws_2d').get_name(subtracted=True, scaling_factor=0.95)) np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [0.05] * 20) np.testing.assert_array_almost_equal(self.test_ws_2d.raw_ws.dataY(0), [1] * 20) - self.assertFalse(WorkspaceNameHandler('test_ws_2d').scaled(0.95) in get_visible_workspace_names()) + self.assertFalse(WorkspaceNameHandler('test_ws_2d').get_name(scaled=True, scaling_factor=0.95) in get_visible_workspace_names()) self.assertRaises(ValueError, subtract, ['test_ws_2d'], 'test_ws_md', 1.0) def test_add_workspace(self): original_data = self.test_ws_2d.raw_ws.dataY(0) add_workspace_runs(['test_ws_2d', 'test_ws_2d']) - result = get_workspace_handle(WorkspaceNameHandler('test_ws_2d').summed()) + result = get_workspace_handle(WorkspaceNameHandler('test_ws_2d').get_name(summed=True)) np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [2.0] * 20) np.testing.assert_array_almost_equal(original_data, [1] * 20) From 9883cf54f3453e93ef3e9a308058da5cf4b1f715 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Wed, 10 Jan 2024 17:07:50 +0000 Subject: [PATCH 14/18] Fixed Flake8 issues --- src/mslice/util/mantid/_workspace_ops.py | 4 +++- src/mslice/util/mantid/algorithm_wrapper.py | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mslice/util/mantid/_workspace_ops.py b/src/mslice/util/mantid/_workspace_ops.py index 367055469..537e4dd57 100644 --- a/src/mslice/util/mantid/_workspace_ops.py +++ b/src/mslice/util/mantid/_workspace_ops.py @@ -66,7 +66,9 @@ def op_wrapper(self, other): result_info = lhs_info() # Replace output workspace name with a unique temporary name hidden in ADS if result_info[0] > 0: - temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) + temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name( + hide_from_ADS=True, mslice_signature=True, temporary_signature=True + ) result_info = (result_info[0], (temp_name,) + result_info[1][1:]) return _binary_op(self, other, algorithm, result_info, inplace, reverse) diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 4555ea910..8170d3430 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -49,7 +49,8 @@ def alg_wrapper(*args, **kwargs): kwargs['InputWorkspaces'] = [_name_or_wrapper_to_workspace(arg) for arg in kwargs['InputWorkspaces']] for ky in [k for k in kwargs.keys() if 'Workspace' in k]: - if isinstance(kwargs[ky], string_types) and not WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True): + is_hidden_msl_ws = WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True) + if isinstance(kwargs[ky], string_types) and not is_hidden_msl_ws: kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) if _alg_has_outputws(algorithm): @@ -57,7 +58,9 @@ def alg_wrapper(*args, **kwargs): ads_name = WorkspaceNameHandler(output_name).get_name(hide_from_ADS=True, mslice_signature=True) else: print("Missing output workspace!") - ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) + ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name( + hide_from_ADS=True, mslice_signature=True, temporary_signature=True + ) store = kwargs.pop('store', True) if not store: @@ -98,5 +101,6 @@ def add_to_ads(workspaces): except TypeError: workspaces = [workspaces] for workspace in workspaces: - startid = (5 if workspace.name.startswith('__mat') else 2) if WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) else 0 + is_hidden_ws = WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) + startid = (5 if workspace.name.startswith('__mat') else 2) if is_hidden_ws else 0 AnalysisDataService.Instance().addOrReplace(workspace.name[startid:], workspace.raw_ws) From 84d3fe7178b17b669e58b05ec7e259b8561c66a5 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Wed, 10 Jan 2024 17:33:54 +0000 Subject: [PATCH 15/18] Revert "Fixed Flake8 issues" This reverts commit 9883cf54f3453e93ef3e9a308058da5cf4b1f715. --- src/mslice/util/mantid/_workspace_ops.py | 4 +--- src/mslice/util/mantid/algorithm_wrapper.py | 10 +++------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/mslice/util/mantid/_workspace_ops.py b/src/mslice/util/mantid/_workspace_ops.py index 537e4dd57..367055469 100644 --- a/src/mslice/util/mantid/_workspace_ops.py +++ b/src/mslice/util/mantid/_workspace_ops.py @@ -66,9 +66,7 @@ def op_wrapper(self, other): result_info = lhs_info() # Replace output workspace name with a unique temporary name hidden in ADS if result_info[0] > 0: - temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name( - hide_from_ADS=True, mslice_signature=True, temporary_signature=True - ) + temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) result_info = (result_info[0], (temp_name,) + result_info[1][1:]) return _binary_op(self, other, algorithm, result_info, inplace, reverse) diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 8170d3430..4555ea910 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -49,8 +49,7 @@ def alg_wrapper(*args, **kwargs): kwargs['InputWorkspaces'] = [_name_or_wrapper_to_workspace(arg) for arg in kwargs['InputWorkspaces']] for ky in [k for k in kwargs.keys() if 'Workspace' in k]: - is_hidden_msl_ws = WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True) - if isinstance(kwargs[ky], string_types) and not is_hidden_msl_ws: + if isinstance(kwargs[ky], string_types) and not WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True): kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) if _alg_has_outputws(algorithm): @@ -58,9 +57,7 @@ def alg_wrapper(*args, **kwargs): ads_name = WorkspaceNameHandler(output_name).get_name(hide_from_ADS=True, mslice_signature=True) else: print("Missing output workspace!") - ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name( - hide_from_ADS=True, mslice_signature=True, temporary_signature=True - ) + ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) store = kwargs.pop('store', True) if not store: @@ -101,6 +98,5 @@ def add_to_ads(workspaces): except TypeError: workspaces = [workspaces] for workspace in workspaces: - is_hidden_ws = WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) - startid = (5 if workspace.name.startswith('__mat') else 2) if is_hidden_ws else 0 + startid = (5 if workspace.name.startswith('__mat') else 2) if WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) else 0 AnalysisDataService.Instance().addOrReplace(workspace.name[startid:], workspace.raw_ws) From dfe981a5ba87b34abe25a332108aaa66aa2b2ca4 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Wed, 10 Jan 2024 17:35:52 +0000 Subject: [PATCH 16/18] Corrected fix for flake8 issues --- src/mslice/util/mantid/_workspace_ops.py | 4 +++- src/mslice/util/mantid/algorithm_wrapper.py | 12 ++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/mslice/util/mantid/_workspace_ops.py b/src/mslice/util/mantid/_workspace_ops.py index 367055469..537e4dd57 100644 --- a/src/mslice/util/mantid/_workspace_ops.py +++ b/src/mslice/util/mantid/_workspace_ops.py @@ -66,7 +66,9 @@ def op_wrapper(self, other): result_info = lhs_info() # Replace output workspace name with a unique temporary name hidden in ADS if result_info[0] > 0: - temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) + temp_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name( + hide_from_ADS=True, mslice_signature=True, temporary_signature=True + ) result_info = (result_info[0], (temp_name,) + result_info[1][1:]) return _binary_op(self, other, algorithm, result_info, inplace, reverse) diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 4555ea910..9cf9aadf5 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -49,15 +49,18 @@ def alg_wrapper(*args, **kwargs): kwargs['InputWorkspaces'] = [_name_or_wrapper_to_workspace(arg) for arg in kwargs['InputWorkspaces']] for ky in [k for k in kwargs.keys() if 'Workspace' in k]: - if isinstance(kwargs[ky], string_types) and not WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True): - kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) + if isinstance(kwargs[ky], string_types): + if not WorkspaceNameHandler(kwargs[ky]).assert_name(is_hidden_from_ADS=True, has_mslice_signature=True): + kwargs[ky] = _name_or_wrapper_to_workspace(kwargs[ky]) if _alg_has_outputws(algorithm): if output_name: ads_name = WorkspaceNameHandler(output_name).get_name(hide_from_ADS=True, mslice_signature=True) else: print("Missing output workspace!") - ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name(hide_from_ADS=True, mslice_signature=True, temporary_signature=True) + ads_name = WorkspaceNameHandler(str(uuid4())[:8]).get_name( + hide_from_ADS=True, mslice_signature=True, temporary_signature=True + ) store = kwargs.pop('store', True) if not store: @@ -98,5 +101,6 @@ def add_to_ads(workspaces): except TypeError: workspaces = [workspaces] for workspace in workspaces: - startid = (5 if workspace.name.startswith('__mat') else 2) if WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) else 0 + is_hidden_ws = WorkspaceNameHandler(workspace.name).assert_name(is_hidden_from_ADS=True) + startid = (5 if workspace.name.startswith('__mat') else 2) if is_hidden_ws else 0 AnalysisDataService.Instance().addOrReplace(workspace.name[startid:], workspace.raw_ws) From f29879e89746dcba90397fa7f1aebebd723bcc30 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Thu, 11 Jan 2024 12:24:37 +0000 Subject: [PATCH 17/18] Updated a recent commit to use the new ws name format --- src/mslice/util/mantid/algorithm_wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index 435dc3779..e78fb4aab 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -110,6 +110,6 @@ def remove_from_ads(workspacename): if AnalysisDataService.Instance().doesExist(workspacename): AnalysisDataService.Instance().remove(workspacename) # Remove hidden workspaces from ADS - workspacename = '__MSL' + workspacename + workspacename = WorkspaceNameHandler(workspacename).get_name(mslice_signature=True, hide_from_ADS=True) # '__MSL' + workspacename if AnalysisDataService.Instance().doesExist(workspacename): AnalysisDataService.Instance().remove(workspacename) From 2b12bdc2c2ec17c13c6a443d2822299d4d6f1e64 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Thu, 11 Jan 2024 13:57:49 +0000 Subject: [PATCH 18/18] Removed outdated comment --- src/mslice/util/mantid/algorithm_wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mslice/util/mantid/algorithm_wrapper.py b/src/mslice/util/mantid/algorithm_wrapper.py index e78fb4aab..acbf13d7f 100644 --- a/src/mslice/util/mantid/algorithm_wrapper.py +++ b/src/mslice/util/mantid/algorithm_wrapper.py @@ -110,6 +110,6 @@ def remove_from_ads(workspacename): if AnalysisDataService.Instance().doesExist(workspacename): AnalysisDataService.Instance().remove(workspacename) # Remove hidden workspaces from ADS - workspacename = WorkspaceNameHandler(workspacename).get_name(mslice_signature=True, hide_from_ADS=True) # '__MSL' + workspacename + workspacename = WorkspaceNameHandler(workspacename).get_name(mslice_signature=True, hide_from_ADS=True) if AnalysisDataService.Instance().doesExist(workspacename): AnalysisDataService.Instance().remove(workspacename)