climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject [1/2] git commit: CLIMATE-541: Port calcRootMeanSquareDifferenceAveragedOverTimeAndDomain method to ocw/metrics.py as RMSError.run
Date Fri, 31 Oct 2014 03:51:19 GMT
Repository: climate
Updated Branches:
  refs/heads/master d61c0eeb9 -> 7deda4000


CLIMATE-541: Port calcRootMeanSquareDifferenceAveragedOverTimeAndDomain method to ocw/metrics.py
as RMSError.run


Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/f30b77cc
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/f30b77cc
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/f30b77cc

Branch: refs/heads/master
Commit: f30b77ccc8a49508e42d0adde0a4d6fa7fa08021
Parents: d61c0ee
Author: rlaidlaw <rlaidlaw.open@gmail.com>
Authored: Thu Oct 30 15:54:30 2014 -0700
Committer: rlaidlaw <rlaidlaw.open@gmail.com>
Committed: Thu Oct 30 15:54:30 2014 -0700

----------------------------------------------------------------------
 ocw/metrics.py            | 24 ++++++++++++++++++++++++
 ocw/tests/test_metrics.py | 29 +++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/f30b77cc/ocw/metrics.py
----------------------------------------------------------------------
diff --git a/ocw/metrics.py b/ocw/metrics.py
index 2ff276b..fb7bce3 100644
--- a/ocw/metrics.py
+++ b/ocw/metrics.py
@@ -196,3 +196,27 @@ class SpatialMeanOfTemporalMeanBias(BinaryMetric):
         bias = reference_dataset.values - target_dataset.values
         return bias.mean()
 
+
+class RMSError(BinaryMetric):
+    '''Calculate the Root Mean Square Difference (RMS Error), with the mean
+       calculated over time and space.'''
+
+    def run(self, eval_dataset, ref_dataset):
+        '''Calculate the Root Mean Square Difference (RMS Error), with the mean
+           calculated over time and space.
+
+        .. note::
+           Overrides BinaryMetric.run()
+
+        :param eval_dataset: The dataset to evaluate against the reference
+            dataset
+        :type eval_dataset: ocw.dataset.Dataset object
+        :param ref_dataset: The reference dataset for the metric
+        :type target_dataset: ocw.dataset.Dataset object
+
+        :returns: The RMS error, with the mean calculated over time and space
+        '''
+
+        sqdiff = (eval_dataset.values - ref_dataset.values) ** 2
+        return numpy.sqrt(sqdiff.mean())
+

http://git-wip-us.apache.org/repos/asf/climate/blob/f30b77cc/ocw/tests/test_metrics.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py
index 74cd8f0..a2ca025 100644
--- a/ocw/tests/test_metrics.py
+++ b/ocw/tests/test_metrics.py
@@ -197,6 +197,35 @@ class TestSpatialMeanOfTemporalMeanBias(unittest.TestCase):
         self.assertEqual(result, 0.0)
 
 
+class TestRMSError(unittest.TestCase):
+    '''Test the metrics.RMSError metric.'''
+    def setUp(self):
+        # Set metric.
+        self.metric = metrics.RMSError()
+        # Initialize evaluation dataset.
+        self.eval_lats = np.array([10, 20, 30, 40, 50])
+        self.eval_lons = np.array([5, 15, 25, 35, 45])
+        self.eval_times = np.array([dt.datetime(2000, x, 1)
+                                    for x in range(1, 13)])
+        self.eval_values = np.array([4] * 300).reshape(12, 5, 5)
+        self.eval_variable = "eval"
+        self.eval_dataset = Dataset(self.eval_lats, self.eval_lons,
+            self.eval_times, self.eval_values, self.eval_variable)
+        # Initialize reference dataset.
+        self.ref_lats = np.array([10, 20, 30, 40, 50])
+        self.ref_lons = np.array([5, 15, 25, 35, 45])
+        self.ref_times = np.array([dt.datetime(2000, x, 1)
+                                   for x in range(1, 13)])
+        self.ref_values = np.array([2] * 300).reshape(12, 5, 5)
+        self.ref_variable = "ref"
+        self.ref_dataset = Dataset(self.ref_lats, self.ref_lons,
+            self.ref_times, self.ref_values, self.ref_variable)
+
+    def test_function_run(self):
+        result = self.metric.run(self.eval_dataset, self.ref_dataset)
+        self.assertEqual(result, 2.0)
+
+
 if __name__ == '__main__':
     unittest.main()
 


Mime
View raw message