def test__transformer(
        self,
        visibilities_7,
        visibilities_noise_map_7,
        uv_wavelengths_7x2,
        sub_mask_2d_7x7,
    ):

        interferometer_7 = aa.Interferometer(
            visibilities=visibilities_7,
            noise_map=visibilities_noise_map_7,
            uv_wavelengths=uv_wavelengths_7x2,
            real_space_mask=sub_mask_2d_7x7,
            settings=aa.SettingsInterferometer(
                transformer_class=transformer.TransformerDFT
            ),
        )

        assert type(interferometer_7.transformer) == transformer.TransformerDFT

        interferometer_7 = aa.Interferometer(
            visibilities=visibilities_7,
            noise_map=visibilities_noise_map_7,
            uv_wavelengths=uv_wavelengths_7x2,
            real_space_mask=sub_mask_2d_7x7,
            settings=aa.SettingsInterferometer(
                transformer_class=transformer.TransformerNUFFT
            ),
        )

        assert type(interferometer_7.transformer) == transformer.TransformerNUFFT
Example #2
0
    def test__visibilities_and_model_are_different__no_masking__check_values_are_correct(
        self, ):

        real_space_mask = aa.Mask2D.manual(mask=[[False, False],
                                                 [False, False]],
                                           sub_size=1,
                                           pixel_scales=(1.0, 1.0))

        data = aa.Visibilities.manual_slim(
            visibilities=[1.0 + 2.0j, 3.0 + 4.0j])
        noise_map = aa.VisibilitiesNoiseMap.manual_slim(
            visibilities=[2.0 + 2.0j, 2.0 + 2.0j])

        interferometer = aa.Interferometer(
            visibilities=data,
            noise_map=noise_map,
            uv_wavelengths=np.ones(shape=(2, 2)),
            real_space_mask=real_space_mask,
        )

        model_visibilities = aa.Visibilities.manual_slim(
            visibilities=[1.0 + 2.0j, 3.0 + 3.0j])

        fit = aa.FitInterferometer(
            interferometer=interferometer,
            model_visibilities=model_visibilities,
            use_mask_in_fit=False,
        )

        assert (fit.visibilities.slim == np.array([1.0 + 2.0j,
                                                   3.0 + 4.0j])).all()

        assert (fit.noise_map.slim == np.array([2.0 + 2.0j, 2.0 + 2.0j])).all()

        assert (fit.signal_to_noise_map.slim == np.array(
            [0.5 + 1.0j, 1.5 + 2.0j])).all()

        assert (fit.model_visibilities.slim == np.array(
            [1.0 + 2.0j, 3.0 + 3.0j])).all()

        assert (fit.residual_map.slim == np.array([0.0 + 0.0j,
                                                   0.0 + 1.0j])).all()

        assert (fit.normalized_residual_map.slim == np.array(
            [0.0 + 0.0j, 0.0 + 0.5j])).all()

        assert (fit.chi_squared_map.slim == np.array([0.0 + 0.0j,
                                                      0.0 + 0.25j])).all()

        assert fit.chi_squared == 0.25
        assert fit.reduced_chi_squared == 0.25 / 2.0
        assert fit.noise_normalization == pytest.approx(
            4.0 * np.log(2 * np.pi * 2.0**2.0), 1.0e-4)
        assert fit.log_likelihood == -0.5 * (fit.chi_squared +
                                             fit.noise_normalization)
    def test__interferometer(self):

        visibilities_mask = np.full(fill_value=False, shape=(7, ))

        real_space_mask = aa.Mask2D.unmasked(shape_native=(7, 7),
                                             pixel_scales=0.1,
                                             sub_size=1)

        grid = aa.Grid2D.from_mask(mask=real_space_mask)

        pix = aa.pix.VoronoiMagnification(shape=(7, 7))

        sparse_grid = pix.sparse_grid_from_grid(grid=grid)

        mapper = pix.mapper_from_grid_and_sparse_grid(
            grid=grid,
            sparse_grid=sparse_grid,
            settings=aa.SettingsPixelization(use_border=False),
        )

        reg = aa.reg.Constant(coefficient=0.0)

        visibilities = aa.Visibilities.manual_slim(visibilities=[
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
        ])
        noise_map = aa.VisibilitiesNoiseMap.ones(shape_slim=(7, ))
        uv_wavelengths = np.ones(shape=(7, 2))

        interferometer = aa.Interferometer(
            visibilities=visibilities,
            noise_map=noise_map,
            uv_wavelengths=uv_wavelengths,
            real_space_mask=real_space_mask,
        )

        inversion = aa.Inversion(
            dataset=interferometer,
            mapper=mapper,
            regularization=reg,
            settings=aa.SettingsInversion(check_solution=False),
        )

        assert inversion.mapped_reconstructed_visibilities == pytest.approx(
            1.0 + 0.0j * np.ones(shape=(7, )), 1.0e-4)
        assert (np.imag(inversion.mapped_reconstructed_visibilities) <
                0.0001).all()
        assert (np.imag(inversion.mapped_reconstructed_visibilities) >
                0.0).all()
    def test__interferometer_linear_operator(self):

        real_space_mask = aa.Mask2D.unmasked(shape_native=(7, 7),
                                             pixel_scales=0.1,
                                             sub_size=1)

        grid = aa.Grid2D.from_mask(mask=real_space_mask)

        pix = aa.pix.Rectangular(shape=(7, 7))

        mapper = pix.mapper_from_grid_and_sparse_grid(
            grid=grid,
            sparse_grid=None,
            settings=aa.SettingsPixelization(use_border=False),
        )

        reg = aa.reg.Constant(coefficient=0.0)

        visibilities = aa.Visibilities.manual_slim(visibilities=[
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
            1.0 + 0.0j,
        ])
        noise_map = aa.VisibilitiesNoiseMap.ones(shape_slim=(7, ))
        uv_wavelengths = np.ones(shape=(7, 2))

        interferometer = aa.Interferometer(
            visibilities=visibilities,
            noise_map=noise_map,
            uv_wavelengths=uv_wavelengths,
            real_space_mask=real_space_mask,
            settings=aa.SettingsInterferometer(
                transformer_class=aa.TransformerNUFFT),
        )

        inversion = aa.Inversion(
            dataset=interferometer,
            mapper=mapper,
            regularization=reg,
            settings=aa.SettingsInversion(use_linear_operators=True,
                                          check_solution=False),
        )

        assert inversion.mapped_reconstructed_visibilities == pytest.approx(
            1.0 + 0.0j * np.ones(shape=(7, )), 1.0e-4)
        assert (np.imag(inversion.mapped_reconstructed_visibilities) <
                0.0001).all()
        assert (np.imag(inversion.mapped_reconstructed_visibilities) >
                0.0).all()
