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, 10, 22, 27), 'numpy.asarray', 'np.asarray', ({(22, 21, 22, 26): 'preds'}, {}), '(preds)', True, 'import numpy as np\n'), ((34, 17, 34, 56), 'pandas.DataFrame', 'pd.DataFrame', ({(34, 30, 34, 55): "{'Quality': final_labels}"}, {}), "({'Quality': final_labels})", True, 'import pandas as pd\n'), ((54, 10, 54, 27), 'numpy.asarray', 'np.asarray', ({(54, 21, 54, 26): 'preds'}, {}), '(preds)', True, 'import numpy as np\n'), ((65, 17, 65, 61), 'pandas.DataFrame', 'pd.DataFrame', ({(65, 30, 65, 60): "{'is_duplicate': final_labels}"}, {}), "({'is_duplicate': final_labels})", True, 'import pandas as pd\n'), ((31, 34, 31, 74), 'sklearn.metrics.accuracy_score', 'accuracy_score', ({(31, 49, 31, 61): 'final_labels', (31, 62, 31, 73): 'test_labels'}, {}), '(final_labels, test_labels)', False, 'from sklearn.metrics import f1_score, accuracy_score\n'), ((32, 35, 32, 70), 'sklearn.metrics.f1_score', 'f1_score', ({(32, 44, 32, 56): 'final_labels', (32, 58, 32, 69): 'test_labels'}, {}), '(final_labels, test_labels)', False, 'from sklearn.metrics import f1_score, accuracy_score\n'), ((59, 24, 59, 43), 'numpy.mean', 'np.mean', ({(59, 32, 59, 42): 'preds[:, (i)]'}, {}), '(preds[:, (i)])', True, 'import numpy as np\n'), ((12, 22, 12, 59), 'utilities.tools.load_model', 'load_model', ({(12, 33, 12, 36): '(i + 1)', (12, 37, 12, 45): 'nb_words', (12, 46, 12, 58): 'n_h_features'}, {}), '(i + 1, nb_words, n_h_features)', False, 'from utilities.tools import load_model\n'), ((27, 30, 27, 49), 'numpy.mean', 'np.mean', ({(27, 38, 27, 48): 'preds[:, (i)]'}, {}), '(preds[:, (i)])', True, 'import numpy as np\n'), ((44, 22, 44, 59), 'utilities.tools.load_model', 'load_model', ({(44, 33, 44, 36): '(i + 1)', (44, 37, 44, 45): 'nb_words', (44, 46, 44, 58): 'n_h_features'}, {}), '(i + 1, nb_words, n_h_features)', False, 'from utilities.tools import load_model\n')]