From 85d23c27b5ef65715ca17ed56924118f9d939e7a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Offringa?= <offringa@astron.nl>
Date: Tue, 1 Nov 2011 16:19:25 +0000
Subject: [PATCH] Task #1892: starting implementation of quality statistics

---
 .gitattributes                                |   2 +
 .../include/AOFlagger/msio/qualitydata.h      | 195 ++++++++++++++++++
 .../experiments/rankoperatorrocexperiment.h   |   2 +-
 CEP/DP3/AOFlagger/src/gui/mswindow.cpp        |   3 +
 CEP/DP3/AOFlagger/src/msio/qualitydata.cpp    |  54 +++++
 5 files changed, 255 insertions(+), 1 deletion(-)
 create mode 100644 CEP/DP3/AOFlagger/include/AOFlagger/msio/qualitydata.h
 create mode 100644 CEP/DP3/AOFlagger/src/msio/qualitydata.cpp

diff --git a/.gitattributes b/.gitattributes
index fd0e981728c..448fddc3731 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -283,6 +283,7 @@ CEP/DP3/AOFlagger/include/AOFlagger/msio/mask2d.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/measurementset.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/parmtable.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/pngfile.h -text
+CEP/DP3/AOFlagger/include/AOFlagger/msio/qualitydata.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/rawdescfile.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/rawreader.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/rspreader.h -text
@@ -519,6 +520,7 @@ CEP/DP3/AOFlagger/src/msio/indirectbaselinereader.cpp -text
 CEP/DP3/AOFlagger/src/msio/mask2d.cpp -text
 CEP/DP3/AOFlagger/src/msio/measurementset.cpp -text
 CEP/DP3/AOFlagger/src/msio/pngfile.cpp -text
+CEP/DP3/AOFlagger/src/msio/qualitydata.cpp -text
 CEP/DP3/AOFlagger/src/msio/rspreader.cpp -text
 CEP/DP3/AOFlagger/src/msio/samplerow.cpp -text
 CEP/DP3/AOFlagger/src/msio/segmentedimage.cpp -text
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/msio/qualitydata.h b/CEP/DP3/AOFlagger/include/AOFlagger/msio/qualitydata.h
new file mode 100644
index 00000000000..1f1ecbeabe8
--- /dev/null
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/msio/qualitydata.h
@@ -0,0 +1,195 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by A.R. Offringa   *
+ *   offringa@astro.rug.nl   *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifndef MSIO_QUALITY_DATA_H
+#define MSIO_QUALITY_DATA_H
+
+#include <ms/MeasurementSets/MeasurementSet.h>
+
+/**
+	@author A.R. Offringa <offringa@astro.rug.nl>
+*/
+
+struct StatisticalValue {
+	StatisticalValue(unsigned _polarizationCount) :
+		polarizationCount(_polarizationCount),
+		values(new std::complex<float>[_polarizationCount])
+	{
+	}
+	
+	StatisticalValue(const StatisticalValue &source) :
+		polarizationCount(source.polarizationCount),
+		values(new std::complex<float>[source.polarizationCount])
+	{
+		kindIndex = source.kindIndex;
+		for(unsigned i=0;i<polarizationCount;++i)
+			values[i] = source.values[i];
+	}
+	
+	~StatisticalValue()
+	{
+		delete[] values;
+	}
+	
+	StatisticalValue &operator=(const StatisticalValue &source)
+	{
+		if(polarizationCount != source.polarizationCount)
+		{
+			polarizationCount = source.polarizationCount;
+			delete[] values;
+			values = new std::complex<float>[polarizationCount];
+		}
+		kindIndex = source.kindIndex;
+		for(unsigned i=0;i<polarizationCount;++i)
+			values[i] = source.values[i];
+		return *this;
+	}
+	
+	unsigned polarizationCount;
+	int kindIndex;
+	std::complex<float> *values;
+};
+
+class QualityData {
+	public:
+		enum StatisticKind
+		{
+			RFIRatioStatistic,
+			FlaggedRatioStatistic,
+			CountStatistic,
+			MeanStatistic,
+			RFIMeanStatistic,
+			RFICountStatistic,
+			SumP2Statistic,
+			SumP4Statistic,
+			VarianceStatistic,
+			VarianceOfVarianceStatistic,
+			SkewnessStatistic,
+			KurtosisStatistic,
+			SignalToNoiseStatistic,
+			DMeanStatistic,
+			DSumP2Statistic,
+			DSumP4Statistic,
+			DVarianceStatistic,
+			DVarianceOfVarianceStatistic,
+			DCountStatistic
+		};
+		
+		enum StatisticDimension
+		{
+			TimeDimension,
+			FrequencyDimension,
+			BaselineDimension,
+			BaselineTimeDimension
+		};
+		
+		enum QualityTable
+		{
+			KindNameTable,
+			TimeStatisticTable,
+			FrequencyStatisticTable,
+			BaselineStatisticTable,
+			BaselineTimeStatisticTable
+		};
+		
+		QualityData(casa::Table &measurementSet)
+		{
+			_measurementSet = new casa::Table(measurementSet);
+		}
+		
+		QualityData(const std::string &measurementSetName)
+		{
+			_measurementSet = new casa::Table(measurementSetName);
+		}
+		
+		~QualityData()
+		{
+			delete _measurementSet;
+		}
+		
+		bool TableExists(enum QualityTable table) const
+		{
+			return _measurementSet->isReadable(TableToName(table));
+		}
+		
+		const std::string &KindToName(const enum StatisticKind kind) const
+		{
+			return _kindToNameTable[(int) kind];
+		}
+		
+		const std::string &TableToName(const enum QualityTable table) const
+		{
+			return _tableToNameTable[(int) table];
+		}
+		
+		bool StatisticAvailable(enum QualityTable table, enum StatisticKind kind) const
+		{
+			return TableExists(KindNameTable) && TableExists(table) && hasOneEntry(table, kind);
+		}
+		
+		void InitializeEmptyStatistic(enum QualityTable table, enum StatisticKind kind)
+		{
+			if(!TableExists(table))
+				InitializeEmptyTable(table);
+			else
+				removeStatistic(table, kind);
+		}
+		
+		void InitializeEmptyTable(enum QualityTable table)
+		{
+			if(TableExists(table))
+				removeEntries(table);
+			else
+				createTable(table);
+		}
+		
+		void RemoveTable(enum QualityTable table)
+		{
+			if(TableExists(table))
+			{
+				casa::Table::deleteTable(TableToName(table));
+			}
+		}
+		
+		void RemoveAllStatistics()
+		{
+			RemoveTable(BaselineTimeStatisticTable);
+			RemoveTable(BaselineStatisticTable);
+			RemoveTable(FrequencyStatisticTable);
+			RemoveTable(TimeStatisticTable);
+			RemoveTable(KindNameTable);
+		}
+		
+		int StoreKindName(enum StatisticKind kind);
+		
+		void StoreTimeValue(double time, double frequency, const StatisticalValue &value);
+		
+	private:
+		casa::Table *_measurementSet;
+		
+		const static std::string _kindToNameTable[];
+		const static std::string _tableToNameTable[];
+		
+		bool hasOneEntry(enum QualityTable table, enum StatisticKind kind) const;
+		void removeStatistic(enum QualityTable table, enum StatisticKind kind);
+		void removeEntries(enum QualityTable table);
+		void createTable(enum QualityTable table);
+};
+
+#endif
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/test/experiments/rankoperatorrocexperiment.h b/CEP/DP3/AOFlagger/include/AOFlagger/test/experiments/rankoperatorrocexperiment.h
index 1371b34c926..dd17782a591 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/test/experiments/rankoperatorrocexperiment.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/test/experiments/rankoperatorrocexperiment.h
@@ -93,7 +93,7 @@ class RankOperatorROCExperiment : public UnitTest {
 		}
 };
 
