import numpy as np
from sklearn.model_selection import train_test_split 
import numpy as np
from micromlgen import port
from sklearn.ensemble import RandomForestClassifier
from pretty_confusion_matrix import pp_matrix_from_data
import os
import glob
import pandas as pd

# Load data from folder
# Setup Directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def getData(pathData):
    data = np.loadtxt(pathData, delimiter=",", dtype=float)
    return data

def getAlldata(files):
    li = []
    
    for filename in files:
        df = pd.read_csv(filename, index_col=None, header=0)
        li.append(df)
        
    data = pd.concat(li, axis=0, ignore_index=True)
    
    return data.to_numpy()
    
noHuman = glob.glob("noHuman" + "/*.csv")
noHumanData = getAlldata(noHuman)
noHumanTarget = np.zeros(noHumanData.shape[0])

Human = glob.glob("Human" + "/*.csv")
humanData = getAlldata(Human)
HumanTarget = np.ones(humanData.shape[0])



features = np.concatenate((noHumanData, humanData), axis=0)
target = np.concatenate((noHumanTarget, HumanTarget), axis=0)
print("Sample size =", features.shape)

X, X_test, y, y_test = train_test_split(features, target, test_size=1./3, random_state=1)
print("Feature test size =",X_test.shape)

clf = RandomForestClassifier(n_estimators=10).fit(X, y)

test_pred = clf.predict(X_test)

pp_matrix_from_data(y_test, test_pred, cmap="winter_r")

c_code = port(clf)

with open('classifier.h', 'w') as file:
    file.write(c_code)