Skip to content
Snippets Groups Projects
Commit fb3d1630 authored by Savici, Andrei T.'s avatar Savici, Andrei T.
Browse files

Add units to AddSampleLog and AddSampleLogMultiple. Refs #13086

parent 715f81b3
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ void AddSampleLog::init() {
"The name that will identify the log entry");
declareProperty("LogText", "", "The content of the log");
declareProperty("LogUnit", "", "The units of the log");
std::vector<std::string> propOptions;
propOptions.push_back("String");
......@@ -48,6 +49,7 @@ void AddSampleLog::exec() {
// get the data that the user wants to add
std::string propName = getProperty("LogName");
std::string propValue = getProperty("LogText");
std::string propUnit = getProperty("LogUnit");
std::string propType = getPropertyValue("LogType");
// Remove any existing log
......@@ -57,6 +59,7 @@ void AddSampleLog::exec() {
if (propType == "String") {
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue));
theRun.getProperty(propName)->setUnits(propUnit);
return;
}
......@@ -94,6 +97,7 @@ void AddSampleLog::exec() {
theRun.addLogData(tsp);
}
}
theRun.getProperty(propName)->setUnits(propUnit);
}
} // namespace Algorithms
......
......@@ -71,9 +71,18 @@ public:
ExecuteAlgorithm(ws, "Another Name", "Number Series", "123456789", 123456789);
}
void test_Units()
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10);
ws->mutableRun().setStartAndEndTime(DateAndTime("2013-12-18T13:40:00"),DateAndTime("2013-12-18T13:42:00"));
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234,false,"myUnit");
ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963,false,"differentUnit");
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0,false,"stringUnit");
}
template<typename T>
void ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName, std::string LogType, std::string LogText,
T expectedValue, bool fails=false)
T expectedValue, bool fails=false, std::string LogUnit="")
{
//add the workspace to the ADS
AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary", testWS);
......@@ -86,6 +95,7 @@ public:
alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary");
alg.setPropertyValue("LogName", LogName);
alg.setPropertyValue("LogText", LogText);
alg.setPropertyValue("LogUnit", LogUnit);
alg.setPropertyValue("LogType", LogType);
TS_ASSERT_THROWS_NOTHING(alg.execute())
if (fails)
......@@ -123,7 +133,6 @@ public:
TS_ASSERT_EQUALS(testProp->firstTime(),DateAndTime("2013-12-18T13:40:00"));
TS_ASSERT_DELTA(testProp->firstValue(), expectedValue, 1e-5);
}
//cleanup
AnalysisDataService::Instance().remove(output->getName());
......
......@@ -24,6 +24,9 @@ class AddSampleLogMultiple(PythonAlgorithm):
self.declareProperty(StringArrayProperty('LogValues', ''),
doc='Comma separated list of log values')
self.declareProperty(StringArrayProperty('LogUnits', ''),
doc='Comma separated list of log units')
self.declareProperty('ParseType', True,
doc='Determine the value type by parsing the string')
......@@ -32,12 +35,17 @@ class AddSampleLogMultiple(PythonAlgorithm):
workspace = self.getPropertyValue('Workspace')
log_names = self.getProperty('LogNames').value
log_values = self.getProperty('LogValues').value
log_units = self.getProperty('LogUnits').value
parse_type = self.getProperty('ParseType').value
if len(log_units) == 0:
log_units = [""]*len(log_names)
for idx in range(0, len(log_names)):
# Get the name and value
# Get the name, value, and unit
name = log_names[idx]
value = log_values[idx]
unit = log_units[idx]
# Try to get the correct type
value_type = 'String'
......@@ -57,6 +65,7 @@ class AddSampleLogMultiple(PythonAlgorithm):
alg.setProperty('LogType', value_type)
alg.setProperty('LogName', name)
alg.setProperty('LogText', value)
alg.setProperty('LogUnit', unit)
alg.execute()
......@@ -65,9 +74,11 @@ class AddSampleLogMultiple(PythonAlgorithm):
log_names = self.getProperty('LogNames').value
log_values = self.getProperty('LogValues').value
log_units = self.getProperty('LogUnits').value
num_names = len(log_names)
num_values = len(log_values)
num_units = len(log_units)
# Ensure there is at leats 1 log name
if num_names == 0:
......@@ -80,6 +91,9 @@ class AddSampleLogMultiple(PythonAlgorithm):
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'
if num_names > 0 and num_units != 0 and num_units != num_names:
issues['LogUnits'] = 'Number of log units must be 0 or match the number of log names'
return issues
......
......@@ -24,16 +24,19 @@ class AddSampleLogMultipleTest(unittest.TestCase):
DeleteWorkspace(self._workspace)
def _validate_sample_logs(self, names, values, types):
def _validate_sample_logs(self, names, values, types, units=None):
"""
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
@param units List of sample unit names
"""
logs = self._workspace.getSampleDetails().getLogData()
logs = self._workspace.getRun().getProperties()
matched_logs = list()
if units==None:
units=['']*len(names)
for log in logs:
if log.name in names:
......@@ -42,6 +45,7 @@ class AddSampleLogMultipleTest(unittest.TestCase):
self.assertEqual(log.value, values[idx])
self.assertEqual(log.type, types[idx])
self.assertEqual(log.units, units[idx])
self.assertEqual(matched_logs, names)
......@@ -75,6 +79,35 @@ class AddSampleLogMultipleTest(unittest.TestCase):
self._validate_sample_logs(names, values, types)
def test_units(self):
"""
Test validation for wrong number of units
"""
names = ['a', 'b', 'c']
values = ['one', 'two', 'three']
units = ['unit_a', 'unit_b', 'unit_c']
types = ['string', 'string', 'string']
AddSampleLogMultiple(Workspace=self._workspace,
LogNames=names,
LogValues=values,
LogUnits=units)
self._validate_sample_logs(names, values, types,units)
def test_validation_wrong_units(self):
"""
Test validation for wrong number of units
"""
names = ['a', 'b', 'c']
values = ['one', 'two', 'three']
units = ['unit_a', 'unit_b']
self.assertRaises(RuntimeError,
AddSampleLogMultiple,
Workspace=self._workspace,
LogNames=names,
LogValues=values,
LogUnits=units)
def test_validation_no_names(self):
"""
......
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