Commit b2b7d13f authored by Jonas Stienen's avatar Jonas Stienen
Browse files

Adding statistics data class for easier exchange of runtime statistics...

Adding statistics data class for easier exchange of runtime statistics (results only, e.g. from ITAStopWatch). Includes JSON export function
parent 6fab0705
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2021
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_STATISTICS
#define INCLUDE_WATCHER_ITA_STATISTICS
// ITABase
#include <ITABaseDefinitions.h>
#include <string>
#include <algorithm>
#include <sstream>
namespace ITABase
{
//! Statistical values class
/**
* JSON exporter available: ITABase::Utils::JSON::Export( CStatistics(), "stats.json" );
*/
class ITA_BASE_API CStatistics
{
public:
//! Konstruktor
inline CStatistics( std::string sName = "", std::string sUnits = "s")
: sName(sName)
, sUnits(sUnits)
{
Reset();
};
inline void Reset()
{
dMean = dStdDev = dMin = dMax = 0.0;
uiNumSamples = 0;
};
inline std::string ToString() const
{
std::stringstream ss;
ss << dMean << " ( " << dStdDev << " ) [ " << dMin << " .. " << dMax << " ], N = " << uiNumSamples << std::endl;
return ss.str();
};
public:
double dMean, dStdDev, dMin, dMax;
std::string sUnits, sName;
unsigned int uiNumSamples;
};
//! Outstream operator
inline std::ostream& operator<<(std::ostream& os, ITABase::CStatistics& stats)
{
os << stats.ToString();
return os;
};
}
#endif // INCLUDE_WATCHER_ITA_STATISTICS
......@@ -21,6 +21,7 @@
#include <ITABaseDefinitions.h>
#include <ITASpectrum.h>
#include <ITABase/ITAStatistics.h>
#include <string>
......@@ -35,7 +36,9 @@ namespace ITABase
* @param[in] oSpectrum Spectrum
* @param[in] sTargetFilePath Target file path
*/
ITA_BASE_API void Export( const CITASpectrum* pSpectrum, const std::string& sTargetFilePath );
ITA_BASE_API void Export(const CITASpectrum* pSpectrum, const std::string& sTargetFilePath);
ITA_BASE_API void Export(const CStatistics& oStats, const std::string& sTargetFilePath);
ITA_BASE_API void Export(const std::vector<CStatistics>& voStats, const std::string& sTargetFilePath);
}
}
}
......
......@@ -6,6 +6,7 @@ set( SubDirs Math )
set( DirFiles
ITAProgress.h
ITAStatistics.h
)
if( ITA_BASE_WITH_JSON_SUPPORT )
......
......@@ -23,6 +23,7 @@
#include <ITABaseDefinitions.h>
#include <ITAClock.h>
#include <ITACriticalSection.h>
#include <ITABase/ITAStatistics.h>
#include <algorithm>
......@@ -158,6 +159,9 @@ public:
//! Einen formattierten String ausgeben, der den status der Stopwatch formuliert
std::string ToString() const;
//! Returns statistics as class
ITABase::CStatistics GetStatistics(std::string sName) const;
private:
bool m_bStarted;
double m_dStart, m_dStop;
......
......@@ -2,27 +2,64 @@
#include <libjson.h>
#include <fstream>
void ITABase::Utils::JSON::Export( const CITASpectrum* pSpectrum, const std::string& sTargetFilePath )
void ITABase::Utils::JSON::Export(const CITASpectrum* pSpectrum, const std::string& sTargetFilePath)
{
JSONNode jnRoot;
jnRoot.push_back( JSONNode( "name", pSpectrum->GetName() ) );
jnRoot.push_back( JSONNode( "num_bands", pSpectrum->GetNumBands() ) );
jnRoot.push_back( JSONNode( "value_unit", pSpectrum->GetValueUnit() ) );
jnRoot.push_back(JSONNode("name", pSpectrum->GetName()));
jnRoot.push_back(JSONNode("num_bands", pSpectrum->GetNumBands()));
jnRoot.push_back(JSONNode("value_unit", pSpectrum->GetValueUnit()));
JSONNode jnSpectrum;
jnSpectrum.set_name( "spectrum" );
for( int i = 0; i < pSpectrum->GetNumBands(); i++ )
jnSpectrum.set_name("spectrum");
for (int i = 0; i < pSpectrum->GetNumBands(); i++)
{
JSONNode jnBin;
jnBin.set_name( "bin" + std::to_string( i ) );
jnBin.push_back( JSONNode( "center_frequency", pSpectrum->GetCenterFrequencies()[ i ] ) );
jnBin.push_back( JSONNode( "value", pSpectrum->GetValues()[ i ] ) );
jnBin.set_name("bin" + std::to_string(i));
jnBin.push_back(JSONNode("center_frequency", pSpectrum->GetCenterFrequencies()[i]));
jnBin.push_back(JSONNode("value", pSpectrum->GetValues()[i]));
jnSpectrum.push_back( jnBin );
jnSpectrum.push_back(jnBin);
}
jnRoot.push_back( jnSpectrum );
jnRoot.push_back(jnSpectrum);
std::ofstream fsOut(sTargetFilePath);
fsOut << jnRoot.write_formatted();
fsOut.close();
}
// Statistics
JSONNode StatsToJSON(const ITABase::CStatistics& oStats)
{
JSONNode jnStats;
jnStats.set_name(oStats.sName);
jnStats.push_back(JSONNode("name", oStats.sName));
jnStats.push_back(JSONNode("N", oStats.uiNumSamples));
jnStats.push_back(JSONNode("units", oStats.sUnits));
jnStats.push_back(JSONNode("min", oStats.dMin));
jnStats.push_back(JSONNode("max", oStats.dMax));
jnStats.push_back(JSONNode("mean", oStats.dMean));
jnStats.push_back(JSONNode("stddev", oStats.dStdDev));
return jnStats;
}
void ITABase::Utils::JSON::Export(const ITABase::CStatistics& oStats, const std::string& sTargetFilePath)
{
std::ofstream fsOut(sTargetFilePath);
fsOut << StatsToJSON(oStats).write_formatted();
fsOut.close();
}
void ITABase::Utils::JSON::Export(const std::vector<ITABase::CStatistics>& voStats, const std::string& sTargetFilePath)
{
JSONNode jnRoot;
jnRoot.set_name("statistics");
for (auto& oStats : voStats)
jnRoot.push_back(StatsToJSON(oStats));
std::ofstream fsOut( sTargetFilePath );
std::ofstream fsOut(sTargetFilePath);
fsOut << jnRoot.write_formatted();
fsOut.close();
}
......@@ -220,3 +220,16 @@ std::string ITAStopWatch::ToString() const
return ss.str();
}
ITABase::CStatistics ITAStopWatch::GetStatistics(std::string sName) const
{
ITABase::CStatistics oStats(sName);
oStats.sUnits = "s";
oStats.dMin = minimum();
oStats.dMax = maximum();
oStats.dMean = mean();
oStats.dStdDev = std_deviation();
oStats.uiNumSamples = cycles();
return oStats;
}
......@@ -27,7 +27,7 @@ int main(int nArgsIn, char** pccArgs)
const double dTargetAttenuationA = 5.89e-1;
double dActualAttenuationA = ITABase::ISO9613::AtmosphericAbsorptionLevel(dFrequencyA, dDistance, dTemperatureA, dHumidityA, dStaticPressure);
const double dTemperatureB =-5.0;
const double dTemperatureB = -5.0;
const double dFrequencyB = 1000.0;
const double dHumidityB = 50.0;
const double dTargetAttenuationB = 9.68;
......@@ -58,7 +58,7 @@ int main(int nArgsIn, char** pccArgs)
double dActualAttenuationF = ITABase::ISO9613::AtmosphericAbsorptionLevel(dFrequencyF, dDistance, dTemperatureF, dHumidityF, dStaticPressure);
//Compare the here calculated values with the values taken from ISO 9613-1 Table 1.
// Compare the here calculated values with the values taken from ISO 9613-1 Table 1.
cout << "Temperature: " << dTemperatureA << " C\t Relative humidity: " << dHumidityA << " %\t Frequency: " << dFrequencyA << " Hz\t Target attenuation: " << dTargetAttenuationA << " dB\t Calculated attenuation: " << dActualAttenuationA << " dB" << endl;
cout << "Temperature: " << dTemperatureB << " C\t Relative humidity: " << dHumidityB << " %\t Frequency: " << dFrequencyB << " Hz\t Target attenuation: " << dTargetAttenuationB << " dB\t Calculated attenuation: " << dActualAttenuationB << " dB" << endl;
cout << "Temperature: " << dTemperatureC << " C\t Relative humidity: " << dHumidityC << " %\t Frequency: " << dFrequencyC << " Hz\t Target attenuation: " << dTargetAttenuationC << " dB\t Calculated attenuation: " << dActualAttenuationC << " dB" << endl;
......@@ -66,4 +66,4 @@ int main(int nArgsIn, char** pccArgs)
cout << "Temperature: " << dTemperatureE << " C\t Relative humidity: " << dHumidityE << " %\t Frequency: " << dFrequencyE << " Hz\t Target attenuation: " << dTargetAttenuationE << " dB\t Calculated attenuation: " << dActualAttenuationE << " dB" << endl;
cout << "Temperature: " << dTemperatureF << " C\t Relative humidity: " << dHumidityF << " %\t Frequency: " << dFrequencyF << " Hz\t Target attenuation: " << dTargetAttenuationF << " dB\t Calculated attenuation: " << dActualAttenuationF << " dB" << endl;
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment