Beispiel #1
0
def main():
    # ---
    # initialize logger

    logger = logging.getLogger("Main_log")
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(message)s")

    # read cli arguments
    cli = anc.get_args()

    #plot_folder = prepare_plot_folder(working_path)
    emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path)
    log_file = os.path.join(emcee_plots, 'Geweke_log.txt')
    flog = logging.FileHandler(log_file, 'w')
    flog.setLevel(logging.DEBUG)
    flog.setFormatter(formatter)
    logger.addHandler(flog)
    # log screen
    slog = logging.StreamHandler()
    slog.setLevel(logging.DEBUG)
    slog.setFormatter(formatter)
    logger.addHandler(slog)

    # computes mass conversion factor
    #m_factor = anc.mass_conversion_factor(cli.m_type)
    m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

    # set emcee and trades folder
    emcee_folder = cli.full_path
    trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
    # and best folder
    emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(
        emcee_folder, cli.temp_status)

    # get data from the hdf5 file
    parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(
        emcee_file, cli.temp_status)
    # print Memory occupation of ...
    anc.print_memory_usage(chains)

    nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(
        chains, cli.temp_status, cli.nburnin, completed_steps)
    logger.info(
        'nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' %
        (nfit, nwalkers, nruns, nburnin, nruns_sel))

    # set label and legend names
    kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

    chains_T, parameter_boundaries = anc.select_transpose_convert_chains(
        nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor,
        parameter_names_emcee, parameter_boundaries, chains)

    if (cli.temp_status):
        n_steps = completed_steps
    else:
        n_steps = nruns

    sel_steps = int(cli.sel_steps)
    if (sel_steps == 0):
        sel_steps = n_steps

    cols = 1 + int(np.rint(nwalkers / 40.))

    for ifit in range(0, nfit):
        logger.info('Parameter: %13s' % (parameter_names_emcee[ifit]))
        #fig = plt.figure(figsize=(12,12))
        fig = plt.figure(figsize=(6, 6))

        lower_interval, z_score = anc.geweke_test(chains_T[:, :, ifit],
                                                  start_frac=0.01,
                                                  n_sel_steps=sel_steps)

        ax = plt.subplot2grid((1, 1), (0, 0))
        for i_c in range(0, nwalkers):
            ax.plot(lower_interval,
                    z_score[:, i_c],
                    '.-',
                    label='walker %d' % (i_c + 1),
                    alpha=0.8)

        ax.axhline(2., color='lightgray')
        ax.axhline(-2., color='lightgray')
        ax.set_xlabel('steps (%s)' % (parameter_names_emcee[ifit].strip()))

        #plt.legend(loc='best',fontsize=9)
        #ax.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5), ncol=cols)

        fig.savefig(os.path.join(
            emcee_plots,
            'geweke_%03d_%s.png' % (ifit + 1, parameter_names_emcee[ifit])),
                    bbox_inches='tight',
                    dpi=200)
        plt.close(fig)
        logger.info('saved plot %s' % (os.path.join(
            emcee_plots, 'geweke_trace_pam_%s.png' %
            (parameter_names_emcee[ifit]))))

    logger.info('')

    return
