Skip to content
Snippets Groups Projects
Commit f11d3047 authored by Michael Wedel's avatar Michael Wedel
Browse files

Merge pull request #440 from mantidproject/11414_addsamplelogmultiple_algorithm

Code and tests look very clear, example works well, tried also with different values. There's a small typo in the documentation, it says "multiple sample log enteries". I would suggest fixing this through github as discussed in the developer meeting to avoid running the whole build process again.
parents 200b6b8e f2a8ce3b
No related branches found
No related tags found
No related merge requests found
#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)
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()
......@@ -3,6 +3,7 @@
##
set ( TEST_PY_FILES
AddSampleLogMultipleTest.py
CalculateSampleTransmissionTest.py
CheckForSampleLogsTest.py
ConjoinSpectraTest.py
......
.. 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::
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment