Repository: climate Updated Branches: refs/heads/master 360f23717 -> 67fd821b2
CLIMATE-444 - Add PatternCorrelation metric and tests Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/2e9afb5f Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/2e9afb5f Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/2e9afb5f Branch: refs/heads/master Commit: 2e9afb5f99f0894ab63d240d89abfeba2276aabb Parents: 23af110 Author: Shakeh <[email protected]> Authored: Thu May 29 12:06:55 2014 -0700 Committer: Shakeh <[email protected]> Committed: Thu May 29 12:06:55 2014 -0700 ---------------------------------------------------------------------- ocw/metrics.py | 33 +++++++++++++++++++++++++++++++++ ocw/tests/test_metrics.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/2e9afb5f/ocw/metrics.py ---------------------------------------------------------------------- diff --git a/ocw/metrics.py b/ocw/metrics.py index c3b8096..0b4e26f 100644 --- a/ocw/metrics.py +++ b/ocw/metrics.py @@ -23,6 +23,7 @@ Classes: from abc import ABCMeta, abstractmethod import ocw.utils as utils import numpy +from scipy import stats class Metric(object): '''Base Metric Class''' @@ -137,3 +138,35 @@ class SpatialStdDevRatio(BinaryMetric): return numpy.std(ref_means) / numpy.std(target_means) +class PatternCorrelation(BinaryMetric): + '''Calculate the spatial correlation''' + + def run(self, ref_dataset, target_dataset): + '''Calculate the spatial correlation between a reference and target dataset. + Using: scipy.stats.pearsonr + + .. 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 spatial correlation between a 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) + + pattern_correlation, p_value = stats.pearsonr(target_means.flatten(),ref_means.flatten()) + return pattern_correlation, p_value + + http://git-wip-us.apache.org/repos/asf/climate/blob/2e9afb5f/ocw/tests/test_metrics.py ---------------------------------------------------------------------- diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py index 788085b..193d48d 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 +from ocw.metrics import Bias, TemporalStdDev, SpatialStdDevRatio, PatternCorrelation from ocw.dataset import Dataset import numpy as np @@ -106,5 +106,34 @@ class TestSpatialStdDevRatio(unittest.TestCase): self.assertTrue(self.spatial_std_dev_ratio.run(self.ref_dataset, self.tar_dataset), 2.5) +class TestPatternCorrelation(unittest.TestCase): + '''Test the metrics.PatternCorrelation metric''' + def setUp(self): + self.pattern_correlation = PatternCorrelation() + 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.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) + + if __name__ == '__main__': unittest.main()