def main():

  print 
  print ' ======================== '
  print ' TRADES+EMCEE CHAIN PLOTS'
  print ' ======================== '
  print

  # read cli arguments
  cli = anc.get_args()
  # computes mass conversion factor
  #m_factor, m_unit = anc.mass_conversion_factor_and_unit(cli.m_type)
  m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

  # set emcee and trades folder
  emcee_folder = cli.full_path
  trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
  # and best folder
  emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(emcee_folder, cli.temp_status)

  parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(emcee_file, cli.temp_status)

  # set label and legend names
  kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

  nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(chains, cli.temp_status, cli.nburnin, completed_steps)

  anc.print_memory_usage(chains)

  chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains(nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains)
  
  if(cli.use_thin or cli.use_thin > 0):
    chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps, chains_T_full_thinned = anc.thin_the_chains(cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False, full_chains_thinned=True)
    nburnin_plt = np.rint(nburnin / thin_steps).astype(int)
    nend = np.rint(nruns / thin_steps).astype(int)
  
  else:
    chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False, full_chains_thinned=False)
    nburnin_plt = nburnin
    nend = nruns
  
  #name_par, name_excluded = anc.get_sample_list(cli.sample_str, parameter_names_emcee)
  #sample_parameters, idx_sample = anc.pick_sample_parameters(flatchain_posterior_0, parameter_names_emcee, name_par = name_par, name_excluded = name_excluded)

  #flatchain_posterior_1 = flatchain_posterior_0

  # fix lambda? 
  #flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0,
                                         #parameter_names_emcee
                                         #)
  
  if(cli.boot_id > 0):
    flatchain_posterior_msun = anc.posterior_back_to_msun(m_factor,parameter_names_emcee,flatchain_posterior_0)
    boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id, parameter_names_emcee, flatchain_posterior_msun)
    logger.info('saved bootstrap like file: %s' %(boot_file))
    del flatchain_posterior_msun

  k = anc.get_auto_bins(flatchain_posterior_0)
  
  try:
    overplot = int(cli.overplot)
  except:
    overplot = None
  
  ## OPEN summary_parameters.hdf5 FILE
  s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r')
  
  if(overplot is not None):
  # sample_parameters
    ci_fitted = s_h5f['confidence_intervals/fitted/ci'][...]
    sample_parameters = s_h5f['parameters/0666/fitted/parameters'][...]
    sample_lgllhd = s_h5f['parameters/0666'].attrs['lgllhd']
    
    try:
      sample2_parameters = s_h5f['parameters/0667/fitted/parameters'][...]
      sample2_lgllhd = s_h5f['parameters/0667'].attrs['lgllhd']
    except:
      sample2_parameters = None
      sample2_lgllhd = None
      
    try:
      sample3_parameters = s_h5f['parameters/0668/fitted/parameters'][...]
      sample3_lgllhd = s_h5f['parameters/0668'].attrs['lgllhd']
    except:
      sample3_parameters = None
      sample3_lgllhd = None
    
    median_parameters = s_h5f['parameters/1051/fitted/parameters'][...]
    median_lgllhd = s_h5f['parameters/1051'].attrs['lgllhd']
    
    max_lnprob_parameters = s_h5f['parameters/2050/fitted/parameters'][...]
    max_lgllhd = s_h5f['parameters/2050'].attrs['lgllhd']
    
    try:
      mode_parameters = s_h5f['parameters/3051/fitted/parameters'][...]
      mode_lgllhd = s_h5f['parameters/3051'].attrs['lgllhd']
    except:
      mode_parameters = None
      mode_lgllhd = None
      
    overp_par = s_h5f['parameters/%04d/fitted/parameters' %(overplot)][...]
    overp_lgllhd = s_h5f['parameters/%04d' %(overplot)].attrs['lgllhd']
  
  #nfit = s_h5f['confidence_intervals/fitted'].attrs['nfit']
  ndata = s_h5f['confidence_intervals/fitted'].attrs['ndata']
  dof = s_h5f['confidence_intervals/fitted'].attrs['dof']
  
  s_h5f.close()

  emcee_plots = os.path.join(cli.full_path,'plots')
  if (not os.path.isdir(emcee_plots)):
    os.makedirs(emcee_plots)

  for i in range(0, nfit):
    if('Ms' in parameter_names_emcee[i]):
      conv_plot = m_factor
    else:
      conv_plot = 1.
    
    emcee_fig_file = os.path.join(emcee_plots, 'chain_%03d_%s.png' %(i+1, parameter_names_emcee[i].strip()))
    print ' %s' %(emcee_fig_file),
    #fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(12,12))
    fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(6,6))

    
    (counts, bins_val, patches) = axHist.hist(flatchain_posterior_0[:,i], bins=k,
                                              range=(flatchain_posterior_0[:,i].min(), flatchain_posterior_0[:,i].max()),
                                              orientation='horizontal',
                                              density=True, stacked=True,
                                              histtype='stepfilled',
                                              color='darkgrey', edgecolor='lightgray', 
                                              align='mid'
                                              )

    xpdf = scipy_norm.pdf(flatchain_posterior_0[:,i], loc = flatchain_posterior_0[:,i].mean(), scale = flatchain_posterior_0[:,i].std())
    idx = np.argsort(flatchain_posterior_0[:,i])
    axHist.plot(xpdf[idx], flatchain_posterior_0[idx,i], color='black', marker='None', ls='-.', lw=1.5, label='pdf')

    # chains after burn-in
    #axChain.plot(chains_T[:,:,i], '-', alpha=0.3)
    # chains with the burn-in
    if(cli.use_thin):
      axChain.plot(chains_T_full_thinned[:,:,i], '-', alpha=0.3)
    else:
      axChain.plot(chains_T_full[:,:,i], '-', alpha=0.3)

    axChain.axvspan(0, nburnin_plt, color='gray', alpha=0.45)
    axChain.axvline(nburnin_plt, color='gray', ls='-', lw=1.5)


    if(overplot is not None):
      if(mode_parameters is not None):
        # plot of mode (mean of higher peak/bin)
        axChain.axhline(mode_parameters[i]*conv_plot, color='red', ls='-', lw=2.1, alpha=1, label='mode')
      
      # plot of median
      axChain.axhline(median_parameters[i]*conv_plot, marker='None', c='blue',ls='-', lw=2.1, alpha=1.0, label='median fit')
      
      # plot of max_lnprob
      axChain.axhline(max_lnprob_parameters[i]*conv_plot, marker='None', c='black',ls='-', lw=1.1, alpha=1.0, label='max lnprob')
      
      if(sample_parameters is not None):
        # plot of sample_parameters
        axChain.axhline(sample_parameters[i]*conv_plot, marker='None', c='orange',ls='--', lw=2.3, alpha=0.77, label='picked: %12.7f' %(sample_parameters[i]))
      
      if(sample2_parameters is not None):
        # plot of sample2_parameters
        axChain.axhline(sample2_parameters[i]*conv_plot, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.77, label='close lgllhd: %12.7f' %(sample2_parameters[i]))
      
      if(sample3_parameters is not None):
        # plot of sample3_parameters
        axChain.axhline(sample3_parameters[i]*conv_plot, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.66, label='close lgllhd: %12.7f' %(sample3_parameters[i]))
      
      if(overplot not in [1050, 1051, 2050, 3050, 3051]):
        axChain.axhline(overp_par[i]*conv_plot, marker='None', c='black',ls='--', lw=2.5, alpha=0.6, label='overplot %d' %(overplot))
      
      
      # plot ci
      axChain.axhline(ci_fitted[i,0]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 15.865th (%.5f)' %(ci_fitted[i,0]*conv_plot))
      axChain.axhline(ci_fitted[i,1]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 84.135th (%.5f)' %(ci_fitted[i,1]*conv_plot))
    
    axChain.ticklabel_format(useOffset=False)
    xlabel = '$N_\mathrm{steps}$'
    if(cli.use_thin):
      xlabel = '$N_\mathrm{steps} \\times %d$' %(thin_steps)
    axChain.set_xlabel(xlabel)
    axChain.set_xlim([0, nend])
    axChain.set_ylabel(kel_labels[i])
    
    y_min = flatchain_posterior_0[:,i].min()
    y_max = flatchain_posterior_0[:,i].max()
    
    axChain.set_ylim([y_min, y_max])
    axChain.set_title('Full chain %s:=[%.3f , %.3f]' %(kel_labels[i], parameter_boundaries[i,0], parameter_boundaries[i,1]))
    plt.draw()

    axHist.ticklabel_format(useOffset=False)
    axHist.tick_params(direction='inout', labelleft=False)
    axHist.set_ylim([y_min, y_max])

    if(overplot is not None):
      if(mode_parameters is not None):
        # plot mode
        axHist.axhline(mode_parameters[i]*conv_plot, color='red', ls='-', lw=2.1, alpha=1, label='mode')
        
      # plot median
      axHist.axhline(median_parameters[i]*conv_plot, marker='None', c='blue',ls='-', lw=2.1, alpha=1.0, label='median fit')
      
      # plot of max_lnprob
      axHist.axhline(max_lnprob_parameters[i]*conv_plot, marker='None', c='black',ls='-', lw=1.1, alpha=1.0, label='max lnprob')
      
      if(sample_parameters is not None):
        # plot of sample_parameters
        axHist.axhline(sample_parameters[i]*conv_plot, marker='None', c='orange',ls='--', lw=2.3, alpha=0.77, label='picked: %12.7f' %(sample_parameters[i]*conv_plot))
      
      if(sample2_parameters is not None):
        # plot of sample2_parameters
        axHist.axhline(sample2_parameters[i]*conv_plot, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.77, label='close lgllhd: %12.7f' %(sample2_parameters[i]))
      
      if(sample3_parameters is not None):
        # plot of sample3_parameters
        axHist.axhline(sample3_parameters[i]*conv_plot, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.66, label='close lgllhd: %12.7f' %(sample3_parameters[i]))
      
      if(overplot not in [1050, 1051, 2050, 3050, 3051]):
        axHist.axhline(overp_par[i]*conv_plot, marker='None', c='black',ls='--', lw=2.5, alpha=0.8, label='overplot %d' %(overplot))
      
      # plot ci
      axHist.axhline(ci_fitted[i,0]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 15.865th (%.5f)' %(ci_fitted[i,0]*conv_plot))
      axHist.axhline(ci_fitted[i,1]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 84.135th (%.5f)' %(ci_fitted[i,1]*conv_plot))
    
    axHist.set_title('Distribution of posterior chain')
    axHist.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5))
    plt.draw()

    fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=150)
    print ' saved'
    print

  #fig = plt.figure(figsize=(12,12))
  fig = plt.figure(figsize=(6,6))
  
  # lnprob
  xlabel = '$N_\mathrm{steps}$'
  if(cli.use_thin):
    xlabel = '$N_\mathrm{steps} \\times %d$' %(thin_steps)
  
  ax = plt.subplot2grid((2,1), (0,0))
  ax.plot(lnprob_burnin.T, '-', alpha=0.3)

  if(overplot is not None):
    posterior_msun = anc.posterior_back_to_msun(m_factor,parameter_names_emcee,flatchain_posterior_0)
    post_sel, lnprob_sel = anc.select_within_all_ci(posterior_msun, ci_fitted[:,0:2], lnprob_burnin.T.reshape(-1))
    #lnprob_sel = lnprob_burnin.T.reshape((-1))
    lgllhd_med = np.percentile(lnprob_burnin.T.reshape(-1), 50., interpolation='midpoint')
    abs_dlg = np.abs(lnprob_sel - lgllhd_med)
    lgllhd_mad = np.percentile(abs_dlg, 50., interpolation='midpoint')
    
    #lnp_min = np.min(lnprob_sel)
    #lnp_max = np.max(lnprob_sel)
    lnp_min = lgllhd_med - lgllhd_mad
    lnp_max = lgllhd_med + lgllhd_mad
    print ' lgllhd_med & mad = ',lgllhd_med, lgllhd_mad
    print ' lnp_min = ',lnp_min, ' lnp_max = ',lnp_max
    print ' lnl_668 = ',sample3_lgllhd
  
    ax.axhline(lgllhd_med, color='black', ls='-', lw=1.6, alpha=0.77)
  
  #if(sample2_lgllhd is not None):
    #ax.axhline(sample2_lgllhd, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9)
  
    if(sample3_lgllhd is not None):
      ax.axhline(sample3_lgllhd, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.9)
    
    ax.axhspan(lnp_min, lnp_max, color='gray', alpha=0.77)
    ax.axhline(lnp_min, color='black', ls='--', lw=1.6, alpha=0.77)
    ax.axhline(lnp_max, color='black', ls='--', lw=1.6, alpha=0.77)
  
  min_lnp = np.min(lnprob_burnin.T, axis=0).min()
  max_lnp = np.max(lnprob_burnin.T, axis=0).max()
  y_min, y_max = anc.compute_limits(np.asarray([min_lnp, max_lnp]), 0.05)
  ax.set_ylim((y_min, y_max))
  ax.set_ylabel('lnprob')
  #ax.get_xaxis().set_visible(False)
  ax.set_xlabel(xlabel)
  
  # chi2r
  chi2r = -2.*(lnprob_burnin.T-ln_err_const)/np.float64(dof)
  
  ax = plt.subplot2grid((2,1), (1,0))
  ax.axhline(1.0, color='gray', ls='-')
  ax.plot(chi2r, '-', alpha=0.3)
  
  if(overplot is not None):
    c2r_med = -(2.*(lgllhd_med - ln_err_const))/np.float64(dof)
    c2r_smax = -(2.*(lnp_min - ln_err_const))/np.float64(dof)
    c2r_smin = -(2.*(lnp_max - ln_err_const))/np.float64(dof)
  
    print ' c2r_med = ',c2r_med
    print ' c2r_smin = ',c2r_smin, ' c2r_smax = ', c2r_smax
    
    ax.axhline(c2r_med, color='black', ls='-', lw=1.6, alpha=0.77)
    ax.axhspan(c2r_smin, c2r_smax, color='gray', alpha=0.77)
    ax.axhline(c2r_smin, color='black', ls='--', lw=1.6, alpha=0.77)
    ax.axhline(c2r_smax, color='black', ls='--', lw=1.6, alpha=0.77)
    #if(sample2_lgllhd is not None):
      #c2r_sample2 = -2.*(sample2_lgllhd - ln_err_const)/np.float64(dof)
      #ax.axhline(c2r_sample2, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9)
    if(sample3_lgllhd is not None):
      c2r_sample3 = -2.*(sample3_lgllhd - ln_err_const)/np.float64(dof)
      ax.axhline(c2r_sample3, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.9)
  
  c2r_min = -2.*(y_max - ln_err_const)/np.float64(dof)
  c2r_max = -2.*(y_min - ln_err_const)/np.float64(dof)
  ax.set_ylim((c2r_min, c2r_max))
  ax.set_ylabel('$\chi^{2}/\mathrm{dof}$')
  #ax.get_xaxis().set_visible(True)
  ax.set_xlabel(xlabel)
  
  fig.savefig(os.path.join(emcee_plots, 'emcee_lnprobability.png'), bbox_inches='tight', dpi=150)
  print ' %s saved' %(os.path.join(emcee_plots, 'emcee_lnprobability.png'))


  return
def main():
  # ---
  # initialize logger

  logger = logging.getLogger("Main_log")
  logger.setLevel(logging.DEBUG)
  formatter = logging.Formatter("%(asctime)s - %(message)s")

  # global variables

  label_separation=-0.90 # if uses this, comment ax.xyaxis.labelpad = label_pad
  label_pad = 12 # it uses this, comment ax.xyaxis.set_label_coords()...
  label_size = 8
  ticklabel_size = 4


  def set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_label, ticks_formatter, tick_fmt='%.4f'):
    ax.get_xaxis().set_visible(True)
    ax.xaxis.set_tick_params(labelsize=ticklabel_size)
    ax.xaxis.set_label_coords(0.5, label_separation)
    #ax.ticklabel_format(style='plain', axis='both', useOffset=False)
    plt.setp(ax.xaxis.get_majorticklabels(), rotation=70 )
    #ax.xaxis.labelpad = label_pad
    ax.set_xlabel(kel_label, fontsize=label_size)
    tick_step = (ticks_formatter[1] - ticks_formatter[0]) / ticks_formatter[2]
    ax.xaxis.set_ticks(np.arange(ticks_formatter[0], ticks_formatter[1], tick_step))
    tick_formatter = FormatStrFormatter(tick_fmt)
    ax.xaxis.set_major_formatter(tick_formatter)
    return
    
  def set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_label, ticks_formatter, tick_fmt='%.4f'):
    ax.get_yaxis().set_visible(True)
    ax.yaxis.set_tick_params(labelsize=ticklabel_size)
    ax.yaxis.set_label_coords(label_separation,0.5)
    #ax.ticklabel_format(style='plain', axis='both', useOffset=False)
    #ax.yaxis.labelpad = label_pad
    ax.set_ylabel(kel_label, fontsize=label_size)
    tick_step = (ticks_formatter[1] - ticks_formatter[0]) / ticks_formatter[2]
    ax.yaxis.set_ticks(np.arange(ticks_formatter[0], ticks_formatter[1], tick_step))
    tick_formatter = FormatStrFormatter(tick_fmt)
    ax.yaxis.set_major_formatter(tick_formatter)
    return

  print 
  print ' ================== '
  print ' CORRELATION PLOTS'
  print ' ================== '
  print

  # read cli arguments
  cli = anc.get_args()

  #plot_folder = prepare_plot_folder(working_path)
  emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path)
  log_file = os.path.join(emcee_plots, 'emcee_triangle_log.txt')
  flog = logging.FileHandler(log_file, 'w')
  flog.setLevel(logging.DEBUG)
  flog.setFormatter(formatter)
  logger.addHandler(flog)
  # log screen
  slog = logging.StreamHandler()
  slog.setLevel(logging.DEBUG)
  slog.setFormatter(formatter)
  logger.addHandler(slog)

  # computes mass conversion factor
  #m_factor = anc.mass_conversion_factor(cli.m_type)
  m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

  # set emcee and trades folder
  emcee_folder = cli.full_path
  trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
  # and best folder
  emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(emcee_folder, cli.temp_status)

  # get data from the hdf5 file
  parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(emcee_file, cli.temp_status)
  # print Memory occupation of ...
  anc.print_memory_usage(chains)

  nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(chains, cli.temp_status, cli.nburnin, completed_steps)
  logger.info('nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' %(nfit, nwalkers, nruns, nburnin, nruns_sel))

  # test label_separation
  #if (nfit <= 3): label_separation = -0.1
  if(nfit > 2):
    #label_separation = -0.1 - ( 0.075 * (nfit-2) ) # good for figsize=(12,12)
    label_separation = -0.15 - ( 0.125 * (nfit-2) ) # testing
  #else:
    #label_separation = -0.15

  #label_size = label_size - 1 * int(nfit / 5.)
  label_size = label_size - 1 * int(nfit / 2.5)

  # set label and legend names
  kel_plot_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

  chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains(nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains)

  chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False)
  
  flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0,
                                         parameter_names_emcee
                                        )

  if(cli.boot_id > 0):
    flatchain_posterior_msun = anc.posterior_back_to_msun(m_factor,parameter_names_emcee,flatchain_posterior_0)
    boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id, parameter_names_emcee, flatchain_posterior_msun)
    logger.info('saved bootstrap like file: %s' %(boot_file))
    del flatchain_posterior_msun

  k = anc.get_auto_bins(flatchain_posterior_0)
  
  if(cli.overplot is not None):
    
    if(cli.adhoc is not None):
      overp_names, read_par = anc.read_fitted_file(cli.adhoc)
      cli.overplot = 777
    else:
      ## OPEN summary_parameters.hdf5 FILE
      s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r')
      # take only the selected sample
      s_overplot = '%04d' %(cli.overplot)
      read_par = s_h5f['parameters/%s/fitted/parameters' %(s_overplot)][...]
      s_h5f.close()
      
    # fitted parameters has always Mp/Ms in Msun/Mstar, so it is needed to rescale it properly
    overp_par = read_par.copy()
    for ii in range(0, nfit):
      if('Ms' in parameter_names_emcee[ii]):
      #if('Ms' in overp_names[ii]):
        overp_par[ii] = overp_par[ii]*m_factor

  #fig, ax = plt.subplots(nrows = nfit-1, ncols=nfit, figsize=(12,12))
  #fig = plt.figure(figsize=(12,12))
  fig = plt.figure(figsize=(6,6))
  fig.subplots_adjust(hspace=0.05, wspace=0.05)

  for ix in range(0, nfit, 1):
    x_data = flatchain_posterior_0[:, ix]
    ##x_med = median_parameters[ix]
    
    x_min, x_max = anc.compute_limits(x_data, 0.05)
    if(x_min == x_max):
      x_min = parameter_boundaries[ix,0]
      x_max = parameter_boundaries[ix,1]

    #x_max_mean = mode_parameters[ix]

    for iy in range(nfit-1, -1, -1):
      y_data = flatchain_posterior_0[:, iy]
      y_min, y_max = anc.compute_limits(y_data, 0.05)
      if(y_min == y_max):
        y_min = parameter_boundaries[iy,0]
        y_max = parameter_boundaries[iy,1]
      
      #y_max_mean = mode_parameters[iy]
      
      if(iy > ix): # correlation plot
        logger.info('%s vs %s' %(parameter_names_emcee[ix], parameter_names_emcee[iy]))

        ax = plt.subplot2grid((nfit+1, nfit), (iy,ix))
        
        #hist2d_counts, xedges, yedges, image2d = ax.hist2d(x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, normed=True)
        hist2d_counts, xedges, yedges, image2d = ax.hist2d(\
          x_data, y_data, bins=k, 
          range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]],
          cmap=cm.gray_r,
          normed=False
          #density=False
          )
        
       #new_k = int(k/3)
        new_k = k
        hist2d_counts_2, xedges_2, yedges_2 = np.histogram2d(\
          x_data, y_data, bins=new_k, 
          range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]],
          #normed=True
          density=False
          )
        
        x_bins = [0.5*(xedges_2[i]+xedges_2[i+1]) for i in range(0, new_k)]
        y_bins = [0.5*(yedges_2[i]+yedges_2[i+1]) for i in range(0, new_k)]

        #ax.contour(x_bins, y_bins, hist2d_counts_2.T, 3, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7))
        nl = 5
        levels = [1.-np.exp(-0.5*ii) for ii in range(0,nl)] # 2D sigmas: 0sigma, 1sigma, 2sigma, 3sigma, ..
        #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7))
        #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=1.)
        #ax.contour(x_bins, y_bins, hist2d_counts_2.T, cmap=cm.viridis, linestyle='solid', linewidths=0.7)
        
        #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=0.7, normed=True)
        ax.contour(x_bins, y_bins, hist2d_counts_2.T,
                   nl, cmap=cm.viridis,
                   linestyles='solid', linewidths=0.5,
                   #normed=True
                   )
        
        
        if(cli.overplot is not None):
          # plot selected overplot sample
          ax.axvline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5)
          ax.axhline(overp_par[iy], color='C0', ls='--', lw=1.1, alpha=0.5)
        
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        if(iy == nfit-1):
          set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_plot_labels[ix], [xedges[0], xedges[-1], 4])
        if(ix == 0): 
          set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_plot_labels[iy], [yedges[0], yedges[-1], 5])
        
        ax.set_ylim([y_min, y_max])
        ax.set_xlim([x_min, x_max])
        plt.draw()
      
      elif(iy == ix): # distribution plot
        logger.info('%s histogram' %(parameter_names_emcee[ix]))

        ax = plt.subplot2grid((nfit+1, nfit), (ix,ix))
        if (ix == nfit-1):
          hist_orientation='horizontal'
        else:
          hist_orientation='vertical'
          
        
        idx = np.argsort(x_data)
        
        if(not cli.cumulative):
          # HISTOGRAM
          hist_counts, edges, patces = ax.hist(x_data, bins=k,
                                               range=[x_data.min(), x_data.max()], histtype='stepfilled', 
                                               color='darkgrey', 
                                               #edgecolor='lightgray',
                                               edgecolor='None',
                                               align='mid', 
                                               orientation=hist_orientation, 
                                               #normed=True,
                                               density=True,
                                               stacked=True
                                               )
          
        
        else:
          # CUMULATIVE HISTOGRAM
          hist_counts, edges, patces = ax.hist(x_data, bins=k,
                                               range=[x_data.min(), x_data.max()],
                                               histtype='stepfilled', 
                                               color='darkgrey', 
                                               #edgecolor='lightgray',
                                               edgecolor='None',
                                               align='mid', 
                                               orientation=hist_orientation, 
                                               density=True,
                                               stacked=True,
                                               cumulative=True
                                               )
          
        if (ix == nfit-1):
          ax.set_ylim([y_min, y_max])
          if(cli.overplot is not None):
            # plot selected overplot sample
            ax.axhline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5)
        else:
          ax.set_xlim([x_min, x_max])
          if(cli.overplot is not None):
            # plot selected overplot sample
            ax.axvline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5)
        
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        ax.set_title(kel_plot_labels[ix], fontsize=label_size)

        plt.draw()


  logger.info('saving plot')
  emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.png')
  fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=300)
  logger.info('png done')
  emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.pdf')
  fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=96)
  logger.info('pdf done')
  plt.close(fig)

  logger.info('')

  return
