def test_single_pixel_overlap_pillbox():
    # NB Grid uv-coordinates are np.arange(n_image) - n_image/2, so e.g. if
    # n_image = 8 then the co-ords in the u/x-direction are:
    # [-4, -3, -2, -1, 0, 1, 2, 3 ]
    n_image = 8
    support = 1
    uv = np.array([(-2., 0), (-2., 0)])
    # Real vis will be complex_, but we can substitute float_ for testing:
    vis_amplitude = 42.123
    vis = vis_amplitude * np.ones(len(uv), dtype=np.float_)
    kernel_func = conv_funcs.Pillbox(0.5)

    vis_grid, sampling_grid = convolve_to_grid(kernel_func,
                                               support=support,
                                               image_size=n_image,
                                               uv=uv, vis=vis,
                                               oversampling=None)
    assert vis_grid.sum() == vis.sum()
    expected_result = np.array(
        [[0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 1., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ]]
    )
    assert (expected_result * vis.sum() == vis_grid).all()
    assert (expected_result * len(uv) == sampling_grid).all()
def test_small_pillbox():
    n_image = 8
    support = 1

    uv = np.array([(-1.5, 0.5)])
    vis = np.ones(len(uv), dtype=np.float_)
    kernel_func = conv_funcs.Pillbox(0.55)

    grid, _ = convolve_to_grid(kernel_func,
                               support=support,
                               image_size=n_image,
                               uv=uv, vis=vis,
                               oversampling=None)
    assert grid.sum() == vis.sum()
    # This time we're on a mid-point, with a smaller pillbox
    # so we should get a 2x2 output
    v = 1. / 4.
    expected_result = np.array(
        # [-4, -3, -2, -1, 0, 1, 2, 3 ]
        [[0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., v, v, 0., 0., 0., 0., ],
         [0., 0., v, v, 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ]]
    )
    assert (expected_result == grid).all()
def test_multi_pixel_pillbox():
    n_image = 8
    support = 1

    uv = np.array([(-2., 0)])
    vis = np.ones(len(uv), dtype=np.float_)
    kernel_func = conv_funcs.Pillbox(1.1)

    vis_grid, sampling_grid = convolve_to_grid(kernel_func,
                                               support=support,
                                               image_size=n_image,
                                               uv=uv, vis=vis,
                                               oversampling=None)
    assert vis_grid.sum() == vis.sum()

    # Since uv is precisely on a sampling point, we'll get a
    # 3x3 pillbox
    v = 1. / 9.
    expected_result = np.array(
        [[0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., v, v, v, 0., 0., 0., 0., ],
         [0., v, v, v, 0., 0., 0., 0., ],
         [0., v, v, v, 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ],
         [0., 0., 0., 0., 0., 0., 0., 0., ]]
    )
    assert (expected_result == vis_grid).all()
    # In this case we expect sampling == vis, since we only have one vis == 1.0
    assert (expected_result == sampling_grid).all()
Exemple #4
0
def test_tophat_func():
    tophat3 = conv_funcs.Pillbox(half_base_width=3.0)
    io_pairs = np.array([
        [0.0, 1.0],
        [2.5, 1.0],
        [2.999, 1.0],
        [3.0, 0.0],
        [4.2, 0.],
    ])
    check_function_results_close(tophat3, io_pairs)
def test_regular_sampling_pillbox():
    testfunc = conv_funcs.Pillbox(half_base_width=1.1)
    support = 2
    oversampling = 1

    # Map subpixel offset to expected results
    expected_results = {}

    # No offset - expect 1's for all central 3x3 pixels:
    expected_results[(0., 0.)] = np.array([[0., 0., 0., 0., 0.],
                                           [0., 1., 1., 1., 0.],
                                           [0., 1., 1., 1., 0.],
                                           [0., 1., 1., 1., 0.],
                                           [0., 0., 0., 0., 0.]])

    # Tiny offset (less than pillbox overlap) - expect same:
    expected_results[(0.05, 0.05)] = expected_results[(0., 0.)]

    ## Now shift the pillbox just enough to +ve x that we drop a column
    expected_results[(0.15, 0.0)] = np.array([[0., 0., 0., 0., 0.],
                                              [0., 0., 1., 1., 0.],
                                              [0., 0., 1., 1., 0.],
                                              [0., 0., 1., 1., 0.],
                                              [0., 0., 0., 0., 0.]])

    ## And shift to -ve x:
    expected_results[(-0.15, 0.0)] = np.array([[0., 0., 0., 0., 0.],
                                               [0., 1., 1., 0., 0.],
                                               [0., 1., 1., 0., 0.],
                                               [0., 1., 1., 0., 0.],
                                               [0., 0., 0., 0., 0.]])

    ## Shift to +ve y:
    expected_results[(0.0, 0.15)] = np.array([[0., 0., 0., 0., 0.],
                                              [0., 0., 0., 0., 0.],
                                              [0., 1., 1., 1., 0.],
                                              [0., 1., 1., 1., 0.],
                                              [0., 0., 0., 0., 0.]])

    ## Shift to +ve y & +ve x:
    expected_results[(0.15, 0.15)] = np.array([[0., 0., 0., 0., 0.],
                                               [0., 0., 0., 0., 0.],
                                               [0., 0., 1., 1., 0.],
                                               [0., 0., 1., 1., 0.],
                                               [0., 0., 0., 0., 0.]])

    for offset, expected_array in expected_results.items():
        k = Kernel(
            kernel_func=testfunc,
            support=support,
            offset=offset,
            oversampling=oversampling,
            normalize=False,
        )
        assert (k.array == expected_array).all()
