BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ObjectiveMetricUtils Namespace Reference

Description

Utility functions related to class ObjectiveMetric.

Functions

std::string availableMetricOptions ()
 Prints available metric options. More...
 
std::unique_ptr< ObjectiveMetriccreateMetric (const std::string &metric)
 Creates the specified metric with the default norm. More...
 
std::unique_ptr< ObjectiveMetriccreateMetric (std::string metric, std::string norm)
 Creates the metric with the specified norm. More...
 
std::string defaultMetricName ()
 Returns default metric name. More...
 
std::string defaultNormName ()
 Returns default norm name. More...
 
std::function< double(double)> l1Norm ()
 Returns L1 normalization function. More...
 
std::function< double(double)> l2Norm ()
 Returns L2 normalization function. More...
 
std::vector< std::string > metricNames ()
 Returns the names of the objective metrics used. More...
 
std::vector< std::string > normNames ()
 Returns the names of the norms used by ObjectiveMetric. More...
 

Function Documentation

◆ availableMetricOptions()

std::string ObjectiveMetricUtils::availableMetricOptions ( )

Prints available metric options.

Definition at line 86 of file ObjectiveMetricUtils.cpp.

87 {
88  std::stringstream ss;
89  ss << "Available metrics:\n";
90  for (auto& item : metricNames())
91  ss << "\t" << item << "\n";
92  ss << "default metric: " << defaultMetricName() << "\n";
93  ss << "Available norms:\n";
94  for (auto& item : normNames())
95  ss << "\t" << item << "\n";
96  ss << "default norm: " << defaultNormName() << "\n";
97  return ss.str();
98 }
std::string defaultMetricName()
Returns default metric name.
std::string defaultNormName()
Returns default norm name.
std::vector< std::string > metricNames()
Returns the names of the objective metrics used.
std::vector< std::string > normNames()
Returns the names of the norms used by ObjectiveMetric.

References defaultMetricName(), defaultNormName(), metricNames(), and normNames().

Referenced by FitObjective::availableMetricOptions(), and createMetric().

Here is the call graph for this function:

◆ createMetric() [1/2]

std::unique_ptr< ObjectiveMetric > ObjectiveMetricUtils::createMetric ( const std::string &  metric)

Creates the specified metric with the default norm.

Definition at line 61 of file ObjectiveMetricUtils.cpp.

62 {
63  return createMetric(metric, defaultNormName());
64 }
std::unique_ptr< ObjectiveMetric > createMetric(const std::string &metric)
Creates the specified metric with the default norm.

References defaultNormName().

Referenced by FitObjective::setObjectiveMetric().

Here is the call graph for this function:

◆ createMetric() [2/2]

std::unique_ptr< ObjectiveMetric > ObjectiveMetricUtils::createMetric ( std::string  metric,
std::string  norm 
)

Creates the metric with the specified norm.

Definition at line 66 of file ObjectiveMetricUtils.cpp.

68 {
69  std::transform(metric.begin(), metric.end(), metric.begin(), ::tolower);
70  std::transform(norm.begin(), norm.end(), norm.begin(), ::tolower);
71  const auto metric_iter = metric_factory.find(metric);
72  const auto norm_iter = norm_factory.find(norm);
73  if (metric_iter == metric_factory.end() || norm_iter == norm_factory.end()) {
74  std::stringstream ss;
75  ss << "Error in ObjectiveMetricUtils::createMetric: either metric (" << metric
76  << ") or norm (" << norm << ") name is unknown.\n";
77  ss << availableMetricOptions();
78  throw std::runtime_error(ss.str());
79  }
80 
81  auto result = metric_iter->second();
82  result->setNorm(norm_iter->second);
83  return result;
84 }
std::string availableMetricOptions()
Prints available metric options.

References availableMetricOptions().

Here is the call graph for this function:

◆ defaultMetricName()

std::string ObjectiveMetricUtils::defaultMetricName ( )

Returns default metric name.

Definition at line 115 of file ObjectiveMetricUtils.cpp.

116 {
117  return default_metric_name;
118 }

Referenced by availableMetricOptions().

◆ defaultNormName()

std::string ObjectiveMetricUtils::defaultNormName ( )

Returns default norm name.

Definition at line 110 of file ObjectiveMetricUtils.cpp.

111 {
112  return default_norm_name;
113 }

Referenced by availableMetricOptions(), createMetric(), and FitObjective::setObjectiveMetric().

◆ l1Norm()

std::function< double(double)> ObjectiveMetricUtils::l1Norm ( )

Returns L1 normalization function.

Definition at line 51 of file ObjectiveMetricUtils.cpp.

52 {
53  return l1_norm;
54 }

◆ l2Norm()

std::function< double(double)> ObjectiveMetricUtils::l2Norm ( )

Returns L2 normalization function.

Definition at line 56 of file ObjectiveMetricUtils.cpp.

57 {
58  return l2_norm;
59 }

◆ metricNames()

std::vector< std::string > ObjectiveMetricUtils::metricNames ( )

Returns the names of the objective metrics used.

Definition at line 105 of file ObjectiveMetricUtils.cpp.

106 {
107  return keys(metric_factory);
108 }

Referenced by availableMetricOptions().

◆ normNames()

std::vector< std::string > ObjectiveMetricUtils::normNames ( )

Returns the names of the norms used by ObjectiveMetric.

Definition at line 100 of file ObjectiveMetricUtils.cpp.

101 {
102  return keys(norm_factory);
103 }

Referenced by availableMetricOptions().