Example #5
0
    def test__visibilities_and_model_are_identical__inversion_included__changes_certain_properties(
        self, ):

        real_space_mask = aa.Mask2D.manual(mask=[[False, False],
                                                 [False, False]],
                                           sub_size=1,
                                           pixel_scales=(1.0, 1.0))

        data = aa.Visibilities.manual_slim(
            visibilities=[1.0 + 2.0j, 3.0 + 4.0j])
        noise_map = aa.VisibilitiesNoiseMap.manual_slim(
            visibilities=[2.0 + 2.0j, 2.0 + 2.0j])

        interferometer = aa.Interferometer(
            visibilities=data,
            noise_map=noise_map,
            uv_wavelengths=np.ones(shape=(2, 2)),
            real_space_mask=real_space_mask,
        )

        model_visibilities = aa.Visibilities.manual_slim(
            visibilities=[1.0 + 2.0j, 3.0 + 4.0j])

        inversion = mock.MockFitInversion(
            regularization_term=2.0,
            log_det_curvature_reg_matrix_term=3.0,
            log_det_regularization_matrix_term=4.0,
        )

        fit = aa.FitInterferometer(
            interferometer=interferometer,
            model_visibilities=model_visibilities,
            inversion=inversion,
            use_mask_in_fit=False,
        )

        assert fit.chi_squared == 0.0
        assert fit.reduced_chi_squared == 0.0
        assert fit.noise_normalization == pytest.approx(
            4.0 * np.log(2 * np.pi * 2.0**2.0), 1.0e-4)
        assert fit.log_likelihood == -0.5 * (fit.chi_squared +
                                             fit.noise_normalization)

        assert fit.log_likelihood_with_regularization == -0.5 * (
            fit.chi_squared + 2.0 + fit.noise_normalization)
        assert fit.log_evidence == -0.5 * (fit.chi_squared + 2.0 + 3.0 - 4.0 +
                                           fit.noise_normalization)
        assert fit.figure_of_merit == fit.log_evidence
    def test__new_interferometer_with_with_modified_visibilities(
        self, sub_mask_2d_7x7, uv_wavelengths_7x2
    ):

        interferometer = aa.Interferometer(
            visibilities=np.array([[1, 1]]),
            noise_map=1,
            uv_wavelengths=uv_wavelengths_7x2,
            real_space_mask=sub_mask_2d_7x7,
        )

        interferometer = interferometer.modified_visibilities_from_visibilities(
            visibilities=np.array([2 + 2j])
        )

        assert (interferometer.visibilities == np.array([[2 + 2j]])).all()
        assert interferometer.noise_map == 1
        assert (interferometer.uv_wavelengths == uv_wavelengths_7x2).all()
    def test__different_interferometer_without_mock_objects__customize_constructor_inputs(
        self, sub_mask_2d_7x7
    ):

        interferometer = aa.Interferometer(
            visibilities=aa.Visibilities.ones(shape_slim=(19,)),
            noise_map=2.0 * aa.Visibilities.ones(shape_slim=(19,)),
            uv_wavelengths=3.0 * np.ones((19, 2)),
            real_space_mask=sub_mask_2d_7x7,
        )

        real_space_mask = aa.Mask2D.unmasked(
            shape_native=(19, 19), pixel_scales=1.0, invert=True, sub_size=8
        )
        real_space_mask[9, 9] = False

        assert (interferometer.visibilities == 1.0 + 1.0j * np.ones((19,))).all()
        assert (interferometer.noise_map == 2.0 + 2.0j * np.ones((19,))).all()
        assert (interferometer.uv_wavelengths == 3.0 * np.ones((19, 2))).all()
    def test__dirty_properties(
        self,
        visibilities_7,
        visibilities_noise_map_7,
        uv_wavelengths_7x2,
        sub_mask_2d_7x7,
    ):

        interferometer = aa.Interferometer(
            visibilities=visibilities_7,
            noise_map=visibilities_noise_map_7,
            uv_wavelengths=uv_wavelengths_7x2,
            real_space_mask=sub_mask_2d_7x7,
        )

        assert interferometer.dirty_image.shape_native == (7, 7)
        assert (
            interferometer.transformer.image_from_visibilities(
                visibilities=interferometer.visibilities
            )
        ).all()

        assert interferometer.dirty_noise_map.shape_native == (7, 7)
        assert (
            interferometer.transformer.image_from_visibilities(
                visibilities=interferometer.noise_map
            )
        ).all()

        assert interferometer.dirty_signal_to_noise_map.shape_native == (7, 7)
        assert (
            interferometer.transformer.image_from_visibilities(
                visibilities=interferometer.signal_to_noise_map
            )
        ).all()

        assert interferometer.dirty_inverse_noise_map.shape_native == (7, 7)
        assert (
            interferometer.transformer.image_from_visibilities(
                visibilities=interferometer.inverse_noise_map
            )
        ).all()