def main():
    print
    print ' TRADES: EMCEE confidence intervals'
    print

    cli = anc.get_args()

    # init trades
    pytrades_lib.pytrades.initialize_trades(os.path.join(cli.full_path, ''),
                                            '', 1)

    nfit, NB, bodies_file, id_fit, id_all, nfit_list, cols_list, case_list = anc.get_fitted(
        cli.full_path)
    ndata = pytrades_lib.pytrades.ndata
    nfree = pytrades_lib.pytrades.nfree
    dof = pytrades_lib.pytrades.dof

    # read emcee data
    emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(
        cli.full_path, cli.temp_status)
    # get data from the hdf5 file
    names_par, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(
        emcee_file, cli.temp_status)
    # print Memory occupation of ...
    anc.print_memory_usage(chains)

    nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(
        chains, cli.temp_status, cli.nburnin, completed_steps)

    #chains_T, parameter_boundaries = anc.select_transpose_convert_chains(nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, names_par, parameter_boundaries, chains)
    chains_T_full = np.zeros((nruns, nwalkers, nfit))
    for ii in xrange(0, nfit):
        chains_T_full[:, :, ii] = chains[:, :nruns, ii].T  # transpose

    chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(
        cli.use_thin,
        nburnin,
        nruns,
        nruns_sel,
        autocor_time,
        chains_T_full,
        lnprobability,
        burnin_done=False)

    # lambda fix
    flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0, names_par)

    # computes mass conversion factor
    #m_factor = anc.mass_conversion_factor(cli.m_type)
    MR_star = pytrades_lib.pytrades.mr_star
    m_factor_0, mass_unit = anc.mass_type_factor(Ms=1.0,
                                                 mtype=cli.m_type,
                                                 mscale=False)
    np.random.seed(seed=cli.seed)
    Ms_gaussian = MR_star[0, 0] + np.random.normal(
        0., 1., size=(np.shape(flatchain_posterior_0)[0])
    ) * MR_star[
        0,
        1]  # if exists an error on the mass, it creates a Normal distribution for the values and use it to re-scale mp/Ms to mp.
    m_factor_boot = m_factor_0 * Ms_gaussian  # given the factor from Msun to mass_unit it multiply it by the Normal Mstar.
    m_factor = m_factor_0 * MR_star[0, 0]

    # set label and legend names
    #kel_legends, labels_list = anc.keplerian_legend(names_par, cli.m_type)

    flatchain_posterior = flatchain_posterior_0.copy()
    for ifit in range(0, nfit):
        if ('Ms' in names_par[ifit]):
            flatchain_posterior[:,
                                ifit] = m_factor_0 * flatchain_posterior[:,
                                                                         ifit]
    posterior_file = os.path.join(cli.full_path, 'posterior.hdf5')
    p_h5f = h5py.File(posterior_file, 'w')
    p_h5f.create_dataset('posterior',
                         data=flatchain_posterior,
                         dtype=np.float64)
    p_h5f.create_dataset('loglikelihood',
                         data=lnprob_burnin.reshape((-1)),
                         dtype=np.float64)
    p_h5f['posterior'].attrs['nfit'] = nfit
    p_h5f['posterior'].attrs['nposterior'] = np.shape(flatchain_posterior)[0]
    p_h5f.create_dataset('parameter_names', data=names_par, dtype='S10')
    p_h5f.close()
    anc.print_both(' Saved posterior file: %s' % (posterior_file))

    top_header, header = anc.get_header(anc.percentile_val)

    # ==============================================================================
    # ==============================================================================
    # 2017-01-26 EMCEE NOW USED sqrt(e)cos(w), sqrt(e)sin(w)
    # GET INTERVALS
    # ==============================================================================
    # ==============================================================================
    def get_intervals(full_path,
                      id_sim,
                      names_par_in,
                      parameters_in,
                      flatchain_posterior_in,
                      derived_type=None,
                      full_output=False,
                      idx_sample=None,
                      summary_file_hdf5=None):

        names_trades = anc.emcee_names_to_trades(
            names_par_in)  # emcee to trades
        parameters_trades = anc.sqrte_to_e_fitting(
            parameters_in, names_par_in)  # emcee to trades

        names_par = names_par_in  # emcee kind
        parameters = parameters_in  # emcee kind
        flatchain_posterior = flatchain_posterior_in  # emcee kind

        loglhdx, checkx = pytrades_lib.pytrades.fortran_loglikelihood(
            np.array(parameters_trades, dtype=np.float64))
        loglhdx = loglhdx + ln_err_const

        out_folder = os.path.join(
            os.path.join(full_path, '%04d_sim' % (id_sim)), '')
        if (not os.path.isdir(out_folder)):
            os.makedirs(out_folder)
        out_file = os.path.join(out_folder, 'parameters_summary.txt')
        out = open(out_file, 'w')
        pytrades_lib.pytrades.path_change(out_folder)

        anc.print_both(' #', out)
        anc.print_both(' # --------------------------------- ', out)
        anc.print_both(' # PARAMETER VALUES -> %d' % (id_sim), out)
        fitness, lgllhd, check = pytrades_lib.pytrades.write_summary_files(
            id_sim, parameters_trades)

        kel_file, kep_elem = anc.elements(out_folder, id_sim, lmf=0)

        #sigma_par = anc.compute_intervals(flatchain_posterior, parameters, anc.percentile_val)
        sigma_par = anc.compute_sigma_hdi(flatchain_posterior,
                                          parameters)  # uses HDI
        sigma_par = sigma_par.T
        units_par = anc.get_units(names_par, mass_unit)

        if (not bool(check)):
            print 'WRTING WARNING FILE: %s' % (os.path.join(
                out_folder, 'WARNING.txt'))
            warn_o = open(os.path.join(out_folder, 'WARNING.txt'), 'w')
            warn_o.write(
                '*******\nWARNING: FITTED PARAMETERS COULD NOT BE PHYSICAL!\nWARNING: BE VERY CAREFUL WITH THIS PARAMETER SET!\n*******'
            )
            warn_o.close()

        nbins = anc.get_auto_bins(flatchain_posterior_0)

        names_derived, der_posterior = anc.compute_derived_posterior(
            names_par,
            kep_elem,
            id_fit,
            case_list,
            cols_list,
            flatchain_posterior,
            conv_factor=m_factor_boot)

        #der_posterior_T = der_posterior
        der_posterior_T = anc.derived_posterior_check(names_derived,
                                                      der_posterior)

        par_type = ''
        descr = ''
        if (str(derived_type).strip().lower() == 'median'):
            # MEDIAN PARAMETERS ID == 1050
            derived_par = np.percentile(der_posterior_T,
                                        50.,
                                        axis=0,
                                        interpolation='midpoint')
            par_type = 'MEDIAN:'
            descr = 'median of posterior and median of derived posterior'
        elif (str(derived_type).strip().lower() == 'mode'):
            # MODE-LIKE PARAMETERS -> id 3050
            #k = anc.get_bins(flatchain_posterior, rule='doane')

            der_bin, derived_par = anc.get_mode_parameters(
                der_posterior_T, nbins)
            par_type = 'MODE'
            descr = 'mode of posterior and mode of derived posterior'
        else:
            # ORIGINAL FITTING PARAMETERS ID == 0
            # or
            # MAX LNPROBABILITY -> id 2050
            names_derived, derived_par = anc.compute_derived_parameters(
                names_par,
                kep_elem,
                id_fit,
                case_list,
                cols_list,
                parameters,
                conv_factor=m_factor)
            derived_par, der_posterior_T = anc.adjust_derived_parameters(
                names_derived, derived_par, der_posterior_T)
            if (id_sim == 0):
                par_type = 'ORIGINAL FIT:'
                descr = 'initial set of parameters'
            elif (id_sim == 1051):
                par_type = 'MEDIAN PARAMETERS TO DERIVED:'
                descr = 'median of posterior and converted to derived parameter'
            elif (id_sim == 2050):
                par_type = 'MAX LNPROB'
            elif (id_sim == 3051):
                par_type = 'MODE PARAMETERS TO DERIVED:'
                descr = 'mode of posterior and converted to derived parameter'
            elif (id_sim == 666):
                par_type = 'SELECTED SAMPLE WITHIN HDI'
                # ***COMMENTED 2017-02-02: TO CHECK IF REALLY NEEDED
                #if(idx_sample is not None):
                #par_type = '%s <-> idx = %d' %(par_type, idx_sample)
                #derived_par = der_posterior_T[idx_sample, :]
                #for ider in range(0,np.shape(derived_par)[0]):
                ##print ider, names_derived[ider], names_derived[ider][0], names_derived[ider][1]
                #if(names_derived[ider][0] == 'm' and names_derived[ider][1] != 'A'):
                ##print 'doing'
                #derived_par[ider] = der_posterior_T[idx_sample, ider]*m_factor/m_factor_boot[idx_sample]
            elif (id_sim == 667):
                par_type = 'SELECTED SAMPLE CLOSE TO MEDIAN LGLLHD WITHIN POSTERIOR HDI'
                descr = ""
            elif (id_sim == 668):
                par_type = 'MAX LGLLHD WITHIN POSTERIOR HDI:'
                descr = "Select posterior within HDI and take the parameter set with higher loglikelihood."
            else:
                par_type = 'AD HOC'
                descr = "from input file"

        par_type = '%s %s' % (par_type, descr)
        #sigma_derived = anc.compute_intervals(der_posterior_T, derived_par, anc.percentile_val)
        sigma_derived = anc.compute_sigma_hdi(der_posterior_T, derived_par)
        sigma_derived = sigma_derived.T

        units_der = anc.get_units(names_derived, mass_unit)

        if (s_h5f is not None):
            s_id_sim = '%04d' % (id_sim)
            s_h5f.create_dataset('parameters/%s/fitted/parameters' %
                                 (s_id_sim),
                                 data=parameters,
                                 dtype=np.float64,
                                 compression='gzip')
            s_h5f.create_dataset('parameters/%s/fitted/names' % (s_id_sim),
                                 data=names_par,
                                 dtype='S10',
                                 compression='gzip')
            s_h5f.create_dataset('parameters/%s/fitted/units' % (s_id_sim),
                                 data=units_par,
                                 dtype='S15',
                                 compression='gzip')
            s_h5f.create_dataset('parameters/%s/fitted/sigma' % (s_id_sim),
                                 data=sigma_par.T,
                                 dtype=np.float64,
                                 compression='gzip')
            s_h5f['parameters/%s/fitted/sigma' %
                  (s_id_sim)].attrs['percentiles'] = anc.percentile_val

            s_h5f.create_dataset('parameters/%s/derived/parameters' %
                                 (s_id_sim),
                                 data=derived_par,
                                 dtype=np.float64,
                                 compression='gzip')
            s_h5f.create_dataset('parameters/%s/derived/names' % (s_id_sim),
                                 data=names_derived,
                                 dtype='S10',
                                 compression='gzip')
            s_h5f.create_dataset('parameters/%s/derived/units' % (s_id_sim),
                                 data=units_der,
                                 dtype='S15',
                                 compression='gzip')
            s_h5f.create_dataset('parameters/%s/derived/sigma' % (s_id_sim),
                                 data=sigma_derived.T,
                                 dtype=np.float64,
                                 compression='gzip')
            s_h5f['parameters/%s/derived/sigma' %
                  (s_id_sim)].attrs['percentiles'] = anc.percentile_val

            s_h5f['parameters/%s' %
                  (s_id_sim)].attrs['info'] = '%s ==> %s' % (s_id_sim,
                                                             par_type)
            s_h5f['parameters/%s' % (s_id_sim)].attrs['fitness'] = fitness
            s_h5f['parameters/%s' % (s_id_sim)].attrs['lgllhd'] = lgllhd
            s_h5f['parameters/%s' % (s_id_sim)].attrs['check'] = check
            if (idx_sample is not None):
                s_h5f['parameters/%s' %
                      (s_id_sim)].attrs['idx_sample'] = idx_sample

        #print '\nComputed sigma_par with shape ',np.shape(sigma_par)
        #print 'Computed sigma_derived with shape ',np.shape(sigma_derived)
        anc.print_both('\n# SUMMARY: %s' % (par_type), out)
        anc.print_both('# FITTED PARAMETERS', out)
        anc.print_parameters(top_header, header, names_par, units_par,
                             parameters, sigma_par, out)

        anc.print_both('# DERIVED PARAMETERS', out)
        anc.print_parameters(top_header, header, names_derived, units_der,
                             derived_par, sigma_derived, out)
        out.close()

        if (full_output):
            return out_folder, names_derived, der_posterior_T
        else:
            return out_folder
