<jupyter_start><jupyter_text>Paper Image Q1: Known prop score, SCAR - IPW & CWA conf Import the rule wrappers<jupyter_code>dataset_name: str = "yago3_10"
target_relation: str = "created"
# target_relation: str = "participatedin"

label_frequency_list: List[float] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
random_seed: int = 3

is_pca_version: bool = False
pca_indicator: str = "not_pca"
true_conf: ConfidenceEnum = ConfidenceEnum.TRUE_CONF
conf_estimators_to_ignore: Set[ConfidenceEnum] = {ConfidenceEnum.ICW_CONF}<jupyter_output><empty_output><jupyter_text>Convert rule wrappers to dataframe<jupyter_code>root_dir_experiment_settings: str = KnownPropScoresSCARConstantLabelFreqFileNamer.get_dir_experiment_high_level(
    dataset_name=dataset_name,
    target_relation=target_relation,
    is_pca_version=is_pca_version
)

image_dir: str = KnownPropScoresSCARConstantLabelFreqFileNamer.get_dir_images(
    use_pca=is_pca_version,
    dataset_name=dataset_name,
)
if not os.path.exists(image_dir):
    os.makedirs(image_dir)
print_file_exists(image_dir)
df_rule_wrappers: pd.DataFrame = _get_rule_wrappers_as_dataframe(
    root_dir_experiment_settings=root_dir_experiment_settings,
    target_relation=target_relation
)
df_rule_wrappers.head()
df_rule_wrappers.columns
column_names_logistics: List[str] = [
    'target_relation',
    'label_frequency',
    'random_trial_index',
    "Rule"
]<jupyter_output><empty_output><jupyter_text>Which rules are included?<jupyter_code>list_of_rules: List[str] = list(df_rule_wrappers["Rule"].unique())
for rule_str in list_of_rules:
    print(rule_str)<jupyter_output>created(A,B) :- directed(A,B)
created(A,B) :- actedin(A,B),directed(A,B)<jupyter_text>Select only 1 rule<jupyter_code>index_of_rule_to_use = 1

df_rule_wrappers = df_rule_wrappers[
    (df_rule_wrappers["Rule"] == list_of_rules[index_of_rule_to_use])
    # & (df_rule_wrappers["random_trial_index"] == 0)
]
rule_name: str = list_of_rules[index_of_rule_to_use]<jupyter_output><empty_output><jupyter_text>True Conf vs non-pca estimators<jupyter_code>conf_estimators_non_pca_comparison = [
    ConfidenceEnum.IPW_CONF,
    ConfidenceEnum.CWA_CONF,
]

confs_non_pca_comparison = [ConfidenceEnum.TRUE_CONF ] + conf_estimators_non_pca_comparison

column_names_non_pca_confidence_comparison = [
    conf.get_name()
    for conf in confs_non_pca_comparison
]
df_non_pca_conf_comp = df_rule_wrappers[
    column_names_logistics + column_names_non_pca_confidence_comparison
]
df_non_pca_conf_comp.head()
df_non_pca_conf_comp_melted = df_non_pca_conf_comp.melt(
    id_vars=column_names_logistics,
    var_name='confidence_type',
    value_name='confidence_value'
)
df_non_pca_conf_comp_melted.head()
n_rules = len(df_non_pca_conf_comp["Rule"].unique())
print(n_rules)

n_random_trials = len(df_non_pca_conf_comp["random_trial_index"].unique())
print(n_random_trials)
column_names_info =ColumnNamesInfo(
        true_conf=true_conf,
        column_name_true_conf=true_conf.get_name(),
        conf_estimators=conf_estimators_non_pca_comparison,
        column_names_conf_estimators=[
            col.get_name()
            for col in conf_estimators_non_pca_comparison
        ],
        column_names_logistics=column_names_logistics
    )

df_diff_to_true_conf_melted: pd.DataFrame = get_df_diffs_between_true_conf_and_confidence_estimators_melted(
    df_rule_wrappers=df_non_pca_conf_comp,
    column_names_info = column_names_info
)<jupyter_output><empty_output><jupyter_text>Plot True conf, CWA conf and IPW conf<jupyter_code>from kbc_pul.confidence_naming import get_color_dict_q1

color_dict_q1: Dict[ConfidenceEnum, str] = get_color_dict_q1()

color_palette = {
    conf.get_name(): color_dict_q1[conf]
    for conf in confs_non_pca_comparison
}

from matplotlib.axes import Axes
from matplotlib.figure import Figure

figsize = (6, 3)
fig, axes = plt.subplots(1,1, figsize=figsize)

error_metric_label: str = get_confidence_difference_label_string(true_conf)

fig: Figure
axes: Axes
ax_confs: Axes = sns.lineplot(
    x="label_frequency",
    y='confidence_value',
    hue="confidence_type",
    # hue_order=order,
    palette=color_palette,
    marker="o",
    data=df_non_pca_conf_comp_melted,
    ax=axes
)
ax_confs.set_xlim([0.0, 1.0])
ax_confs.set_ylim([0.0, None])
ax_confs.set_xlabel(f'label frequency c')
# ax_confs.set_ylabel("$\widehat{conf}(R)$")
ax_confs.set_ylabel("")
ax_confs.set_title(
    "$\widehat{conf}(R)$",
    loc="left"
     # f" for {n_rules} rules predicting {target_relation}\n"
     # f" (Avg over {n_random_trials} random selections)"
             )

# ax_squared_error: Axes = sns.lineplot(
#     x="label_frequency",
#     y='squared_error',
#     hue="error_type",
#     # hue_order=order,
#     palette=color_palette,
#     marker="o",
#     data=df_diff_to_true_conf_melted,
#     ax=axes[1]
# )
# # ax_confs.legend_ = leg
#
#
# ax_squared_error.set_xlim([0.0, 1.0])
# ax_squared_error.set_ylim([0.0, None])
# ax_squared_error.set_xlabel(f'label frequency c')
# ax_squared_error.set_ylabel(error_metric_label)
# ax_squared_error.set_title(
#     "" + error_metric_label + ""
#              # f" for {n_rules} rules predicting {target_relation}\n"
#              f" (Avg over {n_random_trials} random selections)"
# )
# ax_squared_error.get_legend().remove()

plt.suptitle(f"{rule_name}")
plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')

plt.tight_layout(rect=[0, 0.03, 1, 0.95])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10976948408342481, 'fraction_numerical': 0.011159897548481523, 'mean_word_length': 6.0, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '88736', 'license': 'Apache-2.0', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2143, 'n_words': 282, 'path': 'notebooks/artificial_bias_experiments/known_prop_scores/scar/images_paper/known_prop_score_scar_ipw_and_cwa_conf.ipynb', 'repo_name': 'ML-KULeuven/KBC-as-PU-Learning', 'size': '5466'}