PyAutoFit icon indicating copy to clipboard operation
PyAutoFit copied to clipboard

GridSearchResult native grid of property of model

Open Jammy2211 opened this issue 3 years ago • 0 comments

The GridSearchResult can give us a list or ndarray of quantites like the log_likelihood and log_evidence of every model on the grid:

    @property
    def log_likelihoods_native(self):
        """
        The maximum log likelihood of every grid search on a NumPy array whose shape is the native dimensions of the
        grid search.

        For example, for a 2x2 grid search the shape of the Numpy array is (2,2) and it is numerically ordered such
        that the first search's maximum likelihood (corresponding to unit priors (0.0, 0.0)) are in the first
        value (E.g. entry [0, 0]) of the NumPy array.
        """
        return self._list_to_native(lst=[result.log_likelihood for result in self.results])

However, it cannot give us an equivalent array for every estimate of a parameter of the model of every grid search.

In autolens, to compute such a quantity I currently have to do the rather nasty calculation:

    def instance_list_via_results_from(self, results):
        return [
            None
            if result.samples.median_pdf_instance is None
            else result.samples.median_pdf_instance
            for result in results
        ]

    @property
    def masses_native(self) -> List[float]:

        instance_list = self.instance_list_via_results_from(
            results=self.grid_search_result.results
        )

        return self.grid_search_result._list_to_native(
            [
                None if instance is None else instance.galaxies.subhalo.mass.mass_at_200
                for instance in instance_list
            ]
        )

The function instance_list_via_results_from is necessary to handle the None placeholders, which will have a value result.samples.median_pdf_instance=None if the grid cell is not finished.

This produces an array with the mass_at_200 of every model fit on the grid, which is identical to log_likelihoods_native except the values are now the masses instead of log_likelihoods.

Could we have a convience method in the GridSearchResult class that produces these native arrays given an input: (1) string for the path to the model component (or whatever you think the appropriate input is and; (ii) whether to use the median_pdf_model, maximum_log_likelihood or another property of Samples.

Placeholders of None must still be provided for unfinished results.

The same functionality will ultimately make its way into SensitivityResult.

Jammy2211 avatar Nov 12 '21 10:11 Jammy2211