diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py new file mode 100644 index 0000000000000000000000000000000000000000..d6c099d943327441c319b8f3e6c4a29e7a3010c4 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py @@ -0,0 +1,87 @@ +#pylint: disable=no-init +from mantid.simpleapi import * +from mantid.kernel import * +from mantid.api import * + + +class AddSampleLogMultiple(PythonAlgorithm): + + def category(self): + return 'DataHandling\\Logs' + + + def summary(self): + return 'Add multiple sample logs to a workspace' + + + def PyInit(self): + self.declareProperty(WorkspaceProperty('Workspace', '', direction=Direction.InOut), + doc='Workspace to add logs to') + + self.declareProperty(StringArrayProperty('LogNames', ''), + doc='Comma separated list of log names') + + self.declareProperty(StringArrayProperty('LogValues', ''), + doc='Comma separated list of log values') + + self.declareProperty('ParseType', True, + doc='Determine the value type by parsing the string') + + + def PyExec(self): + workspace = self.getPropertyValue('Workspace') + log_names = self.getProperty('LogNames').value + log_values = self.getProperty('LogValues').value + parse_type = self.getProperty('ParseType').value + + for idx in range(0, len(log_names)): + # Get the name and value + name = log_names[idx] + value = log_values[idx] + + # Try to get the correct type + value_type = 'String' + if parse_type: + try: + float(value) + value_type = 'Number' + except ValueError: + pass + + # Add the log + alg = AlgorithmManager.create('AddSampleLog') + alg.initialize() + alg.setChild(True) + alg.setLogging(False) + alg.setProperty('Workspace', workspace) + alg.setProperty('LogType', value_type) + alg.setProperty('LogName', name) + alg.setProperty('LogText', value) + alg.execute() + + + def validateInputs(self): + issues = dict() + + log_names = self.getProperty('LogNames').value + log_values = self.getProperty('LogValues').value + + num_names = len(log_names) + num_values = len(log_values) + + # Ensure there is at leats 1 log name + if num_names == 0: + issues['LogNames'] = 'Must have at least one log name' + + # Ensure there is at leats 1 log value + if num_values == 0: + issues['LogValues'] = 'Must have at least one log value' + + if num_names > 0 and num_values > 0 and num_names != num_values: + issues['LogValues'] = 'Number of log values must match number of log names' + + return issues + + +# Register algorithm with Mantid +AlgorithmFactory.subscribe(AddSampleLogMultiple) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py new file mode 100644 index 0000000000000000000000000000000000000000..8dda9002746d6dbda8878e86e9abecc2d45e1793 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py @@ -0,0 +1,122 @@ +import unittest +from mantid.simpleapi import * +from mantid.api import * + + +class AddSampleLogMultipleTest(unittest.TestCase): + + def setUp(self): + """ + Crates a small sample workspace to test with. + """ + CreateSampleWorkspace(OutputWorkspace='__AddSampleLogMultiple_test', + NumBanks=1, + BankPixelWidth=1, + XMax=10, + BinWidth=1) + self._workspace = mtd['__AddSampleLogMultiple_test'] + + + def tearDown(self): + """ + Removes sample workspaces. + """ + DeleteWorkspace(self._workspace) + + + def _validate_sample_logs(self, names, values, types): + """ + Validates sample logs set on workspace. + + @param names List of sample log names + @param values List of sample log values + @param types List of sample log types + """ + logs = self._workspace.getSampleDetails().getLogData() + matched_logs = list() + + for log in logs: + if log.name in names: + matched_logs.append(log.name) + idx = names.index(log.name) + + self.assertEqual(log.value, values[idx]) + self.assertEqual(log.type, types[idx]) + + self.assertEqual(matched_logs, names) + + + def test_strings(self): + """ + Tests adding multiple strings. + """ + names = ['a', 'b', 'c'] + values = ['one', 'two', 'three'] + types = ['string', 'string', 'string'] + + AddSampleLogMultiple(Workspace=self._workspace, + LogNames=names, + LogValues=values) + + self._validate_sample_logs(names, values, types) + + + def test_strings_and_numbers(self): + """ + Tests adding multiple strings and numbers. + """ + names = ['a', 'b', 'c', 'd', 'e', 'f'] + values = ['one', 'two', 'three', 4, 5.5, 6e2] + types = ['string', 'string', 'string', 'number', 'number', 'number'] + + AddSampleLogMultiple(Workspace=self._workspace, + LogNames=names, + LogValues=values) + + self._validate_sample_logs(names, values, types) + + + def test_validation_no_names(self): + """ + Test validation for no log names. + """ + names = [] + values = ['one', 'two', 'three'] + + self.assertRaises(RuntimeError, + AddSampleLogMultiple, + Workspace=self._workspace, + LogNames=names, + LogValues=values) + + + def test_validation_no_values(self): + """ + Test validation for no log values. + """ + names = ['a', 'b', 'c'] + values = [] + + self.assertRaises(RuntimeError, + AddSampleLogMultiple, + Workspace=self._workspace, + LogNames=names, + LogValues=values) + + + def test_validation_differing_counts(self): + """ + Test validation for differing numbers of log names and log values. + """ + names = ['a', 'b', 'c'] + values = ['one', 'two'] + + self.assertRaises(RuntimeError, + AddSampleLogMultiple, + Workspace=self._workspace, + LogNames=names, + LogValues=values) + + +if __name__ == '__main__': + unittest.main() diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt index 33231f902717c594aaddd60a89746838c623429a..c47909fcd6b99c77c21d07c5674d4ff7ae23656d 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt @@ -3,6 +3,7 @@ ## set ( TEST_PY_FILES + AddSampleLogMultipleTest.py CalculateSampleTransmissionTest.py CheckForSampleLogsTest.py ConjoinSpectraTest.py diff --git a/Code/Mantid/docs/source/algorithms/AddSampleLogMultiple-v1.rst b/Code/Mantid/docs/source/algorithms/AddSampleLogMultiple-v1.rst new file mode 100644 index 0000000000000000000000000000000000000000..157034c66ab69d49447b94fbc731221bdd6a5ff3 --- /dev/null +++ b/Code/Mantid/docs/source/algorithms/AddSampleLogMultiple-v1.rst @@ -0,0 +1,49 @@ +.. algorithm:: + +.. summary:: + +.. alias:: + +.. properties:: + +Description +----------- + +This algorithm provides a way of adding multiple sample log enteries to a +workspace at once by making multiple calls to the :ref:`algm-AddSampleLog` +algorithm. + +Typically this is for use in workflow algorithms and scripts. + +Usage +----- + +**Example - Add multiple sample logs** + +.. testcode:: AddSampleLogMultipleExample + + # Create a host workspace + demo_ws = CreateWorkspace(DataX=range(0,3), DataY=(0,2)) + + # Add sample logs + log_names = ['x', 'y', 'z'] + log_values = ['test', 5, 1.6e-7] + AddSampleLogMultiple(Workspace=demo_ws, + LogNames=log_names, + LogValues=log_values) + + # Print the log values + run = demo_ws.getRun() + print run.getLogData('x').value + print run.getLogData('y').value + print run.getLogData('z').value + +Output: + +.. testoutput:: AddSampleLogMultipleExample + + test + 5 + 1.6e-07 + +.. categories::