<jupyter_start><jupyter_text>Noisy SAR 2 groups - paper table<jupyter_code>dataset_name="yago3_10"
is_pca_version: bool = False


true_prop_score_in_filter = 0.5
true_prop_score_other_list = [0.3, .7]

# true_prop_scores = PropScoresTwoSARGroups(
#     in_filter=true_prop_score_in_filter,
#     other=true_prop_score_other
# )

noisy_prop_score_in_filter: float = true_prop_score_in_filter
noisy_prop_score_not_in_filter_list: List[float] = [0.1, 0.2, .3, .4, .5, .6, .7, .8, .9, 1]

root_experiment_dir: str = os.path.join(
    get_root_dir_experiment_noisy_propensity_scores(),
    'sar_two_subject_groups',
    dataset_name

)
path_root_experiment_dir = Path(root_experiment_dir)

true_prop_score_other_to_df_map: Dict[float, pd.DataFrame] = dict()
df_list_complete: List[pd.DataFrame] = []

for true_prop_score_other in true_prop_score_other_list:
    true_prop_scores = PropScoresTwoSARGroups(
        in_filter=true_prop_score_in_filter,
        other=true_prop_score_other
    )

    # df_list: List[pd.DataFrame] = []

    for target_rel_path in path_root_experiment_dir.iterdir():
        if target_rel_path.is_dir():
            for filter_dir in target_rel_path.iterdir():
                if filter_dir.is_dir():
                    target_relation = target_rel_path.name
                    filter_relation = filter_dir.name
                    print(f"{target_relation} - {filter_relation}")
                    try:
                        experiment_info =  NoisyPropScoresSARExperimentInfo(
                           dataset_name=dataset_name,
                            target_relation=target_relation,
                            filter_relation=filter_relation,
                            true_prop_scores=true_prop_scores,
                            noisy_prop_score_in_filter=noisy_prop_score_in_filter,
                            noisy_prop_score_not_in_filter_list=noisy_prop_score_not_in_filter_list,
                            is_pca_version=is_pca_version
                        )
                        df_rule_wrappers_tmp = load_df_noisy_prop_scores_two_groups(
                            experiment_info=experiment_info
                        )
                        df_list_complete.append(df_rule_wrappers_tmp)
                    except Exception as err:
                        print(err)


df_rule_wrappers_all_targets: pd.DataFrame = pd.concat(df_list_complete, axis=0)
# true_prop_score_other_to_df_map[true_prop_score_other] = df_for_true_prop_score_other
df_rule_wrappers_all_targets.head()
df_rule_wrappers_all_targets.columns
column_names_logistics: List[str] = [
    'target_relation',
    'filter_relation',
    'true_prop_scores_in_filter', 'true_prop_scores_not_in_filter',
    'noisy_prop_scores_in_filter', 'noisy_prop_scores_not_in_filter',

    'random_trial_index',
    "Rule"
]
other_columns = [col for col in df_rule_wrappers_all_targets.columns if col not in column_names_logistics]
resorted_columns = column_names_logistics + other_columns
df_rule_wrappers_all_targets = df_rule_wrappers_all_targets[resorted_columns]
df_rule_wrappers_all_targets.head()
df_rule_wrappers_all_targets.rename(
    columns={
            'true_prop_scores_in_filter': "true_filter",
        'true_prop_scores_not_in_filter': "true_other",
    'noisy_prop_scores_in_filter': "noisy_filter", 'noisy_prop_scores_not_in_filter': "noisy_other",
    },
    inplace=True,
    errors="ignore"
)
column_names_logistics: List[str] = [
    'target_relation',
    'filter_relation',
    'true_filter', 'true_other',
    'noisy_filter', 'noisy_other',
    'random_trial_index',
    "Rule"
]
df_rule_wrappers_all_targets.head()<jupyter_output><empty_output><jupyter_text>2. Only keep a subset of rules 2.1. Only keep the non-recursive rules; drop recursive rules<jupyter_code>from kbc_pul.data_structures.rule_wrapper import get_pylo_rule_from_string, is_pylo_rule_recursive


