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

Add clone and scale MDE Workspace actions #128

Merged
merged 7 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/shiver/models/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
AnalysisDataServiceObserver,
Progress,
)
from mantid.simpleapi import mtd, DeleteWorkspace, RenameWorkspace, SaveMD
from mantid.simpleapi import (
mtd,
CloneMDWorkspace,
DeleteWorkspace,
RenameWorkspace,
SaveMD,
CreateSingleValuedWorkspace,
MultiplyMD,
)
from mantid.kernel import Logger
from mantid.geometry import (
SymmetryOperationFactory,
Expand Down Expand Up @@ -165,6 +173,15 @@ def generate_mde(self, config_dict: dict) -> str:

return config_dict["mde_name"]

def clone(self, ws_name, ws_clone_name):
ktactac-ornl marked this conversation as resolved.
Show resolved Hide resolved
"""Clone the workspace"""
CloneMDWorkspace(InputWorkspace=ws_name, OutputWorkspace=ws_clone_name)

def scale(self, ws_name_in, ws_name_out, scale_factor):
ktactac-ornl marked this conversation as resolved.
Show resolved Hide resolved
ws_scalefactor = CreateSingleValuedWorkspace(OutputWorkspace="scalefactor", DataValue=scale_factor)
MultiplyMD(LHSWorkspace=ws_name_in, RHSWorkspace=ws_scalefactor, OutputWorkspace=ws_name_out)
DeleteWorkspace(ws_scalefactor)

def delete(self, ws_name):
"""Delete the workspace"""
DeleteWorkspace(ws_name, EnableLogging=False)
Expand Down Expand Up @@ -621,6 +638,7 @@ def __init__(self):
self.observeDelete(True)
self.observeReplace(True)
self.observeRename(True)

self.callback = None

def clearHandle(self): # pylint: disable=invalid-name
Expand Down Expand Up @@ -655,6 +673,7 @@ def replaceHandle(self, ws, _): # pylint: disable=invalid-name

def renameHandle(self, old, new): # pylint: disable=invalid-name
"""Callback handle for ADS rename"""
print("doinf this")
ktactac-ornl marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(f"renameHandle: {old} {new}")
self.deleteHandle(old, None)
self.addHandle(new, None)
Expand Down
12 changes: 12 additions & 0 deletions src/shiver/presenters/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def __init__(self, view, model):

self.view.buttons.connect_load_dataset(self.load_dataset)
self.view.buttons.connect_load_file(self.load_file)

self.view.input_workspaces.mde_workspaces.connect_clone_workspace_callback(self.clone_workspace)
self.view.input_workspaces.mde_workspaces.connect_scale_workspace_callback(self.scale_workspace)

self.view.connect_delete_workspace(self.delete_workspace)
self.view.connect_rename_workspace(self.rename_workspace)
self.view.input_workspaces.mde_workspaces.connect_save_mde_workspace_callback(self.save_mde_workspace)
Expand Down Expand Up @@ -147,6 +151,14 @@ def ws_changed(self, action, name, ws_type, frame=None, ndims=0):
elif action == "clear":
self.view.clear_ws()

def clone_workspace(self, name, clone_name):
"""Called by the view to clone a workspace"""
self.model.clone(name, clone_name)

def scale_workspace(self, name, output_name, scale_factor):
"""Called by the view to scale a workspace"""
self.model.scale(name, output_name, scale_factor)

def delete_workspace(self, name):
"""Called by the view to delete a workspace"""
self.model.delete(name)
Expand Down
8 changes: 8 additions & 0 deletions src/shiver/views/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def clear_ws(self):
self.input_workspaces.clear_ws()
self.histogram_workspaces.clear_ws()

def connect_clone_workspace(self, callback):
"""connect a function to clone a workspace"""
self.input_workspaces.mde_workspaces.clone_workspace_callback = callback

def connect_scale_workspace(self, callback):
"""connect a function to scale the workspace"""
self.input_workspaces.mde_workspaces.scale_workspace_callback = callback

def connect_delete_workspace(self, callback):
"""connect a function to the selection of a filename"""
self.input_workspaces.mde_workspaces.delete_workspace_callback = callback
Expand Down
88 changes: 88 additions & 0 deletions src/shiver/views/workspace_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from enum import Enum
from qtpy.QtWidgets import (
QVBoxLayout,
QHBoxLayout,
QDialog,
QDialogButtonBox,
QLineEdit,
QListWidget,
QListWidgetItem,
QGroupBox,
Expand Down Expand Up @@ -201,6 +205,10 @@ def __init__(self, parent=None):
self._data_nsf = None
self._data_sf = None
self._background = None

self.clone_workspace_callback = None
self.scale_workspace_callback = None

self.rename_workspace_callback = None
self.save_mde_workspace_callback = None
self.delete_workspace_callback = None
Expand All @@ -225,6 +233,12 @@ def connect_get_polarization_logs(self, callback):
"""connect a function to get the sample logs for workspace"""
self.get_polarization_logs_callback = callback

def connect_clone_workspace_callback(self, callback):
self.clone_workspace_callback = callback

def connect_scale_workspace_callback(self, callback):
self.scale_workspace_callback = callback

def connect_save_mde_workspace_callback(self, callback):
"""connect a function to save the mde workspace"""
self.save_mde_workspace_callback = callback
Expand All @@ -235,6 +249,7 @@ def initialize_default(self):

def add_ws(self, name, ws_type, frame, ndims):
"""Adds a workspace to the list if it is of the correct type"""

if ws_type == self.ws_type and name != "None":
frame_type = Frame[frame]
item = QListWidgetItem(name, type=frame_type.value)
Expand Down Expand Up @@ -319,6 +334,18 @@ def mousePressEvent(self, event): # pylint: disable=invalid-name
menu.addSeparator()

# data manipulation

clone = QAction("Clone")
clone.triggered.connect(partial(self.clone_ws, selected_ws_name))
menu.addAction(clone)

scale = QAction("Scale")
scale.triggered.connect(partial(self.scale_ws, selected_ws_name))
menu.addAction(scale)

menu.addSeparator()

# workspace handling
save = QAction("Save Workspace")
save.triggered.connect(partial(self.save_mde_ws, selected_ws_name))
menu.addAction(save)
Expand Down Expand Up @@ -493,6 +520,67 @@ def set_pol_options(self, name):
# at least one data workspace should be selected
self.validate_data_workspace_state()

def clone_ws(self, name):
"""method to clone the selected workspace"""

dialog = QInputDialog(self)
dialog.setLabelText(f"Clone {name} as:")
dialog.setTextValue(f"{name}_clone")
dialog.setOkButtonText("Clone")
if not dialog.exec_():
return

if self.clone_workspace_callback:
self.clone_workspace_callback(name, dialog.textValue())

# unselect the previous workspaces state of this workspace with name
self.unset_selected_states_with_name(name)

# at least one data workspace should be selected
self.validate_data_workspace_state()

def scale_ws(self, name):
"""method to scale the workspace data, creating a new workspace"""

dialog = QDialog(self)
dialog.setWindowTitle("Scale MDE Workspace")

layout = QVBoxLayout()

# scale factor input
scale_factor_layout = QHBoxLayout()
scale_factor_layout.addWidget(QLabel("Scale Factor:"))
scale_factor_input = QLineEdit("1.0")
ktactac-ornl marked this conversation as resolved.
Show resolved Hide resolved
scale_factor_layout.addWidget(scale_factor_input)
layout.addLayout(scale_factor_layout)

# output workspace input
output_workspace_layout = QHBoxLayout()
output_workspace_layout.addWidget(QLabel("Output Workspace:"))
output_workspace_input = QLineEdit(name)
output_workspace_layout.addWidget(output_workspace_input)
layout.addLayout(output_workspace_layout)

# OK/Cancel
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(dialog.accept)
button_box.rejected.connect(dialog.reject)
layout.addWidget(button_box)

dialog.setLayout(layout)

if not dialog.exec_():
return

if self.scale_workspace_callback:
self.scale_workspace_callback(name, output_workspace_input.text(), scale_factor_input.text())

# unselect the previous workspaces state of this workspace with name
self.unset_selected_states_with_name(name)

# at least one data workspace should be selected
self.validate_data_workspace_state()

def save_mde_ws(self, name):
"""method to save workspace data in file"""
filename, _ = QFileDialog.getSaveFileName(
Expand Down