コード例 #1
0
    def save_stochastic_outputs(self, paths: af.DirectoryPaths,
                                samples: af.OptimizerSamples):

        stochastic_log_evidences_json_file = path.join(
            paths.output_path, "stochastic_log_evidences.json")

        try:
            with open(stochastic_log_evidences_json_file, "r") as f:
                stochastic_log_evidences = np.asarray(json.load(f))
        except FileNotFoundError:
            instance = samples.max_log_likelihood_instance
            stochastic_log_evidences = self.stochastic_log_evidences_for_instance(
                instance=instance)

        if stochastic_log_evidences is None:
            return

        with open(stochastic_log_evidences_json_file, "w") as outfile:
            json.dump(
                [float(evidence) for evidence in stochastic_log_evidences],
                outfile)

        paths.save_object("stochastic_log_evidences", stochastic_log_evidences)

        visualizer = vis.Visualizer(visualize_path=paths.image_path)

        visualizer.visualize_stochastic_histogram(
            log_evidences=stochastic_log_evidences,
            max_log_evidence=np.max(samples.log_likelihood_list),
            histogram_bins=self.settings_lens.stochastic_histogram_bins,
        )
コード例 #2
0
def test__visualizes_ray_tracing__uses_configs(masked_imaging_7x7,
                                               tracer_x2_plane_7x7,
                                               include_2d_all, plot_path,
                                               plot_patch):

    if os.path.exists(plot_path):
        shutil.rmtree(plot_path)

    visualizer = vis.Visualizer(visualize_path=plot_path)

    visualizer.visualize_tracer(tracer=tracer_x2_plane_7x7,
                                grid=masked_imaging_7x7.grid,
                                during_analysis=False)

    plot_path = path.join(plot_path, "ray_tracing")

    assert path.join(plot_path, "subplot_tracer.png") in plot_patch.paths
    assert path.join(plot_path, "subplot_plane_images.png") in plot_patch.paths
    assert path.join(plot_path, "image_2d.png") in plot_patch.paths
    assert path.join(plot_path,
                     "plane_image_of_plane_1.png") in plot_patch.paths
    assert path.join(plot_path, "convergence_2d.png") in plot_patch.paths
    assert path.join(plot_path, "potential_2d.png") not in plot_patch.paths
    assert path.join(plot_path, "deflections_y_2d.png") not in plot_patch.paths
    assert path.join(plot_path, "deflections_x_2d.png") not in plot_patch.paths
    assert path.join(plot_path, "magnification_2d.png") in plot_patch.paths

    convergence = al.util.array_2d.numpy_array_2d_via_fits_from(
        file_path=path.join(plot_path, "fits", "convergence_2d.fits"), hdu=0)

    assert convergence.shape == (5, 5)
コード例 #3
0
    def test__visualize_stochastic_histogram(self, masked_imaging_7x7,
                                             plot_path, plot_patch):

        visualizer = vis.Visualizer(visualize_path=plot_path)

        visualizer.visualize_stochastic_histogram(
            log_evidences=[1.0, 2.0, 1.0, 2.0, 3.0, 2.5], max_log_evidence=3.0)
        assert (path.join(plot_path, "other", "stochastic_histogram.png")
                in plot_patch.paths)
コード例 #4
0
    def test__visualize_fit_interferometer__uses_configs(
        self,
        fit_interferometer_x2_plane_inversion_7x7,
        include_2d_all,
        plot_path,
        plot_patch,
    ):
        visualizer = vis.Visualizer(visualize_path=plot_path)

        visualizer.visualize_fit_interferometer(
            fit=fit_interferometer_x2_plane_inversion_7x7,
            during_analysis=True)

        plot_path = path.join(plot_path, "fit_interferometer")

        assert (path.join(plot_path, "subplot_fit_interferometer.png")
                in plot_patch.paths)
        assert path.join(plot_path,
                         "subplot_fit_real_space.png") in plot_patch.paths
        assert path.join(plot_path,
                         "subplot_fit_dirty_images.png") in plot_patch.paths

        assert path.join(plot_path, "visibilities.png") in plot_patch.paths
        assert path.join(plot_path, "noise_map.png") not in plot_patch.paths
        assert path.join(plot_path,
                         "signal_to_noise_map.png") not in plot_patch.paths
        assert path.join(plot_path,
                         "model_visibilities.png") in plot_patch.paths
        assert (path.join(plot_path, "real_residual_map_vs_uv_distances.png")
                not in plot_patch.paths)
        assert (path.join(plot_path,
                          "real_normalized_residual_map_vs_uv_distances.png")
                in plot_patch.paths)
        assert (path.join(plot_path,
                          "real_chi_squared_map_vs_uv_distances.png")
                in plot_patch.paths)

        print(plot_patch.paths)

        assert path.join(plot_path, "dirty_image_2d.png") in plot_patch.paths
        assert path.join(plot_path,
                         "dirty_noise_map_2d.png") not in plot_patch.paths
        assert (path.join(plot_path, "dirty_signal_to_noise_map_2d.png")
                not in plot_patch.paths)
        assert path.join(plot_path, "dirty_image_2d.png") in plot_patch.paths
        assert (path.join(plot_path, "dirty_real_residual_map_2d.png")
                not in plot_patch.paths)
        assert (path.join(plot_path, "dirty_normalized_residual_map_2d.png")
                in plot_patch.paths)
        assert path.join(plot_path,
                         "dirty_chi_squared_map_2d.png") in plot_patch.paths
