BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
LogMetric Class Reference
Inheritance diagram for LogMetric:
Collaboration diagram for LogMetric:

Public Member Functions

 LogMetric ()
 
LogMetricclone () const override
 
double computeFromArrays (std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const override
 
double computeFromArrays (std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > weight_factors) const override
 
virtual double compute (const SimDataPair &data_pair, bool use_weights) const
 
void setNorm (std::function< double(double)> norm)
 
auto norm () const
 
virtual void transferToCPP ()
 

Private Attributes

std::function< double(double)> m_norm
 

Detailed Description

Implementation of the standard $ \chi^2 $ metric with intensity $I$ and experimental data $D$ being replaced by $ \log_{10} I $ and $\log_{10} D$ accordingly.

With default L2 norm corresponds to the formula

\[\chi^2 = \sum \frac{(\log_{10} I - log_{10} D)^2 D^2 \ln^2{10}}{\delta_D^2}\]

Definition at line 133 of file ObjectiveMetric.h.

Constructor & Destructor Documentation

◆ LogMetric()

LogMetric::LogMetric ( )

Definition at line 148 of file ObjectiveMetric.cpp.

ObjectiveMetric(std::function< double(double)> norm)
const std::function< double(double)> l2Norm()
Returns L2 normalization function.

Member Function Documentation

◆ clone()

LogMetric * LogMetric::clone ( ) const
overridevirtual

Implements ObjectiveMetric.

Definition at line 150 of file ObjectiveMetric.cpp.

151 {
152  return copyMetric(*this);
153 }

References anonymous_namespace{ObjectiveMetric.cpp}::copyMetric().

Here is the call graph for this function:

◆ computeFromArrays() [1/2]

double LogMetric::computeFromArrays ( std::vector< double >  sim_data,
std::vector< double >  exp_data,
std::vector< double >  uncertainties,
std::vector< double >  weight_factors 
) const
overridevirtual

Computes metric value from data arrays.

Negative values in exp_data are ignored as well as non-positive weight_factors and uncertainties. All arrays involved in the computation must be of the same size.

Parameters
sim_dataarray with simulated intensities.
exp_dataarray with intensity values obtained from an experiment.
uncertaintiesarray with experimental data uncertainties.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Implements ObjectiveMetric.

Definition at line 155 of file ObjectiveMetric.cpp.

158 {
159  checkIntegrity(sim_data, exp_data, uncertainties, weight_factors);
160 
161  double result = 0.0;
162  auto norm_fun = norm();
163  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i) {
164  if (weight_factors[i] <= 0.0 || exp_data[i] < 0.0 || uncertainties[i] <= 0.0)
165  continue;
166  const double sim_val = std::max(double_min, sim_data[i]);
167  const double exp_val = std::max(double_min, exp_data[i]);
168  double value = std::log10(sim_val) - std::log10(exp_val);
169  value *= exp_val * ln10 / uncertainties[i];
170  result += norm_fun(value) * weight_factors[i];
171  }
172 
173  return std::isfinite(result) ? result : double_max;
174 }
auto norm() const
Returns a copy of the normalization function used.
void checkIntegrity(const std::vector< double > &sim_data, const std::vector< double > &exp_data, const std::vector< double > &uncertainties, const std::vector< double > &weight_factors)

References anonymous_namespace{ObjectiveMetric.cpp}::checkIntegrity(), anonymous_namespace{ObjectiveMetric.cpp}::double_max, anonymous_namespace{ObjectiveMetric.cpp}::double_min, anonymous_namespace{ObjectiveMetric.cpp}::ln10, and ObjectiveMetric::norm().

Here is the call graph for this function:

◆ computeFromArrays() [2/2]

double LogMetric::computeFromArrays ( std::vector< double >  sim_data,
std::vector< double >  exp_data,
std::vector< double >  weight_factors 
) const
overridevirtual

Computes metric value from data arrays.

Negative values in exp_data are ignored as well as non-positive weight_factors. All arrays involved in the computation must be of the same size.

Parameters
sim_dataarray with simulated intensities.
exp_dataarray with intensity values obtained from an experiment.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Implements ObjectiveMetric.

