import json
import os
from locale import atof
import pandas as pd
import numpy as np

lines = open("results_pbc4.txt").readlines()
results = []
for line in lines:
    (
        dataset,
        accuracy,
        accuracy_std,
        time_spent,
        time_std,
        nodes,
        nodes_std,
    ) = line.split(";")
    data = pd.read_csv(
        os.path.join("data.r", f"{dataset}_R.dat"),
        sep="\t",
        index_col=0,
    )
    X = data.drop("clase", axis=1).to_numpy()
    y = data["clase"].to_numpy()
    samples, features = X.shape
    n_classes = len(np.unique(y))
    record = {}
    record["dataset"] = dataset
    record["samples"] = samples
    record["features"] = features
    record["classes"] = n_classes
    record["hyperparameters"] = {}
    record["nodes"] = atof(nodes)
    record["leaves"] = 0.0
    record["depth"] = 0.0
    record["score"] = atof(accuracy)
    record["score_std"] = atof(accuracy_std)
    record["time"] = atof(time_spent)
    record["time_std"] = atof(time_std)
    results.append(record)

output = {}
output["score_name"] = "accuracy"
output["title"] = "PBC4cip Ensemble execution"
output["model"] = "pbc4cip"
output["version"] = "1.0"
output["stratified"] = True
output["folds"] = 5
output["date"] = "2024-12-23"
output["time"] = "17:10:00"
output["duration"] = 1.25 * 3600
output["seeds"] = [57, 31, 1714, 17, 23, 79, 83, 97, 7, 1]
output["platform"] = "HugeFedora"
output["language"] = "java"
output["language_version"] = "openjdk 17.0.1"
output["discretization_algorithm"] = "mdlp"
output["smooth_strategy"] = "ORIGINAL"
output["discretized"] = False
output["results"] = results
file_name = "results_accuracy_pbc4cip_HugeFedora_2024-12-23_17:10:00_1.json"
with open(file_name, "w") as f:
    json.dump(output, f)
print(f"File {file_name} created.")
