import numpy as np
from joblib import dump, load

from sklearn import preprocessing
import sklearn.metrics._scorer as _scorer

import argparse
random_seed = 21

if not hasattr(_scorer, "_passthrough_scorer") and hasattr(_scorer, "_PassthroughScorer"):
    _scorer._passthrough_scorer = _scorer._PassthroughScorer


def _patch_svr_compat(model):
    estimator = getattr(model, "best_estimator_", model)
    if hasattr(estimator, "probA_") and not hasattr(estimator, "_probA"):
        estimator._probA = estimator.probA_
    if hasattr(estimator, "probB_") and not hasattr(estimator, "_probB"):
        estimator._probB = estimator.probB_
    return model


parser = argparse.ArgumentParser(description='Test on a video with an SVR that has been trained on the LIVE-APV dataset with a feature dictionary generated by chipqa.py')
parser.add_argument('input_feature_file',help='File containing input features generated by chipqa.py (extension: .z)')
args = parser.parse_args()
sts_kurt_feats = load(args.input_feature_file)


sts_kurt_features = np.reshape(sts_kurt_feats['features'],(1,-1))
print(sts_kurt_features.shape)
#sts_kurt = chipqa_features[:,:109]
#chroma_avg = chipqa_features[:,109:125]
#grad_avg = chipqa_features[:,125:157]
#sigma_avg = chipqa_features[:,157:165]
#
#chroma_sd = chipqa_features[:,165:181]
#grad_sd = chipqa_features[:,181:213]
#sigma_sd = chipqa_features[:,213:221]
#
#sts_kurt_features = np.concatenate((chroma_avg,grad_avg,sigma_avg,chroma_sd,grad_sd,sigma_sd,sts_kurt),axis=1)
#print(sts_kurt_features.shape)

grid_svr = _patch_svr_compat(load("LIVE_Livestream_trained_svr.z"))
scaler =load("LIVE_Livestream_fitted_scaler.z") 
X_train = scaler.transform(sts_kurt_features)
print(X_train)
score = grid_svr.predict(X_train)
print(score)

