22using namespace Kernel;
26std::string generateDifferenceMessage(
const std::string &item,
const std::string &wsName,
const std::string &wsValue,
27 const std::string &firstValue) {
28 std::stringstream stringstream;
29 stringstream <<
"Item \"" << item <<
"\" has different values in workspaces! Found: " << wsValue <<
" in workspace "
30 << wsName <<
" but the value in the first workspace was: " << firstValue <<
"." << std::endl;
31 return stringstream.str();
40 "time series. The initial times are taken as the start times "
41 "for the run. Sample logs must be numeric.";
47 "logs to generate a warning if "
48 "different when merging.";
51 "Can either be empty for a comparison of the strings, a "
52 "single value for all warn sample logs, or a comma "
53 "separated list of values (must be the same length as "
57 "when merging. If there is a "
58 "difference the run is skipped.";
61 "Can either be empty for a comparison of the strings, a "
62 "single value for all fail sample logs, or a comma "
63 "separated list of values (must be the same length as "
67 "logs to sum into a single entry. "
68 "Sample logs must be numeric.";
84 : parameterNames(
std::move(parName)), m_logger(logger) {
96 m_logMap.insert(instrumentMap.begin(), instrumentMap.end());
117 std::string paramsTolerances;
144 bool skipIfInPrimaryMap) {
150 auto tolerancesStringVector = tokenizerTolerances.
asVector();
155 auto j = tolerancesVector.begin();
157 for (; i !=
tokenizer.
end() && j != tolerancesVector.end(); ++i, ++j) {
170 throw std::invalid_argument(
"Error when making list of merge items, sample log \"" + item +
171 "\" defined more than once!");
177 if (uncombinableLogs.count(mergeType) != 0) {
178 bool skipLog =
false;
180 uncombinableLogs.erase(mergeType);
181 for (
auto &logType : uncombinableLogs) {
183 throw std::invalid_argument(
"Error when making list of merge items, sample log " + item +
184 " being used for two incompatible merge types!");
194 std::shared_ptr<Property> prop;
196 prop = std::shared_ptr<Property>(ws.
getLog(item)->
clone());
197 }
catch (std::invalid_argument &) {
199 <<
"\", does not exist in workspace! This sample log will be ignored." << std::endl;
210 <<
" could not be converted to a numeric type. "
211 "This sample log will be ignored.\n"
240 const std::vector<std::string> &tolerances) {
241 size_t numberTolerances = tolerances.size();
243 std::vector<double> tolerancesVector(numberNames);
245 if (numberNames == numberTolerances && numberTolerances > 1) {
247 std::transform(tolerances.begin(), tolerances.end(), tolerancesVector.begin(),
248 [](
const std::string &val) { return std::stod(val); });
249 }
catch (std::invalid_argument &) {
250 throw std::invalid_argument(
"Error when creating tolerances vector. "
251 "Please ensure each comma separated value is "
253 }
catch (std::out_of_range &) {
254 throw std::out_of_range(
"Error when creating tolerances vector. Please "
255 "ensure each comma separated value is within "
256 "double precision range.");
258 if (std::any_of(tolerancesVector.cbegin(), tolerancesVector.cend(), [](
const auto value) { return value < 0.; })) {
259 throw std::out_of_range(
"Error when creating tolerances vector. Please "
260 "ensure all tolerance values are positive.");
262 }
else if (tolerances.empty()) {
263 std::fill(tolerancesVector.begin(), tolerancesVector.end(), -1.0);
264 }
else if (numberTolerances == 1) {
267 value = std::stod(tolerances.front());
268 std::fill(tolerancesVector.begin(), tolerancesVector.end(),
value);
269 }
catch (std::invalid_argument &) {
270 throw std::invalid_argument(
"The single tolerance value requested can "
271 "not be converted to a number. Please ensure "
272 "it is a single number, or a comma separated "
274 }
catch (std::out_of_range &) {
275 throw std::out_of_range(
"The single tolerance value requested can not be "
276 "converted to a double. Please ensure tolerance "
277 "is within double precision range.");
280 throw std::out_of_range(
"The single tolerance value requested is "
281 "negative. Please ensure it is positive.");
283 throw std::invalid_argument(
"Invalid length of tolerances, found " +
std::to_string(numberTolerances) +
284 " tolerance values but " +
std::to_string(numberNames) +
" names.");
287 return tolerancesVector;
300 std::shared_ptr<Property> returnProp;
301 const std::string originalUnit = ws.
getLog(item)->
units();
307 }
catch (std::invalid_argument &) {
310 std::string startTime = ws.
run().
startTime().toISO8601String();
312 timeSeriesProp->addValue(startTime,
value);
331 std::shared_ptr<Property> returnProp;
333 const std::string originalUnit = ws.
getLog(item)->
units();
336 if (returnProp->type() !=
"string") {
359 }
catch (std::invalid_argument &) {
374 const std::string &logName = item.first.first;
376 const Property *addeeWSProperty = addeeWS->getLog(logName);
377 const std::string originalUnit = addeeWS->getLog(logName)->
units();
379 double addeeWSNumericValue = 0.;
380 double outWSNumericValue = 0.;
383 addeeWSNumericValue = addeeWS->getLogAsSingleValue(logName);
384 outWSNumericValue = outWS->getLogAsSingleValue(logName);
385 }
catch (std::invalid_argument &) {
386 if (item.second.isNumeric) {
387 throw std::invalid_argument(logName +
" could not be converted to a numeric type");
391 switch (item.first.second) {
393 this->
updateSumProperty(addeeWSNumericValue, outWSNumericValue, *outWS, logName);
405 this->
checkWarnProperty(*addeeWS, addeeWSProperty, item.second, addeeWSNumericValue, outWSNumericValue, logName);
408 this->
checkErrorProperty(*addeeWS, addeeWSProperty, item.second, addeeWSNumericValue, outWSNumericValue, logName);
411 outWS->getLog(logName)->setUnits(originalUnit);
439 const std::string &
name) {
443 timeSeriesProp->merge(addeeTimeSeries);
444 }
catch (std::invalid_argument &) {
445 Types::Core::DateAndTime startTime = addeeWS.
run().
startTime();
447 timeSeriesProp->addValue(startTime,
value);
465 const std::string &
name) {
488 const double outWSNumericValue,
const std::string &
name) {
514 const double outWSNumericValue,
const std::string &
name) {
518 throw std::invalid_argument(
534 const double outWSNumericValue) {
551 return behaviour.
property->value() == addeeWSProperty->
value();
561 std::string propertyToReset = item.first.first;
567 const Property *outWSProperty = outWS->run().getProperty(propertyToReset);
568 item.second.property = std::shared_ptr<Property>(outWSProperty->
clone());
581 const auto &propName = prop->name();
582 addeeWS->mutableRun().removeProperty(propName);
596 auto property = std::unique_ptr<Kernel::Property>(item->clone());
597 addeeWS->mutableRun().addProperty(std::move(property));
609 std::string
const &propertyToReset = item.first.first;
612 auto property = std::unique_ptr<Kernel::Property>(item.second.property->clone());
613 ws->mutableRun().addProperty(std::move(property),
true);
615 ws->mutableRun().getProperty(propertyToReset)->setValue(item.second.property->value());
double value
The value of the point.
Run & mutableRun()
Writable version of the run object.
const Run & run() const
Run details object access.
Geometry::Instrument_const_sptr getInstrument() const
Returns the parameterized instrument.
double getLogAsSingleValue(const std::string &log) const
Access a single value from a log for this experiment.
Kernel::Property * getLog(const std::string &log) const
Access a log for this experiment.
const Types::Core::DateAndTime startTime() const
Return the run start time.
Kernel::Property * getProperty(const std::string &name) const
Returns the named property as a pointer.
void addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
double getLogAsSingleValue(const std::string &name, Kernel::Math::StatisticType statistic=Kernel::Math::Mean) const
Kernel::TimeSeriesProperty< T > * getTimeSeriesProperty(const std::string &name) const
Returns a property as a time series property.
Base MatrixWorkspace Abstract Class.
const std::string & getName() const override
Get the workspace name.
static const std::string LIST_DOC
static const std::string FAIL_DOC
static const std::string WARN_DOC
void removeSampleLogsFromWorkspace(const API::MatrixWorkspace_sptr &addeeWS)
When doing a time series merge we need to remove, then add back the sample log in the addee workspace...
static const std::string FAIL_PROP
Kernel::Logger & m_logger
static const std::string LIST_PROP
static const std::string TIME_SERIES_PROP
std::pair< std::string, MergeLogType > SampleLogsKey
static const std::string TIME_SERIES_DOC
static const std::string SUM_DOC
static const std::string WARN_TOL_PROP
void readdSampleLogToWorkspace(const API::MatrixWorkspace_sptr &addeeWS)
When doing a time series merge we need to remove, then add back the sample log in the addee workspace...
bool setNumericValue(const std::string &item, const API::MatrixWorkspace &ws, double &value)
Tries to set the numeric value of a property.
void updateListProperty(const API::MatrixWorkspace &addeeWS, API::MatrixWorkspace &outWS, const std::string &name)
Perform the update for a list property, appending a new value to the existing string.
std::vector< double > createTolerancesVector(const size_t numberNames, const std::vector< std::string > &tolerances)
Creates a vector of tolerances with the same size as the number of sample logs for the merge type.
std::vector< std::shared_ptr< Kernel::Property > > m_addeeLogMap
SampleLogsBehaviour(const API::MatrixWorkspace_sptr &ws, Kernel::Logger &logger, const SampleLogNames &logEntries={}, ParameterName parName={})
Create and initialise an object that is responsbile for keeping track of the merge types,...
void updateTimeSeriesProperty(const API::MatrixWorkspace &addeeWS, const API::MatrixWorkspace &outWS, const std::string &name)
Perform the update for a time series property, adding a new value to the existing time series propert...
std::map< SampleLogsKey, SampleLogBehaviour > SampleLogsMap
static const std::string FAIL_TOL_DOC
void setUpdatedSampleLogs(const API::MatrixWorkspace_sptr &outWS)
Set the values in the map to be the same as those in the output workspace.
static const std::string WARN_PROP
void resetSampleLogs(const API::MatrixWorkspace_sptr &ws)
Resets the sample logs in the workspace to the values in the map.
std::shared_ptr< Kernel::Property > addPropertyForTimeSeries(const std::string &item, const double value, API::MatrixWorkspace &ws)
Adds a property to the workspace provided for a TimeSeries merge type.
bool stringPropertiesMatch(const SampleLogBehaviour &behaviour, const Kernel::Property *addeeWSProperty)
Check if a sample log value in the addee workspace matches one in the first workspace.
std::shared_ptr< Kernel::Property > addPropertyForList(const std::string &item, const std::string &value, API::MatrixWorkspace &ws)
Adds a property to the workspace provided for a List merge type.
void setSampleMap(SampleLogsMap &map, const MergeLogType &, const std::string ¶ms, API::MatrixWorkspace &ws, const std::string ¶msTolerances="", bool skipIfInPrimaryMap=false)
This method updates the map with the sample log behaviour, and adds the new property to the workspace...
void mergeSampleLogs(const API::MatrixWorkspace_sptr &addeeWS, const API::MatrixWorkspace_sptr &outWS)
Create and update sample logs according to instrument parameters.
struct Mantid::Algorithms::SampleLogsBehaviour::ParameterName parameterNames
bool isWithinTolerance(const SampleLogBehaviour &behaviour, const double addeeWSNumber, const double outWSNumber)
Check if a sample log value in the addee workspace is numeric and within tolerances.
static const std::string FAIL_TOL_PROP
static const std::string SUM_PROP
static const std::string WARN_TOL_DOC
void updateSumProperty(double addeeWSNumber, double outWSNumber, API::MatrixWorkspace &outWS, const std::string &name)
Perform the update for a sum property, adding a new value to the existing one.
void checkWarnProperty(const API::MatrixWorkspace &addeeWS, const Kernel::Property *addeeWSProperty, const SampleLogBehaviour &behaviour, const double addeeWSNumber, const double outWSNumber, const std::string &name)
Performs the check to see if a warning should be generated because logs are different.
void checkErrorProperty(const API::MatrixWorkspace &addeeWS, const Kernel::Property *addeeWSProperty, const SampleLogBehaviour &behaviour, const double addeeWSNumber, const double outWSNumber, const std::string &name)
Performs the check to see if an error should be generated because logs are different.
void createSampleLogsMapsFromInstrumentParams(SampleLogsMap &instrumentMap, API::MatrixWorkspace &ws)
Extracts all of the settings from the instrument parameters, and adds them to a map of sample log beh...
The Logger class is in charge of the publishing messages from the framework through various channels.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
Base class for properties.
virtual const std::string & units() const
Returns the units of the property, if any, as a string.
virtual void setUnits(const std::string &unit)
Sets the units of the property, as a string.
virtual Property * clone() const =0
'Virtual copy constructor'
virtual std::string value() const =0
Returns the value of the property as a string.
Iterator begin()
Iterator referring to first element in the container.
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
Iterator end()
Iterator referring to the past-the-end element in the container.
const TokenVec & asVector()
Returns a vector of tokenized strings.
A specialised Property class for holding a series of time-value pairs.
Mantid::Kernel::StringTokenizer tokenizer
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
MANTID_KERNEL_DLL bool withinAbsoluteDifference(T const x, T const y, S const tolerance)
Test whether x, y are within absolute tolerance tol.
std::string to_string(const wide_integer< Bits, Signed > &n)
std::string TIME_SERIES_MERGE
std::string WARN_MERGE_TOLERANCES
std::string FAIL_MERGE_TOLERANCES
std::shared_ptr< Kernel::Property > property
std::string sampleLogsWarnTolerances
std::string sampleLogsWarn
std::string sampleLogsFail
std::string sampleLogsSum
std::string sampleLogsList
std::string sampleLogsTimeSeries
std::string sampleLogsFailTolerances