Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

941 append scaling to ws name #951

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ab02ddf
Introduced class that appends to suffixes to workspaces
GuiMacielPereira Oct 10, 2023
29ded70
Removed commented out sections
GuiMacielPereira Oct 10, 2023
1719bff
Changed main ws operations and started looking into hidden ws
GuiMacielPereira Nov 7, 2023
0f687d3
Changed methods to suitable variable names
GuiMacielPereira Nov 9, 2023
a6b9d1b
Removed artifact comment
GuiMacielPereira Nov 9, 2023
5a703ca
Changed naming of scaling ws and added two more methods
GuiMacielPereira Nov 10, 2023
b9b4a0c
Updated unit tests
GuiMacielPereira Nov 10, 2023
37beb37
Fixed ws names for scaling
GuiMacielPereira Nov 13, 2023
633ea86
Merge branch 'main' into 941_append_scaling_to_ws_name
GuiMacielPereira Nov 13, 2023
fda5254
Fixed artifacts from merge
GuiMacielPereira Nov 13, 2023
83ba1b2
Fixed long line and removed unused import
GuiMacielPereira Nov 13, 2023
f2b2527
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 13, 2023
abdb380
Fixed missing blank line
GuiMacielPereira Nov 13, 2023
4e214dc
Updated class for clearer usability
GuiMacielPereira Jan 10, 2024
9883cf5
Fixed Flake8 issues
GuiMacielPereira Jan 10, 2024
84d3fe7
Revert "Fixed Flake8 issues"
GuiMacielPereira Jan 10, 2024
dfe981a
Corrected fix for flake8 issues
GuiMacielPereira Jan 10, 2024
9a68e1a
Merge branch 'main' into 941_append_scaling_to_ws_name
GuiMacielPereira Jan 11, 2024
f29879e
Updated a recent commit to use the new ws name format
GuiMacielPereira Jan 11, 2024
2b12bdc
Removed outdated comment
GuiMacielPereira Jan 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/mslice/models/workspacemanager/workspace_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

@MialLewis MialLewis Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename this WorkspaceNameHandler or something, as we will have to prepend, not just append?


from .file_io import save_ascii, save_matlab, save_nexus, save_nxspe

Expand Down Expand Up @@ -198,27 +199,27 @@ 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)


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

Expand All @@ -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)


Expand Down
15 changes: 15 additions & 0 deletions src/mslice/workspace/helperfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,18 @@ 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}"

def subtract(ws_name: str, scaling_factor: float) -> str:
return ws_name + f"_minus_ssf_{scaling_factor:.2f}"

def sum(ws_name: str) -> str:
return ws_name + "_sum"

def rebose(ws_name: str) -> str:
return ws_name + "_bosed"
7 changes: 4 additions & 3 deletions tests/workspace_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
Loading