Definition at line 176 of file ObjectiveMetric.cpp.

178 {
179  checkIntegrity(sim_data, exp_data, weight_factors);
180 
181  double result = 0.0;
182  auto norm_fun = norm();
183  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i) {
184  if (weight_factors[i] <= 0.0 || exp_data[i] < 0.0)
185  continue;
186  const double sim_val = std::max(double_min, sim_data[i]);
187  const double exp_val = std::max(double_min, exp_data[i]);
188  result += norm_fun(std::log10(sim_val) - std::log10(exp_val)) * weight_factors[i];
189  }
190 
191  return std::isfinite(result) ? result : double_max;
192 }

References anonymous_namespace{ObjectiveMetric.cpp}::checkIntegrity(), anonymous_namespace{ObjectiveMetric.cpp}::double_max, anonymous_namespace{ObjectiveMetric.cpp}::double_min, and ObjectiveMetric::norm().

Here is the call graph for this function:

◆ compute()

double ObjectiveMetric::compute ( const SimDataPair data_pair,
bool  use_weights 
) const
virtualinherited

Computes metric value from SimDataPair object.

Calls computeFromArrays internally.

Parameters
data_pairSimDataPair object. Can optionally contain data uncertainties
use_weightsboolean, defines if data uncertainties should be taken into account

Reimplemented in RQ4Metric.

Definition at line 61 of file ObjectiveMetric.cpp.

62 {
63  if (use_weights && !data_pair.containsUncertainties())
64  throw std::runtime_error("Error in ObjectiveMetric::compute: the metric is weighted, but "
65  "the simulation-data pair does not contain uncertainties");
66 
67  if (use_weights)
68  return computeFromArrays(data_pair.simulation_array(), data_pair.experimental_array(),
69  data_pair.uncertainties_array(), data_pair.user_weights_array());
70  else
71  return computeFromArrays(data_pair.simulation_array(), data_pair.experimental_array(),
72  data_pair.user_weights_array());
73 }
virtual double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const =0
Computes metric value from data arrays.
std::vector< double > experimental_array() const
Returns the flattened experimental data cut to the ROI area.
std::vector< double > user_weights_array() const
Returns a flat array of user weights cut to the ROI area.
std::vector< double > uncertainties_array() const
Returns the flattened experimental uncertainties cut to the ROI area.
std::vector< double > simulation_array() const
Returns the flattened simulated intensities cut to the ROI area.
bool containsUncertainties() const
Definition: SimDataPair.cpp:81

References ObjectiveMetric::computeFromArrays(), SimDataPair::containsUncertainties(), SimDataPair::experimental_array(), SimDataPair::simulation_array(), SimDataPair::uncertainties_array(), and SimDataPair::user_weights_array().

Referenced by RQ4Metric::compute().

Here is the call graph for this function:

◆ setNorm()

void ObjectiveMetric::setNorm ( std::function< double(double)>  norm)
inherited

Definition at line 75 of file ObjectiveMetric.cpp.

76 {
77  m_norm = std::move(norm);
78 }
std::function< double(double)> m_norm

References ObjectiveMetric::m_norm, and ObjectiveMetric::norm().

Here is the call graph for this function:

◆ norm()

auto ObjectiveMetric::norm ( ) const
inlineinherited

Returns a copy of the normalization function used.

Definition at line 63 of file ObjectiveMetric.h.

63 { return m_norm; }

References ObjectiveMetric::m_norm.

Referenced by Chi2Metric::computeFromArrays(), computeFromArrays(), PoissonLikeMetric::computeFromArrays(), RelativeDifferenceMetric::computeFromArrays(), and ObjectiveMetric::setNorm().

◆ transferToCPP()

virtual void ICloneable::transferToCPP ( )
inlinevirtualinherited

Used for Python overriding of clone (see swig/tweaks.py)

Definition at line 34 of file ICloneable.h.

Member Data Documentation

◆ m_norm

std::function<double(double)> ObjectiveMetric::m_norm
privateinherited

Definition at line 66 of file ObjectiveMetric.h.

Referenced by ObjectiveMetric::norm(), and ObjectiveMetric::setNorm().


The documentation for this class was generated from the following files: