21using namespace Kernel;
25std::string generateDifferenceMessage(
const std::string &item,
const std::string &wsName,
const std::string &wsValue,
26 const std::string &firstValue) {
27 std::stringstream stringstream;
28 stringstream <<
"Item \"" << item <<
"\" has different values in workspaces! Found: " << wsValue <<
" in workspace "
29 << wsName <<
" but the value in the first workspace was: " << firstValue <<
"." << std::endl;
30 return stringstream.str();
39 "time series. The initial times are taken as the start times "
40 "for the run. Sample logs must be numeric.";
46 "logs to generate a warning if "
47 "different when merging.";
50 "Can either be empty for a comparison of the strings, a "
51 "single value for all warn sample logs, or a comma "
52 "separated list of values (must be the same length as "
56 "when merging. If there is a "
57 "difference the run is skipped.";
60 "Can either be empty for a comparison of the strings, a "
61 "single value for all fail sample logs, or a comma "
62 "separated list of values (must be the same length as "
66 "logs to sum into a single entry. "
67 "Sample logs must be numeric.";
83 : parameterNames(
std::move(parName)), m_logger(logger) {
95 m_logMap.insert(instrumentMap.begin(), instrumentMap.end());
116 std::string paramsTolerances;
143 bool skipIfInPrimaryMap) {
149 auto tolerancesStringVector = tokenizerTolerances.
asVector();
154 auto j = tolerancesVector.begin();
156 for (; i !=
tokenizer.
end() && j != tolerancesVector.end(); ++i, ++j) {
169 throw std::invalid_argument(
"Error when making list of merge items, sample log \"" + item +
170 "\" defined more than once!");
176 if (uncombinableLogs.count(mergeType) != 0) {
177 bool skipLog =
false;
179 uncombinableLogs.erase(mergeType);
180 for (
auto &logType : uncombinableLogs) {
182 throw std::invalid_argument(
"Error when making list of merge items, sample log " + item +
183 " being used for two incompatible merge types!");
193 std::shared_ptr<Property> prop;
195 prop = std::shared_ptr<Property>(ws.
getLog(item)->
clone());
196 }
catch (std::invalid_argument &) {
198 <<
"\", does not exist in workspace! This sample log will be ignored." << std::endl;
209 <<
" could not be converted to a numeric type. "
210 "This sample log will be ignored.\n"
239 const std::vector<std::string> &tolerances) {
240 size_t numberTolerances = tolerances.size();
242 std::vector<double> tolerancesVector(numberNames);
244 if (numberNames == numberTolerances && numberTolerances > 1) {
246 std::transform(tolerances.begin(), tolerances.end(), tolerancesVector.begin(),
247 [](
const std::string &val) { return std::stod(val); });
248 }
catch (std::invalid_argument &) {
249 throw std::invalid_argument(
"Error when creating tolerances vector. "
250 "Please ensure each comma separated value is "
252 }
catch (std::out_of_range &) {
253 throw std::out_of_range(
"Error when creating tolerances vector. Please "
254 "ensure each comma separated value is within "
255 "double precision range.");
257 if (std::any_of(tolerancesVector.cbegin(), tolerancesVector.cend(), [](
const auto value) { return value < 0.; })) {
258 throw std::out_of_range(
"Error when creating tolerances vector. Please "
259 "ensure all tolerance values are positive.");
261 }
else if (tolerances.empty()) {
262 std::fill(tolerancesVector.begin(), tolerancesVector.end(), -1.0);
263 }
else if (numberTolerances == 1) {
266 value = std::stod(tolerances.front());
267 std::fill(tolerancesVector.begin(), tolerancesVector.end(),
value);
268 }
catch (std::invalid_argument &) {
269 throw std::invalid_argument(
"The single tolerance value requested can "
270 "not be converted to a number. Please ensure "
271 "it is a single number, or a comma separated "
273 }
catch (std::out_of_range &) {
274 throw std::out_of_range(
"The single tolerance value requested can not be "
275 "converted to a double. Please ensure tolerance "
276 "is within double precision range.");
279 throw std::out_of_range(
"The single tolerance value requested is "
280 "negative. Please ensure it is positive.");
282 throw std::invalid_argument(
"Invalid length of tolerances, found " +
std::to_string(numberTolerances) +
283 " tolerance values but " +
std::to_string(numberNames) +
" names.");
286 return tolerancesVector;
299 std::shared_ptr<Property> returnProp;
300 const std::string originalUnit = ws.
getLog(item)->
units();
306 }
catch (std::invalid_argument &) {
309 std::string startTime = ws.
run().
startTime().toISO8601String();
311 timeSeriesProp->addValue(startTime,
value);
330 std::shared_ptr<Property> returnProp;
332 const std::string originalUnit = ws.
getLog(item)->
units();
335 if (returnProp->type() !=
"string") {
358 }
catch (std::invalid_argument &) {
373 const std::string &logName = item.first.first;
375 const Property *addeeWSProperty = addeeWS->getLog(logName);
376 const std::string originalUnit = addeeWS->getLog(logName)->
units();
378 double addeeWSNumericValue = 0.;
379 double outWSNumericValue = 0.;
382 addeeWSNumericValue = addeeWS->getLogAsSingleValue(logName);
383 outWSNumericValue = outWS->getLogAsSingleValue(logName);
384 }
catch (std::invalid_argument &) {
385 if (item.second.isNumeric) {
386 throw std::invalid_argument(logName +
" could not be converted to a numeric type");
390 switch (item.first.second) {
392 this->
updateSumProperty(addeeWSNumericValue, outWSNumericValue, *outWS, logName);
404 this->
checkWarnProperty(*addeeWS, addeeWSProperty, item.second, addeeWSNumericValue, outWSNumericValue, logName);
407 this->
checkErrorProperty(*addeeWS, addeeWSProperty, item.second, addeeWSNumericValue, outWSNumericValue, logName);
410 outWS->getLog(logName)->setUnits(originalUnit);
438 const std::string &
name) {
442 timeSeriesProp->merge(addeeTimeSeries);
443 }
catch (std::invalid_argument &) {
444 Types::Core::DateAndTime startTime = addeeWS.
run().
startTime();
446 timeSeriesProp->addValue(startTime,
value);
464 const std::string &
name) {
487 const double outWSNumericValue,
const std::string &
name) {
513 const double outWSNumericValue,
const std::string &
name) {
517 throw std::invalid_argument(
533 const double outWSNumericValue) {
550 return behaviour.
property->value() == addeeWSProperty->
value();
560 std::string propertyToReset = item.first.first;
566 const Property *outWSProperty = outWS->run().getProperty(propertyToReset);
567 item.second.property = std::shared_ptr<Property>(outWSProperty->
clone());
580 const auto &propName = prop->name();
581 addeeWS->mutableRun().removeProperty(propName);
595 auto property = std::unique_ptr<Kernel::Property>(item->clone());
596 addeeWS->mutableRun().addProperty(std::move(property));
608 std::string
const &propertyToReset = item.first.first;
611 auto property = std::unique_ptr<Kernel::Property>(item.second.property->clone());
612 ws->mutableRun().addProperty(std::move(property),
true);
614 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