#!/usr/bin/env python

import sys
import os
from subprocess import *

sLibPath = "/home/scps/Documents/libsvm-3.17/"

def svm(train_pathname, test_pathname):
    svmscale_exe = sLibPath + "svm-scale"
    svmtrain_exe = sLibPath + "svm-train"
    svmpredict_exe = sLibPath + "svm-predict"
    grid_py = sLibPath + "tools/grid.py"
    gnuplot_exe = "/usr/bin/gnuplot"

    assert os.path.exists(svmscale_exe),"svm-scale executable not found"
    assert os.path.exists(svmtrain_exe),"svm-train executable not found"
    assert os.path.exists(svmpredict_exe),"svm-predict executable not found"
    assert os.path.exists(gnuplot_exe),"gnuplot executable not found"
    assert os.path.exists(grid_py),"grid.py not found"
    
    print train_pathname
    assert os.path.exists(train_pathname),"training file not found"
    file_name = os.path.split(train_pathname)[1]
    scaled_file = file_name + ".scale"
    model_file = file_name + ".model"
    range_file = file_name + ".range"
    
    file_name = os.path.split(test_pathname)[1]
    assert os.path.exists(test_pathname),"testing file not found"
    scaled_test_file = file_name + ".scale"
    predict_test_file = file_name + ".predict"
    
    cmd = '{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file)
    print('Scaling training data...')
    Popen(cmd, shell = True, stdout = PIPE).communicate()	
    clf = 0
    typ = 2
    cmd = '{0} -v 5 -s {1} -t {2} -svmtrain "{3}" -gnuplot "{4}" "{5}"'.format(grid_py, clf, typ, svmtrain_exe, gnuplot_exe, scaled_file)
    print('Cross validation...')
    f = Popen(cmd, shell = True, stdout = PIPE).stdout
    
    line = ''
    while True:
    	last_line = line
    	line = f.readline()
    	if not line: break
    c,g,rate = map(float,last_line.split())
    
    print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate))
    
    cmd = '{0} -s {1} -t {2} -c {3} -g {4} "{5}" "{6}"'.format(svmtrain_exe, clf, typ, c, g, scaled_file, model_file)
    #cmd = '{0} "{1}" "{2}"'.format(svmtrain_exe,scaled_file,model_file)
    print('Training...')
    Popen(cmd, shell = True, stdout = PIPE).communicate()
    
    print('Output model: {0}'.format(model_file))
    # if len(sys.argv) > 2:
    cmd = '{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file)
    print('Scaling testing data...')
    Popen(cmd, shell = True, stdout = PIPE).communicate()	
    
    cmd = '{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file)
    print('Testing...')
    Popen(cmd, shell = True).communicate()
    print('Output prediction: {0}'.format(predict_test_file))



75a4233523259ef1eb397858286a259dd4279ea1
#!/usr/bin/env python

import sys
import os
from subprocess import *

sLibPath = "/home/scps/Documents/libsvm-3.17/"

def svm(train_pathname, test_pathname):
    svmscale_exe = sLibPath + "svm-scale"
    svmtrain_exe = sLibPath + "svm-train"
    svmpredict_exe = sLibPath + "svm-predict"
    grid_py = sLibPath + "tools/grid.py"
    gnuplot_exe = "/usr/bin/gnuplot"

    assert os.path.exists(svmscale_exe),"svm-scale executable not found"
    assert os.path.exists(svmtrain_exe),"svm-train executable not found"
    assert os.path.exists(svmpredict_exe),"svm-predict executable not found"
    assert os.path.exists(gnuplot_exe),"gnuplot executable not found"
    assert os.path.exists(grid_py),"grid.py not found"
    
    print train_pathname
    assert os.path.exists(train_pathname),"training file not found"
    file_name = os.path.split(train_pathname)[1]
    scaled_file = file_name + ".scale"
    model_file = file_name + ".model"
    range_file = file_name + ".range"
    
    file_name = os.path.split(test_pathname)[1]
    assert os.path.exists(test_pathname),"testing file not found"
    scaled_test_file = file_name + ".scale"
    predict_test_file = file_name + ".predict"
    
    cmd = '{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file)
    print('Scaling training data...')
    Popen(cmd, shell = True, stdout = PIPE).communicate()	
    clf = 0
    typ = 2
    cmd = '{0} -v 5 -s {1} -t {2} -svmtrain "{3}" -gnuplot "{4}" "{5}"'.format(grid_py, clf, typ, svmtrain_exe, gnuplot_exe, scaled_file)
    print('Cross validation...')
    f = Popen(cmd, shell = True, stdout = PIPE).stdout
    
    line = ''
    while True:
    	last_line = line
    	line = f.readline()
    	if not line: break
    c,g,rate = map(float,last_line.split())
    
    print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate))
    
    cmd = '{0} -s {1} -t {2} -c {3} -g {4} "{5}" "{6}"'.format(svmtrain_exe, clf, typ, c, g, scaled_file, model_file)
    #cmd = '{0} "{1}" "{2}"'.format(svmtrain_exe,scaled_file,model_file)
    print('Training...')
    Popen(cmd, shell = True, stdout = PIPE).communicate()
    
    print('Output model: {0}'.format(model_file))
    # if len(sys.argv) > 2:
    cmd = '{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file)
    print('Scaling testing data...')
    Popen(cmd, shell = True, stdout = PIPE).communicate()	
    
    cmd = '{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file)
    print('Testing...')
    Popen(cmd, shell = True).communicate()
    print('Output prediction: {0}'.format(predict_test_file))