def is_rule_recursive(rule_string: str) -> bool:
        pylo_rule: PyloClause = get_pylo_rule_from_string(rule_string)
        is_rule_recursive = is_pylo_rule_recursive(pylo_rule)
        return is_rule_recursive


mask_recursive_rules = df_rule_wrappers_all_targets.apply(
    lambda row: is_rule_recursive(row["Rule"]),
    axis=1
)
print(len(df_rule_wrappers_all_targets))
df_rule_wrappers_all_targets: pd.DataFrame = df_rule_wrappers_all_targets[~mask_recursive_rules]
print(len(df_rule_wrappers_all_targets))<jupyter_output>47530
15300<jupyter_text>2.3 Drop the Pair-positive columns (both directions)<jupyter_code>df_rule_wrappers_all_targets.drop(
        [ConfidenceEnum.TRUE_CONF_BIAS_YS_ZERO_S_TO_O.value,
         ConfidenceEnum.TRUE_CONF_BIAS_YS_ZERO_O_TO_S.value],
        axis=1,
        inplace=True,
        errors='ignore'
)
df_rule_wrappers_all_targets.head()<jupyter_output><empty_output><jupyter_text>2.4 Drop the IPW-PCA columns (both directions)<jupyter_code>df_rule_wrappers_all_targets.drop(
        [ConfidenceEnum.IPW_PCA_CONF_S_TO_O.value,
         ConfidenceEnum.IPW_PCA_CONF_O_TO_S.value],
        axis=1,
        inplace=True,
        errors='ignore'
)
df_rule_wrappers_all_targets.head()<jupyter_output><empty_output><jupyter_text>2.4 Drop the $c_{q}=0.5$ column<jupyter_code>df_rule_wrappers_all_targets.drop(
        ["true_filter", "noisy_filter"],
        axis=1,
        inplace=True,
        errors='ignore'
)
column_names_logistics = [
    col for col in column_names_logistics
    if col != "true_filter"
       and col != "noisy_filter"
]
df_rule_wrappers_all_targets.head()
group_by_list =  [
    "target_relation",
    "filter_relation",
    'true_other',
    'noisy_other',
    "Rule",
    "random_trial_index"
]
df_count_trials: pd.DataFrame = df_rule_wrappers_all_targets[
      [
        "target_relation",
        "filter_relation",
        'true_other',
        'noisy_other',
        "Rule",
        "random_trial_index"
    ]
].groupby(
    [
    "target_relation",
    "filter_relation",
    'true_other',
    'noisy_other',
    "Rule",
    ]
).count().reset_index()
df_less_than_ten_trials: pd.DataFrame = df_count_trials[df_count_trials["random_trial_index"].values != 10]
df_less_than_ten_trials
df_rule_wrappers_all_targets = df_rule_wrappers_all_targets[
    ~(
    (df_rule_wrappers_all_targets["target_relation"] == "isaffiliatedto")
    &
    (df_rule_wrappers_all_targets["filter_relation"] == "wasbornin")
    &
    (df_rule_wrappers_all_targets["Rule"]=="isaffiliatedto(A,B) :- playsfor(A,B)")
    )
]
df_rule_wrappers_all_targets.head()<jupyter_output><empty_output><jupyter_text>**Now, we have the full dataframe******  Calculate $[conf(R) - \widehat{conf}(R)]$<jupyter_code>true_conf: ConfidenceEnum = ConfidenceEnum.TRUE_CONF

conf_estimators_list: List[ConfidenceEnum] = [
    ConfidenceEnum.CWA_CONF,
    ConfidenceEnum.ICW_CONF,
    ConfidenceEnum.PCA_CONF_S_TO_O,
    ConfidenceEnum.PCA_CONF_O_TO_S,
     ConfidenceEnum.IPW_CONF,
]
all_confs_list: List[ConfidenceEnum] = [ConfidenceEnum.TRUE_CONF ] + conf_estimators_list

