def make_posterior_workflow(workflow, samples_files, config_file, label, rdir, posterior_file_dir='posterior_files', tags=None): """Adds jobs to a workflow that make a posterior file and subsequent plots. A posterior file is first created from the given samples file(s). The settings for extracting the posterior are set by the ``[extract_posterior]`` section. If that section has a ``parameters`` argument, then the parameters in the posterior file (and for use in all subsequent plotting) will be whatever that option is set to. Otherwise, the parameters in the posterior file will be whatever is common to all of the given samples file(s). Except for prior plots (which use the given inference config file), all subsequent jobs use the posterior file. The following are created: * **Summary table**: an html table created using the ``table_summary`` executable. The parameters to print in the table are retrieved from the ``table-params`` option in the ``[workflow-summary_table]`` section. Metadata may also be printed by adding a ``print-metadata`` option to that section. * **Summary posterior plots**: a collection of posterior plots to include in the summary page, after the summary table. The parameters to plot are read from ``[workflow-summary_plots]``. Parameters should be grouped together by providing ``plot-group-NAME = PARAM1[:LABEL1] PARAM2[:LABEL2]`` in that section, where ``NAME`` is a unique name for each group. One posterior plot will be created for each plot group. For clarity, only one or two parameters should be plotted in each summary group, but this is not enforced. Settings for the plotting executable are read from the ``plot_posterior_summary`` section; likewise, the executable used is read from ``plot_posterior_summary`` in the ``[executables]`` section. * **Sky maps**: if *both* ``create_fits_file`` and ``plot_skymap`` are listed in the ``[executables]`` section, then a ``.fits`` file and sky map plot will be produced. The sky map plot will be included in the summary plots. You must be running in a python 3 environment to create these. * **Prior plots**: plots of the prior will be created using the ``plot_prior`` executable. By default, all of the variable parameters will be plotted. The prior plots are added to ``priors/LALBEL/`` in the results directory, where ``LABEL`` is the given ``label``. * **Posterior plots**: additional posterior plots are created using the ``plot_posterior`` executable. The parameters to plot are read from ``[workflow-plot_params]`` section. As with the summary posterior plots, parameters are grouped together by providing ``plot-group-NAME`` options in that section. A posterior plot will be created for each group, and added to the ``posteriors/LABEL/`` directory. Plot settings are read from the ``[plot_posterior]`` section; this is kept separate from the posterior summary so that different settings can be used. For example, you may want to make a density plot for the summary plots, but a scatter plot colored by SNR for the posterior plots. Parameters ---------- samples_file : pycbc.workflow.core.FileList List of samples files to combine into a single posterior file. config_file : pycbc.worfkow.File The inference configuration file used to generate the samples file(s). This is needed to make plots of the prior. label : str Unique label for the plots. Used in file names. rdir : pycbc.results.layout.SectionNumber The results directory to save the plots to. posterior_file_dir : str, optional The name of the directory to save the posterior file to. Default is ``posterior_files``. tags : list of str, optional Additional tags to add to the file names. Returns ------- posterior_file : pycbc.workflow.File The posterior file that was created. summary_files : list List of files to go on the summary results page. prior_plots : list List of prior plots that will be created. These will be saved to ``priors/LABEL/`` in the resuls directory, where ``LABEL`` is the provided label. posterior_plots : list List of posterior plots that will be created. These will be saved to ``posteriors/LABEL/`` in the results directory. """ # the list of plots to go in the summary summary_files = [] if tags is None: tags = [] analysis_seg = workflow.analysis_time # figure out what parameters user wants to plot from workflow configuration # parameters for the summary plots summary_plot_params = get_plot_group(workflow.cp, 'summary_plots') # parameters to plot in large corner plots plot_params = get_plot_group(workflow.cp, 'plot_params') # get parameters for the summary tables table_params = workflow.cp.get_opt_tag('workflow', 'table-params', 'summary_table') # get any metadata that should be printed if workflow.cp.has_option('workflow-summary_table', 'print-metadata'): table_metadata = workflow.cp.get_opt_tag('workflow', 'print-metadata', 'summary_table') else: table_metadata = None # figure out if we are making a skymap make_skymap = ("create_fits_file" in workflow.cp.options("executables") and "plot_skymap" in workflow.cp.options("executables")) # make node for running extract samples posterior_file = create_posterior_files(workflow, samples_files, posterior_file_dir, analysis_seg=analysis_seg, tags=tags + [label])[0] # summary table summary_files += (make_inference_summary_table( workflow, posterior_file, rdir.base, parameters=table_params, print_metadata=table_metadata, analysis_seg=analysis_seg, tags=tags + [label]), ) # summary posteriors summary_plots = [] for group, params in summary_plot_params.items(): summary_plots += make_inference_posterior_plot( workflow, posterior_file, rdir.base, name='plot_posterior_summary', parameters=params, plot_prior_from_file=config_file, analysis_seg=analysis_seg, tags=tags + [label, group]) # sky map if make_skymap: # create the fits file fits_file = create_fits_file(workflow, posterior_file, rdir.base, analysis_seg=analysis_seg, tags=tags + [label])[0] # now plot the skymap skymap_plot = make_inference_skymap(workflow, fits_file, rdir.base, analysis_seg=analysis_seg, tags=tags + [label]) summary_plots += skymap_plot summary_files += list(layout.grouper(summary_plots, 2)) # files for priors summary section base = "priors/{}".format(label) prior_plots = make_inference_prior_plot( workflow, config_file, rdir[base], analysis_seg=workflow.analysis_time, tags=tags + [label]) layout.single_layout(rdir[base], prior_plots) # files for posteriors summary subsection base = "posteriors/{}".format(label) posterior_plots = [] for group, params in plot_params.items(): posterior_plots += make_inference_posterior_plot( workflow, posterior_file, rdir[base], parameters=params, plot_prior_from_file=config_file, analysis_seg=analysis_seg, tags=tags + [label, group]) layout.single_layout(rdir[base], posterior_plots) return posterior_file, summary_files, prior_plots, posterior_plots
def make_diagnostic_plots(workflow, diagnostics, samples_file, label, rdir, tags=None): """Makes diagnostic plots. Diagnostic plots are sampler-specific plots the provide information on how the sampler performed. All diagnostic plots use the output file produced by ``pycbc_inference`` as their input. Diagnostic plots are added to the results directory ``rdir/NAME`` where ``NAME`` is the name of the diagnostic given in ``diagnostics``. Parameters ---------- workflow : pycbc.workflow.core.Workflow The workflow to add the plotting jobs to. diagnostics : list of str The names of the diagnostic plots to create. See :py:func:`get_diagnostic_plots` for recognized names. samples_file : (list of) pycbc.workflow.File One or more samples files with which to create the diagnostic plots. If a list of files is provided, a diagnostic plot for each file will be created. label : str Event label for the diagnostic plots. rdir : pycbc.results.layout.SectionNumber Results directory layout. tags : list of str, optional Additional tags to add to the file names. Returns ------- dict : Dictionary of diagnostic name -> list of files giving the plots that will be created. """ if tags is None: tags = [] out = {} if not isinstance(samples_file, list): samples_file = [samples_file] if 'samples' in diagnostics: # files for samples summary subsection base = "samples/{}".format(label) samples_plots = [] for kk, sf in enumerate(samples_file): samples_plots += make_inference_samples_plot( workflow, sf, rdir[base], analysis_seg=workflow.analysis_time, tags=tags + [label, str(kk)]) out['samples'] = samples_plots layout.group_layout(rdir[base], samples_plots) if 'acceptance_rate' in diagnostics: # files for samples acceptance_rate subsection base = "acceptance_rate/{}".format(label) acceptance_plots = [] for kk, sf in enumerate(samples_file): acceptance_plots += make_inference_acceptance_rate_plot( workflow, sf, rdir[base], analysis_seg=workflow.analysis_time, tags=tags + [label, str(kk)]) out['acceptance_rate'] = acceptance_plots layout.single_layout(rdir[base], acceptance_plots) if 'mcmc_history' in diagnostics: # files for samples mcmc history subsection base = "mcmc_history/{}".format(label) history_plots = [] for kk, sf in enumerate(samples_file): history_plots += make_inference_plot_mcmc_history( workflow, sf, rdir[base], analysis_seg=workflow.analysis_time, tags=tags + [label, str(kk)]) out['mcmc_history'] = history_plots layout.single_layout(rdir[base], history_plots) if 'dynesty_run' in diagnostics: # files for dynesty run subsection base = "dynesty_run/{}".format(label) dynesty_run_plots = [] for kk, sf in enumerate(samples_file): dynesty_run_plots += make_inference_dynesty_run_plot( workflow, sf, rdir[base], analysis_seg=workflow.analysis_time, tags=tags + [label, str(kk)]) out['dynesty_run'] = dynesty_run_plots layout.single_layout(rdir[base], dynesty_run_plots) if 'dynesty_traceplot' in diagnostics: # files for samples dynesty tyrace plots subsection base = "dynesty_traceplot/{}".format(label) dynesty_trace_plots = [] for kk, sf in enumerate(samples_file): dynesty_trace_plots += make_inference_dynesty_trace_plot( workflow, sf, rdir[base], analysis_seg=workflow.analysis_time, tags=tags + [label, str(kk)]) out['dynesty_traceplot'] = dynesty_trace_plots layout.single_layout(rdir[base], dynesty_trace_plots) return out