Repository: climate Updated Branches: refs/heads/master 3e8d1bf0b -> cff5b5459
CLIMATE-542: Add TemporalCorrelation metric and unit tests Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/16578ed8 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/16578ed8 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/16578ed8 Branch: refs/heads/master Commit: 16578ed8a42d6f5156c6803122c9434e9eb44632 Parents: 3e8d1bf Author: rlaidlaw <[email protected]> Authored: Wed Nov 5 17:16:40 2014 -0800 Committer: rlaidlaw <[email protected]> Committed: Wed Nov 5 17:16:40 2014 -0800 ---------------------------------------------------------------------- ocw/metrics.py | 35 +++++++++++++++++++++++++++++++ ocw/tests/test_metrics.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/16578ed8/ocw/metrics.py ---------------------------------------------------------------------- diff --git a/ocw/metrics.py b/ocw/metrics.py index ee5c1f9..815380c 100644 --- a/ocw/metrics.py +++ b/ocw/metrics.py @@ -149,6 +149,41 @@ class PatternCorrelation(BinaryMetric): return stats.pearsonr(ref_dataset.values.flatten(), target_dataset.values.flatten())[0] +class TemporalCorrelation(BinaryMetric): + '''Calculate the temporal correlation coefficients and associated + confidence levels between two datasets, using Pearson's correlation.''' + + def run(self, reference_dataset, target_dataset): + '''Calculate the temporal correlation coefficients and associated + confidence levels between two datasets, using Pearson's correlation. + + .. note:: + Overrides BinaryMetric.run() + + :param reference_dataset: The reference dataset to use in this metric + run + :type reference_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: A 2D array of temporal correlation coefficients and a 2D + array of confidence levels associated with the temporal correlation + coefficients + ''' + nt, nx, ny = reference_dataset.values.shape + tc = numpy.zeros([nx, ny]) + cl = numpy.zeros([nx, ny]) + for ix in numpy.arange(nx): + for iy in numpy.arange(ny): + tc[ix, iy], cl[ix, iy] = stats.pearsonr( + reference_dataset.values[:, ix, iy], + target_dataset.values[:, ix, iy] + ) + cl[ix, iy] = 1 - cl[ix, iy] + return tc, cl + + class TemporalMeanBias(BinaryMetric): '''Calculate the bias averaged over time.''' http://git-wip-us.apache.org/repos/asf/climate/blob/16578ed8/ocw/tests/test_metrics.py ---------------------------------------------------------------------- diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py index affb937..7f9bbae 100644 --- a/ocw/tests/test_metrics.py +++ b/ocw/tests/test_metrics.py @@ -132,6 +132,53 @@ class TestPatternCorrelation(unittest.TestCase): self.assertEqual(pattern, 1.0) +class TestTemporalCorrelation(unittest.TestCase): + '''Test the metrics.TemporalCorrelation metric.''' + def setUp(self): + # Set metric. + self.metric = metrics.TemporalCorrelation() + # 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 datasets. + 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_variable = "tgt" + self.tgt_values_inc = np.array(range(300, 600)).reshape(12, 5, 5) + self.tgt_values_dec = np.array(range(299, -1, -1)).reshape(12, 5, 5) + self.tgt_dataset_inc = Dataset(self.tgt_lats, self.tgt_lons, + self.tgt_times, self.tgt_values_inc, self.tgt_variable) + self.tgt_dataset_dec = Dataset(self.tgt_lats, self.tgt_lons, + self.tgt_times, self.tgt_values_dec, self.tgt_variable) + + def test_temporal_correlation_identical_inputs(self): + expected = np.ones(25).reshape(5, 5) + tc, cl = self.metric.run(self.ref_dataset, self.ref_dataset) + np.testing.assert_array_equal(tc, expected) + np.testing.assert_array_equal(cl, expected) + + def test_temporal_correlation_positive_correlation(self): + expected = np.ones(25).reshape(5, 5) + tc, cl = self.metric.run(self.ref_dataset, self.tgt_dataset_inc) + np.testing.assert_array_equal(tc, expected) + np.testing.assert_array_equal(cl, expected) + + def test_temporal_correlation_negative_correlation(self): + expected_tc = np.array([-1] * 25).reshape(5, 5) + expected_cl = np.ones(25).reshape(5, 5) + tc, cl = self.metric.run(self.ref_dataset, self.tgt_dataset_dec) + np.testing.assert_array_equal(tc, expected_tc) + np.testing.assert_array_equal(cl, expected_cl) + + class TestTemporalMeanBias(unittest.TestCase): '''Test the metrics.TemporalMeanBias metric.''' def setUp(self):