column_names_all_confs: List[str] = [
    conf.get_name()
    for conf in all_confs_list
]
df_rule_wrappers_all_targets = df_rule_wrappers_all_targets[
    column_names_logistics + column_names_all_confs
]
df_rule_wrappers_all_targets.head()
df_conf_estimators_true_other = df_rule_wrappers_all_targets[
    df_rule_wrappers_all_targets["true_other"] == df_rule_wrappers_all_targets["noisy_other"]
]
df_conf_estimators_true_other.head()
column_names_info =ColumnNamesInfo(
        true_conf=true_conf,
        column_name_true_conf=true_conf.get_name(),
        conf_estimators=conf_estimators_list,
        column_names_conf_estimators=[
            col.get_name()
            for col in conf_estimators_list
        ],
        column_names_logistics=column_names_logistics
    )

def get_df_rulewise_squared_diffs_between_true_conf_and_conf_estimator(
        df_rule_wrappers: pd.DataFrame,
        column_names_info: ColumnNamesInfo
) -> pd.DataFrame:
    df_rulewise_diffs_between_true_conf_and_conf_estimator: pd.DataFrame = df_rule_wrappers[
        column_names_info.column_names_logistics
    ]

    col_name_estimator: str
    for col_name_estimator in column_names_info.column_names_conf_estimators:
        df_rulewise_diffs_between_true_conf_and_conf_estimator \
            = df_rulewise_diffs_between_true_conf_and_conf_estimator.assign(
                **{
                    col_name_estimator: (
                        (df_rule_wrappers[column_names_info.column_name_true_conf]
                            - df_rule_wrappers[col_name_estimator]) ** 2
                    )
                }
            )
    return df_rulewise_diffs_between_true_conf_and_conf_estimator

df_conf_squared_errors: pd.DataFrame = get_df_rulewise_squared_diffs_between_true_conf_and_conf_estimator(
    df_rule_wrappers=df_rule_wrappers_all_targets,
    column_names_info = column_names_info
)
df_conf_squared_errors.head()<jupyter_output><empty_output><jupyter_text>AVERAGE the PCA(S) and PCA(O)<jupyter_code>df_conf_squared_errors["PCA"] = (
    (
            df_conf_squared_errors[ConfidenceEnum.PCA_CONF_S_TO_O.value]
            +
            df_conf_squared_errors[ConfidenceEnum.PCA_CONF_O_TO_S.value]
    ) / 2
)
df_conf_squared_errors.head()
df_conf_squared_errors = df_conf_squared_errors.drop(
        columns=[
            ConfidenceEnum.PCA_CONF_S_TO_O.value
            +
            ConfidenceEnum.PCA_CONF_O_TO_S.value
            ],
        axis=1,
        errors='ignore'
    )
df_conf_squared_errors.head()<jupyter_output><empty_output><jupyter_text>Now start averaging<jupyter_code>df_conf_squared_errors_avg_over_trials: pd.DataFrame = df_conf_squared_errors.groupby(
    by=["target_relation", "filter_relation", 'true_other', "noisy_other", "Rule"],
    sort=True,
    as_index=False
).mean()

df_conf_squared_errors_avg_over_trials.head()
df_conf_squared_errors_avg_over_trials_and_rules: pd.DataFrame = df_conf_squared_errors_avg_over_trials.groupby(
    by=["target_relation", "filter_relation", 'true_other', "noisy_other",],
    sort=True,
    as_index=False
).mean()
df_conf_squared_errors_avg_over_trials_and_rules.head()
len(df_conf_squared_errors_avg_over_trials_and_rules)<jupyter_output><empty_output><jupyter_text>How many $p$, $q$ combinations are there?<jupyter_code>df_p_and_q = df_conf_squared_errors_avg_over_trials_and_rules[["target_relation", "filter_relation"]].drop_duplicates()
df_p_and_q.head()
len(df_p_and_q)
df_conf_errors_avg_over_trials_and_rules_and_q: pd.DataFrame = df_conf_squared_errors_avg_over_trials_and_rules.groupby(
    by=["target_relation", 'true_other', "noisy_other",],
    sort=True,
    as_index=False
).mean()
df_conf_errors_avg_over_trials_and_rules_and_q.head()

