from main_imports import MDScreen
from ProjectFiles.applibs import utils
from kivy.properties import StringProperty
import numpy as np
import pandas as pd
import os
import warnings
from sklearn.model_selection import train_test_split
warnings.filterwarnings('ignore')
utils.load_kv("Instruction.kv")

class Instruction_Screen(MDScreen):
    def btnA(self):
        dataset = pd.read_csv("heartDs/heart.csv")
        dataset["target"].describe()
        unique = dataset["target"].unique()
        predictors = dataset.drop("target", axis=1)
        target = dataset["target"]
        X_train, X_test, Y_train, Y_test = train_test_split(predictors, target, test_size=0.20, random_state=0)
        # V. Model Fitting
        from sklearn.ensemble import RandomForestClassifier
        from sklearn.metrics import accuracy_score

        rf = RandomForestClassifier()
        rf.fit(X_train, Y_train)
        Y_pred_rf = rf.predict(X_test)
        current_accuracy = round(accuracy_score(Y_pred_rf, Y_test) * 100, 2)

        rf = RandomForestClassifier()
        rf.fit(X_train, Y_train)
        Y_pred_rf = rf.predict(X_test)
        Y_pred_rf.shape

        score_rf = round(accuracy_score(Y_pred_rf, Y_test) * 100, 2)
    #    print("The accuracy: " + str(score_rf) + " %")

        cp = dataset["cp"].mean()
        restecg = dataset["restecg"].mean()
        thalach = dataset["thalach"].mean()
        exang = dataset["exang"].mean()
        oldpeak = dataset["oldpeak"].mean()
        slope = dataset["slope"].mean()
        ca = dataset["ca"].mean()
        thal = dataset["thal"].mean()

        age = self.txt2.text
        gender = StringProperty("")
        gender = format(self.gender)

        trestbps = self.txt3.text
        chol = self.txt4.text
        fbs = self.txt5.text
        new_input = [[age, gender, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]]
        pred = rf.predict(new_input)
        if (pred == 1):
            self.l1.text = "Prediction: Positive"
        else:
            self.l1.text = "Prediction: Negative"
    pass
