gwastro / gwin

A python package for Bayesian inference of gravitational-wave data
GNU General Public License v3.0
14 stars 26 forks source link

Update plotting #71

Closed cdcapano closed 5 years ago

cdcapano commented 5 years ago

Updatesread_samples_from_cli, gwin_plot_posterior and gwin_plot_movie (other plotting codes will be addressed in another PR) to handle new sampler files. Also provides a way to print off a more informative help, based on the given input files.

Sampler-specific options are now added by to the argument parser by the sampler's IO class. Specifically, --iteration, --thin-start, --thin-interval, --thin-end, --walkers are added by the BaseMCMCIO class, since these are all options unique to MCMC (and presumably would not be understood by a nested sampler file). Likewise, once support is added for emcee_pt (separate PR), it will add a --temps argument.

These sampler-specific options do not appear when you run the program's --help/-h, but can be viewed by providing an input file and running --file-help/-H. For example, running -h on :

$ gwin_plot_posterior -h
usage: gwin_plot_posterior [-h] [--input-file FILE[:LABEL] [FILE[:LABEL] ...]]
                           [-H] [--parameters PARAM[:LABEL] [PARAM[:LABEL]
                           ...]] [--version] [--output-file OUTPUT_FILE]
                           [--verbose] [--plot-marginal]
                           [--marginal-percentiles MARGINAL_PERCENTILES [MARGINAL_PERCENTILES ...]]
                           [--plot-scatter] [--plot-density] [--plot-contours]
                           [--contour-percentiles CONTOUR_PERCENTILES [CONTOUR_PERCENTILES ...]]
                           [--mins PARAM:VAL [PARAM:VAL ...]]
                           [--maxs PARAM:VAL [PARAM:VAL ...]]
                           [--expected-parameters PARAM:VAL [PARAM:VAL ...]]
                           [--expected-parameters-color EXPECTED_PARAMETERS_COLOR]
                           [--plot-injection-parameters] [--z-arg Z_ARG]
                           [--vmin VMIN] [--vmax VMAX]
                           [--scatter-cmap SCATTER_CMAP]
                           [--density-cmap DENSITY_CMAP]
                           [--contour-color CONTOUR_COLOR] [--use-kombine-kde]

optional arguments:
  -h, --help            show this help message and exit
  --version             Prints version information.
  --output-file OUTPUT_FILE
                        Output plot path.
  --verbose             Be verbose

Arguments for loading results:
  Additional, file-specific arguments may also be provided, depending on
  what input-files are given. See --file-help for details.

  --input-file FILE[:LABEL] [FILE[:LABEL] ...]
                        Path to input HDF file(s). A label may be specified
                        for each input file to use for plots when multiple
                        files are specified.
  -H, --file-help       Based on the provided input-file(s), print all
                        available parameters that may be retrieved and all
                        possible functions on those parameters. Also print
                        available additional arguments that may be passed.
                        This option is like an advanced --help: if run, the
                        program will just print the information to screen,
                        then exit.
  --parameters PARAM[:LABEL] [PARAM[:LABEL] ...]
                        Name of parameters to load. If none provided will load
                        all of the model params in the input-file. If
                        provided, the parameters can be any of the model
                        params or posterior stats (loglikelihood, logprior,
                        etc.) in the input file(s), derived parameters from
                        them, or any function of them. If multiple files are
                        provided, any parameter common to all files may be
                        used. Syntax for functions is python; any math
                        functions in the numpy libary may be used. Can
                        optionally also specify a LABEL for each parameter. If
                        no LABEL is provided, PARAM will used as the LABEL. If
                        LABEL is the same as a parameter in
                        pycbc.waveform.parameters, the label property of that
                        parameter will be used (e.g., if LABEL were 'mchirp'
                        then $\mathcal{M}~(\mathrm{M}_\odot)$ would be used).
                        To see all possible parameters that may be used with
                        the given input file(s), as well as all avaiable
                        functions, run --file-help, along with one or more
                        input files.