# ==============================================================================
# ==============================================================================

# ==============================================================================
## CREATE A HDF5 FILE WITH CONFIDNCE INTERVALS AND ALL THE SUMMARY PARAMETERS
# ==============================================================================

    summary_file = os.path.join(cli.full_path, 'summary_parameters.hdf5')
    s_h5f = h5py.File(summary_file, 'w')

    ### COMPUTE CONFIDENCE INTERVALS OF THE FITTED PARAMETER DISTRIBUTIONS
    #ci_fitted = np.percentile(flatchain_posterior_0, anc.percentile_val[2:], axis=0, interpolation='midpoint') # (n_percentile-2 x nfit) ==> skip 1st and 2nd items, the 68.27th and 50th
    # ==============================================================================
    # HDI INSTEAD OF CREDIBLE INTERVALS
    # ==============================================================================
    nbins = anc.get_auto_bins(flatchain_posterior_0)
    hdi_ci, mode_parameters = anc.compute_hdi_full(flatchain_posterior_0,
                                                   mode_output=True)
    ci_fitted = hdi_ci.T
    print ' shape: hdi_ci = ', np.shape(hdi_ci), ' ci_fitted = ', np.shape(
        ci_fitted)
    # hdi_ci: nfit x nci
    # ci_fitted: nci x nfit
    # nci -> -1sigma(0) +1sigma(1) -2sigma(2) +2sigma(3) -3sigma(4) +3sigma(5)

    #sys.exit()

    units_par = anc.get_units(names_par, mass_unit)

    s_h5f.create_dataset('confidence_intervals/fitted/ci',
                         data=ci_fitted.T,
                         dtype=np.float64,
                         compression='gzip')
    s_h5f.create_dataset('confidence_intervals/fitted/names',
                         data=names_par,
                         dtype='S10',
                         compression='gzip')
    s_h5f.create_dataset('confidence_intervals/fitted/units',
                         data=units_par,
                         dtype='S15',
                         compression='gzip')
    s_h5f.create_dataset('confidence_intervals/fitted/percentiles',
                         data=np.array(anc.percentile_val[2:]),
                         dtype=np.float64,
                         compression='gzip')  # now it not true...

    s_h5f['confidence_intervals/fitted'].attrs['nfit'] = nfit
    s_h5f['confidence_intervals/fitted'].attrs['nfree'] = nfree
    s_h5f['confidence_intervals/fitted'].attrs['ndata'] = ndata
    s_h5f['confidence_intervals/fitted'].attrs['dof'] = dof
    # ==============================================================================

    # ==============================================================================
    ## ORIGINAL FITTING PARAMETERS ID == 0
    # ==============================================================================
    # save initial_fitting parameters into array
    original_fit_parameters = pytrades_lib.pytrades.fitting_parameters  # INITIAL PARAMETER SET (NEEDED ONLY TO HAVE THE PROPER ARRAY/VECTOR)
    #folder_0 = get_intervals(cli.full_path, 0, names_par, original_fit_parameters, flatchain_posterior_0, derived_type=None, summary_file_hdf5=s_h5f)
    # WARNING: original_fit_parameters from TRADES have to be converted to emcee parameters:
    # (ecosw,esinw) => (sqrecosw, sqrtesinw)
    trades_names = anc.emcee_names_to_trades(names_par)
    original_parameters = anc.e_to_sqrte_fitting(original_fit_parameters,
                                                 trades_names)
    folder_0 = get_intervals(cli.full_path,
                             0,
                             names_par,
                             original_parameters,
                             flatchain_posterior_0,
                             derived_type=None,
                             summary_file_hdf5=s_h5f)
    # ==============================================================================

    print
    print

    # ==============================================================================
    ## MAX LNPROBABILITY AND PARAMETERS -> id 2050
    # ==============================================================================
    max_lnprob, max_lnprob_parameters, max_lnprob_perc68, max_lnprob_confint = anc.get_maxlnprob_parameters(
        lnprob_burnin, chains_T, flatchain_posterior_0)
    max_id1, max_id2 = anc.get_max_indices(lnprob_burnin)
    folder_2050, names_derived, der_posterior = get_intervals(
        cli.full_path,
        2050,
        names_par,
        max_lnprob_parameters,
        flatchain_posterior_0,
        derived_type=None,
        full_output=True,
        summary_file_hdf5=s_h5f)
    units_der = anc.get_units(names_derived, mass_unit)
    # write out the derived names and posterior into an hdf5 file
    der_post_file = os.path.join(cli.full_path, 'derived_posterior.hdf5')
    h5f = h5py.File(der_post_file, 'w')
    h5f.create_dataset('derived_names',
                       data=names_derived,
                       dtype='S10',
                       compression='gzip')
    h5f.create_dataset('derived_posterior',
                       data=der_posterior,
                       dtype=np.float64,
                       compression='gzip')
    h5f.create_dataset('units_derived',
                       data=units_der,
                       dtype='S15',
                       compression='gzip')
    h5f.close()
    # ==============================================================================

    ### COMPUTE CONFIDENCE INTERVALS OF THE DERIVED PARAMETER DISTRIBUTIONS
    #ci_derived = np.percentile(der_posterior, anc.percentile_val[2:], axis=0, interpolation='midpoint') # (n_percentile-1 x nder) ==> skip first item, the 68.27th
    # ==============================================================================
    # HDI INSTEAD OF CREDIBLE INTERVALS
    # ==============================================================================
    #npost_der, nder = np.shape(der_posterior)
    #k_der = anc.get_auto_bins(der_posterior)
    hdi_ci_derived = anc.compute_hdi_full(der_posterior, mode_output=False)
    ci_derived = hdi_ci_derived.T

    s_h5f.create_dataset('confidence_intervals/derived/ci',
                         data=ci_derived.T,
                         dtype=np.float64,
                         compression='gzip')
    s_h5f.create_dataset('confidence_intervals/derived/names',
                         data=names_derived,
                         dtype='S10',
                         compression='gzip')
    s_h5f.create_dataset('confidence_intervals/derived/units',
                         data=units_der,
                         dtype='S15',
                         compression='gzip')
    s_h5f.create_dataset('confidence_intervals/derived/percentiles',
                         data=np.array(anc.percentile_val[2:]),
                         dtype=np.float64,
                         compression='gzip')
    # ==============================================================================
    print
    print

    # ==============================================================================
    ## MEDIAN PARAMETERS ID == 1050
    # ==============================================================================
    median_parameters, median_perc68, median_confint = anc.get_median_parameters(
        flatchain_posterior_0)
    folder_1050 = get_intervals(cli.full_path,
                                1050,
                                names_par,
                                median_parameters,
                                flatchain_posterior_0,
                                derived_type='median',
                                summary_file_hdf5=s_h5f)
    ## MEDIAN PARAMETERS ID == 1051
    folder_1051 = get_intervals(cli.full_path,
                                1051,
                                names_par,
                                median_parameters,
                                flatchain_posterior_0,
                                derived_type=None,
                                summary_file_hdf5=s_h5f)
    # ==============================================================================

    print
    print

    # ==============================================================================
    # select n_samples from the posterior within the CI
    # ==============================================================================
    if (cli.n_samples > 0):
        anc.print_both(' Selecting %d samples from the posterior ...' %
                       (cli.n_samples))
        sys.stdout.flush()
        samples_fit_par = anc.take_n_samples(flatchain_posterior_0,
                                             ci_fitted[0:2, :],
                                             n_samples=cli.n_samples)
        samples_fit_par[
            0, :] = median_parameters  # first sample as the median of the posterior
        anc.print_both(' Running TRADES and computing the T0s and RVs ...')
        samples_file = os.path.join(cli.full_path, 'samples_ttra_rv.hdf5')
        anc.print_both(' Saving into %s' % (samples_file))
        smp_h5 = h5py.File(samples_file, 'w')
        save_ttra_and_rv_from_samples(samples_fit_par, names_par, NB,
                                      cli.n_samples, smp_h5)
        #tra_gr = smp_h5.create_group('T0')
        #for key in ttra_samples.keys():
        #tra_gr.create_dataset(key, data=ttra_samples[key], dtype=np.float64, compression='gzip')
        #rv_gr =  smp_h5.create_group('RV')
        #for key in rv_samples.keys():
        #rv_gr.create_dataset(key, data=rv_samples[key], dtype=np.float64, compression='gzip')
        #rv_gr['time_rv_mod'].attrs['tepoch'] = pytrades_lib.pytrades.tepoch
        smp_h5.close()
        anc.print_both(' ... done')
        sys.stdout.flush()
    #sys.exit()
