예제 #1
0
    def progress_report(self):
        """
        Reports the progress of the inference
        """
        self.get_intermediate_results()

        dead_samples = self.intermediate_results['rejected_points']
        live_samples = self.intermediate_results['live_points']
        likelihood = self.intermediate_results['logLikelihood']
        lnX = self.intermediate_results['lnX']
        if not ((dead_samples is None) or (likelihood is None) or
                (lnX is None)):
            fig = visualization.trace_plot(
                parameter_names=self._active_parameters,
                samples=dead_samples,
                live_samples=live_samples,
                likelihood=likelihood,
                lnX=lnX)
            fig_filepath = os.path.join(self._run_directory,
                                        'progress_report.pdf')
            msg = 'Saving progress report to {}'.format(fig_filepath)
            log.info(msg)
            fig.savefig(fig_filepath)
            if misc.is_notebook():
                ipd.clear_output()
                ipd.display(
                    ipd.Markdown(
                        "\n**Progress report:**"
                        '\nnumber of likelihood evaluations  {}'.format(
                            self._likelihood_evaluations_counter)))
                plt.show()
            else:
                print(msg)
예제 #2
0
    def posterior_report(self, sdigits=2, **kwargs):
        """
        Displays the best fit values and 1-sigma errors for each active
        parameter. Also produces a corner plot of the samples, which is
        saved to the run directory.

        If running on a jupyter-notebook, a nice LaTeX display is used, and
        the plot is shown.

        Parameters
        ----------
        sdigits : int
            The number of significant digits to be used
        """
        out = ''
        for param, pdict in self.posterior_summary.items():
            if misc.is_notebook():
                # Extracts LaTeX representation from astropy unit object
                out += r"\\ \text{{ {0}: }}\; ".format(param)
                out += q2tex(*map(pdict.get, ['median', 'errup', 'errlo']),
                             sdigits=sdigits)
                out += r"\\"
            else:
                out += r"{0}: ".format(param)
                md, errlo, errup = map(pdict.get, ['median', 'errlo', 'errup'])
                if isinstance(md, apu.Quantity):
                    unit = str(md.unit)
                    md, errlo, errup = map(lambda x: x.value,
                                           [md, errlo, errup])
                else:
                    unit = ""
                v, l, u = misc.adjust_error_intervals(md,
                                                      errlo,
                                                      errup,
                                                      sdigits=sdigits)
                out += r'{0} (-{1})/(+{2}) {3}\n'.format(v, l, u, unit)

        fig = self.corner_plot(**kwargs)
        fig.savefig(os.path.join(self._run_directory, 'corner_plot.pdf'))
        if misc.is_notebook():
            ipd.display(ipd.Markdown("\n**Posterior report:**"))
            plt.show()
            ipd.display(ipd.Math(out))
        else:
            # Restores linebreaks and prints
            print('Posterior report')
            print(out.replace(r'\n', '\n'))
예제 #3
0
 def evidence_report(self, sdigits=4):
     if misc.is_notebook():
         ipd.display(ipd.Markdown("**Evidence report:**"))
         out = r"\log\mathcal{{ Z }} = "
         out += q2tex(self.log_evidence, self.log_evidence_err)
         ipd.display(ipd.Math(out))
     else:
         print('Evidence report')
         print('logZ =', self.log_evidence, '±', self.log_evidence_err)
예제 #4
0
    def progress_report(self):
        """
        Reports the progress of the inference
        """
        # Try to call get_intermediate_results
        try:
            self.get_intermediate_results()
        # If this method is not implemented, show error instead and continue
        except NotImplementedError:
            print("Progress reports can only be made if the "
                  "'get_intermediate_results()'-method is overridden! "
                  "Skipping report.")
        # If this method is implemented, create progress report
        else:
            if mpirank!=0:
                return

            dead_samples = self.intermediate_results['rejected_points']
            live_samples = self.intermediate_results['live_points']
            likelihood = self.intermediate_results['logLikelihood']
            lnX = self.intermediate_results['lnX']

            if dead_samples is not None:
                fig = visualization.trace_plot(
                    parameter_names=self._active_parameters,
                    samples=dead_samples,
                    live_samples=live_samples,
                    likelihood=likelihood, lnX=lnX)
                fig_filepath = os.path.join(self._run_directory,
                                            'progress_report.pdf')
                msg = 'Saving progress report to {}'.format(fig_filepath)
                log.info(msg)
                fig.savefig(fig_filepath)
                if misc.is_notebook():
                    ipd.clear_output()
                    ipd.display(ipd.Markdown(
                        "\n**Progress report:**\nnumber of likelihood "
                        "evaluations  {}".format(
                            self._likelihood_evaluations_counter)))
                    plt.show()
                else:
                    print(msg)