def test_bounds_checking():
    n_image = 8
    support = 2
    # n_image = 8 then the co-ords in the u/x-direction are:
    # [-4, -3, -2, -1, 0, 1, 2, 3 ]
    # Support = 2 takes this over the edge (since -3 -2 = -5):
    bad_uv = np.array([(-3., 0)])
    vis = np.ones(len(bad_uv), dtype=np.float_)
    kernel_func = conv_funcs.Pillbox(1.5)

    with pytest.raises(ValueError):
        grid = convolve_to_grid(
            kernel_func,
            support=support,
            image_size=n_image,
            uv=bad_uv,
            vis=vis,
            vis_weights=np.ones_like(vis),
        )

    grid, _ = convolve_to_grid(kernel_func,
                               support=support,
                               image_size=n_image,
                               uv=bad_uv,
                               vis=vis,
                               vis_weights=np.ones_like(vis),
                               raise_bounds=False)
    assert grid.sum() == 0.

    # Now check we're filtering indices in the correct order
    # The mixed
    good_uv = np.array([(0., 0.)])
    mixed_uv = np.array([(-3., 0), (0., 0.)])
    good_grid, _ = convolve_to_grid(kernel_func,
                                    support=support,
                                    image_size=n_image,
                                    uv=good_uv,
                                    vis=np.ones(len(good_uv), dtype=np.float_),
                                    vis_weights=np.ones(len(good_uv),
                                                        dtype=np.float_),
                                    raise_bounds=False)
    mixed_grid, _ = convolve_to_grid(kernel_func,
                                     support=support,
                                     image_size=n_image,
                                     uv=mixed_uv,
                                     vis=np.ones(len(mixed_uv),
                                                 dtype=np.float_),
                                     vis_weights=np.ones(len(mixed_uv),
                                                         dtype=np.float_),
                                     raise_bounds=False)

    assert (good_grid == mixed_grid).all()
def test_oversampled_pillbox_small():
    testfunc = conv_funcs.Pillbox(half_base_width=0.25)
    support = 1
    oversampling = 5

    # Map subpixel offset to expected results
    expected_results = {}

    expected_results[(0., 0.)] = np.array([
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
    ])

    expected_results[(0.4, 0.0)] = np.array([
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
    ])

    for offset, expected_array in expected_results.items():
        k = Kernel(
            kernel_func=testfunc,
            support=support,
            offset=offset,
            oversampling=oversampling,
            normalize=False,
        )

        assert (k.array == expected_array).all()