# ==============================================================================

    print
    print

    # ==============================================================================
    ## MODE-LIKE PARAMETERS -> id 3050
    # ==============================================================================
    ## take the mean of 5 bin centered to the higher bin

    #anc.print_both('nbins = %d' %(nbins))
    #sys.stdout.flush()
    #mode_bin, mode_parameters = anc.get_mode_parameters(flatchain_posterior_0, nbins)
    # mode_parameters computed at the beginning with hdi

    if (np.any(np.isnan(mode_parameters))):
        print 'Some values are Nan, skip the mode parameters'
    else:
        folder_3050 = get_intervals(cli.full_path,
                                    3050,
                                    names_par,
                                    mode_parameters,
                                    flatchain_posterior_0,
                                    derived_type='mode',
                                    summary_file_hdf5=s_h5f)
        ## MODE-LIKE PARAMETERS -> id 3051
        folder_3051 = get_intervals(cli.full_path,
                                    3051,
                                    names_par,
                                    mode_parameters,
                                    flatchain_posterior_0,
                                    derived_type=None,
                                    summary_file_hdf5=s_h5f)
# ==============================================================================

    print
    print

    # ==============================================================================
    # ONE SAMPLE PARAMETER SET --> 666
    # ==============================================================================
    name_par, name_excluded = anc.get_sample_list(cli.sample_str, names_par)
    sample_parameters, idx_sample = anc.pick_sample_parameters(
        flatchain_posterior_0,
        names_par,
        name_par=name_par,
        name_excluded=name_excluded,
        post_ci=ci_fitted[0:2, :])
    if (sample_parameters is not None):
        folder_666 = get_intervals(cli.full_path,
                                   666,
                                   names_par,
                                   sample_parameters,
                                   flatchain_posterior_0,
                                   idx_sample=idx_sample,
                                   summary_file_hdf5=s_h5f)
        s_h5f['parameters/%04d' % (666)].attrs['par_selection'] = name_par
        if (name_excluded is not None):
            s_h5f['parameters/%04d' %
                  (666)].attrs['par_excluded'] = name_excluded
    else:
        print 'NONE SAMPLE PARAMETERS!!!'
# ==============================================================================

# ==============================================================================
## SELECT AD HOC PARAMETERS:
# ==============================================================================
#adhoc_par = median_parameters.copy()
##adhoc_par[10:] = mode_parameters[10:].copy()
#adhoc_par[12] = mode_parameters[12].copy()
#if(cli.overplot is not None):
    if (cli.adhoc is not None):
        print cli.overplot, cli.adhoc
        adhoc_names, adhoc_par_trades = anc.read_fitted_file(cli.adhoc)
        adhoc_par = anc.e_to_sqrte_fitting(adhoc_par_trades, adhoc_names)
        folder_777 = get_intervals(cli.full_path,
                                   777,
                                   names_par,
                                   adhoc_par,
                                   flatchain_posterior_0,
                                   derived_type=777,
                                   summary_file_hdf5=s_h5f)
# ==============================================================================

# ==============================================================================
# select the sample within post_ci and close to median lgllhd --> 667
# ==============================================================================
    sample2_parameters, sample2_lgllhd = anc.get_sample_by_sorted_lgllhd(
        flatchain_posterior_0,
        lnprob_burnin.T,
        #post_ci = ci_fitted[0:2,:]
        post_ci=ci_fitted.T)
    folder_667 = get_intervals(cli.full_path,
                               667,
                               names_par,
                               sample2_parameters,
                               flatchain_posterior_0,
                               derived_type=667,
                               summary_file_hdf5=s_h5f)
    # ==============================================================================

    # ==============================================================================
    # another kind of selection: parameter set within HDI, then take the max(loglikelihood) --> 668
    # ==============================================================================
    name_par, name_excluded = anc.get_sample_list(cli.sample_str, names_par)
    #sample3_parameters, sample3_lgllhd = anc.get_sample_by_par_and_lgllhd(flatchain_posterior_0,
    #lnprob_burnin.T,
    #names_par,
    #post_ci = ci_fitted[0:2,:],
    #name_par= name_par)
    sample3_parameters, sample3_lgllhd = \
                               anc.select_maxlglhd_with_hdi(flatchain_posterior_0,
                                                            #ci_fitted[0:2,:],
                                                            ci_fitted.T,
                                                            lnprob_burnin.T
                                                            )
    folder_668 = get_intervals(cli.full_path,
                               668,
                               names_par,
                               sample3_parameters,
                               flatchain_posterior_0,
                               derived_type=668,
                               summary_file_hdf5=s_h5f)
    # ==============================================================================

    s_h5f.close()

    print

    # ==============================================================================
    # print into file CONFIDENCE INTERVALS of fitted and derived parameters
    # ==============================================================================
    ci_file = os.path.join(cli.full_path, 'confidence_intervals.dat')
    oci = open(ci_file, 'w')
    anc.print_both('\n# SUMMARY:\n# CONFIDENCE INTERVALS', oci)

    anc.print_both('## FITTED PARAMETERS', oci)
    #anc.print_confidence_intervals(anc.percentile_val[2:], conf_interv=ci_fitted, name_parameters=names_par, unit_parameters=units_par, output=oci)
    anc.print_hdi(conf_interv=ci_fitted,
                  name_parameters=names_par,
                  unit_parameters=units_par,
                  output=oci)

    anc.print_both('## DERIVED PARAMETERS', oci)
    #anc.print_confidence_intervals(anc.percentile_val[2:], conf_interv=ci_derived, name_parameters=names_derived, unit_parameters=units_der, output=oci)
    anc.print_hdi(conf_interv=ci_derived,
                  name_parameters=names_derived,
                  unit_parameters=units_der,
                  output=oci)

    oci.close()
    # ==============================================================================

    pytrades_lib.pytrades.deallocate_variables()

    return
Beispiel #5
0
def main():
  # ---
  # initialize logger

  logger = logging.getLogger("Main_log")
  logger.setLevel(logging.DEBUG)
  formatter = logging.Formatter("%(asctime)s - %(message)s")

  # read cli arguments
  cli = anc.get_args()

  #plot_folder = prepare_plot_folder(working_path)
  emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path)
  log_file = os.path.join(emcee_plots, 'GelmanRubin_log.txt')
  flog = logging.FileHandler(log_file, 'w')
  flog.setLevel(logging.DEBUG)
  flog.setFormatter(formatter)
  logger.addHandler(flog)
  # log screen
  slog = logging.StreamHandler()
  slog.setLevel(logging.DEBUG)
  slog.setFormatter(formatter)
  logger.addHandler(slog)

  # computes mass conversion factor
  #m_factor = anc.mass_conversion_factor(cli.m_type)
  m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

  # set emcee and trades folder
  emcee_folder = cli.full_path
  trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
  # and best folder
  emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(emcee_folder, cli.temp_status)

  # get data from the hdf5 file
  parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(emcee_file, cli.temp_status)
  # print Memory occupation of ...
  anc.print_memory_usage(chains)

  nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(chains, cli.temp_status, cli.nburnin, completed_steps)
  logger.info('nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' %(nfit, nwalkers, nruns, nburnin, nruns_sel))

  # set label and legend names
  kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

  chains_T, parameter_boundaries = anc.select_transpose_convert_chains(nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains)

  if(cli.temp_status):
    n_steps = completed_steps
  else:
    n_steps = nruns
  sel_steps = int(cli.sel_steps)
  if (sel_steps == 0):
    sel_steps = n_steps

  steps = np.linspace(start=0, stop=n_steps, num=sel_steps, endpoint=True, dtype=np.int)
  steps[0] = 10
  #print steps
  sel_steps = steps.shape[0]

  gr_Rc_1 = np.ones((sel_steps, nfit)) + 99.
  gr_Rc_2 = np.ones((sel_steps, nfit)) + 99.
  gr_Rc_pyorbit = np.ones((sel_steps, nfit)) + 99.
  gr_Rc_pymc = np.ones((sel_steps, nfit)) + 99.

  for ifit in range(0, nfit):
    logger.info('Parameter: %13s' %(parameter_names_emcee[ifit]))
    #fig = plt.figure(figsize=(12,12))
    fig = plt.figure(figsize=(6,6))
    ax = plt.subplot2grid((1, 1), (0,0))
    
    for istep in range(0,sel_steps):
      #print 'istep',istep
      #time0 = time.time()
      #gr_Rc_1[istep,ifit] = anc.GelmanRubin_test_1(chains_T[:steps[istep], :, ifit])
      #if(istep == sel_steps-1):
        #LBo_d, LBo_h, LBo_m, LBo_s = anc.computation_time(time.time()-time0)
        #logger.info('steps = %6d for %13s ==> Gelman-Rubin test:  LBo time = %2d m %6.3f s' %(steps[istep], parameter_names_emcee[ifit], LBo_m, LBo_s))
      
      time0 = time.time()
      gr_Rc_2[istep,ifit] = anc.GelmanRubin(chains_T[:steps[istep], :, ifit])
      if(istep == sel_steps-1):
        LBo_d, LBo_h, LBo_m, LBo_s = anc.computation_time(time.time()-time0)
        logger.info('steps = %6d for %13s ==> Gelman-Rubin test:  LBo time = %2d m %6.3f s' %(steps[istep], parameter_names_emcee[ifit], LBo_m, LBo_s))
      
      time0 = time.time()
      gr_Rc_pyorbit[istep,:] = anc.GelmanRubin_PyORBIT(chains_T[:steps[istep], :, ifit])
      if(istep == sel_steps-1):
        LMa_d, LMa_h, LMa_m, LMa_s = anc.computation_time(time.time()-time0)
        logger.info('steps = %6d for %13s ==> Gelman-Rubin test:  LMa time = %2d m %6.3f s' %(steps[istep], parameter_names_emcee[ifit], LMa_m, LMa_s))

      time0 = time.time()
      gr_Rc_pymc[istep,:] = np.sqrt(anc.GelmanRubin_pymc(chains_T[:steps[istep], :, ifit].T))
      if(istep == sel_steps-1):
        pymc_d, pymc_h, pymc_m, pymc_s = anc.computation_time(time.time()-time0)
        logger.info('steps = %6d for %13s ==> Gelman-Rubin test: pymc time = %2d m %6.3f s' %(steps[istep], parameter_names_emcee[ifit], pymc_m, pymc_s))

    ax.axhline(1.01, color='gray')
    #ax.plot(steps, gr_Rc_1[:,ifit], '-', color='k', label='LBo 1')
    ax.plot(steps, gr_Rc_2[:,ifit], '-', color='k', lw=1.3, label='LBo 2')
    ax.plot(steps, gr_Rc_pyorbit[:,ifit], '--', color='lightgray', alpha=0.7, label='LMa')
    ax.plot(steps, gr_Rc_pymc[:,ifit], '-.', color='red', lw=1.5, alpha=0.7, label='pymc')
    ax.set_ylim(0.95, 2.3)
    ax.set_xlabel('steps (%s)' %(parameter_names_emcee[ifit].strip()))
    ax.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5))
    fig.savefig(os.path.join(emcee_plots, 'GR_%03d_%s.png' %(ifit+1, parameter_names_emcee[ifit])), bbox_inches='tight', dpi=200)
    plt.close(fig)
    logger.info('saved plot %s' %(os.path.join(emcee_plots, 'GRtrace_pam_%s.png' %(parameter_names_emcee[ifit]))))
    
  logger.info('')
  
  return
