Skip to content

Commit

Permalink
Updated IndirectTransmissionMonitor to use workspaces
Browse files Browse the repository at this point in the history
Refs #10057
  • Loading branch information
DanNixon committed Aug 8, 2014
1 parent f757c60 commit e3fb9ee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
Expand Up @@ -7,7 +7,7 @@
import os.path


class IndirectTransmissionReduction(PythonAlgorithm):
class IndirectTransmissionMonitor(PythonAlgorithm):

def category(self):
return "Workflow\\Inelastic;PythonAlgorithms;Inelastic"
Expand All @@ -16,30 +16,33 @@ def summary(self):
return "Calculates the sample transmission using the raw data files of the sample and its background or container."

def PyInit(self):
self.declareProperty(FileProperty('SampleFile', '', FileAction.Load, extensions=['raw']),
doc='Raw sample run file')
self.declareProperty(FileProperty('CanFile', '', FileAction.Load, extensions=['raw']),
doc='Raw background or can run file')
self.declareProperty(WorkspaceProperty('SampleWorkspace', '', direction=Direction.Input),
doc='Sample workspace')
self.declareProperty(WorkspaceProperty('CanWorkspace', '', direction=Direction.Input),
doc='Background/can workspace')

self.declareProperty(WorkspaceProperty('OutputWorkspace', '', direction=Direction.Output, optional=PropertyMode.Optional),
doc='Output workspace group')

self.declareProperty(name='Verbose', defaultValue=False, doc='Output more verbose message to log')
self.declareProperty(name='Plot', defaultValue=False, doc='Plot result workspace')
self.declareProperty(name='Save', defaultValue=False, doc='Save result workspace to nexus file in the default save directory')

def PyExec(self):
from IndirectCommon import StartTime, EndTime
StartTime('IndirectTransmissionReduction')
StartTime('IndirectTransmissionMonitor')

sample_filepath = self.getProperty("SampleFile").value
can_filepath = self.getProperty("CanFile").value
sample_ws_in = self.getPropertyValue("SampleWorkspace")
can_ws_in = self.getPropertyValue("CanWorkspace")
out_ws = self.getPropertyValue('OutputWorkspace')
verbose = self.getProperty("Verbose").value
plot = self.getProperty("Plot").value
save = self.getProperty("Save").value

sample_filename = os.path.basename(sample_filepath)
ws_basename, _ = os.path.splitext(sample_filename)
ws_basename = str(sample_ws_in)

self.TransMon(ws_basename, 'Sam', sample_filepath, verbose)
self.TransMon(ws_basename, 'Can', can_filepath, verbose)
self.TransMon(ws_basename, 'Sam', sample_ws_in, verbose)
self.TransMon(ws_basename, 'Can', can_ws_in, verbose)

# Divide sample and can workspaces
sam_ws = ws_basename + '_Sam'
Expand All @@ -49,27 +52,31 @@ def PyExec(self):
trans = numpy.average(mtd[tr_ws].readY(0))

# Group workspaces
trans_ws = ws_basename + '_Transmission'
if out_ws == '':
out_ws = ws_basename + '_Transmission'

group = sam_ws + ',' + can_ws + ',' + tr_ws
GroupWorkspaces(InputWorkspaces=group, OutputWorkspace=trans_ws)
GroupWorkspaces(InputWorkspaces=group, OutputWorkspace=out_ws)

self.setProperty('OutputWorkspace', out_ws)

if verbose:
logger.notice('Transmission : ' + str(trans))

# Save the tranmissin workspace group to a nexus file
if save:
workdir = config['defaultsave.directory']
path = os.path.join(workdir, trans_ws + '.nxs')
SaveNexusProcessed(InputWorkspace=trans_ws, Filename=path)
path = os.path.join(workdir, out_ws + '.nxs')
SaveNexusProcessed(InputWorkspace=out_ws, Filename=path)
if verbose:
logger.notice('Output file created : ' + path)

# Plot spectra from transmission workspace
if plot:
mtd_plot = import_mantidplot()
mtd_plot.plotSpectrum(trans_ws, 0)
mtd_plot.plotSpectrum(out_ws, 0)

EndTime('IndirectTransmissionReduction')
EndTime('IndirectTransmissionMonitor')

def UnwrapMon(self, inWS):
# Unwrap monitor - inWS contains M1,M2,S1 - outWS contains unwrapped Mon
Expand All @@ -85,13 +92,10 @@ def UnwrapMon(self, inWS):

return out_ws

def TransMon(self, ws_basename, file_type, filename, verbose):
if verbose:
logger.notice('Raw file : ' + file_type)

