climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From boust...@apache.org
Subject [1/2] git commit: CLIMATE-463 refactoring calcBiasAveragedOverTime metric
Date Thu, 05 Jun 2014 17:53:13 GMT
Repository: climate
Updated Branches:
  refs/heads/master 9079213d7 -> 86131eba6


CLIMATE-463 refactoring calcBiasAveragedOverTime metric


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

Branch: refs/heads/master
Commit: fe227cf85ec517802324c927020398d766691287
Parents: 9079213
Author: Maziyar Boustani <maziyar_b4@yahoo.com>
Authored: Thu Jun 5 10:34:07 2014 -0700
Committer: Maziyar Boustani <maziyar_b4@yahoo.com>
Committed: Thu Jun 5 10:34:07 2014 -0700

----------------------------------------------------------------------
 ocw/metrics.py            | 24 ++++++++++++++++++++++++
 ocw/tests/test_metrics.py | 39 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 60 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/fe227cf8/ocw/metrics.py
----------------------------------------------------------------------
diff --git a/ocw/metrics.py b/ocw/metrics.py
index 0b4e26f..d85caec 100644
--- a/ocw/metrics.py
+++ b/ocw/metrics.py
@@ -169,4 +169,28 @@ class PatternCorrelation(BinaryMetric):
         pattern_correlation, p_value = stats.pearsonr(target_means.flatten(),ref_means.flatten())
         return pattern_correlation, p_value
 
+class MeanBias(BinaryMetric):
+    '''Calculate the mean bias'''
+
+    def run(self, ref_dataset, target_dataset, absolute=False):
+        '''Calculate the mean bias between a reference and target dataset over all time.
+
+        .. note::
+           Overrides BinaryMetric.run()
+
+        :param ref_dataset: The reference dataset to use in this metric run.
+        :type ref_dataset: Dataset.
+        :param target_dataset: The target dataset to evaluate against the
+            reference dataset in this metric run.
+        :type target_dataset: Dataset.
+
+        :returns: The the mean bias between a reference and target dataset over all time.
+        '''
+
+        diff = ref_dataset.values - target_dataset.values
+        if absolute:
+            diff = abs(diff)
+        mean_bias = diff.mean(axis=0)
+
+        return mean_bias
 

http://git-wip-us.apache.org/repos/asf/climate/blob/fe227cf8/ocw/tests/test_metrics.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py
index 193d48d..c819f21 100644
--- a/ocw/tests/test_metrics.py
+++ b/ocw/tests/test_metrics.py
@@ -20,7 +20,7 @@
 import unittest
 import datetime as dt
 
-from ocw.metrics import Bias, TemporalStdDev, SpatialStdDevRatio, PatternCorrelation
+from ocw.metrics import Bias, TemporalStdDev, SpatialStdDevRatio, PatternCorrelation, MeanBias
 from ocw.dataset import Dataset
 
 import numpy as np
@@ -102,7 +102,6 @@ class TestSpatialStdDevRatio(unittest.TestCase):
         )
 
     def test_function_run(self):
-        print 'Test the metrics.SpatialStdDevRatio metric'
         self.assertTrue(self.spatial_std_dev_ratio.run(self.ref_dataset, self.tar_dataset),
2.5)
 
 
@@ -129,11 +128,45 @@ class TestPatternCorrelation(unittest.TestCase):
         )
 
     def test_function_run(self):
-        print 'Test the metrics.PatternCorrelation metric'
         pattern, p_value = self.pattern_correlation.run(self.ref_dataset, self.tar_dataset)
         self.assertEqual(pattern, 1.0)
         self.assertEqual(p_value, 0.0)
 
 
+class TestMeanBias(unittest.TestCase):
+    '''Test the metrics.MeanBias metric.'''
+    def setUp(self):
+        self.mean_bias = MeanBias()
+        # Initialize reference dataset
+        self.reference_lat = np.array([10, 12, 14, 16, 18])
+        self.reference_lon = np.array([100, 102, 104, 106, 108])
+        self.reference_time = np.array([dt.datetime(2000, x, 1) for x in range(1, 13)])
+        flat_array = np.array(range(300))
+        self.reference_value = flat_array.reshape(12, 5, 5)
+        self.reference_variable = 'prec'
+        self.reference_dataset = Dataset(self.reference_lat, self.reference_lon,
+            self.reference_time, self.reference_value, self.reference_variable)
+        # Initialize target dataset
+        self.target_lat = np.array([1, 2, 4, 6, 8])
+        self.target_lon = np.array([10, 12, 14, 16, 18])
+        self.target_time = np.array([dt.datetime(2001, x, 1) for x in range(1, 13)])
+        flat_array = np.array(range(300, 600))
+        self.target_value = flat_array.reshape(12, 5, 5)
+        self.target_variable = 'tasmax'
+        self.target_dataset = Dataset(self.target_lat, self.target_lon, self.target_time,
+            self.target_value, self.target_variable)
+
+    def test_function_run(self):
+        '''Test mean bias function between reference dataset and target dataset.'''
+        expected_result = np.zeros((5, 5), dtype=np.int)
+        expected_result.fill(-300)
+        np.testing.assert_array_equal(self.mean_bias.run(self.reference_dataset, self.target_dataset),
expected_result)
+
+    def test_function_run_abs(self):
+        '''Test mean bias function between reference dataset and target dataset with abs
as True.'''
+        expected_result = np.zeros((5, 5), dtype=np.int)
+        expected_result.fill(300)
+        np.testing.assert_array_equal(self.mean_bias.run(self.reference_dataset, self.target_dataset,
True), expected_result)
+
 if __name__ == '__main__':
     unittest.main()


Mime
View raw message