Beispiel #6
0
def main():
    # ---
    # initialize logger

    logger = logging.getLogger("Main_log")
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(message)s")

    # global variables

    label_separation = -0.90  # if uses this, comment ax.xyaxis.labelpad = label_pad
    label_pad = 12  # it uses this, comment ax.xyaxis.set_label_coords()...
    label_size = 8
    ticklabel_size = 4

    def set_xaxis(ax,
                  label_size,
                  label_separation,
                  label_pad,
                  ticklabel_size,
                  kel_label,
                  ticks_formatter,
                  tick_fmt='%.4f'):
        ax.get_xaxis().set_visible(True)
        ax.xaxis.set_tick_params(labelsize=ticklabel_size)
        ax.xaxis.set_label_coords(0.5, label_separation)
        #ax.ticklabel_format(style='plain', axis='both', useOffset=False)
        plt.setp(ax.xaxis.get_majorticklabels(), rotation=70)
        #ax.xaxis.labelpad = label_pad
        ax.set_xlabel(kel_label, fontsize=label_size)
        tick_step = (ticks_formatter[1] -
                     ticks_formatter[0]) / ticks_formatter[2]
        ax.xaxis.set_ticks(
            np.arange(ticks_formatter[0], ticks_formatter[1], tick_step))
        tick_formatter = FormatStrFormatter(tick_fmt)
        ax.xaxis.set_major_formatter(tick_formatter)
        return

    def set_yaxis(ax,
                  label_size,
                  label_separation,
                  label_pad,
                  ticklabel_size,
                  kel_label,
                  ticks_formatter,
                  tick_fmt='%.4f'):
        ax.get_yaxis().set_visible(True)
        ax.yaxis.set_tick_params(labelsize=ticklabel_size)
        ax.yaxis.set_label_coords(label_separation, 0.5)
        #ax.ticklabel_format(style='plain', axis='both', useOffset=False)
        #ax.yaxis.labelpad = label_pad
        ax.set_ylabel(kel_label, fontsize=label_size)
        tick_step = (ticks_formatter[1] -
                     ticks_formatter[0]) / ticks_formatter[2]
        ax.yaxis.set_ticks(
            np.arange(ticks_formatter[0], ticks_formatter[1], tick_step))
        tick_formatter = FormatStrFormatter(tick_fmt)
        ax.yaxis.set_major_formatter(tick_formatter)
        return

    print
    print ' ================== '
    print ' CORRELATION PLOTS'
    print ' ================== '
    print

    # read cli arguments
    cli = anc.get_args()

    #plot_folder = prepare_plot_folder(working_path)
    emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path)
    log_file = os.path.join(emcee_plots, 'emcee_triangle_log.txt')
    flog = logging.FileHandler(log_file, 'w')
    flog.setLevel(logging.DEBUG)
    flog.setFormatter(formatter)
    logger.addHandler(flog)
    # log screen
    slog = logging.StreamHandler()
    slog.setLevel(logging.DEBUG)
    slog.setFormatter(formatter)
    logger.addHandler(slog)

    # computes mass conversion factor
    #m_factor = anc.mass_conversion_factor(cli.m_type)
    m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

    # set emcee and trades folder
    emcee_folder = cli.full_path
    trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
    # and best folder
    emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(
        emcee_folder, cli.temp_status)

    # get data from the hdf5 file
    parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(
        emcee_file, cli.temp_status)
    # print Memory occupation of ...
    anc.print_memory_usage(chains)

    nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(
        chains, cli.temp_status, cli.nburnin, completed_steps)
    logger.info(
        'nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' %
        (nfit, nwalkers, nruns, nburnin, nruns_sel))

    # test label_separation
    #if (nfit <= 3): label_separation = -0.1
    if (nfit > 2):
        #label_separation = -0.1 - ( 0.075 * (nfit-2) ) # good for figsize=(12,12)
        label_separation = -0.15 - (0.125 * (nfit - 2))  # testing
    #else:
    #label_separation = -0.15

    #label_size = label_size - 1 * int(nfit / 5.)
    label_size = label_size - 1 * int(nfit / 2.5)

    # set label and legend names
    kel_plot_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

    chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains(
        nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor,
        parameter_names_emcee, parameter_boundaries, chains)

    chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(
        cli.use_thin,
        nburnin,
        nruns,
        nruns_sel,
        autocor_time,
        chains_T_full,
        lnprobability,
        burnin_done=False)

    flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0,
                                           parameter_names_emcee)

    if (cli.boot_id > 0):
        flatchain_posterior_msun = anc.posterior_back_to_msun(
            m_factor, parameter_names_emcee, flatchain_posterior_0)
        boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id,
                                            parameter_names_emcee,
                                            flatchain_posterior_msun)
        logger.info('saved bootstrap like file: %s' % (boot_file))
        del flatchain_posterior_msun

    k = anc.get_auto_bins(flatchain_posterior_0)

    if (cli.overplot is not None):

        if (cli.adhoc is not None):
            overp_names, read_par = anc.read_fitted_file(cli.adhoc)
            cli.overplot = 777
        else:
            ## OPEN summary_parameters.hdf5 FILE
            s_h5f = h5py.File(
                os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r')
            # take only the selected sample
            s_overplot = '%04d' % (cli.overplot)
            read_par = s_h5f['parameters/%s/fitted/parameters' %
                             (s_overplot)][...]
            s_h5f.close()

        # fitted parameters has always Mp/Ms in Msun/Mstar, so it is needed to rescale it properly
        overp_par = read_par.copy()
        for ii in range(0, nfit):
            if ('Ms' in parameter_names_emcee[ii]):
                #if('Ms' in overp_names[ii]):
                overp_par[ii] = overp_par[ii] * m_factor

    #fig, ax = plt.subplots(nrows = nfit-1, ncols=nfit, figsize=(12,12))
    #fig = plt.figure(figsize=(12,12))
    fig = plt.figure(figsize=(6, 6))
    fig.subplots_adjust(hspace=0.05, wspace=0.05)

    for ix in range(0, nfit, 1):
        x_data = flatchain_posterior_0[:, ix]
        ##x_med = median_parameters[ix]

        x_min, x_max = anc.compute_limits(x_data, 0.05)
        if (x_min == x_max):
            x_min = parameter_boundaries[ix, 0]
            x_max = parameter_boundaries[ix, 1]

        #x_max_mean = mode_parameters[ix]

        for iy in range(nfit - 1, -1, -1):
            y_data = flatchain_posterior_0[:, iy]
            y_min, y_max = anc.compute_limits(y_data, 0.05)
            if (y_min == y_max):
                y_min = parameter_boundaries[iy, 0]
                y_max = parameter_boundaries[iy, 1]

            #y_max_mean = mode_parameters[iy]

            if (iy > ix):  # correlation plot
                logger.info(
                    '%s vs %s' %
                    (parameter_names_emcee[ix], parameter_names_emcee[iy]))

                ax = plt.subplot2grid((nfit + 1, nfit), (iy, ix))

                #hist2d_counts, xedges, yedges, image2d = ax.hist2d(x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, normed=True)
                hist2d_counts, xedges, yedges, image2d = ax.hist2d(\
                  x_data, y_data, bins=k,
                  range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]],
                  cmap=cm.gray_r,
                  normed=False
                  #density=False
                  )

                #new_k = int(k/3)
                new_k = k
                hist2d_counts_2, xedges_2, yedges_2 = np.histogram2d(\
                  x_data, y_data, bins=new_k,
                  range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]],
                  #normed=True
                  density=False
                  )

                x_bins = [
                    0.5 * (xedges_2[i] + xedges_2[i + 1])
                    for i in range(0, new_k)
                ]
                y_bins = [
                    0.5 * (yedges_2[i] + yedges_2[i + 1])
                    for i in range(0, new_k)
                ]

                #ax.contour(x_bins, y_bins, hist2d_counts_2.T, 3, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7))
                nl = 5
                levels = [1. - np.exp(-0.5 * ii) for ii in range(0, nl)
                          ]  # 2D sigmas: 0sigma, 1sigma, 2sigma, 3sigma, ..
                #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7))
                #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=1.)
                #ax.contour(x_bins, y_bins, hist2d_counts_2.T, cmap=cm.viridis, linestyle='solid', linewidths=0.7)

                #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=0.7, normed=True)
                ax.contour(
                    x_bins,
                    y_bins,
                    hist2d_counts_2.T,
                    nl,
                    cmap=cm.viridis,
                    linestyles='solid',
                    linewidths=0.5,
                    #normed=True
                )

                if (cli.overplot is not None):
                    # plot selected overplot sample
                    ax.axvline(overp_par[ix],
                               color='C0',
                               ls='--',
                               lw=1.1,
                               alpha=0.5)
                    ax.axhline(overp_par[iy],
                               color='C0',
                               ls='--',
                               lw=1.1,
                               alpha=0.5)

                ax.get_xaxis().set_visible(False)
                ax.get_yaxis().set_visible(False)
                if (iy == nfit - 1):
                    set_xaxis(ax, label_size, label_separation, label_pad,
                              ticklabel_size, kel_plot_labels[ix],
                              [xedges[0], xedges[-1], 4])
                if (ix == 0):
                    set_yaxis(ax, label_size, label_separation, label_pad,
                              ticklabel_size, kel_plot_labels[iy],
                              [yedges[0], yedges[-1], 5])

                ax.set_ylim([y_min, y_max])
                ax.set_xlim([x_min, x_max])
                plt.draw()

            elif (iy == ix):  # distribution plot
                logger.info('%s histogram' % (parameter_names_emcee[ix]))

                ax = plt.subplot2grid((nfit + 1, nfit), (ix, ix))
                if (ix == nfit - 1):
                    hist_orientation = 'horizontal'
                else:
                    hist_orientation = 'vertical'

                idx = np.argsort(x_data)

                if (not cli.cumulative):
                    # HISTOGRAM
                    hist_counts, edges, patces = ax.hist(
                        x_data,
                        bins=k,
                        range=[x_data.min(), x_data.max()],
                        histtype='stepfilled',
                        color='darkgrey',
                        #edgecolor='lightgray',
                        edgecolor='None',
                        align='mid',
                        orientation=hist_orientation,
                        #normed=True,
                        density=True,
                        stacked=True)

                else:
                    # CUMULATIVE HISTOGRAM
                    hist_counts, edges, patces = ax.hist(
                        x_data,
                        bins=k,
                        range=[x_data.min(), x_data.max()],
                        histtype='stepfilled',
                        color='darkgrey',
                        #edgecolor='lightgray',
                        edgecolor='None',
                        align='mid',
                        orientation=hist_orientation,
                        density=True,
                        stacked=True,
                        cumulative=True)

                if (ix == nfit - 1):
                    ax.set_ylim([y_min, y_max])
                    if (cli.overplot is not None):
                        # plot selected overplot sample
                        ax.axhline(overp_par[ix],
                                   color='C0',
                                   ls='--',
                                   lw=1.1,
                                   alpha=0.5)
                else:
                    ax.set_xlim([x_min, x_max])
                    if (cli.overplot is not None):
                        # plot selected overplot sample
                        ax.axvline(overp_par[ix],
                                   color='C0',
                                   ls='--',
                                   lw=1.1,
                                   alpha=0.5)

                ax.get_xaxis().set_visible(False)
                ax.get_yaxis().set_visible(False)
                ax.set_title(kel_plot_labels[ix], fontsize=label_size)

                plt.draw()

    logger.info('saving plot')
    emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.png')
    fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=300)
    logger.info('png done')
    emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.pdf')
    fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=96)
    logger.info('pdf done')
    plt.close(fig)

    logger.info('')

    return
