LCOV - code coverage report
Current view: top level - src/utils - Standardizer.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 50 66 75.8 %
Date: 2026-05-29 20:40:35 Functions: 9 12 75.0 %
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          76 : 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          76 :   _mean = mean;
      54          76 :   _stdev = stdev;
      55          76 : }
      56             : 
      57             : void
      58         884 : Standardizer::computeSet(const RealEigenMatrix & input)
      59             : {
      60         884 :   _mean.clear();
      61         884 :   _stdev.clear();
      62         884 :   unsigned int num_samples = input.rows();
      63             :   unsigned int n = input.cols();
      64             :   // comptue mean
      65         884 :   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         884 :           .sqrt();
      72             :   // Store in std:vector format
      73         884 :   _mean.resize(n);
      74         884 :   _stdev.resize(n);
      75         884 :   RealEigenVector::Map(&_mean[0], n) = mean;
      76         884 :   RealEigenVector::Map(&_stdev[0], n) = stdev;
      77         884 : }
      78             : 
      79             : void
      80      152809 : 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      152809 :   input = (input.rowwise() - mean.transpose()).array().rowwise() / stdev.transpose().array();
      85      152809 : }
      86             : 
      87             : void
      88      151925 : 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      151925 :       (input.array().rowwise() * stdev.transpose().array()).rowwise() + mean.transpose().array();
      94      151925 : }
      95             : 
      96             : void
      97      151925 : Standardizer::getDescaled(RealEigenMatrix & input) const
      98             : {
      99             :   Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
     100      151925 :   input = input.array().rowwise() * stdev.transpose().array();
     101      151925 : }
     102             : 
     103             : void
     104           0 : Standardizer::getScaled(RealEigenMatrix & input) const
     105             : {
     106             :   Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
     107           0 :   input = input.array().rowwise() / stdev.transpose().array();
     108           0 : }
     109             : 
     110             : /// Helper for dataStore
     111             : void
     112         118 : Standardizer::storeHelper(std::ostream & stream, void * context) const
     113             : {
     114         118 :   unsigned int n = _mean.size();
     115             :   dataStore(stream, n, context);
     116         325 :   for (unsigned int ii = 0; ii < n; ++ii)
     117         207 :     dataStore(stream, _mean[ii], context);
     118         325 :   for (unsigned int ii = 0; ii < n; ++ii)
     119         207 :     dataStore(stream, _stdev[ii], context);
     120         118 : }
     121             : 
     122             : } // StochasticTools namespace
     123             : 
     124             : template <>
     125             : void
     126         118 : dataStore(std::ostream & stream, StochasticTools::Standardizer & standardizer, void * context)
     127             : {
     128         118 :   standardizer.storeHelper(stream, context);
     129         118 : }
     130             : 
     131             : template <>
     132             : void
     133          60 : dataLoad(std::istream & stream, StochasticTools::Standardizer & standardizer, void * context)
     134             : {
     135             :   unsigned int n;
     136             :   dataLoad(stream, n, context);
     137          60 :   std::vector<Real> mean(n);
     138          60 :   std::vector<Real> stdev(n);
     139         173 :   for (unsigned int ii = 0; ii < n; ++ii)
     140         113 :     dataLoad(stream, mean[ii], context);
     141         173 :   for (unsigned int ii = 0; ii < n; ++ii)
     142         113 :     dataLoad(stream, stdev[ii], context);
     143          60 :   standardizer.set(mean, stdev);
     144          60 : }

Generated by: LCOV version 1.14