Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Merge branch 'patch-2' of https://github.com/prateekiiest/climate
  • Loading branch information
lewismc committed Aug 1, 2018
2 parents 7cdaa7c + 0307574 commit cf3ef6015833d476a2d0317e39874aa7413d70fd
Showing 2 changed files with 85 additions and 0 deletions.
@@ -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):
@@ -281,6 +303,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):
@@ -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):

0 comments on commit cf3ef60

Please sign in to comment.