Options for what plots to create and their formats.:
  --plot-marginal       Plot 1D marginalized distributions on the diagonal
                        axes.
[etc]

The thin options are not visible. Running -H with a samples file that was produced by emcee gives:

$ gwin_plot_posterior -H --input-file normal2d.hdf

Parameters available with this (these) input file(s):

logjacobian loglikelihood logprior x y

Available pycbc functions (see http://pycbc.org/pycbc/latest/html for
more details):

cartesian_to_spherical, cartesian_to_spherical_azimuthal,
cartesian_to_spherical_polar, cartesian_to_spherical_rho, chi_a,
chi_eff, chi_eff_from_spherical, chi_p, chi_p_from_spherical,
chi_p_from_xi1_xi2, chi_perp_from_mass1_mass2_xi2,
chi_perp_from_spinx_spiny, chirp_distance, det_tc,
dquadmon_from_lambda, eta_from_mass1_mass2, eta_from_q,
eta_from_tau0_tau3, final_mass_from_f0_tau, final_spin_from_f0_tau,
freq_from_final_mass_spin, invq_from_mass1_mass2, lambda_tilde,
mass1_from_mass2_eta, mass1_from_mchirp_eta, mass1_from_mchirp_q,
mass1_from_mtotal_eta, mass1_from_mtotal_q, mass1_from_tau0_tau3,
mass2_from_mass1_eta, mass2_from_mchirp_eta, mass2_from_mchirp_mass1,
mass2_from_mchirp_q, mass2_from_mtotal_eta, mass2_from_mtotal_q,
mass2_from_tau0_tau3, mass_from_knownmass_eta,
mchirp_from_mass1_mass2, mtotal_from_mass1_mass2,
mtotal_from_mchirp_eta, mtotal_from_tau0_tau3,
nltides_gw_phase_diff_isco, optimal_dec_from_detector,
optimal_ra_from_detector, phi1_from_phi_a_phi_s,
phi2_from_phi_a_phi_s, phi_a, phi_from_spinx_spiny, phi_s,
primary_mass, primary_spin, primary_xi, q_from_mass1_mass2, redshift,
secondary_mass, secondary_spin, secondary_xi, snr_from_loglr,
spherical_to_cartesian, spin1x_from_xi1_phi_a_phi_s,
spin1y_from_xi1_phi_a_phi_s, spin1z_from_mass1_mass2_chi_eff_chi_a,
spin2x_from_mass1_mass2_xi2_phi_a_phi_s,
spin2y_from_mass1_mass2_xi2_phi_a_phi_s,
spin2z_from_mass1_mass2_chi_eff_chi_a, tau0_from_mass1_mass2,
tau0_from_mtotal_eta, tau3_from_mass1_mass2, tau3_from_mtotal_eta,
tau_from_final_mass_spin, xi1_from_spin1x_spin1y,
xi2_from_mass1_mass2_spin2x_spin2y

Available numpy functions:

abs, absolute, add, arccos, arccosh, arcsin, arcsinh, arctan, arctan2,
arctanh, bitwise_and, bitwise_not, bitwise_or, bitwise_xor, cbrt,
ceil, conj, conjugate, copysign, cos, cosh, deg2rad, degrees, divide,
divmod, equal, exp, exp2, expm1, fabs, float_power, floor,
floor_divide, fmax, fmin, fmod, frexp, gcd, greater, greater_equal,
heaviside, hypot, invert, isfinite, isinf, isnan, isnat, lcm, ldexp,
left_shift, less, less_equal, log, log10, log1p, log2, logaddexp,
logaddexp2, logical_and, logical_not, logical_or, logical_xor,
maximum, minimum, mod, modf, multiply, negative, nextafter, not_equal,
positive, power, rad2deg, radians, reciprocal, remainder, right_shift,
rint, sign, signbit, sin, sinh, spacing, sqrt, square, subtract, tan,
tanh, true_divide, trunc