len(df_conf_errors_avg_over_trials_and_rules_and_q)<jupyter_output><empty_output><jupyter_text>Subset of noisy_other<jupyter_code>first_true_label_freq_to_include = 0.3
second_true_label_freq_to_include = 0.7


true_label_frequencies_set: Set[float] = {
    first_true_label_freq_to_include, second_true_label_freq_to_include,
}
true_label_frequency_to_estimate_map: Dict[float, Set[float]] = dict()

label_frequency_est_diff: float = 0.1
label_frequencies_to_keep: Set[float] = set(true_label_frequencies_set)
for true_label_freq in true_label_frequencies_set:
    true_label_frequency_to_estimate_map[true_label_freq] = {
        round(true_label_freq - label_frequency_est_diff, 1),
        round(true_label_freq + label_frequency_est_diff, 1)
    }
    label_frequencies_to_keep.update(true_label_frequency_to_estimate_map[true_label_freq])
df_conf_errors_avg_over_trials_and_rules_and_q_c_subset = df_conf_errors_avg_over_trials_and_rules_and_q[
    df_conf_errors_avg_over_trials_and_rules_and_q["noisy_other"].isin(label_frequencies_to_keep)
]
df_conf_errors_avg_over_trials_and_rules_and_q_c_subset.head()
len(df_conf_errors_avg_over_trials_and_rules_and_q_c_subset)<jupyter_output><empty_output><jupyter_text>Count the rules per $p$<jupyter_code>df_n_rules_per_target = df_rule_wrappers_all_targets[["target_relation", "Rule"]].groupby(
    by=['target_relation'],
    # sort=True,
    # as_index=False
)["Rule"].nunique().to_frame().reset_index().rename(
    columns={"Rule" : "# rules"}
)


df_n_rules_per_target.head()<jupyter_output><empty_output><jupyter_text>**** Format pretty tableGoal:* put smallest value per row in BOLT* per target: mean_value 0.3 / 0.4<jupyter_code>true_label_freq_to_noisy_to_df_map: Dict[float, Dict[float, pd.DataFrame]] = dict()
for true_label_freq in true_label_frequencies_set:
    df_true_tmp: pd.DataFrame = df_conf_errors_avg_over_trials_and_rules_and_q_c_subset[
        df_conf_errors_avg_over_trials_and_rules_and_q_c_subset["true_other"] == true_label_freq
    ]
    noisy_label_freq_to_df_map = dict()
    true_label_freq_to_noisy_to_df_map[true_label_freq] = noisy_label_freq_to_df_map

    df_true_and_noisy_tmp = df_true_tmp[
        df_true_tmp["noisy_other"] == true_label_freq
    ]
    noisy_label_freq_to_df_map[true_label_freq] = df_true_and_noisy_tmp[
        [col for col in df_true_and_noisy_tmp.columns if col != "noisy_other" and col != "true_other"]
    ]

    for noisy_label_freq in true_label_frequency_to_estimate_map[true_label_freq]:
        df_true_and_noisy_tmp = df_true_tmp[
            df_true_tmp["noisy_other"] == noisy_label_freq
        ]
        noisy_label_freq_to_df_map[noisy_label_freq] = df_true_and_noisy_tmp[
            [col for col in df_true_and_noisy_tmp.columns if col != "noisy_other" and col != "true_other"]
        ]

true_label_freq_to_noisy_to_df_map[first_true_label_freq_to_include][0.2].head()
from typing import Iterator

true_label_freq_to_df_map = dict()

