Skip to content

Commit

Permalink
Add MSGDiffractionReduction algorithm, replace Python in UI
Browse files Browse the repository at this point in the history
Refs #10215
  • Loading branch information
DanNixon committed Sep 23, 2014
1 parent f7b94e6 commit 7f1ac35
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 46 deletions.
@@ -0,0 +1,100 @@
from mantid.simpleapi import *
from mantid.api import *
from mantid.kernel import *
from mantid import config
import os.path, math

class MSGDiffractionReduction(PythonAlgorithm):

def category(self):
return 'Workflow\\MIDAS;PythonAlgorithms;Indirect'

def summary(self):
return 'Calculates the scattering & transmission for Indirect Geometry spectrometers.'

def PyInit(self):
self.declareProperty(StringArrayProperty(name='InputFiles'),
doc='Comma separated list of input files.')

self.declareProperty(name='SumFiles', defaultValue=False,
doc='Enabled to sum spectra from each input file.')

self.declareProperty(name='Instrument', defaultValue='IRIS',
validator=StringListValidator(['IRIS', 'OSIRIS', 'TOSCA', 'VESUVIO']),
doc='Instrument used for run')

self.declareProperty(name='Mode', defaultValue='diffspec',
validator=StringListValidator(['diffspec', 'diffonly']),
doc='Diffraction mode used')

self.declareProperty(IntArrayProperty(name='DetectorRange'),
doc='Range of detectors to use.')

self.declareProperty(name='RebinParam', defaultValue='',
doc='Rebin parameters')

self.declareProperty(WorkspaceGroupProperty('OutputWorkspaceGroup', '',
direction=Direction.Output, optional=PropertyMode.Optional),
doc='Optionally group the result workspaces.')

def validateInputs(self):
"""
Checks for issues with user input.
"""
issues = dict()

input_files = self.getProperty('InputFiles').value
if len(input_files) == 0:
issues['InputFiles'] = 'InputFiles must contain at least one filename'

detector_range = self.getProperty('DetectorRange').value
if len(detector_range) != 2:
issues['DetectorRange'] = 'DetectorRange must be an array of 2 values only'
else:
if detector_range[0] > detector_range[1]:
issues['DetectorRange'] = 'DetectorRange must be in format [lower_index,upper_index]'

return issues

def PyExec(self):
from IndirectCommon import StartTime, EndTime
from IndirectDiffractionReduction import MSGDiffractionReducer

StartTime('MSGDiffractionReduction')

input_files = self.getProperty('InputFiles').value
sum_files = self.getProperty('SumFiles').value
instrument_name = self.getPropertyValue('Instrument')
mode = self.getPropertyValue('Mode')
detector_range = self.getProperty('DetectorRange').value
rebin_string = self.getPropertyValue('RebinParam')
output_ws_group = self.getPropertyValue('OutputWorkspaceGroup')

ipf_filename = instrument_name + '_diffraction_' + mode + '_Parameters.xml'

reducer = MSGDiffractionReducer()
reducer.set_instrument_name(instrument_name)
reducer.set_detector_range(detector_range[0] - 1, detector_range[1] - 1)
reducer.set_parameter_file(ipf_filename)
reducer.set_sum_files(sum_files)

for in_file in input_files:
reducer.append_data_file(in_file)

if rebin_string != '':
reducer.set_rebin_string(rebin_string)

if instrument_name == 'VESUVIO':
reducer.append_load_option('Mode', 'FoilOut')

reducer.reduce()

if output_ws_group != '':
result_ws_list = recuer.get_result_workspaces()
GroupWorkspaces(InputWorkspaces=result_ws_list, OutputWorkspace=output_ws_group)
self.setProperty('OutputWorkspaceGroup', output_ws_group)

EndTime('MSGDiffractionReduction')