Beispiel #7
0
def main():
    # ---
    # initialize logger

    logger = logging.getLogger("Main_log")
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(message)s")

    # read cli arguments
    cli = anc.get_args()

    #plot_folder = prepare_plot_folder(working_path)
    emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path)
    log_file = os.path.join(emcee_plots, 'GelmanRubin_log.txt')
    flog = logging.FileHandler(log_file, 'w')
    flog.setLevel(logging.DEBUG)
    flog.setFormatter(formatter)
    logger.addHandler(flog)
    # log screen
    slog = logging.StreamHandler()
    slog.setLevel(logging.DEBUG)
    slog.setFormatter(formatter)
    logger.addHandler(slog)

    # computes mass conversion factor
    #m_factor = anc.mass_conversion_factor(cli.m_type)
    m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

    # set emcee and trades folder
    emcee_folder = cli.full_path
    trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
    # and best folder
    emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(
        emcee_folder, cli.temp_status)

    # get data from the hdf5 file
    parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(
        emcee_file, cli.temp_status)
    # print Memory occupation of ...
    anc.print_memory_usage(chains)

    nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(
        chains, cli.temp_status, cli.nburnin, completed_steps)
    logger.info(
        'nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' %
        (nfit, nwalkers, nruns, nburnin, nruns_sel))

    # set label and legend names
    kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

    chains_T, parameter_boundaries = anc.select_transpose_convert_chains(
        nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor,
        parameter_names_emcee, parameter_boundaries, chains)

    if (cli.temp_status):
        n_steps = completed_steps
    else:
        n_steps = nruns
    sel_steps = int(cli.sel_steps)
    if (sel_steps == 0):
        sel_steps = n_steps

    steps = np.linspace(start=0,
                        stop=n_steps,
                        num=sel_steps,
                        endpoint=True,
                        dtype=np.int)
    steps[0] = 10
    #print steps
    sel_steps = steps.shape[0]

    gr_Rc_1 = np.ones((sel_steps, nfit)) + 99.
    gr_Rc_2 = np.ones((sel_steps, nfit)) + 99.
    gr_Rc_pyorbit = np.ones((sel_steps, nfit)) + 99.
    gr_Rc_pymc = np.ones((sel_steps, nfit)) + 99.

    for ifit in range(0, nfit):
        logger.info('Parameter: %13s' % (parameter_names_emcee[ifit]))
        #fig = plt.figure(figsize=(12,12))
        fig = plt.figure(figsize=(6, 6))
        ax = plt.subplot2grid((1, 1), (0, 0))

        for istep in range(0, sel_steps):
            #print 'istep',istep
            #time0 = time.time()
            #gr_Rc_1[istep,ifit] = anc.GelmanRubin_test_1(chains_T[:steps[istep], :, ifit])
            #if(istep == sel_steps-1):
            #LBo_d, LBo_h, LBo_m, LBo_s = anc.computation_time(time.time()-time0)
            #logger.info('steps = %6d for %13s ==> Gelman-Rubin test:  LBo time = %2d m %6.3f s' %(steps[istep], parameter_names_emcee[ifit], LBo_m, LBo_s))

            time0 = time.time()
            gr_Rc_2[istep, ifit] = anc.GelmanRubin(chains_T[:steps[istep], :,
                                                            ifit])
            if (istep == sel_steps - 1):
                LBo_d, LBo_h, LBo_m, LBo_s = anc.computation_time(time.time() -
                                                                  time0)
                logger.info(
                    'steps = %6d for %13s ==> Gelman-Rubin test:  LBo time = %2d m %6.3f s'
                    %
                    (steps[istep], parameter_names_emcee[ifit], LBo_m, LBo_s))

            time0 = time.time()
            gr_Rc_pyorbit[istep, :] = anc.GelmanRubin_PyORBIT(
                chains_T[:steps[istep], :, ifit])
            if (istep == sel_steps - 1):
                LMa_d, LMa_h, LMa_m, LMa_s = anc.computation_time(time.time() -
                                                                  time0)
                logger.info(
                    'steps = %6d for %13s ==> Gelman-Rubin test:  LMa time = %2d m %6.3f s'
                    %
                    (steps[istep], parameter_names_emcee[ifit], LMa_m, LMa_s))

            time0 = time.time()
            gr_Rc_pymc[istep, :] = np.sqrt(
                anc.GelmanRubin_pymc(chains_T[:steps[istep], :, ifit].T))
            if (istep == sel_steps - 1):
                pymc_d, pymc_h, pymc_m, pymc_s = anc.computation_time(
                    time.time() - time0)
                logger.info(
                    'steps = %6d for %13s ==> Gelman-Rubin test: pymc time = %2d m %6.3f s'
                    % (steps[istep], parameter_names_emcee[ifit], pymc_m,
                       pymc_s))

        ax.axhline(1.01, color='gray')
        #ax.plot(steps, gr_Rc_1[:,ifit], '-', color='k', label='LBo 1')
        ax.plot(steps, gr_Rc_2[:, ifit], '-', color='k', lw=1.3, label='LBo 2')
        ax.plot(steps,
                gr_Rc_pyorbit[:, ifit],
                '--',
                color='lightgray',
                alpha=0.7,
                label='LMa')
        ax.plot(steps,
                gr_Rc_pymc[:, ifit],
                '-.',
                color='red',
                lw=1.5,
                alpha=0.7,
                label='pymc')
        ax.set_ylim(0.95, 2.3)
        ax.set_xlabel('steps (%s)' % (parameter_names_emcee[ifit].strip()))
        ax.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5))
        fig.savefig(os.path.join(
            emcee_plots,
            'GR_%03d_%s.png' % (ifit + 1, parameter_names_emcee[ifit])),
                    bbox_inches='tight',
                    dpi=200)
        plt.close(fig)
        logger.info(
            'saved plot %s' %
            (os.path.join(emcee_plots, 'GRtrace_pam_%s.png' %
                          (parameter_names_emcee[ifit]))))

    logger.info('')

    return
