from pandas import *
import pandas as pd 
import statistics 

def labels(): 
    image_labels = read_csv("subtypes.csv")
    cols = image_labels[["Unnamed: 0", "ml_subtype"]]
    cur_patient = cols.iloc[0]["Unnamed: 0"]
    cur_subtype = [cols.iloc[0]["ml_subtype"]]
    patient_subtypes = []
    for index, row in cols.iterrows():
        if row["Unnamed: 0"] != cur_patient: 
            cur_subtype = list(filter(lambda x: x > 0, cur_subtype))
            if len(cur_subtype) == 0: 
                arr = [cur_patient, 0]
            else: 
                cur_subtype = statistics.mode(cur_subtype)
                arr = [cur_patient, cur_subtype]
            patient_subtypes.append(arr)
            cur_subtype = [row["ml_subtype"]]
            cur_patient = row["Unnamed: 0"]
        else: 
            cur_subtype.append(row["ml_subtype"])
    df = pd.DataFrame(patient_subtypes, columns = ["Patient", "Subtype"])
    df = df.astype(int)
    df.to_csv("patientLabels.csv")
labels()    