# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

import glob
import pandas as pd
import numpy as np
# import scipy.sparse 
from sklearn.preprocessing import normalize
from sklearn.svm import SVC
from sklearn.metrics.pairwise import chi2_kernel
from sklearn.cluster import KMeans
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier
from sklearn.metrics import average_precision_score
from sklearn.feature_extraction import text
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
import pickle
import time
import os
import sys

os.chdir('/home/ubuntu/hw1_git/11775-hws/hw1_code')

os.getcwd()

#python2 scripts/train_svm.py $event "kmeans/" $feat_dim_mfcc mfcc_pred/svm.$event.model || exit 1;
sys.argv = ['cripts/train_svm.py', 'P001', 'kmeans/', '3', 'mfcc_pred/P001.model', 'mfcc']

if __name__ == '__main__':
    if len(sys.argv) != 6:
        print "Usage: {0} event_name feat_dir feat_dim output_file".format(sys.argv[0])
        print "event_name -- name of the event (P001, P002 or P003 in Homework 1)"
        print "feat_dir -- dir of feature files"
        print "feat_dim -- dim of features"
        print "output_file -- path to save the svm model"
        print "model_type -- mfcc or asrs"
        exit(1)

    event_name = sys.argv[1]
    feat_dir = sys.argv[2]
    feat_dim = int(sys.argv[3])
    output_file = sys.argv[4]
    model_type = sys.argv[5]
    print event_name
    print feat_dir
    print feat_dim
    print output_file
    print model_type


    print 'SVM trained successfully for event %s!' % (event_name)

if model_type == 'mfcc':
    train_mart = pd.read_csv('./datamart_train_k{}.csv'.format(feat_dim))
    valid_mart = pd.read_csv('./datamart_valid_k{}.csv'.format(feat_dim))
    test_mart  = pd.read_csv('./datamart_test_k{}.csv'.format(feat_dim))
    print(train_mart.shape, valid_mart.shape, test_mart.shape)
    LGBM_results_p001 = modeling_ap_lgbm(k=feat_dim, train_data = train_mart, valid_data = valid_mart, target = 'target_p001_10')
    Xgb_results_p002 = modeling_ap_xgb(k=feat_dim, train_data = train_mart, valid_data = valid_mart, target = 'target_p002_10')
    Xgb_results_p003 = modeling_ap_xgb(k=feat_dim, train_data = train_mart, valid_data = valid_mart, target = 'target_p003_10')
elif model_type == 'asrs':
    train_mart = pd.read_csv('./datamart_train_asrfeat_{}.csv'.format(feat_dim))
    valid_mart = pd.read_csv('./datamart_valid_asrfeat_{}.csv'.format(feat_dim))
    test_mart  = pd.read_csv('./datamart_test_asrfeat_{}.csv'.format(feat_dim))
    print(train_mart.shape, valid_mart.shape, test_mart.shape)
    Boost_results_p001 = modeling_ap_Boost(k=k, train_data = train_mart, valid_data = valid_mart, target = 'target_p001_10')
    LGBM_results_p002 = modeling_ap_lgbm(k=k, train_data = train_mart, valid_data = valid_mart, target = 'target_p002_10')
    SVM_results_p003 = modeling_ap_SVM(k=k, train_data = train_mart, valid_data = valid_mart, target = 'target_p003_10')

# print(train_mart.dtypes, valid_mart.dtypes, test_mart.dtypes)
print(train_mart.shape, valid_mart.shape, test_mart.shape)


# +
def modeling_ap_SVM(k, train_data, valid_data, target = 'target_p001_10'):
    start_time = time.time()
    k = k
    train_mart = train_data
    valid_mart = valid_data
    target = target
    
    X_train = train_mart.iloc[:,1:k+1]
    y_train = train_mart[target]
    X_valid = valid_mart.iloc[:,1:k+1]
    y_valid = valid_mart[target]
    
    model = SVC(kernel=chi2_kernel, probability=True)
    model.fit(X_train, y_train)
    y_preds = model.predict(X_valid)
    y_probs = model.predict_proba(X_valid)
    results = average_precision_score(y_true=y_valid.values, y_score=y_probs[:,1])
    print("===== The time consuming of SVM Modeling : {} seconds =====".format((time.time() - start_time)))   
    print(results)
    return results, y_probs, model

def modeling_ap_AdaB(k, train_data, valid_data, target = 'target_p001_10'):
    start_time = time.time()
    k = k
    train_mart = train_data
    valid_mart = valid_data
    target = target
    
    X_train = train_mart.iloc[:,1:k+1]
    y_train = train_mart[target]
    X_valid = valid_mart.iloc[:,1:k+1]
    y_valid = valid_mart[target]
    
    model = AdaBoostClassifier(n_estimators=200, random_state=0)
    model.fit(X_train, y_train)
    y_preds = model.predict(X_valid)
    y_probs = model.predict_proba(X_valid)
    results = average_precision_score(y_true=y_valid.values, y_score=y_probs[:,1])
    print("===== The time consuming of AdaBoosting Modeling : {} seconds =====".format((time.time() - start_time)))   
    print(results)
    return results, y_probs, model

def modeling_ap_Boost(k, train_data, valid_data, target = 'target_p001_10'):
    start_time = time.time()
    k = k
    train_mart = train_data
    valid_mart = valid_data
    target = target
    
    X_train = train_mart.iloc[:,1:k+1]
    y_train = train_mart[target]
    X_valid = valid_mart.iloc[:,1:k+1]
    y_valid = valid_mart[target]
    
    model = GradientBoostingClassifier(n_estimators=200, random_state=0)
    model.fit(X_train, y_train)
    y_preds = model.predict(X_valid)
    y_probs = model.predict_proba(X_valid)
    results = average_precision_score(y_true=y_valid.values, y_score=y_probs[:,1])
    print("===== The time consuming of Boosting Modeling : {} seconds =====".format((time.time() - start_time)))   
    print(results)
    return results, y_probs, model

