#!/usr/bin/python3

import numpy as np
import pandas as pd

# Read CSV file generated by table-generator
df = pd.read_csv("./csv/Application.table.csv", sep="\t")
df = df.fillna(value=np.nan)
# Drop useless rows
df = df.drop([0, 1])
# Erase CPU time if the task is not done
df["DC-SSAT .1"][df["DC-SSAT "] != "done"] = np.nan
df["ABC .1"][df["ABC "] != "done"] = np.nan
df["ABC .6"][df["ABC .5"] != "done"] = np.nan
# Erase lower bound if it equals 0.0
df["ABC .3"][df["ABC .3"] == "0.000000"] = np.nan
df["ABC .8"][df["ABC .8"] == "0.000000"] = np.nan
# Erase time for lower bound if it equals 0.0
df["ABC .4"][df["ABC .4"] == "0.00"] = np.nan
df["ABC .9"][df["ABC .9"] == "0.00"] = np.nan
# Drop useless columns and rename left columns
df = df.drop(["DC-SSAT "], axis=1)
df = df.drop(["ABC "], axis=1)
df = df.drop(["ABC .5"], axis=1)
df.columns = [
    "Formula",
    "DC-T",
    "DC-P",
    "erSSAT-T",
    "erSSAT-P",
    "erSSAT-L",
    "erSSAT-TL",
    "erSSATb-T",
    "erSSATb-P",
    "erSSATb-L",
    "erSSATb-TL",
]
# Drop all-empty rows
df = df.dropna(how="all", subset=df.columns[1:])
# Write out parsed csv files for conformant, MaxCount, and MPEC
# Family conformant
df[df["Formula"].str.contains("ere-conformant/")].replace(
    regex=r"ere-conformant/", value=""
).replace(regex=r"\.sdimacs", value="").replace(regex=r"_", value="\_").to_csv(
    "./csv/parsed-conformant.csv", sep="\t", na_rep="nan", index=False
)
# Family MaxCount
df[df["Formula"].str.contains("ere-MaxCount/")].replace(
    regex=r"ere-MaxCount/", value=""
).replace(regex=r"\.sdimacs", value="").to_csv(
    "./csv/parsed-MaxCount.csv", sep="\t", na_rep="nan", index=False
)
# Family MPEC
df[df["Formula"].str.contains("ere-MPEC/")].replace(
    regex=r"ere-MPEC/", value=""
).replace(regex=r"\.sdimacs", value="").replace(regex=r"^ere-", value="").replace(
    regex=r"-0.125", value=""
).replace(
    regex=r"mem_ctrl", value="mem\_ctrl"
).to_csv(
    "./csv/parsed-MPEC.csv", sep="\t", na_rep="nan", index=False
)
