File size: 4,363 Bytes
128403f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f137b36
128403f
 
 
 
 
 
 
 
 
f137b36
128403f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f137b36
128403f
 
 
 
 
 
 
 
 
f137b36
128403f
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Utilities to handle different operations
"""

import os
import pandas as pd
import great_tables as gt
from collections import OrderedDict
import tomli
import numpy as np

with open(os.path.abspath("../../config/config.toml"), "rb") as file_config:
    config = tomli.load(file_config)


def get_greattable_as_html(df: pd.DataFrame) -> gt.GT:
    """
    Get the great_table as HTML from Pandas dataframe.

    Args:
        df (pd.DataFrame): Dataframe to rendera as a table.

    Returns:
        gt.GT: Table in HTML format.
    """
    table_great_table = gt.GT(data=df)

    return table_great_table.as_raw_html()


def populate_summary_table_ARL0_k(summary_table_df_ARL0_k: pd.DataFrame, h) -> gt.GT:
    """
    Populate ARLTheoretical.summary_table_df_ARL0_k.

    Args:
        summary_table_df_ARL0_k (pd.DataFrame): Dataframe of ARL0 and its respective values of k.
        h (float): Normalized threshold.

    Returns:
        gt.GT: Table of ARL0 and k in HTML format.
    """
    table_great_table_ARL0_k = (
        gt.GT(summary_table_df_ARL0_k)
        .tab_header(
            title=gt.html(
                f"Reference Values for an intended ARL<sub>0</sub> with normalized threshold, h = {h}"
            )
        )
        .data_color(
            palette=[
                config["color"]["blue_005"],
                config["color"]["blue_020"],
                config["color"]["blue_040"],
            ]
        )
    )

    if config["control"]["save_figure"] == "true":
        table_great_table_ARL0_k.save(
            os.path.abspath(
                os.path.join(
                    "../../", config["path_output"]["path_figure"], "fig_table_h_arl0_k.png"
                )
            ),
            scale=3,
            window_size=(1200, 1600),
        )
        print(
            "Created",
            os.path.abspath(
                os.path.join(
                    "../../", config["path_output"]["path_figure"], "fig_table_h_arl0_k.png"
                )
            ),
        )

    return table_great_table_ARL0_k.as_raw_html()


def populate_summary_table_ARL1_k(
    summary_table_df_ARL1_k: pd.DataFrame, dict_ARL0_k: OrderedDict, h
) -> gt.GT:
    """
    Populate Multiindex table specific for ARLTheoretical.summary_table_df_ARL1_k

    Args:
        summary_table_df_ARL1_k (pd.DataFrame): Dataframe with ARL1 and k values.
        dict_ARL0_k (OrderedDict): Data Dictionary with the mapping between ARL0 and k.
        h (float): Normalized threshold.

    Returns:
        gt.GT: Table for ARL1 and k in HTML format.
    """
    list_ARL_0 = [str(ARL_0) for ARL_0 in dict_ARL0_k.keys()]
    list_k = ["{:.2f}".format(k) for k in dict_ARL0_k.values()]

    format_k_ARL_0 = lambda k, ARL_0: gt.html(str(k) + "<br>" + "(" + str(ARL_0) + ")")

    column_label_dict = {
        ARL_0: format_k_ARL_0(k, ARL_0) for ARL_0, k in zip(list_ARL_0, list_k)
    }

    table_great_table_ARL1_k = (
        gt.GT(summary_table_df_ARL1_k)
        .tab_header(
            title=gt.html(
                f"Estimate of steady state ARL (ARL<sub>1</sub>) based on the computed reference values and intended zero-state ARL (ARL<sub>0</sub>) with normalized threshold, h = {h})"
            )
        )
        .tab_stubhead(label="Shift in mean")
        .tab_spanner(
            label=gt.html("Reference Values<br>(Intended ARL<sub>0</sub>)"),
            columns=list_ARL_0,
        )
        .cols_move_to_start(columns=["Shift in mean"])
        .cols_label(**column_label_dict)
        .data_color(
            palette=[
                config["color"]["blue_005"],
                config["color"]["blue_020"],
                config["color"]["blue_040"],
            ]
        )
    )

    if config["control"]["save_figure"] == "true":
        table_great_table_ARL1_k.save(
            os.path.abspath(
                os.path.join(
                    "../../", config["path_output"]["path_figure"], "fig_table_h_k_arl1.png"
                )
            ),
            scale=3,
            window_size=(1200, 1600),
        )
        print(
            "Created",
            os.path.abspath(
                os.path.join(
                    "../../", config["path_output"]["path_figure"], "fig_table_h_k_arl1.png"
                )
            ),
        )

    return table_great_table_ARL1_k.as_raw_html()