From 4fd7d4916f12757434fe4494940aac064ca24852 Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Sat, 23 Jun 2018 22:25:12 +0530 Subject: [PATCH 1/3] include absolute bias --- ocw/metrics.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ocw/metrics.py b/ocw/metrics.py index 3a6477ef..b2660126 100644 --- a/ocw/metrics.py +++ b/ocw/metrics.py @@ -281,6 +281,29 @@ def calc_bias(target_array, reference_array, average_over_time=False): return ma.average(bias, axis=0) else: return bias + + +def calc_absbias(target_array, reference_array, average_over_time=False): + ''' Calculate absolute difference between two arrays + + :param target_array: an array to be evaluated, as model output + :type target_array: :class:'numpy.ma.core.MaskedArray' + + :param reference_array: an array of reference dataset + :type reference_array: :class:'numpy.ma.core.MaskedArray' + + :param average_over_time: if True, calculated bias is averaged for the axis=0 + :type average_over_time: 'bool' + + :returns: Absolute Biases array of the target dataset + :rtype: :class:'numpy.ma.core.MaskedArray' + ''' + + bias = abs(target_array - reference_array) + if average_over_time: + return ma.average(bias, axis=0) + else: + return bias def calc_stddev(array, axis=None): From 8104a2b24f1cf04c299d27276fc500650089eac2 Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Wed, 27 Jun 2018 07:15:46 +0530 Subject: [PATCH 2/3] Add a new class for Absolute Bias involving the function --- ocw/metrics.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ocw/metrics.py b/ocw/metrics.py index b2660126..199ab160 100644 --- a/ocw/metrics.py +++ b/ocw/metrics.py @@ -89,6 +89,28 @@ def run(self, ref_dataset, target_dataset): :rtype: :class:`numpy.ndarray` ''' return calc_bias(target_dataset.values, ref_dataset.values) + + +class AbsoluteBias(BinaryMetric): + '''Calculate the absolute bias between a reference and target dataset.''' + + def run(self, ref_dataset, target_dataset): + '''Calculate the absolute bias 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: :class:`dataset.Dataset` + + :param target_dataset: The target dataset to evaluate against the + reference dataset in this metric run. + :type target_dataset: :class:`dataset.Dataset` + + :returns: The absolute difference between the reference and target datasets. + :rtype: :class:`numpy.ndarray` + ''' + return calc_absbias(target_dataset.values, ref_dataset.values) class SpatialPatternTaylorDiagram(BinaryMetric): From 0307574133ff50d8f7f019cf867aa8a2fa2c6bd1 Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Wed, 27 Jun 2018 07:33:51 +0530 Subject: [PATCH 3/3] Add a unit test for Absolute Bias --- ocw/tests/test_metrics.py | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ocw/tests/test_metrics.py b/ocw/tests/test_metrics.py index ac786e54..2257112f 100644 --- a/ocw/tests/test_metrics.py +++ b/ocw/tests/test_metrics.py @@ -66,6 +66,46 @@ def test_function_run(self): expected_result.fill(-300) np.testing.assert_array_equal(self.bias.run( self.target_dataset, self.reference_dataset), expected_result) + + +class TestAbsoluteBias(unittest.TestCase): + '''Test the metrics.Bias metric.''' + + def setUp(self): + self.bias = metrics.AbsoluteBias() + # 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 bias function between reference dataset and target dataset.''' + expected_result = np.zeros((12, 5, 5), dtype=np.int) + expected_result.fill(300) + np.testing.assert_array_equal(self.bias.run( + self.target_dataset, self.reference_dataset), expected_result) class TestSpatialPatternTaylorDiagram(unittest.TestCase):