Example #9
0
def _interferometer_from(
    fit: af.Fit,
    real_space_mask: Optional[aa.Mask2D] = None,
    settings_interferometer: Optional[aa.SettingsInterferometer] = None,
):
    """
    Returns a `Interferometer` object from an aggregator's `SearchOutput` class, which we call an 'agg_obj' to
    describe that it acts as the aggregator object for one result in the `Aggregator`. This uses the aggregator's
    generator outputs such that the function can use the `Aggregator`'s map function to to create a
    `Interferometer` generator.

    The `Interferometer` is created following the same method as the PyAutoGalaxy `Search` classes, including
    using the `SettingsInterferometer` instance output by the Search to load inputs of the `Interferometer`
    (e.g. psf_shape_2d).

    Parameters
    ----------
    fit : ImaginSearchOutput
        A PyAutoFit aggregator's SearchOutput object containing the generators of the results of PyAutoGalaxy
        model-fits.
    """

    data = fit.value(name="data")
    noise_map = fit.value(name="noise_map")
    uv_wavelengths = fit.value(name="uv_wavelengths")
    real_space_mask = real_space_mask or fit.value(name="real_space_mask")
    settings_interferometer = settings_interferometer or fit.value(
        name="settings_dataset"
    )

    interferometer = aa.Interferometer(
        visibilities=data,
        noise_map=noise_map,
        uv_wavelengths=uv_wavelengths,
        real_space_mask=real_space_mask,
    )

    interferometer = interferometer.apply_settings(settings=settings_interferometer)

    return interferometer
    def test__signal_to_noise_limit_below_max_signal_to_noise__signal_to_noise_map_capped_to_limit(
        self, sub_mask_2d_7x7, uv_wavelengths_7x2
    ):

        interferometer = aa.Interferometer(
            real_space_mask=sub_mask_2d_7x7,
            visibilities=aa.Visibilities(visibilities=np.array([1 + 1j, 1 + 1j])),
            noise_map=aa.VisibilitiesNoiseMap(
                visibilities=np.array([1 + 0.25j, 1 + 0.25j])
            ),
            uv_wavelengths=uv_wavelengths_7x2,
        )

        interferometer_capped = interferometer.signal_to_noise_limited_from(
            signal_to_noise_limit=2.0
        )

        assert (
            interferometer_capped.visibilities == np.array([1.0 + 1.0j, 1.0 + 1.0j])
        ).all()
        assert (
            interferometer_capped.noise_map == np.array([1.0 + 0.5j, 1.0 + 0.5j])
        ).all()
        assert (
            interferometer_capped.signal_to_noise_map == np.array([1.0 + 2.0j])
        ).all()

        interferometer_capped = interferometer.signal_to_noise_limited_from(
            signal_to_noise_limit=0.25
        )

        assert (
            interferometer_capped.visibilities == np.array([1.0 + 1.0j, 1.0 + 1.0j])
        ).all()
        assert (
            interferometer_capped.noise_map == np.array([4.0 + 4.0j, 4.0 + 4.0j])
        ).all()
        assert (
            interferometer_capped.signal_to_noise_map == np.array([0.25 + 0.25j])
        ).all()