AlgorithmFactory.subscribe(MSGDiffractionReduction)
Expand Up @@ -84,50 +84,47 @@ void IndirectDiffractionReduction::demonRun()
*/
void IndirectDiffractionReduction::runGenericReduction(QString instName, QString mode)
{
// MSGDiffractionReduction
QString pfile = instName + "_diffraction_" + mode + "_Parameters.xml";
QString pyInput =
"from IndirectDiffractionReduction import MSGDiffractionReducer\n"
"reducer = MSGDiffractionReducer()\n"
"reducer.set_instrument_name('" + instName + "')\n"
"reducer.set_detector_range("+m_uiForm.set_leSpecMin->text()+"-1, " +m_uiForm.set_leSpecMax->text()+"-1)\n"
"reducer.set_parameter_file('" + pfile + "')\n"
"files = [r'" + m_uiForm.dem_rawFiles->getFilenames().join("',r'") + "']\n"
"for file in files:\n"
" reducer.append_data_file(file)\n";
// Fix Vesuvio to FoilOut for now
if(instName == "VESUVIO")
pyInput += "reducer.append_load_option('Mode','FoilOut')\n";

if ( m_uiForm.dem_ckSumFiles->isChecked() )
{
pyInput += "reducer.set_sum_files(True)\n";
}

pyInput += "formats = []\n";
if ( m_uiForm.ckGSS->isChecked() ) pyInput += "formats.append('gss')\n";
if ( m_uiForm.ckNexus->isChecked() ) pyInput += "formats.append('nxs')\n";
if ( m_uiForm.ckAscii->isChecked() ) pyInput += "formats.append('ascii')\n";

QString rebin = m_uiForm.leRebinStart->text() + "," + m_uiForm.leRebinWidth->text()
+ "," + m_uiForm.leRebinEnd->text();
if ( rebin != ",," )
{
pyInput += "reducer.set_rebin_string('" + rebin +"')\n";
}

pyInput += "reducer.set_save_formats(formats)\n";
pyInput +=
"reducer.reduce()\n";
// Get rebin string
QString rebinStart = m_uiForm.leRebinStart->text();
QString rebinWidth = m_uiForm.leRebinWidth->text();
QString rebinEnd = m_uiForm.leRebinEnd->text();

QString rebin = "";
if(!rebinStart.isEmpty() && !rebinWidth.isEmpty() && !rebinEnd.isEmpty())
rebin = rebinStart + "," + rebinWidth + "," + rebinEnd;

// Get detector range
std::vector<long> detRange;
detRange.push_back(m_uiForm.set_leSpecMin->text().toLong());
detRange.push_back(m_uiForm.set_leSpecMax->text().toLong());

// Get MSGDiffractionReduction algorithm instance
IAlgorithm_sptr msgDiffReduction = AlgorithmManager::Instance().create("MSGDiffractionReduction");
msgDiffReduction->initialize();

// Set algorithm properties
msgDiffReduction->setProperty("Instrument", instName.toStdString());
msgDiffReduction->setProperty("Mode", mode.toStdString());
msgDiffReduction->setProperty("SumFiles", m_uiForm.dem_ckSumFiles->isChecked());
msgDiffReduction->setProperty("InputFiles", m_uiForm.dem_rawFiles->getFilenames().join(",").toStdString());
msgDiffReduction->setProperty("DetectorRange", detRange);
msgDiffReduction->setProperty("RebinParam", rebin.toStdString());

m_batchAlgoRunner->addAlgorithm(msgDiffReduction);

/* if ( m_uiForm.ckGSS->isChecked() ) pyInput += "formats.append('gss')\n"; */
/* if ( m_uiForm.ckNexus->isChecked() ) pyInput += "formats.append('nxs')\n"; */
/* if ( m_uiForm.ckAscii->isChecked() ) pyInput += "formats.append('ascii')\n"; */

if ( m_uiForm.cbPlotType->currentText() == "Spectra" )
{
pyInput += "wslist = reducer.get_result_workspaces()\n"
"from mantidplot import *\n"
"plotSpectrum(wslist, 0)\n";
}
m_batchAlgoRunner->executeBatchAsync();

QString pyOutput = runPythonCode(pyInput).trimmed();
/* if ( m_uiForm.cbPlotType->currentText() == "Spectra" ) */
/* { */
/* pyInput += "wslist = reducer.get_result_workspaces()\n" */
/* "from mantidplot import *\n" */
/* "plotSpectrum(wslist, 0)\n"; */
/* } */
/* QString pyOutput = runPythonCode(pyInput).trimmed(); */
}

/**
Expand Down
Expand Up @@ -153,17 +153,13 @@ def _load_single_file(self, filename, output_ws):
def _load_data(self, filename, output_ws):
if self._parameter_file is not None and "VESUVIO" in self._parameter_file:
loaded_ws = LoadVesuvio(Filename=filename, OutputWorkspace=output_ws, SpectrumList="1-198", **self._extra_load_opts)
loader_name = "LoadVesuvio"
else:
loaded_ws = Load(Filename=filename, OutputWorkspace=output_ws, LoadLogFiles=False, **self._extra_load_opts)
if self._load_logs == True:
loaded_ws = Load(Filename=filename, OutputWorkspace=output_ws, LoadLogFiles=True, **self._extra_load_opts)
logger.notice("Loaded sample logs")
else:
loaded_ws = Load(Filename=filename, OutputWorkspace=output_ws, LoadLogFiles=False, **self._extra_load_opts)
loader_handle = loaded_ws.getHistory().lastAlgorithm()
loader_name = loader_handle.getPropertyValue("LoaderName")
return loader_name

def _sum_regular(self, wsname):
merges = [[], []]
Expand Down

0 comments on commit 7f1ac35

Please sign in to comment.