Jammy2211 / PyAutoGalaxy

PyAutoGalaxy: Open-Source Multiwavelength Galaxy Structure & Morphology
https://pyautogalaxy.readthedocs.io/
MIT License
27 stars 13 forks source link

Make Cosmology JSonable #112

Open Jammy2211 opened 1 year ago

Jammy2211 commented 1 year ago

The following classes are also pickled in the Analysis:

https://github.com/Jammy2211/PyAutoGalaxy/tree/feature/autofit_outputs/autogalaxy/cosmology

The pickling is here:

https://github.com/Jammy2211/PyAutoGalaxy/blob/feature/autofit_outputs/autogalaxy/analysis/analysis.py

  def save_attributes(self, paths: af.DirectoryPaths):
        """
        Before the model-fit via the non-linear search begins, this routine saves attributes of the `Analysis` object
        to the `pickles` folder such that they can be loaded after the analysis using PyAutoFit's database and aggregator
        tools.

        For this analysis the following are output:

        - The dataset's data.
        - The dataset's noise-map.
        - The settings associated with the dataset.
        - The settings associated with the inversion.
        - The settings associated with the pixelization.
        - The Cosmology.
        - The adapt dataset's model image and galaxy images, if used.

        It is common for these attributes to be loaded by many of the template aggregator functions given in the
        `aggregator` modules. For example, when using the database tools to reperform a fit, this will by default
        load the dataset, settings and other attributes necessary to perform a fit using the attributes output by
        this function.

        Parameters
        ----------
        paths
            The PyAutoFit paths object which manages all paths, e.g. where the non-linear search outputs are stored, visualization,
            and the pickled objects used by the aggregator output by this function.
        """
        dataset_path = paths._files_path / "dataset"

        self.dataset.output_to_fits(
            data_path=dataset_path / "data.fits",
            noise_map_path=dataset_path / "noise_map.fits",
            overwrite=True,
        )
        self.dataset.settings.output_to_json(file_path=dataset_path / "settings.json")

        self.settings_inversion.output_to_json(
            file_path=paths._files_path / "settings_inversion.json"
        )
        self.settings_pixelization.output_to_json(
            file_path=paths._files_path / "settings_pixelization.json"
        )
        paths.save_object("cosmology", self.cosmology)

        with open(paths._files_path / "cosmology.pickle", "wb") as f:
            pickle.dump(self.cosmology, f)

        adapt_path = paths._files_path / "adapt"

        if self.adapt_model_image is not None:
            self.adapt_model_image.output_to_fits(
                file_path=adapt_path / "adapt_model_image.fits", overwrite=True
            )

        if self.adapt_galaxy_image_path_dict is not None:
            for key, value in self.adapt_galaxy_image_path_dict.items():
                value.output_to_fits(
                    file_path=adapt_path / f"{key}.fits",
                    overwrite=True,
                )

Note these are on the feature/autofit_outputs/ branch.