---
title: "Posterior densities"
format:
html:
code-fold: true
code-tools: true
code-line-numbers: true
page-layout: full
grid:
sidebar-width: 50px
body-width: 1200px
margin-width: 200px
pdf: default
jupyter: python3
---
```{python}
import arviz as az
import numpy as np
import glob
import os
import json
import csv
import pandas as pd
import matplotlib.pyplot as plt
import corner
from agnvar import clean_dataframe,prettyname2dirname,dirname2prettyname,find_spectral_str, replot_lsp_ppc,replot_psd_ppc,plot_posterior_density,replot_psd_overplot, get_all_xmm_lightcurves,plot_total_lc,find_simultaneous,get_scales,plot_intercalibrated_lc,plot_flux_distributions,plot_total_lc,plot_full_light_curve,intercalibrate_lc,get_index_label,prettify_names
plt.rcParams.update({'figure.max_open_warning': 0})
sources = pd.read_csv('website_sources_data.csv')
sources_list = sources["Object"].values.tolist()
dir_list = [prettyname2dirname(source) for source in sources_list]
energies = ["0.3-1.5"]
path = "/home/mehdy/github/Paper_II/website/inference/"
band = "0.3-1.5"
```
## Single bending power-law
```{python}
#| layout-ncol: 2
model = "single_steep"
short_model = "single"
names_single = json.load(open(f"{path}/{dir_list[0]}_combined_{band}_single_123_factor/info/results.json"))["paramnames"]
single_dirs = [f"{path}/{source}_combined_{band}_{short_model}_123_factor/chains/equal_weighted_post.txt" for source in dir_list]
inference_data_single = {}
for i,dir in enumerate(single_dirs):
source = dir_list[i]
data = az.convert_to_inference_data(dict(zip(names_single,np.genfromtxt(dir,skip_header=1).T)))
data.posterior["t₁"] = 1/data.posterior["f₁"]
data.posterior["log t₁"] = np.log10(data.posterior["t₁"])
inference_data_single[source] = data
#chain = Chain.from_arviz(inference_data_single[source], dirname2prettyname(source), color="sky")
#fig = ChainConsumer().add_chain(chain).plotter.plot()
with plt.style.context("ggplot"):
fig = corner.corner(inference_data_single[source], quantiles=[0.16, 0.5, 0.84],
show_titles=True,title_kwargs={"fontsize": 10},label_kwargs={"fontsize": 10},labelpad=0.5,quiet=True)
fig.set_size_inches(8., 7.5)
#fig.tight_layout()
fig.suptitle(f"{dirname2prettyname(source)}")
```
```{python}
#| layout-ncol: 3
for i,dir in enumerate(single_dirs):
source = dir_list[i]
with plt.style.context("https://github.com/mlefkir/beauxgraphs/raw/main/beautifulgraphs_colblind.mplstyle"):
fig,ax = plot_posterior_density(f"{path}/{source}_combined_{band}_{short_model}_123_factor/", short_model,source, band)
fig.suptitle(f"{dirname2prettyname(source)}")
```
## Double bending power-law
```{python}
#| layout-ncol: 2
model = "double_steep"
short_model = "double"
double_dirs = [f"{path}/{source}_combined_{band}_{short_model}_123_factor/chains/equal_weighted_post.txt" for source in dir_list]
names_double = json.load(open(f"{path}/{dir_list[0]}_combined_{band}_{short_model}_123_factor/info/results.json"))["paramnames"]
inference_data_double = {}
for i,dir in enumerate(double_dirs):
source = dir_list[i]
if os.path.exists(dir):
data = az.convert_to_inference_data(dict(zip(names_double,np.genfromtxt(dir,skip_header=1).T)))
data.posterior["t₁"] = 1/data.posterior["f₁"]
data.posterior["log t₁"] = np.log10(data.posterior["t₁"])
data.posterior["t₂"] = 1/data.posterior["f₂"]
data.posterior["log t₂"] = np.log10(data.posterior["f₂"])
inference_data_double[source] = data
#chain = Chain.from_arviz(inference_data_double[source], dirname2prettyname(source), color="sky")
#fig = ChainConsumer().add_chain(chain).plotter.plot()
with plt.style.context("ggplot"):
fig = corner.corner(inference_data_double[source], quantiles=[0.16, 0.5, 0.84], show_titles=True, title_kwargs={"fontsize": 12},label_kwargs={"fontsize": 10},labelpad=0.5,quiet=True)
fig.set_size_inches(8., 7.5)
#fig.tight_layout()
fig.suptitle(f"{dirname2prettyname(source)}")
```
```{python}
#| layout-ncol: 3
for i,dir in enumerate(double_dirs):
source = dir_list[i]
if os.path.exists(dir):
with plt.style.context("https://github.com/mlefkir/beauxgraphs/raw/main/beautifulgraphs_colblind.mplstyle"):
fig,ax = plot_posterior_density(f"{path}/{source}_combined_{band}_{short_model}_123_factor/", short_model,source, band)
fig.suptitle(f"{dirname2prettyname(source)}")
```