def test_get_circle(): ar = np.ones((10, 10), dtype=int) aarc( get_circle(ar, radius=4), np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])) aarc( get_circle(PRETTY_ODD, radius=4, mode="val"), np.array([ 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 3, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1 ])) aarc( get_circle(PRETTY_EVEN, radius=4, mode="val"), np.array([ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 ]))
def test_matrix_scaling(): """ The "truth" values were verified by hand. """ m = np.array([[6, 12, 18], [0, 0, 12]], dtype=float) res = matrix_scaling(m, None) truth = m aarc(res, truth) res = matrix_scaling(m, "temp-mean") truth = np.array([[ 3, 6, 3], [-3, -6, -3]]) aarc(res, truth) res = matrix_scaling(m, "spat-mean") truth = np.array([[-6, 0, 6], [-4, -4, 8]]) aarc(res, truth) res = matrix_scaling(m, "temp-standard") truth = np.array([[ 1, 1, 1], [-1, -1, -1]]) aarc(res, truth) res = matrix_scaling(m, "spat-standard") truth = np.array([[-np.sqrt(3/2), 0, np.sqrt(3/2)], [-np.sqrt(1/2), -np.sqrt(1/2), np.sqrt(2)]]) aarc(res, truth)
def test_matrix_scaling(): """ The "truth" values were verified by hand. """ m = np.array([[6, 12, 18], [0, 0, 12]], dtype=float) res = matrix_scaling(m, None) truth = m aarc(res, truth) res = matrix_scaling(m, "temp-mean") truth = np.array([[3, 6, 3], [-3, -6, -3]]) aarc(res, truth) res = matrix_scaling(m, "spat-mean") truth = np.array([[-6, 0, 6], [-4, -4, 8]]) aarc(res, truth) res = matrix_scaling(m, "temp-standard") truth = np.array([[1, 1, 1], [-1, -1, -1]]) aarc(res, truth) res = matrix_scaling(m, "spat-standard") truth = np.array([[-np.sqrt(3 / 2), 0, np.sqrt(3 / 2)], [-np.sqrt(1 / 2), -np.sqrt(1 / 2), np.sqrt(2)]]) aarc(res, truth)
def test_reshape_matrix(): vectorized_frames = np.array([[1, 1, 1, 2, 2, 2], [1, 2, 3, 4, 5, 6]]) cube = reshape_matrix(vectorized_frames, 2, 3) assert cube.shape == (2, 2, 3) # 2 frames of 2x3 cube_truth = np.array([[[1, 1, 1], [2, 2, 2]], [[1, 2, 3], [4, 5, 6]]]) aarc(cube, cube_truth)
def test_check_scal_vector(): scal_vec = np.array([2, 8, 4]) # === basic function === res = check_scal_vector(scal_vec) truth = np.array([4, 1, 2]) aarc(res, truth) # === do nothing if min factor is already 1 === res2 = check_scal_vector(res) aarc(res2, res) # === wrong input value === with raises(TypeError): check_scal_vector(42)
def test_get_square(): aarc(get_square(PRETTY_ODD, size=3, x=2, y=2), np.array([[2, 2, 2], [2, 3, 2], [2, 2, 2]])) aarc(get_square(PRETTY_ODD, size=2, x=2, y=2), np.array([[2, 2, 2], [2, 3, 2], [2, 2, 2]])) # -> prints warning aarc(get_square(PRETTY_ODD, size=2, x=2, y=2, force=True), np.array([[2, 2], [2, 3]])) aarc(get_square(PRETTY_EVEN, size=2, x=3, y=3), np.array([[3, 3], [3, 3]])) aarc(get_square(PRETTY_EVEN, size=3, x=3, y=3), np.array([[2, 2, 2, 2], [2, 3, 3, 2], [2, 3, 3, 2], [2, 2, 2, 2]])) # -> prints warning aarc(get_square(PRETTY_EVEN, size=2, x=4, y=2, force=True), np.array([[2, 2], [3, 2]]))
def test_get_circle(): ar = np.ones((10, 10), dtype=int) aarc(get_circle(ar, radius=4), np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])) aarc(get_circle(PRETTY_ODD, radius=4, mode="val"), np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 3, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1])) aarc(get_circle(PRETTY_EVEN, radius=4, mode="val"), np.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]))
def dataset(request): """ Create 3D and 4D datasets for use with ``test_cube_inject_companions``. """ if request.param == "3D": cube = np.zeros((3, 5, 5)) psf = np.ones((1, 1)) elif request.param == "4D": cube = np.zeros((2, 3, 5, 5)) # lambda, frames, width, height psf = np.ones((2, 1, 1)) angles = np.array([0, 90, 180]) return cube, psf, angles
def test_get_annulus_segments(): arr = np.ones((10, 10)) # single segment, like the old get_annulus. Note the ``[0]``. res = get_annulus_segments(arr, 2, 3)[0] truth = (np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9]), np.array([3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 6, 7, 8, 9, 0, 1, 2, 7, 8, 9, 0, 1, 2, 7, 8, 9, 0, 1, 2, 3, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6])) aarc(res, truth) res = get_annulus_segments(arr, 2, 3, mode="val")[0] truth = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) aarc(res, truth) # multiple segments: res = get_annulus_segments(PRETTY_EVEN, 2, 3, nsegm=2) truth = [(np.array([0, 0, 0, 1, 1, 2, 3, 4, 4, 5, 5, 5]), np.array([3, 4, 5, 4, 5, 5, 5, 4, 5, 3, 4, 5])), (np.array([0, 0, 0, 1, 1, 2, 3, 4, 4, 5, 5, 5]), np.array([0, 1, 2, 0, 1, 0, 0, 0, 1, 0, 1, 2]))] aarc(res, truth) res = get_annulus_segments(PRETTY_EVEN, 2, 3, nsegm=3) truth = [(np.array([2, 3, 4, 4, 5, 5, 5]), np.array([5, 5, 4, 5, 3, 4, 5])), (np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1]), np.array([0, 1, 2, 3, 4, 5, 0, 1, 4, 5])), (np.array([2, 3, 4, 4, 5, 5, 5]), np.array([0, 0, 0, 1, 0, 1, 2]))] assert repr(res) == repr(truth) # TODO: cannot compare using `allclose`, as elements have variable length! # tuple as input: res = get_annulus_segments((6, 6), 2, 3, nsegm=3) assert repr(res) == repr(truth) # masked arr: res = get_annulus_segments(arr, 2, 3, mode="mask")[0] truth = np.array([[0., 0., 0., 1., 1., 1., 1., 0., 0., 0.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [1., 1., 1., 1., 0., 0., 1., 1., 1., 1.], [1., 1., 1., 0., 0., 0., 0., 1., 1., 1.], [1., 1., 1., 0., 0., 0., 0., 1., 1., 1.], [1., 1., 1., 1., 0., 0., 1., 1., 1., 1.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [0., 0., 0., 1., 1., 1., 1., 0., 0., 0.]]) aarc(res, truth) # tuple as input: res = get_annulus_segments((10, 10), 2, 3, mode="mask")[0] # masking a zeros array -> only zeros left! assert res.sum() == 0
from __future__ import division, print_function, absolute_import __author__ = "Ralf Farkas" from helpers import aarc, np from vip_hci.var.shapes import (frame_center, mask_circle, get_square, get_circle, get_annulus_segments, dist, matrix_scaling, reshape_matrix, get_ell_annulus, get_ellipse) PRETTY_ODD = np.array([ [1, 1, 1, 1, 1], [1, 2, 2, 2, 1], [1, 2, 3, 2, 1], [1, 2, 2, 2, 1], [1, 1, 1, 1, 1] ]) PRETTY_EVEN = np.array([ [1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 2, 1], [1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1], [1, 2, 2, 2, 2, 1], [1, 1, 1, 1, 1, 1] ]) def test_frame_center():
def test_get_annulus_segments(): arr = np.ones((10, 10)) # single segment, like the old get_annulus. Note the ``[0]``. res = get_annulus_segments(arr, 2, 3)[0] truth = (np.array([ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9 ]), np.array([ 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 6, 7, 8, 9, 0, 1, 2, 7, 8, 9, 0, 1, 2, 7, 8, 9, 0, 1, 2, 3, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6 ])) aarc(res, truth) res = get_annulus_segments(arr, 2, 3, mode="val")[0] truth = np.array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1. ]) aarc(res, truth) # multiple segments: res = get_annulus_segments(PRETTY_EVEN, 2, 3, nsegm=2) truth = [(np.array([0, 0, 0, 1, 1, 2, 3, 4, 4, 5, 5, 5]), np.array([3, 4, 5, 4, 5, 5, 5, 4, 5, 3, 4, 5])), (np.array([0, 0, 0, 1, 1, 2, 3, 4, 4, 5, 5, 5]), np.array([0, 1, 2, 0, 1, 0, 0, 0, 1, 0, 1, 2]))] aarc(res, truth) res = get_annulus_segments(PRETTY_EVEN, 2, 3, nsegm=3) truth = [(np.array([2, 3, 4, 4, 5, 5, 5]), np.array([5, 5, 4, 5, 3, 4, 5])), (np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1]), np.array([0, 1, 2, 3, 4, 5, 0, 1, 4, 5])), (np.array([2, 3, 4, 4, 5, 5, 5]), np.array([0, 0, 0, 1, 0, 1, 2]))] assert repr(res) == repr(truth) # TODO: cannot compare using `allclose`, as elements have variable length! # tuple as input: res = get_annulus_segments((6, 6), 2, 3, nsegm=3) assert repr(res) == repr(truth) # masked arr: res = get_annulus_segments(arr, 2, 3, mode="mask")[0] truth = np.array([[0., 0., 0., 1., 1., 1., 1., 0., 0., 0.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [1., 1., 1., 1., 0., 0., 1., 1., 1., 1.], [1., 1., 1., 0., 0., 0., 0., 1., 1., 1.], [1., 1., 1., 0., 0., 0., 0., 1., 1., 1.], [1., 1., 1., 1., 0., 0., 1., 1., 1., 1.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], [0., 0., 0., 1., 1., 1., 1., 0., 0., 0.]]) aarc(res, truth) # tuple as input: res = get_annulus_segments((10, 10), 2, 3, mode="mask")[0] # masking a zeros array -> only zeros left! assert res.sum() == 0
""" Tests for var/shapes.py """ from __future__ import division, print_function, absolute_import __author__ = "Ralf Farkas" from helpers import aarc, np from vip_hci.var.shapes import (frame_center, mask_circle, get_square, get_circle, get_annulus_segments, dist, matrix_scaling, reshape_matrix, get_ell_annulus, get_ellipse) PRETTY_ODD = np.array([[1, 1, 1, 1, 1], [1, 2, 2, 2, 1], [1, 2, 3, 2, 1], [1, 2, 2, 2, 1], [1, 1, 1, 1, 1]]) PRETTY_EVEN = np.array([[1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 2, 1], [1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1], [1, 2, 2, 2, 2, 1], [1, 1, 1, 1, 1, 1]]) def test_frame_center(): frames = 39 nlambda = 2 res44 = (1.5, 1.5) res55 = (2.0, 2.0) # 2D assert frame_center(np.zeros((4, 4))) == res44