def test_unity_3x3(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type): ''' Test that a 3x3 unit kernel returns the same array (except when boundary is None). ''' if fft_type == 'fftw' and not HAS_FFTW: pytest.skip('fftw3 is not installed') elif fft_type == 'scipy' and not HAS_SCIPY: pytest.skip('scipy is not installed') x = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]], dtype='float64') y = np.array([[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]], dtype='float64') z = convolve_fft(x, y, boundary=boundary, interpolate_nan=interpolate_nan, normalize_kernel=normalize_kernel, ignore_edge_zeros=ignore_edge_zeros, fft_type=fft_type) assert_array_almost_equal_nulp(z, x, 10)
def generate_or_test(self, generate, figure, image, adjust_bbox=True): if generate is None: result_dir = tempfile.mkdtemp() test_image = os.path.abspath(os.path.join(result_dir, image)) # distutils will put the baseline images in non-accessible places, # copy to our tmpdir to be sure to keep them in case of failure orig_baseline_image = os.path.abspath(os.path.join(self._baseline_images_dir, image)) baseline_image = os.path.abspath(os.path.join(result_dir, 'baseline-'+image)) shutil.copyfile(orig_baseline_image, baseline_image) figure.save(test_image) if not os.path.exists(baseline_image): raise Exception("""Image file not found for comparision test Generated Image: \t{test} This is expected for new tests.""".format( test=test_image)) msg = compare_images(baseline_image, test_image, tol=self._tolerance) if msg is None: shutil.rmtree(result_dir) else: raise Exception(msg) else: figure.save(os.path.abspath(os.path.join(generate, image)), adjust_bbox=adjust_bbox) pytest.skip("Skipping test, since generating data")
def generate_or_test(self, generate, figure, image, adjust_bbox=True): if generate is None: result_dir = tempfile.mkdtemp() test_image = os.path.abspath(os.path.join(result_dir, image)) # distutils will put the baseline images in non-accessible places, # copy to our tmpdir to be sure to keep them in case of failure orig_baseline_image = os.path.abspath( os.path.join(self._baseline_images_dir, image)) baseline_image = os.path.abspath( os.path.join(result_dir, 'baseline-' + image)) shutil.copyfile(orig_baseline_image, baseline_image) figure.save(test_image) if not os.path.exists(baseline_image): raise Exception("""Image file not found for comparision test Generated Image: \t{test} This is expected for new tests.""".format( test=test_image)) msg = compare_images(baseline_image, test_image, tol=self._tolerance) if msg is None: shutil.rmtree(result_dir) else: raise Exception(msg) else: figure.save(os.path.abspath(os.path.join(generate, image)), adjust_bbox=adjust_bbox) pytest.skip("Skipping test, since generating data")
def test_unity_1_withnan(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type): ''' Test that a unit kernel with three elements returns the same array (except when boundary is None). This version includes a NaN value in the original array. ''' if fft_type == 'fftw' and not HAS_FFTW: pytest.skip('fftw3 is not installed') elif fft_type == 'scipy' and not HAS_SCIPY: pytest.skip('scipy is not installed') x = np.array([1., np.nan, 3.], dtype='float64') y = np.array([1.], dtype='float64') z = convolve_fft(x, y, boundary=boundary, interpolate_nan=interpolate_nan, normalize_kernel=normalize_kernel, ignore_edge_zeros=ignore_edge_zeros, fft_type=fft_type) if fft_type == 'numpy': # for whatever reason, numpy's fft has very limited precision, and # the comparison fails unless you cast the float64 to a float16 # np1.4 incompatible assert_array_almost_equal_nulp(np.asarray(z, dtype=np.float16), np.array([1.,0.,3.], dtype=np.float16), 10) assert np.all(np.abs(z - np.array([1., 0., 3.])) < 1e-14) else: assert_array_almost_equal_nulp(z, np.array([1., 0., 3.], dtype='float64'), 10)
def test_uniform_3x3(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type): ''' Test that the different modes are producing the correct results using a 3x3 uniform kernel. ''' if fft_type == 'fftw' and not HAS_FFTW: pytest.skip('fftw3 is not installed') elif fft_type == 'scipy' and not HAS_SCIPY: pytest.skip('scipy is not installed') x = np.array([[0., 0., 3.], [1., 0., 0.], [0., 2., 0.]], dtype='float64') y = np.array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype='float64') z = convolve_fft(x, y, boundary=boundary, interpolate_nan=interpolate_nan, normalize_kernel=normalize_kernel, ignore_edge_zeros=ignore_edge_zeros, fft_type=fft_type) w = np.array([[4., 6., 4.], [6., 9., 6.], [4., 6., 4.]], dtype='float64') answer_dict = { 'sum': np.array([[1., 4., 3.], [3., 6., 5.], [3., 3., 2.]], dtype='float64'), 'sum_wrap': np.array([[6., 6., 6.], [6., 6., 6.], [6., 6., 6.]], dtype='float64'), } answer_dict['average'] = answer_dict['sum'] / w answer_dict['average_wrap'] = answer_dict['sum_wrap'] / 9. answer_dict['average_withzeros'] = answer_dict['sum'] / 9. answer_dict['sum_withzeros'] = answer_dict['sum'] if normalize_kernel: answer_key = 'average' else: answer_key = 'sum' if boundary == 'wrap': answer_key += '_wrap' elif not ignore_edge_zeros: answer_key += '_withzeros' a = answer_dict[answer_key] print boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, answer_key print "z: ", z print "answer: ", a print "ratio: ", z / a # for reasons unknown, the Windows FFT returns an answer for the [0,0] # component that is EXACTLY 10*np.spacing assert np.all(np.abs(z - a) <= np.spacing(np.where(z > a, z, a)) * 10)
def test_uniform_3_withnan(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type): ''' Test that the different modes are producing the correct results using a uniform kernel with three elements. This version includes a NaN value in the original array. ''' if fft_type == 'fftw' and not HAS_FFTW: pytest.skip('fftw3 is not installed') elif fft_type == 'scipy' and not HAS_SCIPY: pytest.skip('scipy is not installed') x = np.array([1., np.nan, 3.], dtype='float64') y = np.array([1., 1., 1.], dtype='float64') z = convolve_fft(x, y, boundary=boundary, interpolate_nan=interpolate_nan, normalize_kernel=normalize_kernel, ignore_edge_zeros=ignore_edge_zeros, fft_type=fft_type) answer_dict = { 'sum': np.array([1., 4., 3.], dtype='float64'), 'sum_nozeros': np.array([1., 4., 3.], dtype='float64'), 'sum_zeros': np.array([1., 4., 3.], dtype='float64'), 'sum_zeros_noignan': np.array([1., 4., 3.], dtype='float64'), 'sum_nozeros_noignan': np.array([1., 4., 3.], dtype='float64'), 'average': np.array([1., 2., 3.], dtype='float64'), 'sum_wrap': np.array([4., 4., 4.], dtype='float64'), 'sum_wrap_noignan': np.array([4., 4., 4.], dtype='float64'), 'average_wrap': np.array([(1 + 3) / 2., 2., 2.], dtype='float64'), 'average_wrap_noignan': np.array([4 / 3., 4 / 3., 4 / 3.], dtype='float64'), 'average_nozeros': np.array([1, 2, 3], dtype='float64'), 'average_nozeros_noignan': np.array([1 / 2., 4 / 3., 3 / 2.], dtype='float64'), 'average_zeros': np.array([1 / 2., 4 / 2., 3 /2.], dtype='float64'), 'average_zeros_noignan': np.array([1/3., 4/3., 3/3.], dtype='float64'), } if normalize_kernel: answer_key = 'average' else: answer_key = 'sum' if boundary == 'wrap': answer_key += '_wrap' elif ignore_edge_zeros: answer_key += '_nozeros' else: # average = average_zeros; sum = sum_zeros answer_key += '_zeros' if not interpolate_nan: answer_key += '_noignan' print boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, answer_key assert_array_almost_equal_nulp(z, answer_dict[answer_key], 10)
def test_helper_poisson_exact(): pytest.skip('distribution stretch goal not yet implemented') centerq = [1, 5, 30, 400] * u.one ds.poisson(centerq, n_samples=1000) with pytest.raises(u.UnitsError) as exc: centerq = [1, 5, 30, 400] * u.kpc ds.poisson(centerq, n_samples=1000) assert exc.value.args[0] == ("Poisson distribution can only be computed " "for dimensionless quantities")
def pytest_runtest_setup(item): if 'generate_reference' in item.keywords: if item.config.getvalue("generate_reference"): if item.config.getvalue("generate_reference").startswith('-'): raise Exception("Need to specify output directory for generating reference files") if not item.config.getvalue("generate_reference").startswith('/'): raise Exception("Need to specify output directory for generating reference files as an absolute path") item.funcargs['generate'] = item.config.getvalue("generate_reference") if 'enable_bit_level_tests' in item.keywords and not item.config.getvalue("enable_bit_level_tests"): pytest.skip("need --enable-bit-level-tests option to run")
def test_array_repr_latex(): # as of this writing ndarray does not have a _repr_latex_, and this test # ensure distributions account for that. However, if in the future ndarray # gets a _repr_latex_, we can skip this. arr = np.random.randn(4, 1000) if hasattr(arr, '_repr_latex_'): pytest.skip('in this version of numpy, ndarray has a _repr_latex_') distr = Distribution(arr) assert distr._repr_latex_() is None
def test_mask_area(self): try: mask = self.reg.to_mask(mode='exact') assert_allclose(np.sum(mask.data), self.expected_area) except NotImplementedError: try: mask = self.reg.to_mask(mode='subpixels', subpixels=30) assert_allclose(np.sum(mask.data), self.expected_area, rtol=0.005) except NotImplementedError: pytest.skip()
def pytest_runtest_setup(item): if 'generate_reference' in item.keywords: if item.config.getvalue("generate_reference"): if item.config.getvalue("generate_reference").startswith('-'): raise Exception( "Need to specify output directory for generating reference files" ) if not item.config.getvalue("generate_reference").startswith('/'): raise Exception( "Need to specify output directory for generating reference files as an absolute path" ) item.funcargs['generate'] = item.config.getvalue( "generate_reference") if 'enable_bit_level_tests' in item.keywords and not item.config.getvalue( "enable_bit_level_tests"): pytest.skip("need --enable-bit-level-tests option to run")
def test_unity_1_none(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type): ''' Test that a unit kernel with a single element returns the same array ''' if fft_type == 'fftw' and not HAS_FFTW: pytest.skip('fftw3 is not installed') elif fft_type == 'scipy' and not HAS_SCIPY: pytest.skip('scipy is not installed') x = np.array([1., 2., 3.], dtype='float64') y = np.array([1.], dtype='float64') z = convolve_fft(x, y, boundary=boundary, interpolate_nan=interpolate_nan, normalize_kernel=normalize_kernel, ignore_edge_zeros=ignore_edge_zeros, fft_type=fft_type) assert_array_almost_equal_nulp(z, x, 10)
def test_uniform_3x3_withnan(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type): ''' Test that the different modes are producing the correct results using a 3x3 uniform kernel. This version includes a NaN value in the original array. ''' if fft_type == 'fftw' and not HAS_FFTW: pytest.skip('fftw3 is not installed') elif fft_type == 'scipy' and not HAS_SCIPY: pytest.skip('scipy is not installed') x = np.array([[0., 0., 3.], [1., np.nan, 0.], [0., 2., 0.]], dtype='float64') y = np.array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype='float64') z = convolve_fft(x, y, boundary=boundary, interpolate_nan=interpolate_nan, normalize_kernel=normalize_kernel, ignore_edge_zeros=ignore_edge_zeros, fft_type=fft_type) w_n = np.array([[3., 5., 3.], [5., 8., 5.], [3., 5., 3.]], dtype='float64') w_z = np.array([[4., 6., 4.], [6., 9., 6.], [4., 6., 4.]], dtype='float64') answer_dict = { 'sum': np.array([[1., 4., 3.], [3., 6., 5.], [3., 3., 2.]], dtype='float64'), 'sum_wrap': np.array([[6., 6., 6.], [6., 6., 6.], [6., 6., 6.]], dtype='float64'), } answer_dict['average'] = answer_dict['sum'] / w_z answer_dict['average_ignan'] = answer_dict['sum'] / w_n answer_dict['average_wrap_ignan'] = answer_dict['sum_wrap'] / 8. answer_dict['average_wrap'] = answer_dict['sum_wrap'] / 9. answer_dict['average_withzeros'] = answer_dict['sum'] / 9. answer_dict['average_withzeros_ignan'] = answer_dict['sum'] / 8. answer_dict['sum_withzeros'] = answer_dict['sum'] answer_dict['sum_ignan'] = answer_dict['sum'] answer_dict['sum_withzeros_ignan'] = answer_dict['sum'] answer_dict['sum_wrap_ignan'] = answer_dict['sum_wrap'] if normalize_kernel: answer_key = 'average' else: answer_key = 'sum' if boundary == 'wrap': answer_key += '_wrap' elif not ignore_edge_zeros: answer_key += '_withzeros' if interpolate_nan: answer_key += '_ignan' a = answer_dict[answer_key] print boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, answer_key print "z: ", z print "answer: ", a print "ratio: ", z / a assert np.all(np.abs(z - a) < np.spacing(np.where(z > a, z, a)) * 10)
def test_helper_normal_exact(): pytest.skip('distribution stretch goal not yet implemented') centerq = [1, 5, 30, 400] * u.kpc ds.normal(centerq, std=[0.2, 1.5, 4, 1]*u.kpc) ds.normal(centerq, var=[0.04, 2.25, 16, 1]*u.kpc**2) ds.normal(centerq, ivar=[25, 0.44444444, 0.625, 1]*u.kpc**-2)
def test_area(self): try: assert_allclose(self.reg.area, self.expected_area) except ImportError: pytest.skip()
def test_pix_area(region): try: area = region.area assert not isinstance(area, u.Quantity) except ImportError: # for shapely pytest.skip()