コード例 #5
0
def test__visualizes_image_with_positions__uses_configs(
        image_7x7, positions_x2, include_2d_all, plot_path, plot_patch):

    if os.path.exists(plot_path):
        shutil.rmtree(plot_path)

    visualizer = vis.Visualizer(visualize_path=plot_path)

    visualizer.visualize_image_with_positions(image=image_7x7,
                                              positions=positions_x2)

    plot_path = path.join(plot_path, "positions")

    assert path.join(plot_path, "image_with_positions.png") in plot_patch.paths
コード例 #6
0
    def visualize(self, paths: af.DirectoryPaths, instance, during_analysis):

        instance = self.associate_hyper_images(instance=instance)
        tracer = self.tracer_for_instance(instance=instance)
        hyper_image_sky = self.hyper_image_sky_for_instance(instance=instance)
        hyper_background_noise = self.hyper_background_noise_for_instance(
            instance=instance)

        fit = self.fit_imaging_for_tracer(
            tracer=tracer,
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
        )

        visualizer = vis.Visualizer(visualize_path=paths.image_path)

        visualizer.visualize_imaging(imaging=self.imaging)
        visualizer.visualize_fit_imaging(fit=fit,
                                         during_analysis=during_analysis)
        visualizer.visualize_tracer(tracer=fit.tracer,
                                    grid=fit.grid,
                                    during_analysis=during_analysis)
        if fit.inversion is not None:
            visualizer.visualize_inversion(inversion=fit.inversion,
                                           during_analysis=during_analysis)

        visualizer.visualize_hyper_images(
            hyper_galaxy_image_path_dict=self.hyper_galaxy_image_path_dict,
            hyper_model_image=self.hyper_model_image,
            tracer=tracer,
        )

        if visualizer.plot_fit_no_hyper:
            fit = self.fit_imaging_for_tracer(
                tracer=tracer,
                hyper_image_sky=None,
                hyper_background_noise=None,
                use_hyper_scalings=False,
            )

            visualizer.visualize_fit_imaging(fit=fit,
                                             during_analysis=during_analysis,
                                             subfolders="fit_no_hyper")
コード例 #7
0
    def test__visualizes_fit_imaging__uses_configs(
            self, fit_imaging_x2_plane_inversion_7x7, include_2d_all,
            plot_path, plot_patch):

        if os.path.exists(plot_path):
            shutil.rmtree(plot_path)

        visualizer = vis.Visualizer(visualize_path=plot_path)

        visualizer.visualize_fit_imaging(
            fit=fit_imaging_x2_plane_inversion_7x7, during_analysis=False)

        plot_path = path.join(plot_path, "fit_imaging")

        assert path.join(plot_path,
                         "subplot_fit_imaging.png") in plot_patch.paths
        assert path.join(plot_path, "image_2d.png") in plot_patch.paths
        assert path.join(plot_path, "noise_map.png") not in plot_patch.paths
        assert path.join(plot_path,
                         "signal_to_noise_map.png") not in plot_patch.paths
        assert path.join(plot_path, "model_image.png") in plot_patch.paths
        assert path.join(plot_path, "residual_map.png") not in plot_patch.paths
        assert path.join(plot_path,
                         "normalized_residual_map.png") in plot_patch.paths
        assert path.join(plot_path, "chi_squared_map.png") in plot_patch.paths
        assert (path.join(plot_path, "subtracted_image_of_plane_0.png")
                in plot_patch.paths)
        assert (path.join(plot_path, "subtracted_image_of_plane_1.png")
                in plot_patch.paths)
        assert (path.join(plot_path, "model_image_of_plane_0.png")
                not in plot_patch.paths)
        assert (path.join(plot_path, "model_image_of_plane_1.png")
                not in plot_patch.paths)
        assert path.join(plot_path,
                         "plane_image_of_plane_0.png") in plot_patch.paths
        assert path.join(plot_path, "reconstruction.png") in plot_patch.paths

        image = al.util.array_2d.numpy_array_2d_from_fits(file_path=path.join(
            plot_path, "fits", "image_2d.fits"),
                                                          hdu=0)

        assert image.shape == (5, 5)
コード例 #8
0
    def visualize(self, paths, instance, during_analysis):

        tracer = self.tracer_for_instance(instance=instance)

        visualizer = vis.Visualizer(visualize_path=paths.image_path)