Skip to content

Commit

Permalink
fix: Remove unnecessary computations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMichelsen committed Oct 28, 2022
1 parent 79bdcaa commit 3515083
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 58 deletions.
41 changes: 21 additions & 20 deletions src/metaDMG/fit/bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,33 @@ def add_D_information(
beta = (1 - Dx) * phi

# 1000x faster approximation for sp_betabinom(N, alpha, beta)
pdf_approx = sp_betabinom(N, alpha.mean(), beta.mean())
# pdf_approx = sp_betabinom(N, alpha.mean(), beta.mean())

fit_result[f"{prefix}damage"] = mu.item()
fit_result[f"{prefix}damage_std"] = std.item()
fit_result[f"{prefix}significance"] = mu.item() / std.item()
fit_result[f"{prefix}damage_median"] = np.median(A)

# fit_result[f"{prefix}damage_median"] = np.median(A)

# fit_result[f"{prefix}prob_lt_1p_damage"] = pdf_beta.cdf(0.01).mean()
# # fit_result[f"{prefix}prob_lt_1p_damage_betabinom"] = pdf.cdf(0.01 * N).mean()
# fit_result[f"{prefix}prob_zero_damage"] = pdf.cdf(0).mean()

for n_sigma in [1, 2, 3]:
conf = get_n_sigma_probability(n_sigma)
fit_result[f"{prefix}damage_CI_{n_sigma}_sigma_low"] = (
pdf_approx.ppf((1 - conf) / 2.0).mean() / N
)
fit_result[f"{prefix}damage_CI_{n_sigma}_sigma_high"] = (
pdf_approx.ppf((1 + conf) / 2.0).mean() / N
)

fit_result[f"{prefix}damage_CI_95_low"] = (
pdf_approx.ppf((1 - 0.95) / 2.0).mean() / N
)
fit_result[f"{prefix}damage_CI_95_high"] = (
pdf_approx.ppf((1 + 0.95) / 2.0).mean() / N
)
# for n_sigma in [1, 2, 3]:
# conf = get_n_sigma_probability(n_sigma)
# fit_result[f"{prefix}damage_CI_{n_sigma}_sigma_low"] = (
# pdf_approx.ppf((1 - conf) / 2.0).mean() / N
# )
# fit_result[f"{prefix}damage_CI_{n_sigma}_sigma_high"] = (
# pdf_approx.ppf((1 + conf) / 2.0).mean() / N
# )

# fit_result[f"{prefix}damage_CI_95_low"] = (
# pdf_approx.ppf((1 - 0.95) / 2.0).mean() / N
# )
# fit_result[f"{prefix}damage_CI_95_high"] = (
# pdf_approx.ppf((1 + 0.95) / 2.0).mean() / N
# )

return fit_result

Expand Down Expand Up @@ -158,9 +159,9 @@ def add_summary_of_variable(

fit_result[f"{s}"] = np.mean(values, axis=axis)
fit_result[f"{s}_std"] = np.std(values, axis=axis)
fit_result[f"{s}_median"] = np.median(values, axis=axis)
fit_result[f"{s}_CI_1_sigma_low"] = np.quantile(values, q_low, axis=axis)
fit_result[f"{s}_CI_1_sigma_high"] = np.quantile(values, q_high, axis=axis)
# fit_result[f"{s}_median"] = np.median(values, axis=axis)
# fit_result[f"{s}_CI_1_sigma_low"] = np.quantile(values, q_low, axis=axis)
# fit_result[f"{s}_CI_1_sigma_high"] = np.quantile(values, q_high, axis=axis)


def compute_rho_Ac(mcmc):
Expand Down
36 changes: 18 additions & 18 deletions src/metaDMG/viz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,39 +263,39 @@ def update_sidebar_right_datatable_results(click_data):
bayesian_list = []
if viz_results.Bayesian:

s_D = f", [{ds['damage_CI_1_sigma_low']:.2%}, {ds['damage_CI_1_sigma_high']:.2%}]"
# s_D = f", [{ds['damage_CI_1_sigma_low']:.2%}, {ds['damage_CI_1_sigma_high']:.2%}]"

conf_q_low = ds["q_CI_1_sigma_low"]
conf_q_high = ds["q_CI_1_sigma_high"]
s_q = f", [{conf_q_low:.3f}, {conf_q_high:.3f}]"
# conf_q_low = ds["q_CI_1_sigma_low"]
# conf_q_high = ds["q_CI_1_sigma_high"]
# s_q = f", [{conf_q_low:.3f}, {conf_q_high:.3f}]"

conf_phi_low = viz_utils.human_format(ds["phi_CI_1_sigma_low"])
conf_phi_high = viz_utils.human_format(ds["phi_CI_1_sigma_high"])
s_phi = f", [{conf_phi_low}, {conf_phi_high}]"
# conf_phi_low = viz_utils.human_format(ds["phi_CI_1_sigma_low"])
# conf_phi_high = viz_utils.human_format(ds["phi_CI_1_sigma_high"])
# s_phi = f", [{conf_phi_low}, {conf_phi_high}]"

conf_A_low = ds["A_CI_1_sigma_low"]
conf_A_high = ds["A_CI_1_sigma_high"]
s_A = f", [{conf_A_low:.3f}, {conf_A_high:.3f}]"
# conf_A_low = ds["A_CI_1_sigma_low"]
# conf_A_high = ds["A_CI_1_sigma_high"]
# s_A = f", [{conf_A_low:.3f}, {conf_A_high:.3f}]"

conf_c_low = ds["c_CI_1_sigma_low"]
conf_c_high = ds["c_CI_1_sigma_high"]
s_c = f", [{conf_c_low:.3f}, {conf_c_high:.3f}]"
# conf_c_low = ds["c_CI_1_sigma_low"]
# conf_c_high = ds["c_CI_1_sigma_high"]
# s_c = f", [{conf_c_low:.3f}, {conf_c_high:.3f}]"

bayesian_list = [
"Fit results:",
html.Br(),
f"D: {ds['damage']:.2%} " f"± {ds['damage_std']:.2%}" + s_D,
f"D: {ds['damage']:.2%} " f"± {ds['damage_std']:.2%}", # + s_D,
html.Br(),
f"significance: {ds['significance']:.2f}",
html.Br(),
f"q: {ds['q']:.3f} " f"± {ds['q_std']:.3f}" + s_q,
f"q: {ds['q']:.3f} " f"± {ds['q_std']:.3f}", # + s_q,
html.Br(),
f"phi: {viz_utils.human_format(ds['phi'])} "
f"± {viz_utils.human_format(ds['phi_std'])}" + s_phi,
f"± {viz_utils.human_format(ds['phi_std'])}", # + s_phi,
html.Br(),
f"A: {ds['A']:.3f} " f"± {ds['A_std']:.3f}" + s_A,
f"A: {ds['A']:.3f} " f"± {ds['A_std']:.3f}", # + s_A,
html.Br(),
f"c: {ds['c']:.3f} " f"± {ds['c_std']:.3f}" + s_c,
f"c: {ds['c']:.3f} " f"± {ds['c_std']:.3f}", # + s_c,
html.Br(),
f"rho Ac: {ds['rho_Ac']:.3f}",
html.Br(),
Expand Down
15 changes: 10 additions & 5 deletions src/metaDMG/viz/figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
from matplotlib import ticker
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.ticker import EngFormatter
from tqdm import tqdm
from tqdm.rich import tqdm

from metaDMG.viz import viz_utils


# from tqdm import tqdm


#%%

plt.rcParams["text.usetex"] = True
Expand Down Expand Up @@ -255,7 +258,8 @@ def plot_group(
# add damage as single errorbar
if D_info is not None:

D, D_low, D_high = D_info
# D, D_low, D_high = D_info
D, D_std = D_info

# fit with errorbars
fig.add_trace(
Expand All @@ -264,9 +268,10 @@ def plot_group(
y=[D],
error_y=dict(
type="data",
symmetric=False,
array=[D_high - D],
arrayminus=[D - D_low],
array=[D_std],
# symmetric=False,
# array=[D_high - D],
# arrayminus=[D - D_low],
visible=True,
color="black",
),
Expand Down
19 changes: 10 additions & 9 deletions src/metaDMG/viz/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ def correct_for_non_LCA(df):

def add_MAP_measures(df):
df["MAP_rho_Ac_abs"] = np.abs(df["MAP_rho_Ac"])
df["MAP_damage_CI_low"] = df["MAP_damage"] - df["MAP_damage_std"]
df["MAP_damage_CI_high"] = df["MAP_damage"] + df["MAP_damage_std"]
# df["MAP_damage_CI_low"] = df["MAP_damage"] - df["MAP_damage_std"]
# df["MAP_damage_CI_high"] = df["MAP_damage"] + df["MAP_damage_std"]


def add_bayesian_measures(df):
df["rho_Ac_abs"] = np.abs(df["rho_Ac"])
df["damage_CI_low"] = df["damage_CI_1_sigma_low"]
df["damage_CI_high"] = df["damage_CI_1_sigma_high"]
# df["damage_CI_low"] = df["damage_CI_1_sigma_low"]
# df["damage_CI_high"] = df["damage_CI_1_sigma_high"]


#%%
Expand Down Expand Up @@ -502,13 +502,14 @@ def get_D(self, sample, tax_id):
prefix = "MAP_"

D = ds[f"{prefix}damage"].iloc[0]
D_sigma = ds[f"{prefix}damage_std"].iloc[0]

s1 = f"{prefix}damage_CI_low"
s2 = f"{prefix}damage_CI_high"
D_low = ds[s1].iloc[0]
D_high = ds[s2].iloc[0]
# s1 = f"{prefix}damage_CI_low"
# s2 = f"{prefix}damage_CI_high"
# D_low = ds[s1].iloc[0]
# D_high = ds[s2].iloc[0]

return D, D_low, D_high
return D, D_sigma

def _ds_to_fit_text(self, ds):

Expand Down
12 changes: 6 additions & 6 deletions src/metaDMG/viz/viz_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ def get_range_slider_keywords(viz_results, column="N_reads", N_steps=100):
"q",
"A",
"c",
"damage_CI_low",
"damage_CI_high",
# "damage_CI_low",
# "damage_CI_high",
]:
range_min = 0.0
range_max = 1.0
Expand Down Expand Up @@ -633,10 +633,10 @@ def get_d_columns_latex(viz_results):
"significance": r"$\text{significance}$",
"MAP_significance": r"$\text{significance} \,\, \text{(MAP)}$",
#
"damage_CI_low": r"$D \,\, \text{CI. low}$",
"damage_CI_high": r"$D \,\, \text{CI. high}$",
"MAP_damage_CI_low": r"$D \,\, \text{CI. low} \,\, \text{(MAP)}$",
"MAP_damage_CI_high": r"$D \,\, \text{CI. high} \,\, \text{(MAP)}$",
# "damage_CI_low": r"$D \,\, \text{CI. low}$",
# "damage_CI_high": r"$D \,\, \text{CI. high}$",
# "MAP_damage_CI_low": r"$D \,\, \text{CI. low} \,\, \text{(MAP)}$",
# "MAP_damage_CI_high": r"$D \,\, \text{CI. high} \,\, \text{(MAP)}$",
#
"q": r"$q$",
"MAP_q": r"$q \text{(MAP)}$",
Expand Down

0 comments on commit 3515083

Please sign in to comment.