diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h index e5476cf0c7798d31188f4f8cb01c8baf4350ec24..b27a11398a9fac1c41d6b061c544fc1c9f424266 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h @@ -206,6 +206,10 @@ private: public slots: /// apply mask void applyMask(const QString& wsName,bool time_pixel); + /// Create a zero error free clone for the specified workspace + void createZeroErrorFreeClone(QString& originalWorkspaceName,QString& clonedWorkspaceName); + /// Destroy a zero error free cloned workspace + void deleteZeroErrorFreeClone(QString& clonedWorkspaceName); private slots: /// phi masking has changed @@ -363,8 +367,6 @@ private: QString reduceSingleRun() const; void setValidators(); - void createZeroErrorFreeClone(QString originalWorkspaceName,QString clonedWorkspaceName); - UserSubWindow * slicingWindow; }; diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp index 3db8a8d1b7c2dd201387bf0d4b443490f9eb3017..f462dddfef31cf26dafb725af91dbb8051dbd3de 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp @@ -429,6 +429,9 @@ void SANSRunWindow::saveWorkspacesDialog() //we need know if we have a pointer to a valid window or not connect(m_saveWorkspaces, SIGNAL(closing()), this, SLOT(saveWorkspacesClosed())); + // Connect the request for a zero-error-free workspace + connect(m_saveWorkspaces, SIGNAL(createZeroErrorFreeWorkspace(QString& , QString&)), + this, SLOT(createZeroErrorFreeClone(QString&, QString&))); m_uiForm.saveSel_btn->setEnabled(false); m_saveWorkspaces->show(); } @@ -3825,13 +3828,12 @@ void SANSRunWindow::setValidators() * @param clonedWorkspaceName :: The name of cloned workspace which should have its zero erros removed. * @returns The name of the cloned workspace */ -void SANSRunWindow::createZeroErrorFreeClone(QString originalWorkspaceName, QString clonedWorkspaceName) { +void SANSRunWindow::createZeroErrorFreeClone(QString& originalWorkspaceName, QString& clonedWorkspaceName) { if (workspaceExists(originalWorkspaceName)) { // Run the python script which creates the cloned workspace - QString clonedWorkspaceName = originalWorkspaceName + "_cloned_temp"; - QString pythonCode("print i.CreateZeroErrorFreeClonedWorkspace(InputWorkspace="); + QString pythonCode("print i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name="); pythonCode += originalWorkspaceName + ","; - pythonCode += "OutputWorkspace=" + clonedWorkspaceName + ")"; + pythonCode += " output_workspace_name=" + clonedWorkspaceName + ")"; QString result(runPythonCode(pythonCode, false)); result.trimmed(); @@ -3842,6 +3844,25 @@ void SANSRunWindow::createZeroErrorFreeClone(QString originalWorkspaceName, QStr } } +/** + * Destroy a zero-error free workspace clone. + * @param clonedWorkspaceName :: The name of cloned workspace which should have its zero erros removed. + */ +void SANSRunWindow::deleteZeroErrorFreeClone(QString& clonedWorkspaceName) { + if (workspaceExists(clonedWorkspaceName)) { + // Run the python script which destroys the cloned workspace + QString pythonCode("print i.DeleteZeroErrorFreeClonedWorkspace(input_workspace_name="); + pythonCode += clonedWorkspaceName + ")"; + + QString result(runPythonCode(pythonCode, false)); + result.trimmed(); + + if (!result.startsWith("Success")) { + QMessageBox::critical(this, "Error deleting a zerror error free cloned workspace", result); + } + } +} + } //namespace CustomInterfaces } //namespace MantidQt diff --git a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h index cac153f861d09deff22c19dd9b113c1ca54b9488..9ffbd68b9d1432934bd416e4737445fcbfd8f4b1 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h +++ b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h @@ -55,6 +55,8 @@ namespace MantidQt signals: void closing(); + void createZeroErrorFreeWorkspace(QString& originalWorkspace, QString& zeroFreeWorkspace); + void deleteZeroErrorFreeWorkspace(QString& zeroFreeWorkspace); private: QLineEdit *m_fNameEdit; @@ -75,6 +77,8 @@ namespace MantidQt void addButtonsDisab(int row); void closeEvent(QCloseEvent *event); QString saveList(const QList<QListWidgetItem*> & list, const QString & algorithm, QString fileBase, bool toAppend); + QHash<QString, QString> provideZeroFreeWorkspaces(const QListWidget * workspaces); + void removeZeroFreeWorkspaces(QHash<QString, QString> workspaces); private slots: void saveSel(); diff --git a/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp b/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp index a896f8e3360d01dcb0eea5e2bcb840275e9ff3ce..cea78357eee1c28930788f00e2b329cce4c2d619 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp +++ b/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp @@ -306,6 +306,9 @@ QString SaveWorkspaces::getSaveAlgExt(const QString & algName) */ void SaveWorkspaces::saveSel() { + // For each selected workspace, provide an zero-error free clone + QHash<QString, QString> workspaces = provideZeroFreeWorkspaces(m_workspaces); + QString saveCommands; for(SavFormatsConstIt i = m_savFormats.begin(); i != m_savFormats.end(); ++i) {//the key to a pointer to the check box that the user may have clicked @@ -335,7 +338,10 @@ void SaveWorkspaces::saveSel() }//end loop over formats saveCommands += "print 'success'"; - QString status(runPythonCode(saveCommands).trimmed()); + QString status(runPythonCode(saveCommands).trimmed()); + + removeZeroFreeWorkspaces(workspaces); + if ( status != "success" ) { QMessageBox::critical(this, "Error saving workspace", "One of the workspaces could not be saved in one of the selected formats"); @@ -375,3 +381,32 @@ void SaveWorkspaces::saveFileBrowse() prevValues.setValue("dir", directory); } } + +/** + * Goes through all selected workspaces and maps them to a zero-error free clone + * @param workspaces :: a QListWIdget which contains the selected workspaces + * @returns a hash which maps the original workspace to the zero-error free workspace + */ +QHash<QString, QString> SaveWorkspaces::provideZeroFreeWorkspaces(const QListWidget * workspaces) { + auto wsList = workspaces->selectedItems(); + QHash<QString, QString> workspaceMap; + for (auto it = wsList.begin(); it != wsList.end(); ++it) { + auto wsName = (*it)->text(); + auto cloneName = wsName + "_clone"; + emit createZeroErrorFreeWorkspace(wsName, cloneName); + workspaceMap.insert(wsName, cloneName); + } + + return workspaceMap; +} + +/** + * Remove all the zero-error free workspaces + * @param workspaces :: a map containing the names of all zero-error-free workspaces. + */ +void SaveWorkspaces::removeZeroFreeWorkspaces(QHash<QString, QString> workspaces) { + auto zeroFreeWorkspaceNames = workspaces.values(); + for (auto it = zeroFreeWorkspaceNames.begin(); it != zeroFreeWorkspaceNames.end(); ++it) { + emit deleteZeroErrorFreeWorkspace((*it)); + } +} diff --git a/Code/Mantid/scripts/SANS/ISISCommandInterface.py b/Code/Mantid/scripts/SANS/ISISCommandInterface.py index d4c9b600bb5d5a7f94f7ae2f8331b2bc2d5c180c..7368efbbddcc7820e5847100dff38539953da3ca 100644 --- a/Code/Mantid/scripts/SANS/ISISCommandInterface.py +++ b/Code/Mantid/scripts/SANS/ISISCommandInterface.py @@ -1157,7 +1157,13 @@ def FindBeamCentre(rlow, rupp, MaxIter = 10, xstart = None, ystart = None, toler ###################### Utility functions #################################################### -def CreateZeroFreeClonedWorkspace(input_workspace_name, output_workspace_name): +def CreateZeroErrorFreeClonedWorkspace(input_workspace_name, output_workspace_name): + """ + Creates a zero-error-free workspace + @param input_workspace_name : name of the workspace which might contain zero-error values + @param output_workspace_name : name of the workspace which will have no zero-error values + @return: success message + """ # Load the input workspace if not input_workspace_name in mtd: message = 'Failed to create a zero error free cloned workspace: The input workspace does not seem to exist.' @@ -1184,6 +1190,24 @@ def CreateZeroFreeClonedWorkspace(input_workspace_name, output_workspace_name): print message return message +def DeleteZeroErrorFreeClonedWorkspace(input_workspace_name): + """ + Deletes a zero-error-free workspace + @param input_workspace_name : name of the workspace which might contain zero-error values + @return: success message + """ + message = "" + + if input_workspace_name in mtd: + DeleteWorkspace(Workspace=input_workspace_name) + message = 'Sucess' + else: + message = 'Failed to delete a zero-error free workspace' + + print message + return message + + ############################################################################### ######################### Start of Deprecated Code ############################ ############################################################################### diff --git a/Code/Mantid/scripts/test/SansIsisGuiSettings.py b/Code/Mantid/scripts/test/SansIsisGuiSettings.py index 699b96058e787e8b4665bd91513f71732887ae50..c530c6c63af1a76514f394623df99202063fed20 100644 --- a/Code/Mantid/scripts/test/SansIsisGuiSettings.py +++ b/Code/Mantid/scripts/test/SansIsisGuiSettings.py @@ -262,7 +262,7 @@ class TestSans2DIsisRemoveZeroErrors(unittest.TestCase): ws_name = 'original' ws_clone_name = 'clone' # Act - message = i.CreateZeroFreeClonedWorkspace(input_workspace_name = ws_name, output_workspace_name = ws_clone_name) + message = i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name = ws_name, output_workspace_name = ws_clone_name) # Assert message.strip() self.assertTrue(not message.startswith('Success')) @@ -273,7 +273,7 @@ class TestSans2DIsisRemoveZeroErrors(unittest.TestCase): ws_clone_name = 'clone' self._setup_workspace(ws_name, 'Event') # Act - message = i.CreateZeroFreeClonedWorkspace(input_workspace_name = ws_name, output_workspace_name = ws_clone_name) + message = i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name = ws_name, output_workspace_name = ws_clone_name) # Assert message.strip() self.assertTrue(not message.startswith('Success')) @@ -282,14 +282,13 @@ class TestSans2DIsisRemoveZeroErrors(unittest.TestCase): self._removeWorkspace(ws_name) self.assertTrue(not ws_name in mtd) - def test_that_zeros_are_removed_correctly(self): # Arrange ws_name = 'original' ws_clone_name = 'clone' self._setup_workspace(ws_name, 'Histogram') # Act - message = i.CreateZeroFreeClonedWorkspace(input_workspace_name = ws_name, output_workspace_name = ws_clone_name) + message = i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name = ws_name, output_workspace_name = ws_clone_name) # Assert message.strip() self.assertTrue(message.startswith('Success')) @@ -300,5 +299,25 @@ class TestSans2DIsisRemoveZeroErrors(unittest.TestCase): self.assertTrue(not ws_name in mtd) self.assertTrue(not ws_clone_name in mtd) + def test_that_deletion_of_non_existent_ws_creates_error_message(self): + # Arrange + ws_name = 'ws' + # Act + message = i.DeleteZeroErrorFreeClonedWorkspace(input_workspace_name = ws_name) + # Assert + message.strip() + self.assertTrue(not message.startswith('Success')) + + def test_that_deletion_of_extent_ws_is_successful(self): + # Arrange + ws_name = 'ws' + self._setup_workspace(ws_name, 'Histogram') + # Act + Assert + self.assertTrue(ws_name in mtd) + message = i.DeleteZeroErrorFreeClonedWorkspace(input_workspace_name = ws_name) + message.strip() + self.assertTrue(not message.startswith('Success')) + self.assertTrue(not ws_name in mtd) + if __name__ == '__main__': unittest.main()