def private_inversion_accuracy()

in scripts/make_figures.py [0:0]


def private_inversion_accuracy(results_path, save_path):
    L2s = ['1e-5', '1e-3', '1e-1', '1']
    noise_scales = [
        '1e-05', '2e-05', '5e-05', '0.0001', '0.0002',
        '0.0005', '0.001', '0.002', '0.005', '0.01',
        '0.02', '0.05', '0.1', '0.2', '0.5', '1.0',
    ]

    inversion_results = load_results(
        results_path, f"iwpc_least_squares_inversion_l2_1e-3.json")[0]
    target = np.array(inversion_results["target"])
    baseline = (target == np.array(inversion_results["baseline"])).mean()

    # Load the max eta for each L2.
    mean_etas = []
    for l2 in L2s:
        etas = load_results(
            results_path, f"iwpc_least_squares_fil_l2_{l2}.json")["etas"]
        mean_etas.append(np.mean(etas))
    sigmas = np.array([float(ns) for ns in noise_scales])
    mean_etas = np.array(mean_etas)[:, None] / sigmas

    results = {"whitebox": {}, "fredrikson14": {}}
    for inverter in ["whitebox", "fredrikson14"]:
        results = []
        for l2 in L2s:
            # inversion results are in a list ordered by ieration or IRFIL,
            # each dictionary is the results at a given noise scale along with
            # the target values
            inversion_results = load_results(
                results_path,
                f"iwpc_least_squares_{inverter}_private_inversion_l2_{l2}.json")
            all_accs = []
            for noise_scale in noise_scales:
                accs = []
                for prediction in inversion_results[0][noise_scale]['predictions']:
                    accs.append((np.array(prediction) == target).mean())
                all_accs.append([np.mean(accs), np.std(accs)])
            results.append(all_accs)

        results = np.array(results) # [L2, noise scale, mean/std]
        means = results[:, :, 0]
        stds = results[:, :, 1]

        legend = ["$\lambda=10^{%d}$"%int(math.log10(float(l2))) for l2 in L2s]

        plotting.line_plot(
            means, mean_etas, legend=legend,
            xlabel="Mean $\\bar{\eta}$",
            ylabel="Attribute inversion accuracy",
            errors=stds,
            ymax=1.02,
            ymin=0.2,
            xlog=True,
            size=(5, 5))
        plt.semilogx([0, 1e3], [baseline]*2, 'k--', label="Prior")
        plt.legend()
        plt.xlim(right=1e3)
        plotting.savefig(os.path.join(
            args.save_path,
            f"iwpc_{inverter}_vs_eta_varying_l2"))