Unverified Commit a379afc9 authored by Pete Peterson's avatar Pete Peterson Committed by GitHub
Browse files

Merge pull request #23485 from mantidproject/23388-load-gudrun-improvements

LoadGudrunOutput improvements
parents faac501e f4d2f196
from __future__ import (absolute_import, division, print_function)
from mantid.api import FileProperty, WorkspaceProperty, PythonAlgorithm, AlgorithmFactory, FileAction
from mantid.api import (FileProperty, WorkspaceProperty, PythonAlgorithm,
AlgorithmFactory, FileAction, PropertyMode)
from mantid.kernel import Direction
from mantid.simpleapi import LoadAscii, CreateWorkspace
......@@ -19,16 +20,38 @@ class LoadGudrunOutput(PythonAlgorithm):
def PyInit(self):
self.declareProperty(FileProperty(name='InputFile', defaultValue='', action=FileAction.Load,
extensions=['dcs01', 'mdcs01', 'mint01', 'mdor01', 'mgor01']))
self.declareProperty(WorkspaceProperty(name='OutputWorkspace', defaultValue='', direction=Direction.Output))
extensions=[".dcs01",
".mdsc01",
".mint01",
".mdor01",
".mgor01"]),
doc="Gudrun output file to be loaded.")
self.declareProperty(WorkspaceProperty(name='OutputWorkspace', defaultValue='',
direction=Direction.Output,
optional=PropertyMode.Optional),
doc="If No OutputWorkspace is provided, then the workpsace name "
"will be obtained from the meta data in the input file.")
def PyExec(self):
input_file = self.getProperty('InputFile').value
output_ws = self.getPropertyValue('OutputWorkspace')
if not output_ws:
output_ws = self.get_title(input_file)
number_of_columns, data_line_start = self.find_number_of_columns(input_file)
self.load_gudrun_file(input_file, output_ws, number_of_columns, data_line_start)
self.setProperty('OutputWorkspace', output_ws)
def get_title(self, input_file):
"""
Return the title from the file meta data
:param input_file: file to get meta data from
:return: (title)
"""
with open(input_file, 'r') as gudrun_file:
first_line = gudrun_file.readline()
first_line = first_line[2:]
return first_line.replace('.', '-')
def find_number_of_columns(self, input_file):
"""
Evaluate how many columns of data there are in the file
......@@ -50,7 +73,7 @@ class LoadGudrunOutput(PythonAlgorithm):
:param output_workspace: The workspace to be the result of the load
:param number_of_columns: The number of columns in the file being loaded
:param first_data_line: The first line to expect data on
:return: The outputWorkspace of the Load Algoritm
:return: The outputWorkspace of the Load Algorithm
"""
if number_of_columns % 2 == 0:
......
......@@ -8,14 +8,14 @@ import tempfile
class LoadGudrunOutputTest(unittest.TestCase):
def setUp(self):
self.file_name = 'POLARIS00097947-min{}'
self.file_name = 'POLARIS00097947-min{}01'
def test_valid_extensions(self):
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.dcs01'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mdcs01'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mint01'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mdor01'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mgor01'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.dcs'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mdcs'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mint'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mdor'), OutputWorkspace='out_ws'))
self.assertIsNotNone(LoadGudrunOutput(InputFile=self.file_name.format('.mgor'), OutputWorkspace='out_ws'))
def test_invalid_extension(self):
self.assertRaises(ValueError, LoadGudrunOutput, 'file.nxs', 'out_ws')
......@@ -24,31 +24,31 @@ class LoadGudrunOutputTest(unittest.TestCase):
self.assertRaises(ValueError, LoadGudrunOutput, 'file.dcs01', 'out_ws')
def test_load_dcs(self):
actual = LoadGudrunOutput(self.file_name.format('.dcs01'))
actual = LoadGudrunOutput(self.file_name.format('.dcs'))
self.assertIsInstance(actual, Workspace)
self.assertEqual(actual.getNumberBins(), 100)
self.assertEqual(actual.getNumberHistograms(), 5)
def test_load_mdsc(self):
actual = LoadGudrunOutput(self.file_name.format('.mdcs01'))
actual = LoadGudrunOutput(self.file_name.format('.mdcs'))
self.assertIsInstance(actual, Workspace)
self.assertEqual(actual.getNumberBins(), 100)
self.assertEqual(actual.getNumberHistograms(), 1)
def test_load_mint(self):
actual = LoadGudrunOutput(self.file_name.format('.mint01'))
actual = LoadGudrunOutput(self.file_name.format('.mint'))
self.assertIsInstance(actual, Workspace)
self.assertEqual(actual.getNumberBins(), 100)
self.assertEqual(actual.getNumberHistograms(), 1)
def test_load_mdor(self):
actual = LoadGudrunOutput(self.file_name.format('.mdor01'))
actual = LoadGudrunOutput(self.file_name.format('.mdor'))
self.assertIsInstance(actual, Workspace)
self.assertEqual(actual.getNumberBins(), 100)
self.assertEqual(actual.getNumberHistograms(), 1)
def test_load_mgor(self):
actual = LoadGudrunOutput(self.file_name.format('.mgor01'))
actual = LoadGudrunOutput(self.file_name.format('.mgor'))
self.assertIsInstance(actual, Workspace)
self.assertEqual(actual.getNumberBins(), 100)
self.assertEqual(actual.getNumberHistograms(), 1)
......@@ -65,6 +65,14 @@ class LoadGudrunOutputTest(unittest.TestCase):
tmp.close()
self.assertRaises(ValueError, LoadGudrunOutput, tmp.name, 'out_ws')
def test_rename_with_default_output(self):
actual = LoadGudrunOutput(InputFile=self.file_name.format('.dcs'), OutputWorkspace='')
self.assertEqual(actual.name(), 'POLARIS00097947-dcs01')
def test_name_when_given(self):
actual = LoadGudrunOutput(InputFile=self.file_name.format('.dcs'), OutputWorkspace='actual')
self.assertEqual(actual.name(), 'actual')
if __name__ == '__main__':
unittest.main()
......@@ -161,6 +161,9 @@ QString getFilter(const std::vector<std::string> &exts) {
*/
QString formatExtension(const std::string &extension) {
QString formattedExtension = QString::fromStdString(extension);
if (extension.empty()) {
return formattedExtension;
}
if (extension.at(0) == '*' && extension.at(1) == '.') {
return formattedExtension;
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment