commit 6c4b26d061c7e52796a8914e64870f2162819a66
parent 1e05f88eea700b279b9152a5883e1f6168cbc89d
Author: Fabian Wermelinger <fabianw@mavt.ethz.ch>
Date: Tue, 12 Jul 2016 17:58:16 +0200
added statistics cartridge and info photo paper
Diffstat:
9 files changed, 175 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -30,6 +30,7 @@ FORCE:
clean:
find . -iname "*~" -exec rm -f {} \;
rm -f $(OBJ)
+ rm -f $(APPOBJ)
rm -f lib/libPolaroid.a
rm -f bin/polaroidCamera
$(MAKE) -C third_party clean
diff --git a/apps/polaroidCamera/Cartridges.h b/apps/polaroidCamera/Cartridges.h
@@ -14,5 +14,6 @@
#include "BoundedLogNormalizerCartridge.h"
#include "SchlierenCartridge.h"
#include "LineExtractor.h"
+#include "SliceStat.h"
#endif /* CARTRIDGES_H_LTWKNANR */
diff --git a/apps/polaroidCamera/SceneProcessor.cpp b/apps/polaroidCamera/SceneProcessor.cpp
@@ -31,6 +31,8 @@ void SceneProcessor::_prepare_cam()
m_cartridge = new SchlierenCartridge(m_parser);
else if (cart == "line_extractor")
m_cartridge = new LineExtractor(m_parser);
+ else if (cart == "statistics")
+ m_cartridge = new SliceStat(m_parser);
else
{
if (m_mpi.isroot())
@@ -47,6 +49,8 @@ void SceneProcessor::_prepare_cam()
m_photo = new PhotoHDF5;
else if (paper == "dat1d")
m_photo = new PhotoDAT1D;
+ else if (paper == "info")
+ m_photo = new PhotoINFO;
else
{
if (m_mpi.isroot())
diff --git a/apps/polaroidCamera/SliceStat.h b/apps/polaroidCamera/SliceStat.h
@@ -0,0 +1,44 @@
+// File : SliceStat.h
+// Date : Tue 12 Jul 2016 03:05:34 PM CEST
+// Author : Fabian Wermelinger
+// Description: Extract statistics of current slice
+// Copyright 2016 ETH Zurich. All Rights Reserved.
+#ifndef SLICEINFO_H_FGI1MKEH
+#define SLICEINFO_H_FGI1MKEH
+
+#include <string>
+#include <sstream>
+#include <cassert>
+#include "Cartridge.h"
+#include "Types.h"
+#include "PhotoINFO.h"
+
+class SliceStat: public Cartridge
+{
+public:
+ SliceStat(ArgumentParser& parser) : Cartridge(parser) {}
+
+ virtual void capture(PhotoPaper& photo, Slice& data)
+ {
+ assert(photo.suffix() == std::string(".info"));
+ PhotoINFO& pi = dynamic_cast<PhotoINFO&>(photo);
+
+ // set description
+ string desc("2D_Slice_Info");
+ pi.set_description(desc.c_str());
+
+ const std::string basename(pi.get_name());
+
+ std::ostringstream buf;
+ buf << "-info_sliceDir=" << data.get_sliceDir() << "_sliceID=" << data.get_sliceID();
+ pi.make_new(basename+buf.str()+pi.suffix());
+
+ // collect statistics
+ Slice2D_Statistics stat(data);
+ pi.write(stat);
+ }
+
+ virtual void compute(Slice& data) {}
+};
+
+#endif /* SLICEINFO_H_FGI1MKEH */
diff --git a/include/PhotoFormats.h b/include/PhotoFormats.h
@@ -10,5 +10,6 @@
#include "PhotoHDF5.h"
#include "PhotoPNG.h"
#include "PhotoDAT1D.h"
+#include "PhotoINFO.h"
#endif /* PHOTOFORMATS_H_3IWQHPAU */
diff --git a/include/PhotoINFO.h b/include/PhotoINFO.h
@@ -0,0 +1,34 @@
+// File : PhotoINFO.h
+// Date : Tue 12 Jul 2016 04:17:47 PM CEST
+// Author : Fabian Wermelinger
+// Description: Statistics photo
+// Copyright 2016 ETH Zurich. All Rights Reserved.
+#ifndef PHOTOINFO_H_SVPPOILX
+#define PHOTOINFO_H_SVPPOILX
+
+#include <string>
+#include <sstream>
+#include "common.h"
+#include "PhotoPaper.h"
+#include "Types.h"
+
+class PhotoINFO: public PhotoPaper
+{
+ std::ostringstream m_buf;
+
+public:
+ PhotoINFO(const std::string filename="ascii") : PhotoPaper(0,0,filename)
+ {
+ m_description = "ASCII information";
+ }
+ virtual ~PhotoINFO() { }
+
+ virtual void make_new(const std::string name, const int dummy1=0, const int dummy2=0);
+ virtual void resize(const int dummy1, const int dummy2) { }
+ virtual void write() { }
+ virtual void set_pixel(const double dummy1, const int dummy2, const int dummy3) { }
+ virtual std::string suffix() const { return std::string(".info"); }
+ void write(const Slice2D_Statistics& stat);
+};
+
+#endif /* PHOTOINFO_H_SVPPOILX */
diff --git a/include/PhotoPaper.h b/include/PhotoPaper.h
@@ -20,7 +20,7 @@ public:
PhotoPaper(const int width, const int height, const std::string& name) : m_width(width), m_height(height), m_fname(name), m_description("data") {};
virtual ~PhotoPaper() {};
- virtual void make_new(const std::string name, const int width, const int height=0) = 0;
+ virtual void make_new(const std::string name, const int width=1, const int height=0) = 0;
virtual void resize(const int width, const int height=0) = 0;
virtual void write() = 0;
virtual void set_pixel(const double phi, const int x, const int y=0) = 0;
diff --git a/include/Types.h b/include/Types.h
@@ -8,6 +8,7 @@
#include <cassert>
#include <algorithm>
+#include <cmath>
#include "common.h"
template <int _SX, int _EX, int _SY, int _EY>
@@ -83,4 +84,60 @@ public:
typedef Slice2D<0,0,0,0> Slice;
+
+class Slice2D_Statistics
+{
+ double m_mean; // 1st moment
+ double m_var; // 2nd moment
+ double m_skew; // 3rd moment
+ double m_kurt; // 4th moment
+ double m_min, m_max;
+
+ void _compute_stat(const Slice& s)
+ {
+ m_min = s.min();
+ m_max = s.max();
+
+ int k = 0;
+ double mean = 0;
+ double M2 = 0, M3 = 0, M4 = 0;
+ for (int h=0; h < s.height(); ++h)
+ for (int w=0; w < s.width(); ++w)
+ {
+ const int k1 = k;
+ ++k;
+ const double delta = s(w,h) - mean;
+ const double delta_k = delta / k;
+ const double delta_k2 = delta_k * delta_k;
+ const double term1 = delta * delta_k * k1;
+ mean += delta_k;
+ M4 += term1 * delta_k2 * (k*k - 3*k + 3) + 6 * delta_k2 * M2 - 4 * delta_k * M3;
+ M3 += term1 * delta_k * (k - 2) - 3 * delta_k * M2;
+ M2 += term1;
+ }
+ assert(k > 1);
+
+ m_mean = mean;
+ m_var = M2 / (k - 1);
+ m_skew = std::sqrt(k) * M3 / std::pow(M2, 1.5);
+ m_kurt = k * M4 / (M2 * M2) - 3;
+ }
+
+public:
+ Slice2D_Statistics(const Slice& s) :
+ m_mean(0), m_var(0), m_skew(0), m_kurt(0), m_min(HUGE_VAL), m_max(-HUGE_VAL)
+ {
+ _compute_stat(s);
+ }
+
+ // accessors
+ double mean() const { return m_mean; }
+ double var() const { return m_var; }
+ double std() const { return std::sqrt(m_var); }
+ double skew() const { return m_skew; }
+ double kurt() const { return m_kurt; }
+ double min() const { return m_min; }
+ double max() const { return m_max; }
+};
+
#endif /* TYPES_H_QATFIWCK */
diff --git a/src/PhotoINFO.cpp b/src/PhotoINFO.cpp
@@ -0,0 +1,32 @@
+// File : PhotoINFO.cpp
+// Date : Tue 12 Jul 2016 04:33:18 PM CEST
+// Author : Fabian Wermelinger
+// Description: PhotoINFO implementation
+// Copyright 2016 ETH Zurich. All Rights Reserved.
+#include <cassert>
+#include <fstream>
+#include <iomanip>
+#include "PhotoINFO.h"
+
+using namespace std;
+
+void PhotoINFO::make_new(const string name, const int dummy1, const int dummy2)
+{
+ m_fname = name;
+ m_buf.str().clear();
+}
+
+void PhotoINFO::write(const Slice2D_Statistics& stat)
+{
+ ofstream info(m_fname.c_str());
+ info.setf(std::ios::scientific, std::ios::floatfield);
+ info.precision(12);
+ info << setfill('.') << setw(32) << left << "Mean" << ": " << stat.mean() << endl;
+ info << setfill('.') << setw(32) << left << "Variance" << ": " << stat.var() << endl;
+ info << setfill('.') << setw(32) << left << "Standard deviation" << ": " << stat.std() << endl;
+ info << setfill('.') << setw(32) << left << "Skewness" << ": " << stat.skew() << endl;
+ info << setfill('.') << setw(32) << left << "Kurtosis" << ": " << stat.kurt() << endl;
+ info << setfill('.') << setw(32) << left << "Minimum" << ": " << stat.min() << endl;
+ info << setfill('.') << setw(32) << left << "Maximum" << ": " << stat.max() << endl;
+ info.close();
+}