-const unsigned RankOperatorROCExperiment::_repeatCount = 2;
+const unsigned RankOperatorROCExperiment::_repeatCount = 100;
 
 inline rfiStrategy::Strategy *RankOperatorROCExperiment::createThresholdStrategy()
 {
diff --git a/CEP/DP3/AOFlagger/src/gui/mswindow.cpp b/CEP/DP3/AOFlagger/src/gui/mswindow.cpp
index ef599afa3b3..c841764b279 100644
--- a/CEP/DP3/AOFlagger/src/gui/mswindow.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/mswindow.cpp
@@ -1024,6 +1024,7 @@ void MSWindow::onSetToOne()
 		real->SetAll(1.0);
 		imaginary->SetAll(0.0);
 		TimeFrequencyData newData(data.Polarisation(), real, imaginary);
+		newData.SetMask(data);
 		_timeFrequencyWidget.SetNewData(newData, _timeFrequencyWidget.GetMetaData());
 		_timeFrequencyWidget.Update();
 	} catch(std::exception &e)
@@ -1043,6 +1044,7 @@ void MSWindow::onSetToI()
 		real->SetAll(0.0);
 		imaginary->SetAll(1.0);
 		TimeFrequencyData newData(data.Polarisation(), real, imaginary);
+		newData.SetMask(data);
 		_timeFrequencyWidget.SetNewData(newData, _timeFrequencyWidget.GetMetaData());
 		_timeFrequencyWidget.Update();
 	} catch(std::exception &e)
@@ -1062,6 +1064,7 @@ void MSWindow::onSetToOnePlusI()
 		real->SetAll(1.0);
 		imaginary->SetAll(1.0);
 		TimeFrequencyData newData(data.Polarisation(), real, imaginary);
+		newData.SetMask(data);
 		_timeFrequencyWidget.SetNewData(newData, _timeFrequencyWidget.GetMetaData());
 		_timeFrequencyWidget.Update();
 	} catch(std::exception &e)
diff --git a/CEP/DP3/AOFlagger/src/msio/qualitydata.cpp b/CEP/DP3/AOFlagger/src/msio/qualitydata.cpp
new file mode 100644
index 00000000000..1adea802ca3
--- /dev/null
+++ b/CEP/DP3/AOFlagger/src/msio/qualitydata.cpp
@@ -0,0 +1,54 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by A.R. Offringa   *
+ *   offringa@astro.rug.nl   *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#include <AOFlagger/msio/qualitydata.h>
+
+const std::string QualityData::_kindToNameTable[] =
+{
+	"RFIRatio",
+	"FlaggedRatio",
+	"Count",
+	"Mean",
+	"RFIMean",
+	"RFICount",
+	"SumP2",
+	"SumP4",
+	"Variance",
+	"VarianceOfVariance",
+	"Skewness",
+	"Kurtosis",
+	"SignalToNoise",
+	"DMean",
+	"DSumP2",
+	"DSumP4",
+	"DVariance",
+	"DVarianceOfVariance",
+	"DCount"
+};
+
+const std::string QualityData::_tableToNameTable[] =
+{
+	"QUALITY_KIND_NAME",
+	"QUALITY_TIME_STATISTIC",
+	"QUALITY_FREQUENCY_STATISTIC",
+	"QUALITY_BASELINE_STATISTIC",
+	"QUALITY_BASELINE_TIME_STATISTIC"
+};
+
-- 
GitLab