def test_bias_dataframe():
    "Make sure the data is loaded properly"
    fname = DATADIR.joinpath("bfw-datatable.pkl")
    data = load_bfw_datatable(fname)
    assert len(data) == 923898
    npt.assert_allclose(data["score"].mean(), 0.5864211374778945)
import numpy as np

import facebias.utils as futils
from facebias.iotools import load_bfw_datatable

version = "0.1.5"
dir_data = "../../data/bfw/meta/"
f_datatable = f"{dir_data}bfw-v{version}-datatable.pkl"

thresholds = np.linspace(0.15, 0.6, 1000)
data = load_bfw_datatable(f_datatable, default_score_col="sphereface")

# reorder for find_best_threshold(s
data = data.loc[data.a1 == data.a2]
# data = data[["p1", "p2", "score", "label", "fold"]]
global_threshold, _ = futils.find_best_threshold(
    thresholds, data[["p1", "p2", "score", "label", "fold"]])

running_scores = []
data["y1"] = (data["score"] > global_threshold).values.astype(int)
data.fold = data.fold.astype(int)
folds = data.fold.unique()

for k in folds:
    # for each fold
    df_test = data.loc[data.fold == k]
    y_pred = df_test["y1"]
    y_true = df_test["label"].values.astype(int)
    running_scores.append(sum((y_pred == y_true).astype(int)) / len(y_pred))
    print(running_scores[-1])
print(np.mean(running_scores))
import numpy as np
import pandas as pd
from facebias.utils import add_package_path
from sklearn.metrics import confusion_matrix, roc_curve

# add_package_path()

from facebias.iotools import load_bfw_datatable

global_threshold = 0.6

dir_data = "../../data/"
dir_features = f"{dir_data}features/senet50/"
f_datatable = f"{dir_data}bfw-datatable.pkl"

data = load_bfw_datatable(f_datatable, default_score_col="senet50")
data["yp0"] = (data["score"] > global_threshold).astype(int)

fpath = f"{dir_data}/interm/thresholds.pkl"
threholds = pd.read_pickle(fpath)
threholds.optimal_threshold = threholds.loc[
    threholds.optimal_threshold is None, "optimal_threshold"
] = global_threshold
data["yp3"] = (data["score"] > threholds["optimal_threshold"]).astype(int)

target_far_values = np.array([0.3, 0.1, 0.01, 0.001, 0.0001])

tags = (
    r"\textbf{\gls{af}}",
    r"\textbf{\gls{am}}",
    r"\textbf{\gls{bf}}",