label_freq_estimators: Iterator[float]
for true_label_freq in true_label_frequencies_set:
    noisy_to_df_map: Dict[float, pd.DataFrame]  = true_label_freq_to_noisy_to_df_map[true_label_freq]

    df_true_label_freq: pd.DataFrame = noisy_to_df_map[true_label_freq]

    lower_est: float = round(true_label_freq - label_frequency_est_diff, 1)
    higher_est: float = round(true_label_freq + label_frequency_est_diff, 1)


    df_lower: pd.DataFrame = noisy_to_df_map[lower_est][
        ['target_relation', ConfidenceEnum.IPW_CONF.value]
    ].rename(
        columns={
            ConfidenceEnum.IPW_CONF.value: f"{ConfidenceEnum.IPW_CONF.value}_lower"
        }
    )

    df_true_label_freq = pd.merge(
        left=df_true_label_freq,
        right=df_lower,
        on="target_relation"
    )

    df_higher = noisy_to_df_map[higher_est][
        ['target_relation', ConfidenceEnum.IPW_CONF.value]
    ].rename(
        columns={
            ConfidenceEnum.IPW_CONF.value: f"{ConfidenceEnum.IPW_CONF.value}_higher"
        }
    )
    df_true_label_freq = pd.merge(
        left=df_true_label_freq,
        right=df_higher,
        on="target_relation"
    )
    true_label_freq_to_df_map[true_label_freq] = df_true_label_freq

true_label_freq_to_df_map[0.3].head()
for key, df in true_label_freq_to_df_map.items():
    true_label_freq_to_df_map[key] = df.drop(
        columns=["random_trial_index"],
        axis=1,
        errors='ignore'
    )

df_one_row_per_target = pd.merge(
    left=true_label_freq_to_df_map[first_true_label_freq_to_include],
    right=true_label_freq_to_df_map[second_true_label_freq_to_include],
    on="target_relation",
    suffixes=(f"_{first_true_label_freq_to_include}", f"_{second_true_label_freq_to_include}")
)
df_one_row_per_target.head()<jupyter_output><empty_output><jupyter_text>What is the smallest value?<jupyter_code>all_values: np.ndarray = df_one_row_per_target[
    [ col
      for col in df_one_row_per_target.columns
      if col != "target_relation"
      ]
].values

min_val = np.amin(all_values)
min_val
min_val * 10000
max_val = np.amax(all_values)
max_val
max_val * 10000
df_one_row_per_target.head() * 10000
df_one_row_per_target.dtypes
exponent = 4

multiplication_factor = 10 ** exponent
multiplication_factor
df_one_row_per_target[
    df_one_row_per_target.select_dtypes(include=['number']).columns
] *= multiplication_factor

df_one_row_per_target
df_one_row_per_target.head()<jupyter_output><empty_output><jupyter_text>Output files definitions<jupyter_code>dir_latex_table: str = os.path.join(
    kbc_e_metrics_project_dir,
    "paper_latex_tables",
    'known_prop_scores',
    'sar_two_groups'
)

if not os.path.exists(dir_latex_table):
    os.makedirs(dir_latex_table)

filename_tsv_rule_stats = os.path.join(
    dir_latex_table,
    "conf_error_stats_v3.tsv"
)

filename_tsv_single_row_summary = os.path.join(
    dir_latex_table,
    "noisy_sar_two_groups_single_row_summary.tsv"
)<jupyter_output><empty_output><jupyter_text>Create single-row summary<jupyter_code>df_one_row_in_total: pd.Series = df_one_row_per_target.mean(
)


df_one_row_in_total
df_n_rules_per_target.head()
df_one_row_in_total["# rules"] = int(df_n_rules_per_target["# rules"].sum())
df_one_row_in_total
type(df_one_row_in_total)
df_one_row_in_total.to_csv(
    filename_tsv_single_row_summary,
    sep = "\t",
    header=None
)<jupyter_output><empty_output><jupyter_text>Now create a pretty table<jupyter_code>column_names_info.column_names_conf_estimators
simplified_column_names_conf_estimators = ['CWA', 'PCA', 'ICW', 'IPW',]
multi_index_columns = [
    ("$p$", ""),
    ("\# rules", "")
]
from itertools import product

# conf_upper_cols = column_names_info.column_names_conf_estimators + [
#     f"{ConfidenceEnum.IPW_CONF.value} " + "($\Delta c=-" + f"{label_frequency_est_diff}" + "$)",
#     f"{ConfidenceEnum.IPW_CONF.value} " + "($\Delta c=" + f"{label_frequency_est_diff}" + "$)",
# ]
conf_upper_cols = simplified_column_names_conf_estimators + [
    f"{ConfidenceEnum.IPW_CONF.value} " + "($-\Delta$)",
    f"{ConfidenceEnum.IPW_CONF.value} " + "($+\Delta$)",
]

