climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject [1/3] git commit: CLIMATE-540: Add SpatialMeanOfTemporalMeanBias class and unit test
Date Thu, 30 Oct 2014 02:27:47 GMT
Repository: climate
Updated Branches:
  refs/heads/master 2104da0c9 -> d61c0eeb9


CLIMATE-540: Add SpatialMeanOfTemporalMeanBias class and unit test


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

Branch: refs/heads/master
Commit: 5220710a5d72fd0323271805c4940bdda6de52c3
Parents: 2104da0
Author: rlaidlaw <rlaidlaw.open@gmail.com>
Authored: Wed Oct 29 18:15:14 2014 -0700
Committer: rlaidlaw <rlaidlaw.open@gmail.com>
Committed: Wed Oct 29 18:15:14 2014 -0700

----------------------------------------------------------------------
 ocw/metrics.py            | 23 +++++++++++++++++++++++
 ocw/tests/test_metrics.py | 31 +++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/5220710a/ocw/metrics.py
----------------------------------------------------------------------
diff --git a/ocw/metrics.py b/ocw/metrics.py
index 8458210..68860a9 100644
--- a/ocw/metrics.py
+++ b/ocw/metrics.py
@@ -173,3 +173,26 @@ class MeanBias(BinaryMetric):
         mean_bias = diff.mean(axis=0)
 
         return mean_bias
+
+
+class SpatialMeanOfTemporalMeanBias(BinaryMetric):
+    '''Calculate the bias averaged over time and domain.'''
+
+    def run(self, reference_dataset, target_dataset):
+        '''Calculate the bias averaged over time and domain.
+
+        .. note::
+           Overrides BinaryMetric.run()
+
+        :param ref_dataset: The reference dataset to use in this metric run
+        :type ref_dataset: ocw.dataset.Dataset object
+        :param target_dataset: The target dataset to evaluate against the
+            reference dataset in this metric run
+        :type target_dataset: ocw.dataset.Dataset object
+
+        :returns: The bias averaged over time and domain
+        '''
+
+        bias = reference_dataset.values - target_dataset.values
+        return bias.mean()
+

http://git-wip-us.apache.org/repos/asf/climate/blob/5220710a/ocw/tests/test_metrics.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py
index 739224c..f27cc9d 100644
--- a/ocw/tests/test_metrics.py
+++ b/ocw/tests/test_metrics.py
@@ -167,5 +167,36 @@ class TestMeanBias(unittest.TestCase):
         expected_result.fill(300)
         np.testing.assert_array_equal(self.mean_bias.run(self.reference_dataset, self.target_dataset,
True), expected_result)
 
+
+class TestSpatialMeanOfTemporalMeanBias(unittest.TestCase):
+    '''Test the metrics.SpatialMeanOfTemporalMeanBias metric.'''
+    def setUp(self):
+        # Set metric.
+        self.metric = metrics.SpatialMeanOfTemporalMeanBias()
+        # 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(range(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)
+        # Initialize target dataset.
+        self.tgt_lats = np.array([10, 20, 30, 40, 50])
+        self.tgt_lons = np.array([5, 15, 25, 35, 45])
+        self.tgt_times = np.array([dt.datetime(2000, x, 1)
+                                   for x in range(1, 13)])
+        self.tgt_values = np.array(range(299, -1, -1)).reshape(12, 5, 5)
+        self.tgt_variable = "tgt"
+        self.tgt_dataset = Dataset(self.tgt_lats, self.tgt_lons,
+            self.tgt_times, self.tgt_values, self.tgt_variable)
+
+    def test_function_run(self):
+        result = self.metric.run(self.ref_dataset, self.tgt_dataset)
+        self.assertEqual(result, 0.0)
+
+
 if __name__ == '__main__':
     unittest.main()
+


Mime
View raw message