Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

CLIMATE-483 - Add basic PatternCorrelation metric #85

Merged
merged 2 commits into from
Jul 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions ocw/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,29 @@ def run(self, ref_dataset, target_dataset):


class PatternCorrelation(BinaryMetric):
'''Calculate the correlation coefficient between two datasets'''

def run(self, ref_dataset, target_dataset):
'''Calculate the correlation coefficient between two dataset.

.. 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 correlation coefficient between a reference and target dataset.
'''
# stats.pearsonr returns correlation_coefficient, 2-tailed p-value
# We only care about the correlation coefficient
# Docs at http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html
return stats.pearsonr(ref_dataset.values.flatten(), target_dataset.values.flatten())[0]


class TemporalPatternCorrelation(BinaryMetric):
'''Calculate the spatial correlation'''

def run(self, ref_dataset, target_dataset):
Expand Down
3 changes: 1 addition & 2 deletions ocw/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ def setUp(self):
)

def test_function_run(self):
pattern, p_value = self.pattern_correlation.run(self.ref_dataset, self.tar_dataset)
pattern = self.pattern_correlation.run(self.tar_dataset, self.ref_dataset)
self.assertEqual(pattern, 1.0)
self.assertEqual(p_value, 0.0)


class TestMeanBias(unittest.TestCase):
Expand Down