def modeling_ap_xgb(k, train_data, valid_data, target = 'target_p001_10'):
    start_time = time.time()
    k = k
    train_mart = train_data
    valid_mart = valid_data
    target = target
    
    X_train = train_mart.iloc[:,1:k+1]
    y_train = train_mart[target]
    X_valid = valid_mart.iloc[:,1:k+1]
    y_valid = valid_mart[target]
    
    model = XGBClassifier()
    model.fit(X_train, y_train)
    y_preds = model.predict(X_valid)
    y_probs = model.predict_proba(X_valid)
    results = average_precision_score(y_true=y_valid.values, y_score=y_probs[:,1])
    print("===== The time consuming of XgBoosting Modeling : {} seconds =====".format((time.time() - start_time)))   
    print(results)
    return results, y_probs, model

def modeling_ap_lgbm(k, train_data, valid_data, target = 'target_p001_10'):
    start_time = time.time()
    k = k
    train_mart = train_data
    valid_mart = valid_data
    target = target
    
    X_train = train_mart.iloc[:,1:k+1]
    y_train = train_mart[target]
    X_valid = valid_mart.iloc[:,1:k+1]
    y_valid = valid_mart[target]
    
    model = LGBMClassifier(random_state=0, n_jobs=-1)
    model.fit(X_train, y_train)
    y_preds = model.predict(X_valid)
    y_probs = model.predict_proba(X_valid)
    results = average_precision_score(y_true=y_valid.values, y_score=y_probs[:,1])
    print("===== The time consuming of XgBoosting Modeling : {} seconds =====".format((time.time() - start_time)))   
    print(results)
    return results, y_probs, model


# -

precision, y_probs, model = modeling_ap_lgbm(k=feat_dim, train_data = train_mart, valid_data = valid_mart, target = 'target_p001_10')
# Xgb_results_p002 = modeling_ap_xgb(k=feat_dim, train_data = train_mart, valid_data = valid_mart, target = 'target_p002_10')
# Xgb_results_p003 = modeling_ap_xgb(k=feat_dim, train_data = train_mart, valid_data = valid_mart, target = 'target_p003_10')

foo = pd.Series({'Validation-AP':precision})
foo_y = pd.DataFrame(y_probs)
foo_y.columns = ['N', 'Y']
foo_y2 = pd.concat([valid_mart, foo_y], axis = 1)[['video', 'N', 'Y']]
foo.to_csv('./mfcc_pred/{}_k{}_model_Val_AP.csv'.format(event_name, feat_dim), index = False)
foo_y2.to_csv('./mfcc_pred/{}_k{}_model_Val_Scores.csv'.format(event_name, feat_dim), index = False)
# save the model to disk
filename = output_file+'.sav'
pickle.dump(model, open(filename, 'wb'))

pickle.dump(foo, open(filename, 'wb'))

# ## test_svm.py

#python2 scripts/test_svm.py mfcc_pred/$event.model "kmeans/" $feat_dim_mfcc mfcc_pred/${event}_mfcc.lst || exit 1;
sys.argv = ['scripts/test_svm.py', 'P001', 'kmeans/', '100', 'mfcc_pred/P001_mfcc.list', 'mfcc']

if __name__ == '__main__':
    if len(sys.argv) != 6:
        print "Usage: {0} model_file feat_dir feat_dim output_file".format(sys.argv[0])
        print "model_file -- path of the trained svm file"
        print "feat_dir -- dir of feature files"
        print "feat_dim -- dim of features; provided just for debugging"
        print "output_file -- path to save the prediction score"
        print "model_type -- mfcc or asrs"
        exit(1)

    model_file = sys.argv[1]
    feat_dir = sys.argv[2]
    feat_dim = int(sys.argv[3])
    output_file = sys.argv[4]
    model_type = sys.argv[5]
    print model_file
    print feat_dir
    print feat_dim
    print output_file
    print model_type

feat_dim = 100

if model_type == 'mfcc':
    test_mart  = pd.read_csv('./datamart_test_k{}.csv'.format(feat_dim))
    print('test datamart shape is', test_mart.shape)
elif model_type == 'asrs':
    test_mart  = pd.read_csv('./datamart_test_asrfeat_{}.csv'.format(feat_dim))
    print('test datamart shape is', test_mart.shape)

os.getcwd()

# some time later...
# load the model from disk
import pickle
filename = 'mfcc_pred/'+model_file+'.model.sav'
model = pickle.load(open(filename, 'rb'))
print(model)

test_mart  = pd.read_csv('./datamart_test_k{}.csv'.format(feat_dim))
X_test = test_mart.iloc[:,1:feat_dim+1]
X_test.head(3)

scores = model.predict_proba(X_test)
scores_df = pd.DataFrame(scores)
scores_df.columns = ['N', 'Y']
scores_total = pd.concat([test_mart, scores_df], axis = 1)
scores_total.head(3)

test_list = pd.read_csv('./list/test.video', header = None)
test_list.columns = ['video']
test_list.head(3)

final_list = test_list.merge(scores_total, how = 'left', on = 'video')[['video', 'Y']]
final_list.to_csv(output_file+'.csv', index = False)
final_list['Y'].to_csv(output_file+'.score'+'.csv', index = False, header = True)

final_list['Y']



StarcoderdataJupyterScriptsDedupFiltered