Skip to content

Commit 12cdfdd

Browse files
adriazalvarezDiaz Alvarez
andauthored
Improve output name in Container Subtraction algorithm (#41028)
Co-authored-by: Diaz Alvarez <twy67886@NDLT2054LINUX.Home>
1 parent bd9dfe7 commit 12cdfdd

File tree

7 files changed

+58
-66
lines changed

7 files changed

+58
-66
lines changed

Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/ApplyPaalmanPingsCorrection.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from mantid.api import (
1010
PythonAlgorithm,
1111
AlgorithmFactory,
12+
AnalysisDataService as ads,
1213
MatrixWorkspaceProperty,
1314
WorkspaceGroupProperty,
1415
PropertyMode,
@@ -147,6 +148,10 @@ def PyExec(self):
147148
if output_workspace.name():
148149
RenameWorkspace(InputWorkspace=output_workspace, OutputWorkspace=self.getPropertyValue("OutputWorkspace"))
149150
self.setProperty("OutputWorkspace", output_workspace)
151+
152+
# Remove hidden workspaces
153+
self._remove_hidden_ws()
154+
150155
prog_wrkflow.report("Algorithm Complete")
151156

152157
def validateInputs(self):
@@ -216,6 +221,7 @@ def _setup(self):
216221
self._shift_can = self._can_shift_factor != 0.0
217222

218223
self._rebin_container_ws = self.getProperty("RebinCanToSample").value
224+
self._hidden_ws_cache = {ws_name for ws_name in ads.getObjectNames() if ws_name.startswith("_")}
219225

220226
if self._use_corrections:
221227
if self._corrections_workspace.size() == 1:
@@ -339,7 +345,7 @@ def _correct_sample(self, sample_workspace, a_ss_workspace):
339345
"""
340346
logger.information("Correcting sample")
341347
correction_in_lambda = self._convert_units_wavelength(a_ss_workspace)
342-
corrected = Divide(LHSWorkspace=sample_workspace, RHSWorkspace=correction_in_lambda)
348+
corrected = Divide(LHSWorkspace=sample_workspace, RHSWorkspace=correction_in_lambda, OutputWorkspace="__corrected")
343349
return corrected
344350

345351
def _correct_sample_can(self, sample_workspace, container_workspace, factor_workspaces):
@@ -379,6 +385,11 @@ def _two_factor_corrections_approximation(self, sample_workspace, container_work
379385
difference = Minus(minuend, subtrahend, OutputWorkspace="__difference")
380386
return difference
381387

388+
def _remove_hidden_ws(self):
389+
hidden_ws_on_alg_end = {ws_name for ws_name in ads.getObjectNames() if ws_name.startswith("_")}
390+
for ws_name in hidden_ws_on_alg_end.difference(self._hidden_ws_cache):
391+
ads.remove(ws_name)
392+
382393

383394
# Register algorithm with Mantid
384395
AlgorithmFactory.subscribe(ApplyPaalmanPingsCorrection)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix a bug in the :ref:`Container Subtraction <container-subtraction>` and :ref:`Apply Absorbtion corrections <apply_absorp_correct>` tabs of the :ref:`Corrections <interface-inelastic-corrections>` interface where the output name of the corrected workspace when a container with multiple runs is used contains commas.

qt/scientific_interfaces/Inelastic/Corrections/ApplyAbsorptionCorrections.cpp

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,16 @@ void ApplyAbsorptionCorrections::handleRun() {
234234

235235
const bool useCan = m_uiForm.ckUseCan->isChecked();
236236
// Get Can and Clone
237-
MatrixWorkspace_sptr canClone;
238237
if (useCan) {
239238
const auto canName = m_uiForm.dsContainer->getCurrentDataName().toStdString();
240239
const auto cloneName = "__algorithm_can";
241240
IAlgorithm_sptr clone = AlgorithmManager::Instance().create("CloneWorkspace");
242241
clone->initialize();
243242
clone->setProperty("InputWorkspace", canName);
244-
clone->setProperty("Outputworkspace", cloneName);
243+
clone->setProperty("OutputWorkspace", cloneName);
245244
clone->execute();
246245

247-
canClone = getADSWorkspace(cloneName);
246+
MatrixWorkspace_sptr canClone = getADSWorkspace(cloneName);
248247
// Check for same binning across sample and container
249248
if (!checkWorkspaceBinningMatches(sampleWs, canClone)) {
250249
const char *text = "Binning on sample and container does not match."
@@ -281,8 +280,6 @@ void ApplyAbsorptionCorrections::handleRun() {
281280
applyCorrAlg->setProperty("RebinCanToSample", rebinContainer);
282281
}
283282

284-
QString correctionsWsName = m_uiForm.dsCorrections->getCurrentDataName();
285-
286283
bool interpolateAll = false;
287284
for (std::size_t i = 0; i < m_ppCorrectionsGp->size(); i++) {
288285
MatrixWorkspace_sptr factorWs = std::dynamic_pointer_cast<MatrixWorkspace>(m_ppCorrectionsGp->getItem(i));
@@ -321,13 +318,25 @@ void ApplyAbsorptionCorrections::handleRun() {
321318
applyCorrAlg->setProperty("CorrectionsWorkspace", m_correctionsGroupName);
322319
}
323320

324-
// Generate output workspace name
325-
auto QStrSampleWsName = QString::fromStdString(m_sampleWorkspaceName);
326-
int nameCutIndex = QStrSampleWsName.lastIndexOf("_");
327-
if (nameCutIndex == -1)
328-
nameCutIndex = QStrSampleWsName.length();
321+
const auto outputWsName = createOutputName();
322+
applyCorrAlg->setProperty("OutputWorkspace", outputWsName);
323+
324+
// Add corrections algorithm to queue
325+
m_batchAlgoRunner->addAlgorithm(applyCorrAlg, std::move(absCorProps));
326+
327+
// Run algorithm queue
328+
connect(m_batchAlgoRunner, &API::BatchAlgorithmRunner::batchComplete, this,
329+
&ApplyAbsorptionCorrections::absCorComplete);
330+
m_batchAlgoRunner->executeBatchAsync();
331+
332+
// Set the result workspace for Python script export
333+
m_pythonExportWsName = outputWsName;
334+
}
329335

330-
QString geometryType;
336+
std::string ApplyAbsorptionCorrections::createOutputName() const {
337+
// Find type of correction prefixes
338+
QString correctionsWsName = m_uiForm.dsCorrections->getCurrentDataName();
339+
std::string geometryType;
331340
if (correctionsWsName.contains("FlatPlate")) {
332341
geometryType = "_flt";
333342
} else if (correctionsWsName.contains("Annulus")) {
@@ -336,45 +345,21 @@ void ApplyAbsorptionCorrections::handleRun() {
336345
geometryType = "_cyl";
337346
}
338347

339-
QString correctionType;
348+
std::string correctionType;
340349
if (correctionsWsName.contains("PP")) {
341350
correctionType = "_PP";
342351
} else if (correctionsWsName.contains("MC")) {
343352
correctionType = "_MC";
344353
}
345354

346-
QString outputWsName = QStrSampleWsName.left(nameCutIndex);
347-
outputWsName += geometryType + correctionType + "_Corrected";
348-
355+
std::string outputName = m_sampleWorkspaceName.substr(0, m_sampleWorkspaceName.find_last_of("_"));
356+
outputName += geometryType + correctionType + "_Corrected";
349357
// Using container
350358
if (m_uiForm.ckUseCan->isChecked()) {
351-
auto const canName = m_uiForm.dsContainer->getCurrentDataName().toStdString();
352-
auto const containerWs = getADSWorkspace(canName);
353-
auto const &logs = containerWs->run();
354-
if (logs.hasProperty("run_number")) {
355-
outputWsName += "_" + QString::fromStdString(logs.getProperty("run_number")->value());
356-
} else {
357-
auto canCutIndex = QString::fromStdString(canName).indexOf("_");
358-
outputWsName += "_" + QString::fromStdString(canName).left(canCutIndex);
359-
}
359+
outputName += "_" + prepareContainerName(m_uiForm.dsContainer->getCurrentDataName().toStdString());
360360
}
361361

362-
outputWsName += "_red";
363-
364-
applyCorrAlg->setProperty("OutputWorkspace", outputWsName.toStdString());
365-
366-
// Add corrections algorithm to queue
367-
m_batchAlgoRunner->addAlgorithm(applyCorrAlg, std::move(absCorProps));
368-
369-
// Run algorithm queue
370-
connect(m_batchAlgoRunner, &API::BatchAlgorithmRunner::batchComplete, this,
371-
&ApplyAbsorptionCorrections::absCorComplete);
372-
m_batchAlgoRunner->executeBatchAsync();
373-
374-
// Set the result workspace for Python script export
375-
m_pythonExportWsName = outputWsName.toStdString();
376-
// m_containerWorkspaceName = m_uiForm.dsContainer->getCurrentDataName();
377-
// updateContainer();
362+
return outputName + "_red";
378363
}
379364

380365
/**

qt/scientific_interfaces/Inelastic/Corrections/ApplyAbsorptionCorrections.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private slots:
5555
void plotInPreview(const QString &curveName, Mantid::API::MatrixWorkspace_sptr &ws, const QColor &curveColor);
5656

5757
void setSaveResultEnabled(bool enabled);
58+
std::string createOutputName() const;
5859

5960
Ui::ApplyAbsorptionCorrections m_uiForm;
6061

qt/scientific_interfaces/Inelastic/Corrections/ContainerSubtraction.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,9 @@ void ContainerSubtraction::handleRun() {
154154
}
155155

156156
std::string ContainerSubtraction::createOutputName() {
157-
QString QStrContainerWs = QString::fromStdString(m_csContainerWS->getName());
158-
auto QStrSampleWs = QString::fromStdString(m_csSampleWS->getName());
159-
int sampleNameCutIndex = QStrSampleWs.lastIndexOf("_");
160-
if (sampleNameCutIndex == -1)
161-
sampleNameCutIndex = QStrSampleWs.length();
162-
163-
std::string runNum = "";
164-
int containerNameCutIndex = 0;
165-
if (m_csContainerWS->run().hasProperty("run_number")) {
166-
runNum = m_csContainerWS->run().getProperty("run_number")->value();
167-
} else {
168-
containerNameCutIndex = QStrContainerWs.indexOf("_");
169-
if (containerNameCutIndex == -1)
170-
containerNameCutIndex = QStrContainerWs.length();
171-
}
172-
173-
QString outputWsName = QStrSampleWs.left(sampleNameCutIndex) + "_Subtract_";
174-
if (runNum.compare("") != 0) {
175-
outputWsName += QString::fromStdString(runNum);
176-
} else {
177-
outputWsName += QStrContainerWs.left(containerNameCutIndex);
178-
}
179-
180-
outputWsName += "_red";
181-
return outputWsName.toStdString();
157+
const auto &sampleName = m_csSampleWS->getName();
158+
const auto containerName = prepareContainerName(m_csContainerWS->getName());
159+
return sampleName.substr(0, sampleName.find_last_of("_")) + "_Subtract_" + containerName + "_red";
182160
}
183161

184162
void ContainerSubtraction::loadSettings(const QSettings &settings) {

qt/scientific_interfaces/Inelastic/Corrections/CorrectionsTab.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "MantidAPI/WorkspaceGroup.h"
1010
#include "MantidQtWidgets/Common/WorkspaceUtils.h"
1111

12+
#include <MantidAPI/Run.h>
1213
#include <QSettings>
1314

1415
using namespace Mantid::API;
@@ -143,4 +144,18 @@ void CorrectionsTab::displayInvalidWorkspaceTypeError(const std::string &workspa
143144
emit showMessageBox(errorMessage);
144145
}
145146

147+
std::string CorrectionsTab::prepareContainerName(const std::string &containerName) {
148+
std::string runNum;
149+
const auto containerWs = getADSWorkspace(containerName);
150+
if (const auto &logs = containerWs->run(); logs.hasProperty("run_number")) {
151+
runNum = logs.getProperty("run_number")->value();
152+
if (const auto pos = runNum.find_first_of(","); pos != std::string::npos) {
153+
runNum = runNum.substr(0, pos) + "-" + runNum.substr(runNum.find_last_of(",") + 1);
154+
}
155+
} else {
156+
runNum = containerName.substr(0, containerName.find_first_of("_"));
157+
}
158+
return runNum;
159+
}
160+
146161
} // namespace MantidQt::CustomInterfaces

qt/scientific_interfaces/Inelastic/Corrections/CorrectionsTab.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class MANTIDQT_INELASTIC_DLL CorrectionsTab : public InelasticTab {
8989
double eFixed = 0.0);
9090
/// Displays and logs the error for a workspace with an invalid type
9191
void displayInvalidWorkspaceTypeError(const std::string &workspaceName, Mantid::Kernel::Logger &log);
92+
static std::string prepareContainerName(const std::string &containerName);
9293

9394
/// DoubleEditorFactory
9495
DoubleEditorFactory *m_dblEdFac;

0 commit comments

Comments
 (0)