Recognized constants:

e euler_gamma inf nan pi

Python arthimetic (+ - * / // ** %), binary (&, |, etc.), and
comparison (>, <, >=, etc.) operators may also be used.

The following are additional command-line options that may be
provided, along with the input files that understand them:

usage: normal2d.hdf

optional arguments:
  --thin-start THIN_START
                        Sample number to start collecting samples to plot. If
                        none provided, will use the input file's `thin_start`
                        attribute.
  --thin-interval THIN_INTERVAL
                        Interval to use for thinning samples. If none
                        provided, will use the input file's `thin_interval`
                        attribute.
  --thin-end THIN_END   Sample number to stop collecting samples to plot. If
                        none provided, will use the input file's `thin_end`
                        attribute.
  --iteration ITERATION
                        Only retrieve the given iteration. To load the last
                        n-th sampe use -n, e.g., -1 will load the last
                        iteration. This overrides the thin-start/interval/end
                        options.
  --walkers WALKERS [WALKERS ...]
                        Only retrieve samples from the listed walkers. Default
                        is to retrieve from all walkers.

The options are shown, along with the parameters that may be plotted and functions that may be used.

This also allows for one or more of these options to be turned off. For example, gwin_plot_movie does not accept the thin- and iterations arguments, since these are figured out internally. These options are therefore turned off:

$ gwin_plot_movie -H --input-file normal2d.hdf 

<snip>

The following are additional command-line options that may be
provided, along with the input files that understand them:

usage: normal2d.hdf

optional arguments:
  --walkers WALKERS [WALKERS ...]
                        Only retrieve samples from the listed walkers. Default
                        is to retrieve from all walkers.

If no --input-file is given when --file-help/-H is run, then an error is raised.

pep8speaks commented 5 years ago

Hello @cdcapano! Thanks for updating the PR.

file_to_check.py:419:-92: W605 invalid escape sequence '*'

file_to_check.py:107:-81: W605 invalid escape sequence '*' file_to_check.py:178:-188: W605 invalid escape sequence '*' file_to_check.py:212:-90: W605 invalid escape sequence '*' file_to_check.py:252:-492: W605 invalid escape sequence '*' file_to_check.py:280:-457: W605 invalid escape sequence '*' file_to_check.py:312:-244: W605 invalid escape sequence '*' file_to_check.py:771:-48: W605 invalid escape sequence '*'

file_to_check.py:176:-336: W605 invalid escape sequence '*'

file_to_check.py:491:-177: W605 invalid escape sequence '*' file_to_check.py:531:-228: W605 invalid escape sequence '*' file_to_check.py:614:-186: W605 invalid escape sequence '*' file_to_check.py:734:-159: W605 invalid escape sequence '*'

Comment last updated on September 22, 2018 at 11:23 Hours UTC
cdcapano commented 5 years ago

Tests:

This PR also allows you to plot any parameter or likelihood statistic on any of the corner plots, or in the color bar using the parameters option, as these are now all stored in the samples group. For example:

gwin_plot_posterior \
   --verbose \
   --iteration -1 \
   --input-file gwin.hdf.checkpoint \
   --output-file scatter_with_stats.png  \
   --plot-scatter --plot-marginal \
   --z-arg 'mchirp_from_mass1_mass2(mass1, mass2):mchirp' \
   --parameters \
                 tc \
                 'snr_from_loglr(abs(H1_cplx_loglr)):$\rho_H$' \
                 'snr_from_loglr(abs(L1_cplx_loglr)):$\rho_L$' \
                 'snr_from_loglr(abs(loglr)):$\rho$'

Yields: scatter_with_stats.png

cdcapano commented 5 years ago

The travis tests fail because I haven't updated the unit tests yet. I'll do that in another PR, when the project is ready to be merged to master.

cdcapano commented 5 years ago

Need this now, so just merging... if there are issues, can always fix on the project branch.