LCOV - code coverage report
Current view: top level - src/utils - Standardizer.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 50 63 79.4 %
Date: 2025-07-25 05:00:46 Functions: 9 11 81.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "Standardizer.h"
      11             : 
      12             : namespace StochasticTools
      13             : {
      14             : 
      15             : void
      16         176 : Standardizer::set(const Real & n)
      17             : {
      18         176 :   _mean.clear();
      19         176 :   _stdev.clear();
      20         584 :   for (unsigned int ii = 0; ii < n; ++ii)
      21             :   {
      22         408 :     _mean.push_back(0);
      23         408 :     _stdev.push_back(1);
      24             :   }
      25         176 : }
      26             : 
      27             : void
      28           0 : Standardizer::set(const Real & mean, const Real & stdev)
      29             : {
      30           0 :   _mean.clear();
      31           0 :   _stdev.clear();
      32           0 :   _mean.push_back(mean);
      33           0 :   _stdev.push_back(stdev);
      34           0 : }
      35             : 
      36             : void
      37           0 : Standardizer::set(const Real & mean, const Real & stdev, const Real & n)
      38             : {
      39           0 :   _mean.clear();
      40           0 :   _stdev.clear();
      41           0 :   for (unsigned int ii = 0; ii < n; ++ii)
      42             :   {
      43           0 :     _mean.push_back(mean);
      44           0 :     _stdev.push_back(stdev);
      45             :   }
      46           0 : }
      47             : 
      48             : void
      49         112 : Standardizer::set(const std::vector<Real> & mean, const std::vector<Real> & stdev)
      50             : {
      51             :   mooseAssert(mean.size() == stdev.size(),
      52             :               "Provided mean and standard deviation vectors are of differing size.");
      53         112 :   _mean = mean;
      54         112 :   _stdev = stdev;
      55         112 : }
      56             : 
      57             : void
      58        1244 : Standardizer::computeSet(const RealEigenMatrix & input)
      59             : {
      60        1244 :   _mean.clear();
      61        1244 :   _stdev.clear();
      62        1244 :   unsigned int num_samples = input.rows();
      63             :   unsigned int n = input.cols();
      64             :   // comptue mean
      65        1244 :   RealEigenVector mean = input.colwise().mean();
      66             :   // Compute standard deviation
      67             :   RealEigenVector stdev =
      68             :       ((input.rowwise() - mean.transpose()).colwise().squaredNorm() / num_samples)
      69             :           .transpose()
      70             :           .array()
      71        1244 :           .sqrt();
      72             :   // Store in std:vector format
      73        1244 :   _mean.resize(n);
      74        1244 :   _stdev.resize(n);
      75        1244 :   RealEigenVector::Map(&_mean[0], n) = mean;
      76        1244 :   RealEigenVector::Map(&_stdev[0], n) = stdev;
      77        1244 : }
      78             : 
      79             : void
      80      117208 : Standardizer::getStandardized(RealEigenMatrix & input) const
      81             : {
      82             :   Eigen::Map<const RealEigenVector> mean(_mean.data(), _mean.size());
      83             :   Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
      84      117208 :   input = (input.rowwise() - mean.transpose()).array().rowwise() / stdev.transpose().array();
      85      117208 : }
      86             : 
      87             : void
      88      115964 : Standardizer::getDestandardized(RealEigenMatrix & input) const
      89             : {
      90             :   Eigen::Map<const RealEigenVector> mean(_mean.data(), _mean.size());
      91             :   Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
      92             :   input =
      93      115964 :       (input.array().rowwise() * stdev.transpose().array()).rowwise() + mean.transpose().array();
      94      115964 : }
      95             : 
      96             : void
      97      115964 : Standardizer::getDescaled(RealEigenMatrix & input) const
      98             : {
      99             :   Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
     100      115964 :   input = input.array().rowwise() * stdev.transpose().array();
     101      115964 : }
     102             : 
     103             : /// Helper for dataStore
     104             : void
     105          70 : Standardizer::storeHelper(std::ostream & stream, void * context) const
     106             : {
     107          70 :   unsigned int n = _mean.size();
     108             :   dataStore(stream, n, context);
     109         200 :   for (unsigned int ii = 0; ii < n; ++ii)
     110         130 :     dataStore(stream, _mean[ii], context);
     111         200 :   for (unsigned int ii = 0; ii < n; ++ii)
     112         130 :     dataStore(stream, _stdev[ii], context);
     113          70 : }
     114             : 
     115             : } // StochasticTools namespace
     116             : 
     117             : template <>
     118             : void
     119          70 : dataStore(std::ostream & stream, StochasticTools::Standardizer & standardizer, void * context)
     120             : {
     121          70 :   standardizer.storeHelper(stream, context);
     122          70 : }
     123             : 
     124             : template <>
     125             : void
     126          96 : dataLoad(std::istream & stream, StochasticTools::Standardizer & standardizer, void * context)
     127             : {
     128             :   unsigned int n;
     129             :   dataLoad(stream, n, context);
     130          96 :   std::vector<Real> mean(n);
     131          96 :   std::vector<Real> stdev(n);
     132         272 :   for (unsigned int ii = 0; ii < n; ++ii)
     133         176 :     dataLoad(stream, mean[ii], context);
     134         272 :   for (unsigned int ii = 0; ii < n; ++ii)
     135         176 :     dataLoad(stream, stdev[ii], context);
     136          96 :   standardizer.set(mean, stdev);
     137          96 : }

Generated by: LCOV version 1.14