Return-Path: X-Original-To: apmail-climate-commits-archive@minotaur.apache.org Delivered-To: apmail-climate-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EBCB010B16 for ; Tue, 27 May 2014 21:34:02 +0000 (UTC) Received: (qmail 73331 invoked by uid 500); 27 May 2014 21:34:02 -0000 Delivered-To: apmail-climate-commits-archive@climate.apache.org Received: (qmail 73287 invoked by uid 500); 27 May 2014 21:34:02 -0000 Mailing-List: contact commits-help@climate.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@climate.apache.org Delivered-To: mailing list commits@climate.apache.org Received: (qmail 73274 invoked by uid 99); 27 May 2014 21:34:02 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 May 2014 21:34:02 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id A346532AE3B; Tue, 27 May 2014 21:34:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: skhudiky@apache.org To: commits@climate.apache.org Date: Tue, 27 May 2014 21:34:02 -0000 Message-Id: <8628634c54474dad8052695cb1ca9657@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] git commit: CLIMATE-443 - Add metrics.SpatialStdDevRatio metric and tests Repository: climate Updated Branches: refs/heads/master f38d82ddc -> ffff32b9c CLIMATE-443 - Add metrics.SpatialStdDevRatio metric and tests - Metric includes the calcClimYear function from RCMET. Waiting for CLIMATE-439 to complete. Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/b368bf33 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/b368bf33 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/b368bf33 Branch: refs/heads/master Commit: b368bf33bdbf536bef6d90683243ba4d3c7a1632 Parents: 278b35f Author: Shakeh Authored: Tue May 27 11:41:22 2014 -0700 Committer: Shakeh Committed: Tue May 27 11:41:22 2014 -0700 ---------------------------------------------------------------------- ocw/metrics.py | 38 ++++++++++++++++++++++++++++++++++++++ ocw/tests/test_metrics.py | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/b368bf33/ocw/metrics.py ---------------------------------------------------------------------- diff --git a/ocw/metrics.py b/ocw/metrics.py index 0c27415..c3b8096 100644 --- a/ocw/metrics.py +++ b/ocw/metrics.py @@ -21,6 +21,8 @@ Classes: ''' from abc import ABCMeta, abstractmethod +import ocw.utils as utils +import numpy class Metric(object): '''Base Metric Class''' @@ -99,3 +101,39 @@ class TemporalStdDev(UnaryMetric): :rtype: Numpy Array ''' return target_dataset.values.std(axis=0, ddof=1) + + +class SpatialStdDevRatio(BinaryMetric): + '''Calculate the ratio of spatial standard deviation (model standard + deviation)/(observed standard deviation)''' + + def run(self, ref_dataset, target_dataset): + '''Calculate the ratio of spatial std. dev. between a reference and + target dataset. + + .. 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 ratio of standard deviation of the reference and target + dataset. + ''' + + # This is calcClimYear function for ref_dataset + reshaped_ref_data = utils.reshape_monthly_to_annually(ref_dataset) + ref_t_series = reshaped_ref_data.mean(axis=1) + ref_means = ref_t_series.mean(axis=0) + + # This is calcClimYear function for target_dataset + reshaped_target_data = utils.reshape_monthly_to_annually(target_dataset) + target_t_series = reshaped_target_data.mean(axis=1) + target_means = target_t_series.mean(axis=0) + + return numpy.std(ref_means) / numpy.std(target_means) + + http://git-wip-us.apache.org/repos/asf/climate/blob/b368bf33/ocw/tests/test_metrics.py ---------------------------------------------------------------------- diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py index 9f6ad2c..788085b 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 +from ocw.metrics import Bias, TemporalStdDev, SpatialStdDevRatio from ocw.dataset import Dataset import numpy as np @@ -30,7 +30,7 @@ class TestBias(unittest.TestCase): '''Test the metrics.Bias metric.''' def setUp(self): self.bias = Bias() - #Initialize reference dataset + # 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)]) @@ -39,7 +39,7 @@ class TestBias(unittest.TestCase): 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 + # 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)]) @@ -61,7 +61,7 @@ class TestTemporalStdDev(unittest.TestCase): '''Test the metrics.TemporalStdDev metric.''' def setUp(self): self.temporal_std_dev = TemporalStdDev() - #Initialize target dataset + # Initialize target dataset self.target_lat = np.array([10, 12, 14, 16, 18]) self.target_lon = np.array([100, 102, 104, 106, 108]) self.target_time = np.array([dt.datetime(2000, x, 1) for x in range(1, 13)]) @@ -78,5 +78,33 @@ class TestTemporalStdDev(unittest.TestCase): expected_result.fill(90.13878189) npt.assert_almost_equal(self.temporal_std_dev.run(self.target_dataset), expected_result) + +class TestSpatialStdDevRatio(unittest.TestCase): + '''Test the metrics.SpatialStdDevRatio metric''' + def setUp(self): + self.spatial_std_dev_ratio = SpatialStdDevRatio() + self.ref_dataset = Dataset( + np.array([1., 1., 1., 1., 1.]), + np.array([1., 1., 1., 1., 1.]), + np.array([dt.datetime(2000, x, 1) for x in range(1, 13)]), + # Reshapped array with 300 values incremented by 5 + np.arange(0, 1500, 5).reshape(12, 5, 5), + 'ds1' + ) + + self.tar_dataset = Dataset( + np.array([1., 1., 1., 1., 1.]), + np.array([1., 1., 1., 1., 1.]), + np.array([dt.datetime(2000, x, 1) for x in range(1, 13)]), + # Reshapped array with 300 values incremented by 2 + np.arange(0, 600, 2).reshape(12, 5, 5), + 'ds2' + ) + + 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) + + if __name__ == '__main__': unittest.main()