LoadRaw(Filename=filename, OutputWorkspace='__m1', SpectrumMin=1, SpectrumMax=1)
LoadRaw(Filename=filename, OutputWorkspace='__m2', SpectrumMin=2, SpectrumMax=2)
LoadRaw(Filename=filename, OutputWorkspace='__det', SpectrumMin=3, SpectrumMax=3)
def TransMon(self, ws_basename, file_type, input_ws, verbose):
CropWorkspace(InputWorkspace=input_ws, OutputWorkspace='__m1', StartWorkspaceIndex=0, EndWorkspaceIndex=0)
CropWorkspace(InputWorkspace=input_ws, OutputWorkspace='__m2', StartWorkspaceIndex=1, EndWorkspaceIndex=1)
CropWorkspace(InputWorkspace=input_ws, OutputWorkspace='__det', StartWorkspaceIndex=2, EndWorkspaceIndex=2)

# Check for single or multiple time regimes
mon_tcb_start = mtd['__m1'].readX(0)[0]
Expand All @@ -106,7 +110,6 @@ def TransMon(self, ws_basename, file_type, filename, verbose):
else:
ConvertUnits(InputWorkspace='__m1', OutputWorkspace='__Mon1', Target="Wavelength")


ConvertUnits(InputWorkspace='__m2', OutputWorkspace='__Mon2', Target="Wavelength")

DeleteWorkspace('__m2') # delete monWS
Expand All @@ -129,4 +132,4 @@ def TransMon(self, ws_basename, file_type, filename, verbose):
DeleteWorkspace('__Mon2') # delete monWS

# Register algorithm with Mantid
AlgorithmFactory.subscribe(IndirectTransmissionReduction)
AlgorithmFactory.subscribe(IndirectTransmissionMonitor)
Expand Up @@ -14,7 +14,7 @@ set ( TEST_PY_FILES
FindReflectometryLinesTest.py
GetEiT0atSNSTest.py
IndirectILLReductionTest.py
IndirectTransmissionReductionTest.py
IndirectTransmissionMonitorTest.py
LoadFullprofFileTest.py
LoadLiveDataTest.py
LoadLogPropertyTableTest.py
Expand Down
Expand Up @@ -2,7 +2,7 @@
import mantid
from mantid.simpleapi import *

class IndirectTransmissionReductionTest(unittest.TestCase):
class IndirectTransmissionMonitorTest(unittest.TestCase):

def setUp(self):
self.kwargs = {}
Expand All @@ -23,7 +23,7 @@ def tearDown(self):
pass

def test_basic(self):
IndirectTransmissionReduction(**self.kwargs)
IndirectTransmissionMonitor(**self.kwargs)

trans_workspace = mtd[self.run_number + '_Transmission']

Expand All @@ -33,20 +33,20 @@ def test_basic(self):
def test_nexus_save(self):
self.kwargs['Save'] = True

IndirectTransmissionReduction(**self.kwargs)
IndirectTransmissionMonitor(**self.kwargs)

path = os.path.join(config['defaultsave.directory'], self.run_number + '_Transmission.nxs')
self.assertTrue(os.path.isfile(path), msg='Transmission workspace should be saved to default save directory')

def test_empty_filenames(self):
self.kwargs['CanFile'] = ''

self.assertRaises(ValueError, IndirectTransmissionReduction, **self.kwargs)
self.assertRaises(ValueError, IndirectTransmissionMonitor, **self.kwargs)

def test_nonexistent_file(self):
self.kwargs['CanFile'] = 'NoFile.raw'

self.assertRaises(ValueError, IndirectTransmissionReduction, **self.kwargs)
self.assertRaises(ValueError, IndirectTransmissionMonitor, **self.kwargs)

if __name__ == '__main__':
unittest.main()
Expand Up @@ -34,10 +34,13 @@ namespace CustomInterfaces
QString sampleNo = m_uiForm.transInputFile->getFirstFilename();
QString canNo = m_uiForm.transCanFile->getFirstFilename();

IAlgorithm_sptr transAlg = AlgorithmManager::Instance().create("IndirectTransmissionReduction", -1);
IAlgorithm_sptr transAlg = AlgorithmManager::Instance().create("IndirectTransmissionMonitor", -1);
transAlg->initialize();
transAlg->setProperty("SampleFile", sampleNo.toStdString());
transAlg->setProperty("CanFile", canNo.toStdString());

//TODO
transAlg->setProperty("SampleWorkspace", sampleNo.toStdString());
transAlg->setProperty("CanWorkspace", canNo.toStdString());

transAlg->setProperty("Verbose", m_uiForm.trans_ckVerbose->isChecked());
transAlg->setProperty("Plot", m_uiForm.trans_ckPlot->isChecked());
transAlg->setProperty("Save", m_uiForm.trans_ckSave->isChecked());
Expand Down

0 comments on commit e3fb9ee

Please sign in to comment.