{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Plots: UltraNestPlotter\n", "=======================\n", "\n", "This example illustrates how to plot visualization summarizing the results of a ultranest non-linear search using\n", "a `UltraNestPlotter`.\n", "\n", "Installation\n", "------------\n", "\n", "Because UltraNest is an optional library, you will likely have to install it manually via the command:\n", "\n", "`pip install ultranest`\n", "\n", "__Start Here Notebook__\n", "\n", "If any code in this script is unclear, refer to the `plot/start_here.ipynb` notebook." ] }, { "cell_type": "code", "metadata": {}, "source": [ "%matplotlib inline\n", "from pyprojroot import here\n", "workspace_path = str(here())\n", "%cd $workspace_path\n", "print(f\"Working Directory has been set to `{workspace_path}`\")\n", "\n", "from os import path\n", "\n", "import autofit as af\n", "import autolens as al\n", "import autolens.plot as aplt" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, lets create a result via ultranest by repeating the simple model-fit that is performed in \n", "the `modeling/start_here.py` example." ] }, { "cell_type": "code", "metadata": {}, "source": [ "dataset_name = \"simple__no_lens_light\"\n", "\n", "search = af.UltraNest(path_prefix=\"plot\", name=\"UltraNestPlotter\")\n", "\n", "dataset_path = path.join(\"dataset\", \"imaging\", dataset_name)\n", "\n", "dataset = al.Imaging.from_fits(\n", " data_path=path.join(dataset_path, \"data.fits\"),\n", " psf_path=path.join(dataset_path, \"psf.fits\"),\n", " noise_map_path=path.join(dataset_path, \"noise_map.fits\"),\n", " pixel_scales=0.1,\n", ")\n", "\n", "mask = al.Mask2D.circular(\n", " shape_native=dataset.shape_native, pixel_scales=dataset.pixel_scales, radius=3.0\n", ")\n", "\n", "dataset = dataset.apply_mask(mask=mask)\n", "\n", "# Lens:\n", "\n", "mass = af.Model(al.mp.Isothermal)\n", "shear = af.Model(al.mp.ExternalShear)\n", "\n", "lens = af.Model(al.Galaxy, redshift=0.5, mass=mass, shear=shear)\n", "\n", "# Source:\n", "\n", "bulge = af.Model(al.lp.Sersic)\n", "\n", "source = af.Model(al.Galaxy, redshift=1.0, bulge=bulge)\n", "\n", "# Overall Lens Model:\n", "\n", "model = af.Collection(galaxies=af.Collection(lens=lens, source=source))\n", "\n", "analysis = al.AnalysisImaging(dataset=dataset)\n", "\n", "result = search.fit(model=model, analysis=analysis)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now pass the samples to a `UltraNestPlotter` which will allow us to use ultranest's in-built plotting libraries to \n", "make figures.\n", "\n", "The ultranest readthedocs describes fully all of the methods used below \n", "\n", " - https://johannesbuchner.github.io/UltraNest/readme.html\n", " - https://johannesbuchner.github.io/UltraNest/ultranest.html#module-ultranest.plot\n", " \n", "In all the examples below, we use the `kwargs` of this function to pass in any of the input parameters that are \n", "described in the API docs." ] }, { "cell_type": "code", "metadata": {}, "source": [ "ultranest_plotter = aplt.UltraNestPlotter(samples=result.samples)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `cornerplot` method produces a triangle of 1D and 2D PDF's of every parameter in the model fit." ] }, { "cell_type": "code", "metadata": {}, "source": [ "ultranest_plotter.cornerplot()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `runplot` method shows how the estimates of the log evidence and other quantities progress as a function of\n", "iteration number during the ultranest model-fit." ] }, { "cell_type": "code", "metadata": {}, "source": [ "ultranest_plotter.runplot(\n", " span=None,\n", " logplot=False,\n", " kde=True,\n", " nkde=1000,\n", " color=\"blue\",\n", " plot_kwargs=None,\n", " label_kwargs=None,\n", " lnz_error=True,\n", " lnz_truth=None,\n", " truth_color=\"red\",\n", " truth_kwargs=None,\n", " max_x_ticks=8,\n", " max_y_ticks=3,\n", " use_math_text=True,\n", " mark_final_live=True,\n", " fig=None,\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `traceplot` method shows how the live points of each parameter converged alongside their PDF." ] }, { "cell_type": "code", "metadata": {}, "source": [ "ultranest_plotter.traceplot(\n", " span=None,\n", " quantiles=[0.025, 0.5, 0.975],\n", " smooth=0.02,\n", " post_color=\"blue\",\n", " post_kwargs=None,\n", " kde=True,\n", " nkde=1000,\n", " trace_cmap=\"plasma\",\n", " trace_color=None,\n", " trace_kwargs=None,\n", " connect=False,\n", " connect_highlight=10,\n", " connect_color=\"red\",\n", " connect_kwargs=None,\n", " max_n_ticks=5,\n", " use_math_text=False,\n", " labels=None,\n", " label_kwargs=None,\n", " show_titles=False,\n", " title_fmt=\".2f\",\n", " title_kwargs=None,\n", " truths=None,\n", " truth_color=\"red\",\n", " truth_kwargs=None,\n", " verbose=False,\n", " fig=None,\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finish." ] }, { "cell_type": "code", "metadata": {}, "source": [], "outputs": [], "execution_count": null } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 4 }