Beispiel #8
0
def main():

    print
    print ' ======================== '
    print ' TRADES+EMCEE CHAIN PLOTS'
    print ' ======================== '
    print

    # read cli arguments
    cli = anc.get_args()
    # computes mass conversion factor
    #m_factor, m_unit = anc.mass_conversion_factor_and_unit(cli.m_type)
    m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False)

    # set emcee and trades folder
    emcee_folder = cli.full_path
    trades_folder = os.path.join(os.path.dirname(cli.full_path), '')
    # and best folder
    emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(
        emcee_folder, cli.temp_status)

    parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(
        emcee_file, cli.temp_status)

    # set label and legend names
    kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type)

    nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(
        chains, cli.temp_status, cli.nburnin, completed_steps)

    anc.print_memory_usage(chains)

    chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains(
        nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor,
        parameter_names_emcee, parameter_boundaries, chains)

    if (cli.use_thin or cli.use_thin > 0):
        chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps, chains_T_full_thinned = anc.thin_the_chains(
            cli.use_thin,
            nburnin,
            nruns,
            nruns_sel,
            autocor_time,
            chains_T_full,
            lnprobability,
            burnin_done=False,
            full_chains_thinned=True)
        nburnin_plt = np.rint(nburnin / thin_steps).astype(int)
        nend = np.rint(nruns / thin_steps).astype(int)

    else:
        chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(
            cli.use_thin,
            nburnin,
            nruns,
            nruns_sel,
            autocor_time,
            chains_T_full,
            lnprobability,
            burnin_done=False,
            full_chains_thinned=False)
        nburnin_plt = nburnin
        nend = nruns

    #name_par, name_excluded = anc.get_sample_list(cli.sample_str, parameter_names_emcee)
    #sample_parameters, idx_sample = anc.pick_sample_parameters(flatchain_posterior_0, parameter_names_emcee, name_par = name_par, name_excluded = name_excluded)

    #flatchain_posterior_1 = flatchain_posterior_0

    # fix lambda?
    #flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0,
    #parameter_names_emcee
    #)

    if (cli.boot_id > 0):
        flatchain_posterior_msun = anc.posterior_back_to_msun(
            m_factor, parameter_names_emcee, flatchain_posterior_0)
        boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id,
                                            parameter_names_emcee,
                                            flatchain_posterior_msun)
        logger.info('saved bootstrap like file: %s' % (boot_file))
        del flatchain_posterior_msun

    k = anc.get_auto_bins(flatchain_posterior_0)

    try:
        overplot = int(cli.overplot)
    except:
        overplot = None

    ## OPEN summary_parameters.hdf5 FILE
    s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'),
                      'r')

    if (overplot is not None):
        # sample_parameters
        ci_fitted = s_h5f['confidence_intervals/fitted/ci'][...]
        sample_parameters = s_h5f['parameters/0666/fitted/parameters'][...]
        sample_lgllhd = s_h5f['parameters/0666'].attrs['lgllhd']

        try:
            sample2_parameters = s_h5f['parameters/0667/fitted/parameters'][
                ...]
            sample2_lgllhd = s_h5f['parameters/0667'].attrs['lgllhd']
        except:
            sample2_parameters = None
            sample2_lgllhd = None

        try:
            sample3_parameters = s_h5f['parameters/0668/fitted/parameters'][
                ...]
            sample3_lgllhd = s_h5f['parameters/0668'].attrs['lgllhd']
        except:
            sample3_parameters = None
            sample3_lgllhd = None

        median_parameters = s_h5f['parameters/1051/fitted/parameters'][...]
        median_lgllhd = s_h5f['parameters/1051'].attrs['lgllhd']

        max_lnprob_parameters = s_h5f['parameters/2050/fitted/parameters'][...]
        max_lgllhd = s_h5f['parameters/2050'].attrs['lgllhd']

        try:
            mode_parameters = s_h5f['parameters/3051/fitted/parameters'][...]
            mode_lgllhd = s_h5f['parameters/3051'].attrs['lgllhd']
        except:
            mode_parameters = None
            mode_lgllhd = None

        overp_par = s_h5f['parameters/%04d/fitted/parameters' %
                          (overplot)][...]
        overp_lgllhd = s_h5f['parameters/%04d' % (overplot)].attrs['lgllhd']

    #nfit = s_h5f['confidence_intervals/fitted'].attrs['nfit']
    ndata = s_h5f['confidence_intervals/fitted'].attrs['ndata']
    dof = s_h5f['confidence_intervals/fitted'].attrs['dof']

    s_h5f.close()

    emcee_plots = os.path.join(cli.full_path, 'plots')
    if (not os.path.isdir(emcee_plots)):
        os.makedirs(emcee_plots)

    for i in range(0, nfit):
        if ('Ms' in parameter_names_emcee[i]):
            conv_plot = m_factor
        else:
            conv_plot = 1.

        emcee_fig_file = os.path.join(
            emcee_plots,
            'chain_%03d_%s.png' % (i + 1, parameter_names_emcee[i].strip()))
        print ' %s' % (emcee_fig_file),
        #fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(12,12))
        fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(6, 6))

        (counts, bins_val,
         patches) = axHist.hist(flatchain_posterior_0[:, i],
                                bins=k,
                                range=(flatchain_posterior_0[:, i].min(),
                                       flatchain_posterior_0[:, i].max()),
                                orientation='horizontal',
                                density=True,
                                stacked=True,
                                histtype='stepfilled',
                                color='darkgrey',
                                edgecolor='lightgray',
                                align='mid')

        xpdf = scipy_norm.pdf(flatchain_posterior_0[:, i],
                              loc=flatchain_posterior_0[:, i].mean(),
                              scale=flatchain_posterior_0[:, i].std())
        idx = np.argsort(flatchain_posterior_0[:, i])
        axHist.plot(xpdf[idx],
                    flatchain_posterior_0[idx, i],
                    color='black',
                    marker='None',
                    ls='-.',
                    lw=1.5,
                    label='pdf')

        # chains after burn-in
        #axChain.plot(chains_T[:,:,i], '-', alpha=0.3)
        # chains with the burn-in
        if (cli.use_thin):
            axChain.plot(chains_T_full_thinned[:, :, i], '-', alpha=0.3)
        else:
            axChain.plot(chains_T_full[:, :, i], '-', alpha=0.3)

        axChain.axvspan(0, nburnin_plt, color='gray', alpha=0.45)
        axChain.axvline(nburnin_plt, color='gray', ls='-', lw=1.5)

        if (overplot is not None):
            if (mode_parameters is not None):
                # plot of mode (mean of higher peak/bin)
                axChain.axhline(mode_parameters[i] * conv_plot,
                                color='red',
                                ls='-',
                                lw=2.1,
                                alpha=1,
                                label='mode')

            # plot of median
            axChain.axhline(median_parameters[i] * conv_plot,
                            marker='None',
                            c='blue',
                            ls='-',
                            lw=2.1,
                            alpha=1.0,
                            label='median fit')

            # plot of max_lnprob
            axChain.axhline(max_lnprob_parameters[i] * conv_plot,
                            marker='None',
                            c='black',
                            ls='-',
                            lw=1.1,
                            alpha=1.0,
                            label='max lnprob')

            if (sample_parameters is not None):
                # plot of sample_parameters
                axChain.axhline(sample_parameters[i] * conv_plot,
                                marker='None',
                                c='orange',
                                ls='--',
                                lw=2.3,
                                alpha=0.77,
                                label='picked: %12.7f' %
                                (sample_parameters[i]))

            if (sample2_parameters is not None):
                # plot of sample2_parameters
                axChain.axhline(sample2_parameters[i] * conv_plot,
                                marker='None',
                                c='cyan',
                                ls=':',
                                lw=2.7,
                                alpha=0.77,
                                label='close lgllhd: %12.7f' %
                                (sample2_parameters[i]))

            if (sample3_parameters is not None):
                # plot of sample3_parameters
                axChain.axhline(sample3_parameters[i] * conv_plot,
                                marker='None',
                                c='yellow',
                                ls='-',
                                lw=3.1,
                                alpha=0.66,
                                label='close lgllhd: %12.7f' %
                                (sample3_parameters[i]))

            if (overplot not in [1050, 1051, 2050, 3050, 3051]):
                axChain.axhline(overp_par[i] * conv_plot,
                                marker='None',
                                c='black',
                                ls='--',
                                lw=2.5,
                                alpha=0.6,
                                label='overplot %d' % (overplot))

            # plot ci
            axChain.axhline(ci_fitted[i, 0] * conv_plot,
                            marker='None',
                            c='forestgreen',
                            ls='-',
                            lw=2.1,
                            alpha=1.0,
                            label='CI 15.865th (%.5f)' %
                            (ci_fitted[i, 0] * conv_plot))
            axChain.axhline(ci_fitted[i, 1] * conv_plot,
                            marker='None',
                            c='forestgreen',
                            ls='-',
                            lw=2.1,
                            alpha=1.0,
                            label='CI 84.135th (%.5f)' %
                            (ci_fitted[i, 1] * conv_plot))

        axChain.ticklabel_format(useOffset=False)
        xlabel = '$N_\mathrm{steps}$'
        if (cli.use_thin):
            xlabel = '$N_\mathrm{steps} \\times %d$' % (thin_steps)
        axChain.set_xlabel(xlabel)
        axChain.set_xlim([0, nend])
        axChain.set_ylabel(kel_labels[i])

        y_min = flatchain_posterior_0[:, i].min()
        y_max = flatchain_posterior_0[:, i].max()

        axChain.set_ylim([y_min, y_max])
        axChain.set_title('Full chain %s:=[%.3f , %.3f]' %
                          (kel_labels[i], parameter_boundaries[i, 0],
                           parameter_boundaries[i, 1]))
        plt.draw()

        axHist.ticklabel_format(useOffset=False)
        axHist.tick_params(direction='inout', labelleft=False)
        axHist.set_ylim([y_min, y_max])

        if (overplot is not None):
            if (mode_parameters is not None):
                # plot mode
                axHist.axhline(mode_parameters[i] * conv_plot,
                               color='red',
                               ls='-',
                               lw=2.1,
                               alpha=1,
                               label='mode')

            # plot median
            axHist.axhline(median_parameters[i] * conv_plot,
                           marker='None',
                           c='blue',
                           ls='-',
                           lw=2.1,
                           alpha=1.0,
                           label='median fit')

            # plot of max_lnprob
            axHist.axhline(max_lnprob_parameters[i] * conv_plot,
                           marker='None',
                           c='black',
                           ls='-',
                           lw=1.1,
                           alpha=1.0,
                           label='max lnprob')

            if (sample_parameters is not None):
                # plot of sample_parameters
                axHist.axhline(sample_parameters[i] * conv_plot,
                               marker='None',
                               c='orange',
                               ls='--',
                               lw=2.3,
                               alpha=0.77,
                               label='picked: %12.7f' %
                               (sample_parameters[i] * conv_plot))

            if (sample2_parameters is not None):
                # plot of sample2_parameters
                axHist.axhline(sample2_parameters[i] * conv_plot,
                               marker='None',
                               c='cyan',
                               ls=':',
                               lw=2.7,
                               alpha=0.77,
                               label='close lgllhd: %12.7f' %
                               (sample2_parameters[i]))

            if (sample3_parameters is not None):
                # plot of sample3_parameters
                axHist.axhline(sample3_parameters[i] * conv_plot,
                               marker='None',
                               c='yellow',
                               ls='-',
                               lw=3.1,
                               alpha=0.66,
                               label='close lgllhd: %12.7f' %
                               (sample3_parameters[i]))

            if (overplot not in [1050, 1051, 2050, 3050, 3051]):
                axHist.axhline(overp_par[i] * conv_plot,
                               marker='None',
                               c='black',
                               ls='--',
                               lw=2.5,
                               alpha=0.8,
                               label='overplot %d' % (overplot))

            # plot ci
            axHist.axhline(ci_fitted[i, 0] * conv_plot,
                           marker='None',
                           c='forestgreen',
                           ls='-',
                           lw=2.1,
                           alpha=1.0,
                           label='CI 15.865th (%.5f)' %
                           (ci_fitted[i, 0] * conv_plot))
            axHist.axhline(ci_fitted[i, 1] * conv_plot,
                           marker='None',
                           c='forestgreen',
                           ls='-',
                           lw=2.1,
                           alpha=1.0,
                           label='CI 84.135th (%.5f)' %
                           (ci_fitted[i, 1] * conv_plot))

        axHist.set_title('Distribution of posterior chain')
        axHist.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5))
        plt.draw()

        fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=150)
        print ' saved'
        print

    #fig = plt.figure(figsize=(12,12))
    fig = plt.figure(figsize=(6, 6))

    # lnprob
    xlabel = '$N_\mathrm{steps}$'
    if (cli.use_thin):
        xlabel = '$N_\mathrm{steps} \\times %d$' % (thin_steps)

    ax = plt.subplot2grid((2, 1), (0, 0))
    ax.plot(lnprob_burnin.T, '-', alpha=0.3)

    if (overplot is not None):
        posterior_msun = anc.posterior_back_to_msun(m_factor,
                                                    parameter_names_emcee,
                                                    flatchain_posterior_0)
        post_sel, lnprob_sel = anc.select_within_all_ci(
            posterior_msun, ci_fitted[:, 0:2], lnprob_burnin.T.reshape(-1))
        #lnprob_sel = lnprob_burnin.T.reshape((-1))
        lgllhd_med = np.percentile(lnprob_burnin.T.reshape(-1),
                                   50.,
                                   interpolation='midpoint')
        abs_dlg = np.abs(lnprob_sel - lgllhd_med)
        lgllhd_mad = np.percentile(abs_dlg, 50., interpolation='midpoint')

        #lnp_min = np.min(lnprob_sel)
        #lnp_max = np.max(lnprob_sel)
        lnp_min = lgllhd_med - lgllhd_mad
        lnp_max = lgllhd_med + lgllhd_mad
        print ' lgllhd_med & mad = ', lgllhd_med, lgllhd_mad
        print ' lnp_min = ', lnp_min, ' lnp_max = ', lnp_max
        print ' lnl_668 = ', sample3_lgllhd

        ax.axhline(lgllhd_med, color='black', ls='-', lw=1.6, alpha=0.77)

        #if(sample2_lgllhd is not None):
        #ax.axhline(sample2_lgllhd, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9)

        if (sample3_lgllhd is not None):
            ax.axhline(sample3_lgllhd,
                       marker='None',
                       c='yellow',
                       ls='-',
                       lw=3.1,
                       alpha=0.9)

        ax.axhspan(lnp_min, lnp_max, color='gray', alpha=0.77)
        ax.axhline(lnp_min, color='black', ls='--', lw=1.6, alpha=0.77)
        ax.axhline(lnp_max, color='black', ls='--', lw=1.6, alpha=0.77)

    min_lnp = np.min(lnprob_burnin.T, axis=0).min()
    max_lnp = np.max(lnprob_burnin.T, axis=0).max()
    y_min, y_max = anc.compute_limits(np.asarray([min_lnp, max_lnp]), 0.05)
    ax.set_ylim((y_min, y_max))
    ax.set_ylabel('lnprob')
    #ax.get_xaxis().set_visible(False)
    ax.set_xlabel(xlabel)

    # chi2r
    chi2r = -2. * (lnprob_burnin.T - ln_err_const) / np.float64(dof)

    ax = plt.subplot2grid((2, 1), (1, 0))
    ax.axhline(1.0, color='gray', ls='-')
    ax.plot(chi2r, '-', alpha=0.3)

    if (overplot is not None):
        c2r_med = -(2. * (lgllhd_med - ln_err_const)) / np.float64(dof)
        c2r_smax = -(2. * (lnp_min - ln_err_const)) / np.float64(dof)
        c2r_smin = -(2. * (lnp_max - ln_err_const)) / np.float64(dof)

        print ' c2r_med = ', c2r_med
        print ' c2r_smin = ', c2r_smin, ' c2r_smax = ', c2r_smax

        ax.axhline(c2r_med, color='black', ls='-', lw=1.6, alpha=0.77)
        ax.axhspan(c2r_smin, c2r_smax, color='gray', alpha=0.77)
        ax.axhline(c2r_smin, color='black', ls='--', lw=1.6, alpha=0.77)
        ax.axhline(c2r_smax, color='black', ls='--', lw=1.6, alpha=0.77)
        #if(sample2_lgllhd is not None):
        #c2r_sample2 = -2.*(sample2_lgllhd - ln_err_const)/np.float64(dof)
        #ax.axhline(c2r_sample2, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9)
        if (sample3_lgllhd is not None):
            c2r_sample3 = -2. * (sample3_lgllhd -
                                 ln_err_const) / np.float64(dof)
            ax.axhline(c2r_sample3,
                       marker='None',
                       c='yellow',
                       ls='-',
                       lw=3.1,
                       alpha=0.9)

    c2r_min = -2. * (y_max - ln_err_const) / np.float64(dof)
    c2r_max = -2. * (y_min - ln_err_const) / np.float64(dof)
    ax.set_ylim((c2r_min, c2r_max))
    ax.set_ylabel('$\chi^{2}/\mathrm{dof}$')
    #ax.get_xaxis().set_visible(True)
    ax.set_xlabel(xlabel)

    fig.savefig(os.path.join(emcee_plots, 'emcee_lnprobability.png'),
                bbox_inches='tight',
                dpi=150)
    print ' %s saved' % (os.path.join(emcee_plots, 'emcee_lnprobability.png'))

    return