from sklearn.metrics import f1_score,accuracy_score
import numpy as np
from utilities.tools import load_model
import pandas as pd

def predict_MSRP_test_data(n_models,nb_words,nlp_f,test_data_1,test_data_2,test_labels):

    models=[]
    n_h_features=nlp_f.shape[1]
    print('loading the models...')
    for i in range(n_models):
        models.append(load_model(i+1,nb_words,n_h_features))

    preds=[]
    print('predicting the test data...\n')
    i=0
    for m in models:
      i+=1
      preds_prob=m.predict([test_data_1, test_data_2,nlp_f], batch_size=64, verbose=0)
      preds.append(preds_prob[:,1])

    preds=np.asarray(preds)
    final_labels=np.zeros(len(test_data_1),dtype=int)

   #average the predicttion
    for i in range(len(test_data_1)):
        final_labels[i]=round(np.mean(preds[:,i]))
        if i%100==0:
            print(i ,' out of ',len(test_data_1))

    print("test data accuracy: ", accuracy_score(final_labels,test_labels))
    print("test data f_measure: ", f1_score(final_labels, test_labels))

    submission = pd.DataFrame({"Quality": final_labels})
    submission.to_csv("predictions/MSRP.tsv", index=True,index_label='test_id')


def predict_Quora_test_data(n_models,nb_words,nlp_f,test_data_1,test_data_2):

    models=[]
    n_h_features=nlp_f.shape[1]
    print('loading the models...')
    for i in range(n_models):
        models.append(load_model(i+1,nb_words,n_h_features))

    preds=[]
    print('predicting the test data...\n')
    i=0
    for m in models:
      i+=1
      preds_prob=m.predict([test_data_1, test_data_2,nlp_f], batch_size=125, verbose=0)
      preds.append(preds_prob[:,1])

    preds=np.asarray(preds)
    final_labels=np.zeros(len(test_data_1),dtype=float)

   #average the predicttion
    for i in range(len(test_data_1)):
        final_labels[i]=np.mean(preds[:,i])
        if i%10000==0:
            print(i ,' out of ',len(test_data_1))

    print('making the sumbission file')

    submission = pd.DataFrame({"is_duplicate": final_labels})
    submission.to_csv("predictions/Quora.tsv", index=True,index_label='test_id')
[(22, 'numpy.asarray', 'np.asarray', True, 'import numpy as np\n'), (34, 'pandas.DataFrame', 'pd.DataFrame', True, 'import pandas as pd\n'), (54, 'numpy.asarray', 'np.asarray', True, 'import numpy as np\n'), (65, 'pandas.DataFrame', 'pd.DataFrame', True, 'import pandas as pd\n'), (31, 'sklearn.metrics.accuracy_score', 'accuracy_score', False, 'from sklearn.metrics import f1_score, accuracy_score\n'), (32, 'sklearn.metrics.f1_score', 'f1_score', False, 'from sklearn.metrics import f1_score, accuracy_score\n'), (59, 'numpy.mean', 'np.mean', True, 'import numpy as np\n'), (12, 'utilities.tools.load_model', 'load_model', False, 'from utilities.tools import load_model\n'), (27, 'numpy.mean', 'np.mean', True, 'import numpy as np\n'), (44, 'utilities.tools.load_model', 'load_model', False, 'from utilities.tools import load_model\n')]