Skip to content

Commit

Permalink
Added S(Q, w) reduction using batch runner
Browse files Browse the repository at this point in the history
Refs #10451
  • Loading branch information
DanNixon committed Oct 29, 2014
1 parent 793f4ea commit 96d3d7e
Showing 1 changed file with 78 additions and 53 deletions.
131 changes: 78 additions & 53 deletions Code/Mantid/MantidQt/CustomInterfaces/src/IndirectSqw.cpp
Expand Up @@ -5,6 +5,7 @@
#include <QFileInfo>

using namespace Mantid::API;
using MantidQt::API::BatchAlgorithmRunner;

namespace MantidQt
{
Expand Down Expand Up @@ -52,7 +53,7 @@ namespace CustomInterfaces
emit showMessageBox(error);
}

if(m_uiForm.sqw_ckRebinE->isChecked() && validateEnergyRebin())
if(m_uiForm.sqw_ckRebinE->isChecked() && !validateEnergyRebin())
valid = false;

if(!validateQRebin())
Expand Down Expand Up @@ -147,80 +148,104 @@ namespace CustomInterfaces

void IndirectSqw::run()
{
QString sampleWsName = m_uiForm.sqw_dsSampleInput->getCurrentDataName();
QString sqwWsName = sampleWsName.left(sampleWsName.length() - 4) + "_sqw";
QString eRebinWsName = sampleWsName.left(sampleWsName.length() - 4) + "_r";

QString rebinString = m_uiForm.sqw_leQLow->text() + "," + m_uiForm.sqw_leQWidth->text() +
"," + m_uiForm.sqw_leQHigh->text();

QString wsname;
if(m_uiForm.sqw_dsSampleInput->isFileSelectorVisible())
{
// Load Nexus file into workspace
QString filename = m_uiForm.sqw_dsSampleInput->getFullFilePath();
QFileInfo fi(filename);
wsname = fi.baseName();

if(!loadFile(filename, wsname))
{
emit showMessageBox("Could not load Nexus file");
}
}
else
// Rebin in energy
bool rebinInEnergy = m_uiForm.sqw_ckRebinE->isChecked();
if(rebinInEnergy)
{
// Get the workspace
wsname = m_uiForm.sqw_dsSampleInput->getCurrentDataName();
}
QString eRebinString = m_uiForm.sqw_leELow->text() + "," + m_uiForm.sqw_leEWidth->text() +
"," + m_uiForm.sqw_leEHigh->text();

QString pyInput = "from mantid.simpleapi import *\n";
IAlgorithm_sptr energyRebinAlg = AlgorithmManager::Instance().create("Rebin");
energyRebinAlg->initialize();

// Create output name before rebinning
pyInput += "sqwInput = '" + wsname + "'\n";
pyInput += "sqwOutput = sqwInput[:-3] + 'sqw'\n";
energyRebinAlg->setProperty("InputWorkspace", sampleWsName.toStdString());
energyRebinAlg->setProperty("OutputWorkspace", eRebinWsName.toStdString());
energyRebinAlg->setProperty("Params", eRebinString.toStdString());

if ( m_uiForm.sqw_ckRebinE->isChecked() )
{
QString eRebinString = m_uiForm.sqw_leELow->text() + "," + m_uiForm.sqw_leEWidth->text() +
"," + m_uiForm.sqw_leEHigh->text();

pyInput += "Rebin(InputWorkspace=sqwInput, OutputWorkspace=sqwInput+'_r', Params='" + eRebinString + "')\n"
"sqwInput += '_r'\n";
m_batchAlgoRunner->addAlgorithm(energyRebinAlg);
}

pyInput +=
"efixed = " + m_uiForm.leEfixed->text() + "\n"
"rebin = '" + rebinString + "'\n";
// Get correct S(Q, w) algorithm
QString efixed = m_uiForm.leEfixed->text();

IAlgorithm_sptr sqwAlg;
QString rebinType = m_uiForm.sqw_cbRebinType->currentText();

if(rebinType == "Centre (SofQW)")
pyInput += "SofQW(InputWorkspace=sqwInput, OutputWorkspace=sqwOutput, QAxisBinning=rebin, EMode='Indirect', EFixed=efixed)\n";
sqwAlg = AlgorithmManager::Instance().create("SofQW");
else if(rebinType == "Parallelepiped (SofQW2)")
pyInput += "SofQW2(InputWorkspace=sqwInput, OutputWorkspace=sqwOutput, QAxisBinning=rebin, EMode='Indirect', EFixed=efixed)\n";
sqwAlg = AlgorithmManager::Instance().create("SofQW2");
else if(rebinType == "Parallelepiped/Fractional Area (SofQW3)")
pyInput += "SofQW3(InputWorkspace=sqwInput, OutputWorkspace=sqwOutput, QAxisBinning=rebin, EMode='Indirect', EFixed=efixed)\n";
sqwAlg = AlgorithmManager::Instance().create("SofQW3");

pyInput += "AddSampleLog(Workspace=sqwOutput, LogName='rebin_type', LogType='String', LogText='"+rebinType+"')\n";
// S(Q, w) algorithm
sqwAlg->initialize();

if ( m_uiForm.sqw_ckSave->isChecked() )
{
pyInput += "SaveNexus(InputWorkspace=sqwOutput, Filename=sqwOutput+'.nxs')\n";
BatchAlgorithmRunner::AlgorithmRuntimeProps sqwInputProps;
if(rebinInEnergy)
sqwInputProps["InputWorkspace"] = eRebinWsName.toStdString();
else
sqwInputProps["InputWorkspace"] = sampleWsName.toStdString();

if (m_uiForm.sqw_ckVerbose->isChecked())
{
pyInput += "logger.notice(\"Resolution file saved to default save directory.\")\n";
}
}
sqwAlg->setProperty("OutputWorkspace", sqwWsName.toStdString());
sqwAlg->setProperty("QAxisBinning", rebinString.toStdString());
sqwAlg->setProperty("EMode", "Indirect");
sqwAlg->setProperty("EFixed", efixed.toStdString());

if ( m_uiForm.sqw_cbPlotType->currentText() == "Contour" )
{
pyInput += "importMatrixWorkspace(sqwOutput).plotGraph2D()\n";
}
m_batchAlgoRunner->addAlgorithm(sqwAlg, sqwInputProps);

// Add sample log for S(Q, w) algorithm used
IAlgorithm_sptr sampleLogAlg = AlgorithmManager::Instance().create("AddSampleLog");
sampleLogAlg->initialize();

sampleLogAlg->setProperty("LogName", "rebin_type");
sampleLogAlg->setProperty("LogType", "String");
sampleLogAlg->setProperty("LogText", rebinType.toStdString());

else if ( m_uiForm.sqw_cbPlotType->currentText() == "Spectra" )
BatchAlgorithmRunner::AlgorithmRuntimeProps inputToAddSampleLogProps;
inputToAddSampleLogProps["Workspace"] = sqwWsName.toStdString();

m_batchAlgoRunner->addAlgorithm(sampleLogAlg, inputToAddSampleLogProps);

// Save S(Q, w) workspace
if(m_uiForm.sqw_ckSave->isChecked())
{
pyInput +=
"nspec = mtd[sqwOutput].getNumberHistograms()\n"
"plotSpectrum(sqwOutput, range(0, nspec))\n";
QString saveFilename = sqwWsName + ".nxs";

IAlgorithm_sptr saveNexusAlg = AlgorithmManager::Instance().create("SaveNexus");
saveNexusAlg->initialize();

saveNexusAlg->setProperty("Filename", saveFilename.toStdString());

BatchAlgorithmRunner::AlgorithmRuntimeProps inputToSaveNexusProps;
inputToSaveNexusProps["InputWorkspace"] = sqwWsName.toStdString();

m_batchAlgoRunner->addAlgorithm(saveNexusAlg, inputToSaveNexusProps);
}

QString pyOutput = m_pythonRunner.runPythonCode(pyInput).trimmed();
m_batchAlgoRunner->executeBatch();

//TODO
/* if(m_uiForm.sqw_cbPlotType->currentText() == "Contour") */
/* { */
/* pyInput += "importMatrixWorkspace(sqwOutput).plotGraph2D()\n"; */
/* } */

/* else if( m_uiForm.sqw_cbPlotType->currentText() == "Spectra") */
/* { */
/* pyInput += */
/* "nspec = mtd[sqwOutput].getNumberHistograms()\n" */
/* "plotSpectrum(sqwOutput, range(0, nspec))\n"; */
/* } */

/* QString pyOutput = m_pythonRunner.runPythonCode(pyInput).trimmed(); */
}

/**
Expand Down

0 comments on commit 96d3d7e

Please sign in to comment.