c_subcols = ["$c_{\\neg q}=0.3$", "$c_{\\neg q}=0.7$"]

multi_index_columns = multi_index_columns + list(product(c_subcols, conf_upper_cols))
# multi_index_list

multi_index_columns = pd.MultiIndex.from_tuples(multi_index_columns)
multi_index_columns

rule_counter: int = 1
rule_str_to_rule_id_map: Dict[str, int] = {}


float_precision: int = 1

col_name_conf_estimator: str


pretty_rows: List[List] = []

row_index: int
row: pd.Series

# columns_to_use = [
#     "$p$",
#     "\# rules"
# ] + column_names_info.column_names_conf_estimators + [
#     f"{ConfidenceEnum.IPW_CONF.value} " + "($\Delta c=-" + f"{label_frequency_est_diff}" + "$)",
#     f"{ConfidenceEnum.IPW_CONF.value} " + "($\Delta c=" + f"{label_frequency_est_diff}" + "$)",
# ]
LabelFreq = float
def get_dict_with_smallest_estimator_per_label_freq(row: pd.Series) -> Dict[LabelFreq, Set[str]]:
    # Find estimator with smallest mean value for label frequency###################
    label_freq_to_set_of_smallest_est_map: Dict[LabelFreq, Set[str]] = dict()
    for label_freq in [first_true_label_freq_to_include, second_true_label_freq_to_include]:
        o_set_of_col_names_with_min_value: Optional[Set[str]] = None
        o_current_smallest_value: Optional[float] = None
        # Find smallest squared error
        for col_name_conf_estimator in simplified_column_names_conf_estimators:
            current_val: float = row[f"{col_name_conf_estimator}_{label_freq}"]
            # print(current_val)
            if o_set_of_col_names_with_min_value is None or o_current_smallest_value > current_val:
                o_set_of_col_names_with_min_value = {col_name_conf_estimator}
                o_current_smallest_value = current_val
            elif current_val == o_current_smallest_value:
                o_set_of_col_names_with_min_value.update(col_name_conf_estimator)

        label_freq_to_set_of_smallest_est_map[label_freq] = o_set_of_col_names_with_min_value
    return label_freq_to_set_of_smallest_est_map


def format_value_depending_on_whether_it_is_smallest(
        value: float,
        is_smallest: bool,
        float_precision: float,
        use_si: bool = False
)-> str:
    if is_smallest:
        if not use_si:
            formatted_value = "$\\bm{" + f"{value:0.{float_precision}f}" + "}$"
            # formatted_value = "$\\bm{" + f"{value:0.{float_precision}e}" + "}$"
        else:
            formatted_value = "\\textbf{$" + f"\\num[round-precision={float_precision},round-mode=figures,scientific-notation=true]"+\
                              "{"+ str(value) + "}"+ "$}"
    else:
        if not use_si:
            formatted_value = f"${value:0.{float_precision}f}$"
            # formatted_value = f"${value:0.{float_precision}e}$"
        else:
            formatted_value = "$" + f"\\num[round-precision={float_precision},round-mode=figures,scientific-notation=true]"+\
                              "{"+ str(value) + "}"+ "$"

    return formatted_value


estimator_columns = simplified_column_names_conf_estimators + [
    f"{ConfidenceEnum.IPW_CONF.value}_lower",
    f"{ConfidenceEnum.IPW_CONF.value}_higher"
]

