diff --git a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp index c7bc0a5da1050cfba2f84bfe5e36095f60c775e6..e21718cf1c2870755db0048f5b711ba3dbec9329 100644 --- a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp +++ b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp @@ -151,9 +151,11 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName, std::multimap<std::string, int> names; names.insert(std::make_pair(algorithmName, -1)); IAlgorithm_sptr loader; - if (nexus && NexusDescriptor::isHDF(filename)) { - loader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>( - filename, names, m_log); + if (nexus) { + if( NexusDescriptor::isHDF(filename)) { + loader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>( + filename, names, m_log); + } } else { loader = searchForLoader<FileDescriptor, IFileLoader<FileDescriptor>>( filename, names, m_log); diff --git a/Code/Mantid/MantidQt/CustomInterfaces/CMakeLists.txt b/Code/Mantid/MantidQt/CustomInterfaces/CMakeLists.txt index 204087c2d85f6c1aab7be466dcd8c26b979df3b7..fb125624cfc370800c88569d1f7e5b1f9d9c2003 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/CMakeLists.txt +++ b/Code/Mantid/MantidQt/CustomInterfaces/CMakeLists.txt @@ -79,6 +79,7 @@ set ( SRC_FILES src/ReflSearchModel.cpp src/SampleTransmission.cpp src/SANSAddFiles.cpp + src/SANSConstants.cpp src/SANSDiagnostics.cpp src/SANSEventSlicing.cpp src/SANSPlotSpecial.cpp @@ -186,6 +187,7 @@ set ( INC_FILES inc/MantidQtCustomInterfaces/QtReflOptionsDialog.h inc/MantidQtCustomInterfaces/SampleTransmission.h inc/MantidQtCustomInterfaces/SANSAddFiles.h + inc/MantidQtCustomInterfaces/SANSConstants.h inc/MantidQtCustomInterfaces/SANSDiagnostics.h inc/MantidQtCustomInterfaces/SANSEventSlicing.h inc/MantidQtCustomInterfaces/SANSPlotSpecial.h diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSAddFiles.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSAddFiles.h index 15bf132635a9720a3ec9ceef01d50bda475bf8bc..f80ca42edbf513760945237a6a07ca7e0f8d7070 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSAddFiles.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSAddFiles.h @@ -2,6 +2,7 @@ #define MANTIDQTCUSTOMINTERFACES_SANSADDFILES_H_ #include "ui_SANSRunWindow.h" +#include "MantidQtCustomInterfaces/SANSConstants.h" #include "MantidQtAPI/UserSubWindow.h" #include "MantidKernel/ConfigService.h" #include <Poco/NObserver.h> @@ -55,6 +56,8 @@ private: void setInputEnabled(bool enabled); /// Create Python string list QString createPythonStringList(QString inputString); + /// SANS constants + SANSConstants m_constants; void initLayout(); void setToolTips(); @@ -85,6 +88,12 @@ private slots: void onCurrentIndexChangedForHistogramChoice(int index); /// reacts to changes of the overlay check box void onStateChangedForOverlayCheckBox(int); + /// checks if a file corresponds to a histogram worksapce + bool isEventWorkspace(QString file_name); + /// checks if the files which are to be added are all based on event workspaces + bool existNonEventFiles(); + /// sets the binning options + void setBinningOptions(bool enable); }; } diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSConstants.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSConstants.h new file mode 100644 index 0000000000000000000000000000000000000000..77286a688dd659ce14e42d961638e0a07329be15 --- /dev/null +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSConstants.h @@ -0,0 +1,24 @@ +#ifndef MANTIDQTCUSTOMINTERFACES_SANSCONSTANTS_H_ +#define MANTIDQTCUSTOMINTERFACES_SANSCONSTANTS_H_ + +#include <QString> + +namespace MantidQt +{ +namespace CustomInterfaces +{ + +class SANSConstants +{ +public: + SANSConstants(); + ~SANSConstants(); + QString getPythonSuccessKeyword(); + QString getPythonEmptyKeyword(); + QString getPythonTrueKeyword(); +}; + +} +} + +#endif //MANTIDQTCUSTOMINTERFACES_SANSADDFILES_H_ \ No newline at end of file diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h index 3f1a1cf455e95935bb5bb874bfc887fccb8b8b10..9e307745714bc89c51f754550e321e6a82838a6b 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h @@ -10,6 +10,7 @@ #include "MantidQtMantidWidgets/SaveWorkspaces.h" #include "MantidQtCustomInterfaces/SANSDiagnostics.h" #include "MantidQtCustomInterfaces/SANSPlotSpecial.h" +#include "MantidQtCustomInterfaces/SANSConstants.h" #include <QHash> #include <QSettings> @@ -360,7 +361,9 @@ private: static const QString m_pythonEmptyKeyword; /// Stores the URL of each tab's help page. QMap<Tab, QString> m_helpPageUrls; - + /// SANS constants + SANSConstants m_constants; + void initAnalysDetTab(); void makeValidator(QLabel * const newValid, QWidget * control, QWidget * tab, const QString & errorMsg); void upDateDataDir(); diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui index 90f1a1534660a12c95070ae9cc1e7637392cb5f5..4569a5bc0f3c86a980a2030ba107048527975604 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui @@ -3952,7 +3952,7 @@ p, li { white-space: pre-wrap; } </widget> </item> <item row="9" column="2"> - <widget class="QLabel" name="label"> + <widget class="QLabel" name="histogram_binning_label"> <property name="text"> <string>Histogram binning (when adding event data):</string> </property> @@ -3997,7 +3997,7 @@ p, li { white-space: pre-wrap; } <item row="12" column="2"> <layout class="QHBoxLayout" name="horizontalLayout_24"> <item> - <widget class="QLabel" name="labelBinning"> + <widget class="QLabel" name="binning_label"> <property name="minimumSize"> <size> <width>70</width> diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSAddFiles.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSAddFiles.cpp index 98a2b4272fee0a5199f291d388da03dc75203002..e41439b5f60bf37e9cfa9260dbaa94e27da49405 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSAddFiles.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSAddFiles.cpp @@ -248,6 +248,11 @@ void SANSAddFiles::add2Runs2Add() if ( search.isValid() == "" ) {//this means the file was found newL->setToolTip(QString::fromStdString(search.value())); + + // If we don't have an event workspace data set, then we disable the event options + if (!isEventWorkspace(QString::fromStdString(search.value()))) { + setBinningOptions(false); + } } } } @@ -422,6 +427,7 @@ void SANSAddFiles::clearClicked() { m_SANSForm->toAdd_List->clear(); insertListFront(""); + setBinningOptions(true); } void SANSAddFiles::removeSelected() @@ -433,6 +439,11 @@ void SANSAddFiles::removeSelected() delete m_SANSForm->toAdd_List->takeItem(selRow); sels = m_SANSForm->toAdd_List->selectedItems(); } + + // Check if the remaining files correspond to only event workspaces + if (!existNonEventFiles()) { + setBinningOptions(true); + } } /** @@ -526,8 +537,8 @@ void SANSAddFiles::setHistogramUiLogic(QString label, QString toolTip, QString l m_SANSForm->eventToHistBinning->setToolTip(toolTip); // Label for line edit field - m_SANSForm->labelBinning->setText(label); - m_SANSForm->labelBinning->setToolTip(toolTip); + m_SANSForm->binning_label->setText(label); + m_SANSForm->binning_label->setToolTip(toolTip); setInputEnabled(enabled); } @@ -539,7 +550,7 @@ void SANSAddFiles::setHistogramUiLogic(QString label, QString toolTip, QString l */ void SANSAddFiles::setInputEnabled(bool enabled) { m_SANSForm->eventToHistBinning->setEnabled(enabled); - m_SANSForm->labelBinning->setEnabled(enabled); + m_SANSForm->binning_label->setEnabled(enabled); } /** @@ -570,6 +581,59 @@ QString SANSAddFiles::createPythonStringList(QString inputString) { return formattedString; } +/** + * Check if a file corresponds to a histogram workspace + * @param fileName: the file name + * @returns true if it is a histogram workspace + */ +bool SANSAddFiles::isEventWorkspace(QString fileName) { + auto isEvent = false; + fileName.replace("\\", "/"); + QString code_torun = "import ISISCommandInterface as i\n"; + code_torun += "i.check_if_event_workspace(file_name='"; + code_torun += fileName; + code_torun += + "')\n"; + + auto status = runPythonCode(code_torun, false); + if (status.contains(m_constants.getPythonTrueKeyword())) { + isEvent = true; + } + return isEvent; +} + +/** + * Enable or disable the binning options + * @param enable: If the options should be enabled or disabled + */ +void SANSAddFiles::setBinningOptions(bool enable) { + m_SANSForm->eventToHistBinning->setEnabled(enable); + m_SANSForm->comboBox_histogram_choice->setEnabled(enable); + m_SANSForm->overlayCheckBox->setEnabled(enable); + m_SANSForm->histogram_binning_label->setEnabled(enable); + m_SANSForm->binning_label->setEnabled(enable); +} + +/** + * Check if non-event type files exist + * returns true if all are event files or if there are no files else false + */ +bool SANSAddFiles::existNonEventFiles() { + auto elements = m_SANSForm->toAdd_List->count(); + for (int i = 0; i < elements; ++i) { + auto fileName = m_SANSForm->toAdd_List->item(i)->data(Qt::WhatsThisRole).toString(); + if ( !fileName.isEmpty() ) + { + // Make sure that the file separators are valid + fileName.replace("\\", "/"); + // Run the check + if (!isEventWorkspace(fileName)) { + return true; + } + } + } + + return false; +} }//namespace CustomInterfaces diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSConstants.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSConstants.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d34051f49f63831215aa44f83149ebf96693fe8 --- /dev/null +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSConstants.cpp @@ -0,0 +1,40 @@ +#include "MantidQtCustomInterfaces/SANSConstants.h" + + +namespace MantidQt +{ +namespace CustomInterfaces +{ +SANSConstants::SANSConstants() {} + +SANSConstants::~SANSConstants() {} + +/** + * Defines the python keyword for a succssful operation + * @returns the keyword for success + */ +QString SANSConstants::getPythonSuccessKeyword() { + const static QString pythonSuccessKeyword = "pythonExecutionWasSuccessful"; + return pythonSuccessKeyword; +} + +/** + * Defines the python keyword for an empty object , ie None + * @returns the python keyword for an empty object + */ +QString SANSConstants::getPythonEmptyKeyword() { + const static QString pythonSuccessKeyword = "None"; + return pythonSuccessKeyword; +} + +/** + * Defines the python keyword for true , ie True + * @returns the python true keyword + */ +QString SANSConstants::getPythonTrueKeyword() { + const static QString pythonSuccessKeyword = "True"; + return pythonSuccessKeyword; +} + +} +} \ No newline at end of file diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp index 2ef3237e2aaf6504a6db613ce751bf18b6913996..97de7a5fcf2465d8ba8abc08797299e2d88b67ed 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp @@ -149,11 +149,7 @@ namespace } } -//---------------------------------------------- -// Static key strings -//---------------------------------------------- -const QString SANSRunWindow::m_pythonSuccessKeyword = "pythonExecutionWasSuccessful"; -const QString SANSRunWindow::m_pythonEmptyKeyword = "None"; + //---------------------------------------------- // Public member functions //---------------------------------------------- @@ -3020,7 +3016,7 @@ void SANSRunWindow::handleInstrumentChange() int ind = m_uiForm.detbank_sel->findText(detect); // We set the detector selection only if nothing is set yet. // Previously, we didn't handle merged and both at this point - if (detectorSelection == m_pythonEmptyKeyword || detectorSelection.isEmpty()) { + if (detectorSelection == m_constants.getPythonEmptyKeyword() || detectorSelection.isEmpty()) { if( ind != -1 ) { m_uiForm.detbank_sel->setCurrentIndex(ind); } @@ -3881,11 +3877,11 @@ void SANSRunWindow::createZeroErrorFreeClone(QString& originalWorkspaceName, QSt QString pythonCode("print i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name='"); pythonCode += originalWorkspaceName + "',"; pythonCode += " output_workspace_name='" + clonedWorkspaceName + "')\n"; - pythonCode += "print '" + m_pythonSuccessKeyword + "'\n"; + pythonCode += "print '" + m_constants.getPythonSuccessKeyword() + "'\n"; QString result(runPythonCode(pythonCode, false)); result = result.simplified(); - if (result != m_pythonSuccessKeyword) { - result.replace(m_pythonSuccessKeyword, ""); + if (result != m_constants.getPythonSuccessKeyword()) { + result.replace(m_constants.getPythonSuccessKeyword(), ""); g_log.warning("Error creating a zerror error free cloned workspace. Will save original workspace. More info: " + result.toStdString()); } } @@ -3900,11 +3896,11 @@ void SANSRunWindow::deleteZeroErrorFreeClone(QString& clonedWorkspaceName) { // Run the python script which destroys the cloned workspace QString pythonCode("print i.DeleteZeroErrorFreeClonedWorkspace(input_workspace_name='"); pythonCode += clonedWorkspaceName + "')\n"; - pythonCode += "print '" + m_pythonSuccessKeyword + "'\n"; + pythonCode += "print '" + m_constants.getPythonSuccessKeyword() + "'\n"; QString result(runPythonCode(pythonCode, false)); result = result.simplified(); - if (result != m_pythonSuccessKeyword) { - result.replace(m_pythonSuccessKeyword, ""); + if (result != m_constants.getPythonSuccessKeyword()) { + result.replace(m_constants.getPythonSuccessKeyword(), ""); g_log.warning("Error deleting a zerror error free cloned workspace. More info: " + result.toStdString()); } } @@ -3917,12 +3913,12 @@ void SANSRunWindow::deleteZeroErrorFreeClone(QString& clonedWorkspaceName) { bool SANSRunWindow::isValidWsForRemovingZeroErrors(QString& wsName) { QString pythonCode("\nprint i.IsValidWsForRemovingZeroErrors(input_workspace_name='"); pythonCode += wsName + "')"; - pythonCode += "\nprint '" + m_pythonSuccessKeyword + "'"; + pythonCode += "\nprint '" + m_constants.getPythonSuccessKeyword() + "'"; QString result(runPythonCode(pythonCode, false)); result = result.simplified(); bool isValid = true; - if (result != m_pythonSuccessKeyword) { - result.replace(m_pythonSuccessKeyword, ""); + if (result != m_constants.getPythonSuccessKeyword()) { + result.replace(m_constants.getPythonSuccessKeyword(), ""); g_log.warning("Not a valid workspace for zero error replacement. Will save original workspace. More info: " + result.toStdString()); isValid = false; } diff --git a/Code/Mantid/scripts/CMakeLists.txt b/Code/Mantid/scripts/CMakeLists.txt index a1c21e75322852bf4ed990304719fb33691d4464..7c00c508283118881ab92f0d7c8e6b079bc1f0f3 100644 --- a/Code/Mantid/scripts/CMakeLists.txt +++ b/Code/Mantid/scripts/CMakeLists.txt @@ -29,6 +29,7 @@ set ( TEST_PY_FILES test/RunDescriptorTest.py test/SansIsisGuiSettings.py test/SANSBatchModeTest.py + test/SANSCommandInterfaceTest.py test/SANSUtilityTest.py test/SANSIsisInstrumentTest.py test/SANSReductionStepsUserFileTest.py diff --git a/Code/Mantid/scripts/SANS/ISISCommandInterface.py b/Code/Mantid/scripts/SANS/ISISCommandInterface.py index 15e984d71865825a79954aab836dfa849a3b2e0b..61412c30a698b8c7ce050fec64cd46b1d820cab8 100644 --- a/Code/Mantid/scripts/SANS/ISISCommandInterface.py +++ b/Code/Mantid/scripts/SANS/ISISCommandInterface.py @@ -13,7 +13,7 @@ import isis_reducer from centre_finder import CentreFinder as CentreFinder #import SANSReduction from mantid.simpleapi import * -from mantid.api import WorkspaceGroup +from mantid.api import WorkspaceGroup, FileLoaderRegistry import copy from SANSadd2 import * import SANSUtility as su @@ -1198,6 +1198,18 @@ def IsValidWsForRemovingZeroErrors(input_workspace_name): else: return "" + +def check_if_event_workspace(file_name): + ''' + Checks if a file is associated with an event workspace. It tests if + the workspace can be loaded. + @param file_name: The file name to test + @returns true if the workspace is an event workspace otherwise false + ''' + result = FileLoaderRegistry.canLoad("LoadEventNexus", file_name) + print result + return result + ################################################################################ # Input check functions diff --git a/Code/Mantid/scripts/test/SANSCommandInterfaceTest.py b/Code/Mantid/scripts/test/SANSCommandInterfaceTest.py new file mode 100644 index 0000000000000000000000000000000000000000..24519908b66687a4d322d9d68c809a12bcc4d2b4 --- /dev/null +++ b/Code/Mantid/scripts/test/SANSCommandInterfaceTest.py @@ -0,0 +1,40 @@ +import unittest +import re +# Need to import mantid before we import SANSUtility +import mantid +from mantid.simpleapi import * +import ISISCommandInterface as ci + + +class TestEventWorkspaceCheck(unittest.TestCase): + def _create_file_name(self, name): + temp_save_dir = config['defaultsave.directory'] + if (temp_save_dir == ''): + temp_save_dir = os.getcwd() + return os.path.join(temp_save_dir, names + '.nxs') + + def _clean_up(self, file_name): + if os.path.exists(file_name): + os.remove(file_name) + + def test_that_event_workspace_is_detected(self): + # Arrange + ws = CreateSampleWorkspace() + file_name = self._create_file_name('dummy') + SaveNexus(Filename= file_name, InputWorkspace=ws) + # Act + result = ci.check_if_event_workspace(file_name) + self.assertFalse(result) + # Clean Up + self._clean_up(file_name) + + def test_that_histogram_workspace_is_detected(self): + # Arrange + ws = CreateSampleWorkspace('Event') + file_name = self._create_file_name('dummy') + SaveNexus(Filename= file_name, InputWorkspace=ws) + # Act + result = ci.check_if_event_workspace(file_name) + self.assertTrue(result) + # Clean Up + self._clean_up(file_name)