diff --git a/pyresample/test/test_geometry.py b/pyresample/test/test_geometry.py index ff2cc62a0..0e7c07f31 100644 --- a/pyresample/test/test_geometry.py +++ b/pyresample/test/test_geometry.py @@ -1,23 +1,14 @@ from __future__ import with_statement import sys -import unittest - import numpy as np - -# import warnings -# if sys.version_info < (2, 6): -# warnings.simplefilter("ignore") -# else: -# warnings.simplefilter("always") from pyresample.test.utils import catch_warnings - from pyresample import geometry, geo_filter - -def tmp(f): - f.tmp = True - return f +if sys.version_info < (2, 7): + import unittest2 as unittest +else: + import unittest class Test(unittest.TestCase): @@ -69,7 +60,6 @@ def test_lonlat_precomp(self): self.assertAlmostEqual(lat, 52.566998432390619, msg='lat retrieval from precomputated grid failed') - @tmp def test_cartesian(self): area_def = geometry.AreaDefinition('areaD', 'Europe (3km, HRV, VTC)', 'areaD', {'a': '6378144.0', @@ -181,10 +171,6 @@ def test_swath_wrap(self): lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) lons1 += 180. - # if (sys.version_info < (2, 6) or - # (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): - # swath_def = geometry.BaseDefinition(lons1, lats1) - # else: with catch_warnings() as w1: swath_def = geometry.BaseDefinition(lons1, lats1) self.assertFalse( @@ -542,7 +528,6 @@ def test_area_extent_ll(self): 122.06448093539757, 5, 'Failed to get lon and lats of area extent') - @tmp def test_latlong_area(self): area_def = geometry.AreaDefinition('', '', '', {'proj': 'latlong'}, @@ -552,9 +537,7 @@ def test_latlong_area(self): self.assertEqual(lons[0, 0], -179.5) self.assertEqual(lats[0, 0], 89.5) - def test_lonlat2colrow(self): - from pyresample import utils area_id = 'meteosat_0deg' area_name = 'Meteosat 0 degree Service' diff --git a/pyresample/test/test_kd_tree.py b/pyresample/test/test_kd_tree.py index bb0da0fbe..891d2b4b4 100644 --- a/pyresample/test/test_kd_tree.py +++ b/pyresample/test/test_kd_tree.py @@ -2,32 +2,15 @@ import os import sys -import unittest - -import warnings -if sys.version_info < (2, 6): - warnings.simplefilter("ignore") -else: - warnings.simplefilter("always") import numpy - +from pyresample.test.utils import catch_warnings from pyresample import kd_tree, utils, geometry, data_reduce - -def mp(f): - f.mp = True - return f - - -def quick(f): - f.quick = True - return f - - -def tmp(f): - f.tmp = True - return f +if sys.version_info < (2, 7): + import unittest2 as unittest +else: + import unittest class Test(unittest.TestCase): @@ -59,23 +42,15 @@ def test_nearest_base(self): 100000, reduce_data=False, segments=1) self.assertTrue(res[0] == 2, 'Failed to calculate nearest neighbour') - @tmp def test_gauss_base(self): - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): - + with catch_warnings() as w: res = kd_tree.resample_gauss(self.tswath, self.tdata.ravel(), self.tgrid, 50000, 25000, reduce_data=False, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(self.tswath, - self.tdata.ravel(), self.tgrid, - 50000, 25000, reduce_data=False, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour warning') - self.assertFalse(('Searching' not in str( - w[0].message)), 'Failed to create correct neighbour warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour warning') + self.assertFalse(('Searching' not in str( + w[0].message)), 'Failed to create correct neighbour warning') self.assertAlmostEqual(res[0], 2.2020729, 5, 'Failed to calculate gaussian weighting') @@ -83,40 +58,27 @@ def test_custom_base(self): def wf(dist): return 1 - dist / 100000.0 - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_custom(self.tswath, self.tdata.ravel(), self.tgrid, 50000, wf, reduce_data=False, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_custom(self.tswath, - self.tdata.ravel(), self.tgrid, - 50000, wf, reduce_data=False, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour warning') - self.assertFalse(('Searching' not in str( - w[0].message)), 'Failed to create correct neighbour warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour warning') + self.assertFalse(('Searching' not in str( + w[0].message)), 'Failed to create correct neighbour warning') self.assertAlmostEqual(res[0], 2.4356757, 5, 'Failed to calculate custom weighting') - @tmp def test_gauss_uncert(self): sigma = utils.fwhm2sigma(41627.730557884883) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res, stddev, count = kd_tree.resample_gauss(self.tswath, self.tdata, self.tgrid, 100000, sigma, with_uncert=True) - else: - with warnings.catch_warnings(record=True) as w: - res, stddev, count = kd_tree.resample_gauss(self.tswath, self.tdata, - self.tgrid, 100000, sigma, - with_uncert=True) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour warning') - self.assertFalse(('Searching' not in str( - w[0].message)), 'Failed to create correct neighbour warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour warning') + self.assertFalse(('Searching' not in str( + w[0].message)), 'Failed to create correct neighbour warning') expected_res = 2.20206560694 expected_stddev = 0.707115076173 @@ -128,25 +90,18 @@ def test_gauss_uncert(self): self.assertEqual( count[0], expected_count, 'Wrong data point count for gaussian weighting with uncertainty') - @tmp def test_custom_uncert(self): def wf(dist): return 1 - dist / 100000.0 - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res, stddev, counts = kd_tree.resample_custom(self.tswath, self.tdata, self.tgrid, 100000, wf, with_uncert=True) - else: - with warnings.catch_warnings(record=True) as w: - res, stddev, counts = kd_tree.resample_custom(self.tswath, - self.tdata, self.tgrid, - 100000, wf, with_uncert=True) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour warning') - self.assertFalse(('Searching' not in str( - w[0].message)), 'Failed to create correct neighbour warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour warning') + self.assertFalse(('Searching' not in str( + w[0].message)), 'Failed to create correct neighbour warning') self.assertAlmostEqual(res[0], 2.32193149, 5, 'Failed to calculate custom weighting with uncertainty') @@ -315,18 +270,13 @@ def test_gauss(self): lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data.ravel(), self.area_def, 50000, 25000, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, data.ravel(), - self.area_def, 50000, 25000, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 4872.81050892 self.assertAlmostEqual(cross_sum, expected, @@ -339,18 +289,13 @@ def test_gauss_fwhm(self): lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data.ravel(), self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, data.ravel(), - self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 4872.81050892 self.assertAlmostEqual(cross_sum, expected, @@ -365,24 +310,18 @@ def test_gauss_multi(self): swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [25000, 15000, 10000], segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, data_multi, - self.area_def, 50000, [25000, 15000, 10000], segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.84313918 self.assertAlmostEqual(cross_sum, expected, msg='Swath multi channel resampling gauss failed') - @tmp def test_gauss_multi_uncert(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( @@ -392,35 +331,19 @@ def test_gauss_multi_uncert(self): swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: + # The assertion below checks if there is only one warning raised + # and whether it contains a specific message from pyresample + # On python 2.7.9+ the resample_gauss method raises multiple deprecation warnings + # that cause to fail, so we ignore the unrelated warnings. res, stddev, counts = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [ 25000, 15000, 10000], segments=1, with_uncert=True) - else: - with warnings.catch_warnings(record=True) as w: - # The assertion below checks if there is only one warning raised - # and whether it contains a specific message from pyresample - # On python 2.7.9+ the resample_gauss method raises multiple deprecation warnings - # that cause to fail, so we ignore the unrelated warnings. - # TODO: better way would be to filter UserWarning correctly - ignore_list = [DeprecationWarning] - try: - from numpy import VisibleDeprecationWarning - except ImportError: - pass - else: - ignore_list.append(VisibleDeprecationWarning) - warnings.simplefilter('ignore', tuple(ignore_list)) - res, stddev, counts = kd_tree.resample_gauss(swath_def, data_multi, - self.area_def, 50000, [ - 25000, 15000, 10000], - segments=1, with_uncert=True) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertTrue( + len(w) >= 1, 'Failed to create neighbour radius warning') + self.assertTrue(any(['Possible more' in str( + x.message) for x in w]), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() cross_sum_stddev = stddev.sum() cross_sum_counts = counts.sum() @@ -445,22 +368,15 @@ def test_gauss_multi_mp(self): swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [ 25000, 15000, 10000], nprocs=2, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, data_multi, - self.area_def, 50000, [ - 25000, 15000, 10000], - nprocs=2, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.84313918 self.assertAlmostEqual(cross_sum, expected, @@ -475,22 +391,15 @@ def test_gauss_multi_mp_segments(self): swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [ 25000, 15000, 10000], nprocs=2, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, data_multi, - self.area_def, 50000, [ - 25000, 15000, 10000], - nprocs=2, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.84313918 self.assertAlmostEqual(cross_sum, expected, @@ -524,18 +433,13 @@ def wf(dist): lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data.ravel(), self.area_def, 50000, wf, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_custom(swath_def, data.ravel(), - self.area_def, 50000, wf, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 4872.81050729 self.assertAlmostEqual(cross_sum, expected, @@ -559,18 +463,13 @@ def wf3(dist): swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data_multi, self.area_def, 50000, [wf1, wf2, wf3], segments=1) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_custom(swath_def, data_multi, - self.area_def, 50000, [wf1, wf2, wf3], segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.842980746 self.assertAlmostEqual(cross_sum, expected, @@ -847,22 +746,15 @@ def wf3(dist): data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: valid_input_index, valid_output_index, index_array, distance_array = \ kd_tree.get_neighbour_info(swath_def, self.area_def, 50000, segments=1) - else: - with warnings.catch_warnings(record=True) as w: - valid_input_index, valid_output_index, index_array, distance_array = \ - kd_tree.get_neighbour_info(swath_def, - self.area_def, - 50000, segments=1) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') res = kd_tree.get_sample_from_neighbour_info('custom', (800, 800), data_multi, diff --git a/pyresample/test/test_swath.py b/pyresample/test/test_swath.py index b7228a8bf..e7a26cdac 100644 --- a/pyresample/test/test_swath.py +++ b/pyresample/test/test_swath.py @@ -7,13 +7,13 @@ warnings.simplefilter("always") import numpy as np - +from pyresample.test.utils import catch_warnings from pyresample import kd_tree, geometry - -def tmp(f): - f.tmp = True - return f +if sys.version_info < (2, 7): + import unittest2 as unittest +else: + import unittest class Test(unittest.TestCase): @@ -32,20 +32,15 @@ class Test(unittest.TestCase): lats = lats[valid_fov] tb37v = tb37v[valid_fov] - @tmp def test_self_map(self): swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) - if sys.version_info < (2, 6): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, self.tb37v.copy(), swath_def, radius_of_influence=70000, sigmas=56500) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, self.tb37v.copy(), swath_def, - radius_of_influence=70000, sigmas=56500) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') if sys.platform == 'darwin': # OSX seems to get slightly different results for `_spatial_mp.Cartesian` @@ -59,18 +54,13 @@ def test_self_map_multi(self): data = np.column_stack((self.tb37v, self.tb37v, self.tb37v)) swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) - if (sys.version_info < (2, 6) or - (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): + with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data, swath_def, radius_of_influence=70000, sigmas=[56500, 56500, 56500]) - else: - with warnings.catch_warnings(record=True) as w: - res = kd_tree.resample_gauss(swath_def, data, swath_def, - radius_of_influence=70000, sigmas=[56500, 56500, 56500]) - self.assertFalse( - len(w) != 1, 'Failed to create neighbour radius warning') - self.assertFalse(('Possible more' not in str( - w[0].message)), 'Failed to create correct neighbour radius warning') + self.assertFalse( + len(w) != 1, 'Failed to create neighbour radius warning') + self.assertFalse(('Possible more' not in str( + w[0].message)), 'Failed to create correct neighbour radius warning') if sys.platform == 'darwin': # OSX seems to get slightly different results for `_spatial_mp.Cartesian` diff --git a/pyresample/test/utils.py b/pyresample/test/utils.py index 440968dad..39033e4fe 100644 --- a/pyresample/test/utils.py +++ b/pyresample/test/utils.py @@ -1,26 +1,24 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - -# Copyright (c) 2014, 2015 Martin Raspaud - +# Copyright (c) 2016 David Hoese # Author(s): - -# Martin Raspaud - +# David Hoese +# # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. - +# # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. - +# # You should have received a copy of the GNU Lesser General Public License along # with this program. If not, see . +"""Utilities for testing. -"""The test base. +This mostly takes from astropy's method for checking warnings during tests. """ import sys import six @@ -29,6 +27,9 @@ _deprecations_as_exceptions = False +_include_astropy_deprecations = False +AstropyDeprecationWarning = None +AstropyPendingDeprecationWarning = None def treat_deprecations_as_exceptions(): @@ -77,6 +78,11 @@ def treat_deprecations_as_exceptions(): # Now, turn DeprecationWarnings into exceptions warnings.filterwarnings("error", ".*", DeprecationWarning) + # Only turn astropy deprecation warnings into exceptions if requested + if _include_astropy_deprecations: + warnings.filterwarnings("error", ".*", AstropyDeprecationWarning) + warnings.filterwarnings("error", ".*", AstropyPendingDeprecationWarning) + if sys.version_info[:2] >= (3, 4): # py.test reads files with the 'U' flag, which is now # deprecated in Python 3.4.