You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by ja...@apache.org on 2016/06/14 06:43:28 UTC
[1/4] climate git commit: Add tests for
calc_area_weighted_spatial_average
Repository: climate
Updated Branches:
refs/heads/master 54986818e -> 03e9d9f05
Add tests for calc_area_weighted_spatial_average
Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/a389b628
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/a389b628
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/a389b628
Branch: refs/heads/master
Commit: a389b628f307e57aa693f0aea1396b5cb8f0e200
Parents: c9b7c4e
Author: Ibrahim <ja...@gmail.com>
Authored: Sun Jun 12 22:34:05 2016 +0530
Committer: Ibrahim <ja...@gmail.com>
Committed: Tue Jun 14 01:18:41 2016 +0530
----------------------------------------------------------------------
ocw/tests/test_utils.py | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/climate/blob/a389b628/ocw/tests/test_utils.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_utils.py b/ocw/tests/test_utils.py
index 1137bea..d35bc55 100644
--- a/ocw/tests/test_utils.py
+++ b/ocw/tests/test_utils.py
@@ -313,6 +313,26 @@ class TestCalcAreaWeightedSpatialAverage(unittest.TestCase):
result = utils.calc_area_weighted_spatial_average(self.test_dataset)
np.testing.assert_array_equal(avg, result)
+ def test_spatial_average_with_area_weight(self):
+ avg = np.ma.array([3.985158, 12.985158, 21.985158,
+ 30.985158, 39.985158, 48.985158])
+ result = utils.calc_area_weighted_spatial_average(
+ self.test_dataset, area_weight=True)
+ np.testing.assert_array_almost_equal(avg, result)
+
+ def test__2_dim_lats_lons_area_weight(self):
+ self.lat = np.array([10, 12, 14]).reshape(3, 1)
+ self.lon = np.array([100, 102, 104]).reshape(3, 1)
+ flat_array = np.array(range(18))
+ self.value = flat_array.reshape(6, 3, 1)
+ self.test_dataset = Dataset(self.lat, self.lon, self.time,
+ self.value, self.variable)
+ avg = np.ma.array([0.995053, 3.995053, 6.995053, 9.995053, 12.995053,
+ 15.995053])
+ result = utils.calc_area_weighted_spatial_average(
+ self.test_dataset, area_weight=True)
+ np.testing.assert_array_almost_equal(avg, result)
+
class TestCalcClimatologyYear(unittest.TestCase):
''' Testing function 'calc_climatology_year' from ocw.utils.py '''
[4/4] climate git commit: Resolve merge conflict
Posted by ja...@apache.org.
Resolve merge conflict
Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/03e9d9f0
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/03e9d9f0
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/03e9d9f0
Branch: refs/heads/master
Commit: 03e9d9f05c50f8b82ced017f9939117180fe15a7
Parents: 5498681 23551e2
Author: Ibrahim <ja...@gmail.com>
Authored: Tue Jun 14 12:12:31 2016 +0530
Committer: Ibrahim <ja...@gmail.com>
Committed: Tue Jun 14 12:12:31 2016 +0530
----------------------------------------------------------------------
.coveragerc | 4 +
ocw/tests/lat_lon_time_daily.nc | Bin 0 -> 6242 bytes
ocw/tests/test_utils.py | 161 ++++++++++++++++++++++++++++++++++-
3 files changed, 162 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/climate/blob/03e9d9f0/.coveragerc
----------------------------------------------------------------------
diff --cc .coveragerc
index fa45ba4,2bab358..accf369
--- a/.coveragerc
+++ b/.coveragerc
@@@ -18,8 -18,5 +18,12 @@@
[report]
exclude_lines =
if __name__ == .__main__.:
++
+ # These lines will never be executed under any circumstances
+ if level_index == 0:
+ elif level_index == 1:
+ elif level_index == 2:
+ else:
++
+ # Due to issue https://issues.apache.org/jira/browse/CLIMATE-803
+ def calc_subregion_area_mean_and_std*
[2/4] climate git commit: Add tests for Utils module
Posted by ja...@apache.org.
Add tests for Utils module
- Add multiple tests for utils module
- Add test data lat_lon_time_daily.nc file
Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/c9b7c4e6
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/c9b7c4e6
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/c9b7c4e6
Branch: refs/heads/master
Commit: c9b7c4e603e6575d999a28889231e182b1ed267a
Parents: aaa3bc5
Author: Ibrahim <ja...@gmail.com>
Authored: Sun Jun 12 22:10:52 2016 +0530
Committer: Ibrahim <ja...@gmail.com>
Committed: Tue Jun 14 01:18:41 2016 +0530
----------------------------------------------------------------------
ocw/tests/lat_lon_time_daily.nc | Bin 0 -> 6242 bytes
ocw/tests/test_utils.py | 141 ++++++++++++++++++++++++++++++++++-
2 files changed, 138 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/climate/blob/c9b7c4e6/ocw/tests/lat_lon_time_daily.nc
----------------------------------------------------------------------
diff --git a/ocw/tests/lat_lon_time_daily.nc b/ocw/tests/lat_lon_time_daily.nc
new file mode 100644
index 0000000..cf884da
Binary files /dev/null and b/ocw/tests/lat_lon_time_daily.nc differ
http://git-wip-us.apache.org/repos/asf/climate/blob/c9b7c4e6/ocw/tests/test_utils.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_utils.py b/ocw/tests/test_utils.py
index 1b6cfb7..1137bea 100644
--- a/ocw/tests/test_utils.py
+++ b/ocw/tests/test_utils.py
@@ -28,12 +28,17 @@ import ocw.utils as utils
class TestDecodeTimes(unittest.TestCase):
- test_model = os.getcwd() + '/ocw-ui/backend/tests/example_data/'\
- 'lat_lon_time.nc'
def setUp(self):
+ path = os.path.dirname(os.path.realpath(__file__))
+ self.test_model = path + \
+ '/../../ocw-ui/backend/tests/example_data/lat_lon_time.nc'
+ self.test_model_daily = path + '/lat_lon_time_daily.nc'
+
self.netcdf = netCDF4.Dataset(
os.path.abspath(self.test_model), mode='r')
+ self.netcdf_daily = netCDF4.Dataset(
+ os.path.abspath(self.test_model_daily), mode='r')
def test_proper_return_format(self):
times = utils.decode_time_values(self.netcdf, 'time')
@@ -46,10 +51,18 @@ class TestDecodeTimes(unittest.TestCase):
end_time = datetime.datetime.strptime(
'2008-12-01 00:00:00', '%Y-%m-%d %H:%M:%S')
times = utils.decode_time_values(self.netcdf, 'time')
-
self.assertEquals(times[0], start_time)
self.assertEquals(times[-1], end_time)
+ def test_days_time_processing(self):
+ start_time = datetime.datetime.strptime(
+ '1951-4-14 00:00:00', '%Y-%m-%d %H:%M:%S')
+ end_time = datetime.datetime.strptime(
+ '1951-12-9 00:00:00', '%Y-%m-%d %H:%M:%S')
+ new_times = utils.decode_time_values(self.netcdf_daily, 'time')
+ self.assertEqual(new_times[0], start_time)
+ self.assertEqual(new_times[-1], end_time)
+
class TestTimeUnitsParse(unittest.TestCase):
def test_valid_parse(self):
@@ -125,6 +138,23 @@ class TestNormalizeLatLonValues(unittest.TestCase):
np.testing.assert_array_equal(lats, self.lats)
np.testing.assert_array_equal(values, self.values)
+ def test_lons_reversed(self):
+ self.lats = np.arange(-10, 10)
+ self.lons = np.arange(40)
+ times = np.array([datetime.datetime(2000, x, 1) for x in range(1, 7)])
+ flat_array = np.arange(len(times) * len(self.lats) * len(self.lons))
+ self.variable = flat_array.reshape(len(times),
+ len(self.lats),
+ len(self.lons))
+ lats, lons, values = utils.normalize_lat_lon_values(self.lats,
+ self.lons[::-1],
+ self.values[:,
+ ::,
+ ::-1])
+ np.testing.assert_array_equal(lats, self.lats)
+ np.testing.assert_array_equal(values, self.values)
+ np.testing.assert_array_equal(lons, self.lons)
+
def test_lons_shift_values(self):
expected_vals = np.array([[2, 3, 0, 1],
[6, 7, 4, 5],
@@ -157,6 +187,49 @@ class TestNormalizeLatLonValues(unittest.TestCase):
self.lons_unsorted,
self.values2)
+ def test_lons_greater_than_180(self):
+ self.lons = np.array([190, 210, 230, 250])
+ self.lats = np.array([-30, 0, 30])
+ self.values = np.arange(12).reshape(3, 4)
+ expected_lons = np.array([-170, -150, -130, -110])
+ expected_values = np.array([[0, 1, 2, 3],
+ [4, 5, 6, 7],
+ [8, 9, 10, 11]])
+ lats, lons, values = utils.normalize_lat_lon_values(self.lats,
+ self.lons,
+ self.values)
+ np.testing.assert_array_equal(lons, expected_lons)
+ np.testing.assert_array_equal(expected_values, values)
+
+
+class TestGetTemporalOverlap(unittest.TestCase):
+ def setUp(self):
+ self.lat = np.array([10, 12, 14, 16, 18])
+ self.lon = np.array([100, 102, 104, 106, 108])
+ self.time = np.array(
+ [datetime.datetime(2000, x, 1) for x in range(1, 13)])
+ flat_array = np.array(range(300))
+ self.value = flat_array.reshape(12, 5, 5)
+ self.variable = 'prec'
+ self.test_dataset = Dataset(self.lat, self.lon, self.time,
+ self.value, self.variable)
+ self.dataset_array = [self.test_dataset, self.test_dataset]
+
+ def test_same_dataset_temporal_overlap(self):
+ maximum, minimum = utils.get_temporal_overlap(self.dataset_array)
+ self.assertEqual(maximum, datetime.datetime(2000, 1, 1))
+ self.assertEqual(minimum, datetime.datetime(2000, 12, 1))
+
+ def test_different_dataset_temporal_overlap(self):
+ new_times = np.array(
+ [datetime.datetime(2002, x, 1) for x in range(1, 13)])
+ another_dataset = Dataset(self.lat, self.lon, new_times,
+ self.value, self.variable)
+ self.dataset_array = [self.test_dataset, another_dataset]
+ maximum, minimum = utils.get_temporal_overlap(self.dataset_array)
+ self.assertEqual(maximum, datetime.datetime(2002, 1, 1))
+ self.assertEqual(minimum, datetime.datetime(2000, 12, 1))
+
class TestReshapeMonthlyToAnnually(unittest.TestCase):
''' Testing function 'reshape_monthly_to_annually' from ocw.utils.py '''
@@ -191,6 +264,56 @@ class TestReshapeMonthlyToAnnually(unittest.TestCase):
ValueError, utils.reshape_monthly_to_annually, bad_dataset)
+class TestCalcTemporalMean(unittest.TestCase):
+ def setUp(self):
+ self.lat = np.array([10, 12, 14])
+ self.lon = np.array([100, 102, 104])
+ self.time = np.array(
+ [datetime.datetime(2000, x, 1) for x in range(1, 7)])
+ flat_array = np.array(range(54))
+ self.value = flat_array.reshape(6, 3, 3)
+ self.variable = 'prec'
+ self.test_dataset = Dataset(self.lat, self.lon, self.time,
+ self.value, self.variable)
+
+ def test_returned_mean(self):
+ mean_values = np.array([[22.5, 23.5, 24.5],
+ [25.5, 26.5, 27.5],
+ [28.5, 29.5, 30.5]])
+
+ result = utils.calc_temporal_mean(self.test_dataset)
+ np.testing.assert_array_equal(result, mean_values)
+
+
+class TestCalcAreaWeightedSpatialAverage(unittest.TestCase):
+ def setUp(self):
+ self.lat = np.array([10, 12, 14])
+ self.lon = np.array([100, 102, 104])
+ self.time = np.array(
+ [datetime.datetime(2000, x, 1) for x in range(1, 7)])
+ flat_array = np.array(range(54))
+ self.value = flat_array.reshape(6, 3, 3)
+ self.variable = 'prec'
+ self.test_dataset = Dataset(self.lat, self.lon, self.time,
+ self.value, self.variable)
+
+ def test_spatial_average(self):
+ avg = np.ma.array([4., 13., 22., 31., 40., 49.])
+ result = utils.calc_area_weighted_spatial_average(self.test_dataset)
+ np.testing.assert_array_equal(avg, result)
+
+ def test_2_dim_lats_lons(self):
+ self.lat = np.array([10, 12, 14]).reshape(3, 1)
+ self.lon = np.array([100, 102, 104]).reshape(3, 1)
+ flat_array = np.array(range(18))
+ self.value = flat_array.reshape(6, 3, 1)
+ self.test_dataset = Dataset(self.lat, self.lon, self.time,
+ self.value, self.variable)
+ avg = np.ma.array([1., 4., 7., 10., 13., 16.])
+ result = utils.calc_area_weighted_spatial_average(self.test_dataset)
+ np.testing.assert_array_equal(avg, result)
+
+
class TestCalcClimatologyYear(unittest.TestCase):
''' Testing function 'calc_climatology_year' from ocw.utils.py '''
@@ -219,6 +342,12 @@ class TestCalcClimatologyYear(unittest.TestCase):
np.testing.assert_array_equal(
utils.calc_climatology_year(self.test_dataset)[1], total_mean)
+ def test_invalid_time_shape(self):
+ flat_array = np.array(range(350))
+ self.test_dataset.values = flat_array.reshape(14, 5, 5)
+ with self.assertRaises(ValueError):
+ utils.calc_climatology_year(self.test_dataset)
+
class TestCalcClimatologyMonthly(unittest.TestCase):
''' Tests the 'calc_climatology_monthly' method from ocw.utils.py '''
@@ -245,6 +374,12 @@ class TestCalcClimatologyMonthly(unittest.TestCase):
np.testing.assert_array_equal(actual_result, expected_result)
np.testing.assert_array_equal(actual_times, expected_times)
+ def test_invalid_time_shape(self):
+ flat_array = np.array(range(350))
+ self.dataset.values = flat_array.reshape(14, 5, 5)
+ with self.assertRaises(ValueError):
+ utils.calc_climatology_monthly(self.dataset)
+
class TestCalcTimeSeries(unittest.TestCase):
''' Tests the 'calc_time_series' method from ocw.utils.py '''
[3/4] climate git commit: Remove function having a bug from test
coverage
Posted by ja...@apache.org.
Remove function having a bug from test coverage
Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/23551e28
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/23551e28
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/23551e28
Branch: refs/heads/master
Commit: 23551e28cec66f8263b48b3a933c9d1e42dd87a4
Parents: a389b62
Author: Ibrahim <ja...@gmail.com>
Authored: Sun Jun 12 22:35:07 2016 +0530
Committer: Ibrahim <ja...@gmail.com>
Committed: Tue Jun 14 01:19:17 2016 +0530
----------------------------------------------------------------------
.coveragerc | 2 ++
1 file changed, 2 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/climate/blob/23551e28/.coveragerc
----------------------------------------------------------------------
diff --git a/.coveragerc b/.coveragerc
index 4e4a627..2bab358 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -18,3 +18,5 @@
[report]
exclude_lines =
if __name__ == .__main__.:
+ # Due to issue https://issues.apache.org/jira/browse/CLIMATE-803
+ def calc_subregion_area_mean_and_std*