# For each row, i.e. for each target relation
for row_index, row in df_one_row_per_target.iterrows():

    # Find estimator with smallest mean value for label frequency###################
    label_freq_to_set_of_smallest_est_map: Dict[float, Set[str]] = get_dict_with_smallest_estimator_per_label_freq(
        row=row
    )
    ##################################################################################
    # Construct the new row
    ######################

    target_relation = row["target_relation"]


    nb_of_rules = df_n_rules_per_target[df_n_rules_per_target['target_relation'] == target_relation][
        "# rules"
    ].iloc[0]

    new_row: List[str] = [
        target_relation,
        nb_of_rules
    ]
    # For each Confidence estimator, get the value at c 0.3 and 0.7
    # for col_name_conf_estimator in estimator_columns:
    #     mean_val_03:float = row[f"{col_name_conf_estimator}_0.3"]
    #     mean_val_07:float = row[f"{col_name_conf_estimator}_0.7"]
    #
    #     new_row_value = (
    #         format_value_depending_on_whether_it_is_smallest(
    #             value=mean_val_03,
    #             is_smallest=col_name_conf_estimator == label_freq_to_smallest_est_map[0.3],
    #             float_precision=float_precision
    #         )
    #         + " / "
    #         + format_value_depending_on_whether_it_is_smallest(
    #             value=mean_val_07,
    #             is_smallest=col_name_conf_estimator == label_freq_to_smallest_est_map[0.7],
    #             float_precision=float_precision
    #         )
    #     )
    #     new_row.append(new_row_value)
    for col_name_conf_estimator in estimator_columns:
        mean_val_03:float = row[f"{col_name_conf_estimator}_{first_true_label_freq_to_include}"]



        new_row_value_03 = format_value_depending_on_whether_it_is_smallest(
                value=mean_val_03,
                is_smallest=(
                        col_name_conf_estimator in label_freq_to_set_of_smallest_est_map[first_true_label_freq_to_include]
                ),
                float_precision=float_precision
        )
        new_row.append(new_row_value_03)


    for col_name_conf_estimator in estimator_columns:
        mean_val_07:float = row[f"{col_name_conf_estimator}_{second_true_label_freq_to_include}"]
        new_row_value_07 = format_value_depending_on_whether_it_is_smallest(
                value=mean_val_07,
                is_smallest=(
                        col_name_conf_estimator in label_freq_to_set_of_smallest_est_map[second_true_label_freq_to_include]
                ),
                float_precision=float_precision

        )
        new_row.append(new_row_value_07)

    pretty_rows.append(new_row)


df_pretty: pd.DataFrame = pd.DataFrame(
    data=pretty_rows,
    columns=multi_index_columns
)
df_pretty.head()
df_pretty: pd.DataFrame = df_pretty.sort_values(
    by=["$p$"]
)

df_pretty.head()<jupyter_output><empty_output><jupyter_text>To file<jupyter_code># dir_latex_table: str = os.path.join(
#     kbc_e_metrics_project_dir,
#     "paper_latex_tables",
#     'known_prop_scores',
#     'scar'
# )
#
# if not os.path.exists(dir_latex_table):
#     os.makedirs(dir_latex_table)

filename_latex_table: str = os.path.join(
    dir_latex_table,
    "confidence-error-table-sar-two-subject-groups-agg-per-p.tex"
)
filename_tsv_table: str = os.path.join(
    dir_latex_table,
    "confidence-error-table-sar-two-subject-groups-agg-per-p.tsv"
)


with open(filename_latex_table, "w") as latex_ofile:
    with pd.option_context("max_colwidth", 1000):
        latex_ofile.write(
            df_pretty.to_latex(
                column_format="lr|lllllll|lllllll",
                index=False,
                float_format="{:0.3f}".format,
                escape=False,
            #     caption="$[widehat{conf}-conf]^2$ for SCAR. "
            #             "std=standard confidence, "
            #             "PCA (S) = PCA confidence with $s$ as domain, "
            #             "PCA (O) = PCA confidence with $o$  as domain, "
            #             "IPW = PCA confidence with $\hat{e}=e$, "
            #             "IPW +/- $" + f"{label_frequency_est_diff:0.1}" + "$ = IPW confidence with $\hat{e}=e+/-" +  f"{label_frequency_est_diff:0.1}" + "$."
            )
        )

with open(filename_tsv_table, "w") as tsv_ofile:
    tsv_ofile.write(df_pretty.to_csv(
        index=False,
        sep="\t"
    ))

print(filename_latex_table)<jupyter_output>/home/joschout/Documents/Repos/KUL-PUL/paper_latex_tables/known_prop_scores/sar_two_groups/confidence-error-table-sar-two-subject-groups-agg-per-p.tex
StarcoderdataJupyterStructuredCleanDedup