def test_oversampled_pillbox():
    testfunc = conv_funcs.Pillbox(half_base_width=0.7)
    support = 1
    oversampling = 3

    # Map subpixel offset to expected results
    expected_results = {}

    # No offset - expect 1's for all central 5x5 pixels, since cut-off is
    # just above 2/3:
    expected_results[(0., 0.)] = np.array([[0., 0., 0., 0., 0., 0., 0.],
                                           [0., 1., 1., 1., 1., 1., 0.],
                                           [0., 1., 1., 1., 1., 1., 0.],
                                           [0., 1., 1., 1., 1., 1., 0.],
                                           [0., 1., 1., 1., 1., 1., 0.],
                                           [0., 1., 1., 1., 1., 1., 0.],
                                           [0., 0., 0., 0., 0., 0., 0.]])
    # Same for tiny offset
    expected_results[(0.01, 0.01)] = expected_results[(0.00, 0.00)]

    # Displace towards -ve x a bit:
    expected_results[(-.05, 0.)] = np.array([[0., 0., 0., 0., 0., 0., 0.],
                                             [0., 1., 1., 1., 1., 0., 0.],
                                             [0., 1., 1., 1., 1., 0., 0.],
                                             [0., 1., 1., 1., 1., 0., 0.],
                                             [0., 1., 1., 1., 1., 0., 0.],
                                             [0., 1., 1., 1., 1., 0., 0.],
                                             [0., 0., 0., 0., 0., 0., 0.]])

    expected_results[(0.4, 0.)] = np.array([[0., 0., 0., 0., 0., 0., 0.],
                                            [0., 0., 0., 1., 1., 1., 1.],
                                            [0., 0., 0., 1., 1., 1., 1.],
                                            [0., 0., 0., 1., 1., 1., 1.],
                                            [0., 0., 0., 1., 1., 1., 1.],
                                            [0., 0., 0., 1., 1., 1., 1.],
                                            [0., 0., 0., 0., 0., 0., 0.]])
    for offset, expected_array in expected_results.items():
        k = Kernel(
            kernel_func=testfunc,
            support=support,
            offset=offset,
            oversampling=oversampling,
            normalize=False,
        )

        assert (k.array == expected_array).all()
def test_nearby_complex_vis():
    # Quick sanity check for multiple visibilities, kernel footprints
    # overlapping:
    n_image = 8
    support = 2

    uv = np.array([
        (-2., 1),
        (0., -1),
    ])
    # vis = np.ones(len(uv), dtype=np.float_)
    vis = np.ones(len(uv), dtype=np.complex_)
    vis_weights = np.ones_like(vis)
    kernel_func = conv_funcs.Pillbox(1.1)

    vis_grid, sampling_grid = convolve_to_grid(
        kernel_func,
        support=support,
        image_size=n_image,
        uv=uv,
        vis=vis,
        vis_weights=vis_weights,
    )
    # simplification true since weights are all 1:
    assert vis_grid.sum() == vis.sum()

    # Since uv is precisely on a sampling point, we'll get a
    # 3x3 pillbox
    v = 1. / 9. + 0j

    expected_sample_grid = np.array(
        # [-4, -3, -2, -1, 0, 1, 2, 3 ]
        [[0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., v, v, v, 0., 0.], [0., 0., 0., v, v, v, 0., 0.],
         [0., v, v, 2. * v, v, v, 0., 0.], [0., v, v, v, 0., 0., 0., 0.],
         [0., v, v, v, 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.]])
    # simplification true since weights are all 1:
    assert (expected_sample_grid == vis_grid).all()
    assert (expected_sample_grid == sampling_grid).all()
def test_bounds_checking():
    n_image = 8
    support = 2
    # n_image = 8 then the co-ords in the u/x-direction are:
    # [-4, -3, -2, -1, 0, 1, 2, 3 ]
    # Support = 2 takes this over the edge (since -3 -2 = -5):
    uv = np.array([(-3., 0)])
    vis = np.ones(len(uv), dtype=np.float_)
    kernel_func = conv_funcs.Pillbox(1.5)

    with pytest.raises(ValueError):
        grid = convolve_to_grid(kernel_func,
                                support=support,
                                image_size=n_image,
                                uv=uv, vis=vis,
                                oversampling=None)

    grid, _ = convolve_to_grid(kernel_func,
                               support=support,
                               image_size=n_image,
                               uv=uv, vis=vis,
                               raise_bounds=False
                               )
    assert grid.sum() == 0.
def test_multiple_complex_vis():
    # Quick sanity check for multiple visibilities, complex_ this time:
    n_image = 8
    support = 2

    uv = np.array([(-2., 1),
                   (1., -1),
                   ])
    # vis = np.ones(len(uv), dtype=np.float_)
    vis = np.ones(len(uv), dtype=np.complex_)
    kernel_func = conv_funcs.Pillbox(1.1)

    vis_grid, sampling_grid = convolve_to_grid(kernel_func,
                                               support=support,
                                               image_size=n_image,
                                               uv=uv, vis=vis,
                                               oversampling=None)
    assert vis_grid.sum() == vis.sum()

    # Since uv is precisely on a sampling point, we'll get a
    # 3x3 pillbox
    v = 1. / 9. + 0j

    expected_result = np.array(
        # [-4, -3, -2, -1, 0, 1, 2, 3 ]
        [[0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., v, v, v, 0.],
         [0., 0., 0., 0., v, v, v, 0.],
         [0., v, v, v, v, v, v, 0.],
         [0., v, v, v, 0., 0., 0., 0.],
         [0., v, v, v, 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0.]]
    )
    assert (expected_result == vis_grid).all()
    assert (expected_result == sampling_grid).all()