from sklearn import neighbors
import json
import directoryGetter as dg
from sklearn.externals import joblib

# testing
print 'testing kNN model ... ...'
knn = joblib.load('jingju_pitchContoursClassificationModel.pkl') 

testingFeaturesFolder = './testingFeatures/'
testingFeaturesfilenames = dg.jsonFilenameGetter(testingFeaturesFolder)

for testingFilename in testingFeaturesfilenames:
    with open(testingFilename) as data_file:    
        data = json.load(data_file)

        X = data['featureVec']
        y = data['toRmIndex']

    classification = knn.predict(X) # 0: voice 1: nonvoice
    classification = classification.tolist()
    
    outputFilename = testingFilename[:-5] + '_classification.json'
    with open(outputFilename, 'w') as outfile:
        data = {'featureVec': X, 'toRmIndex': y, 'classification': classification}
        json.dump(data, outfile)
# -*- coding: utf-8 -*-

import contourReader as cr
import featuresCalc as fc
import directoryGetter as dg
import matplotlib.pyplot as plt
import json

voicePath = '/home/rgong/MTG/jingjuMelody/voicePitchContours'
nonvoicePath = '/home/rgong/MTG/jingjuMelody/nonvoicePitchContours'

filenamesvoicePath = dg.jsonFilenameGetter(voicePath)
filenamesnonvoicePath = dg.jsonFilenameGetter(nonvoicePath)

allfilenames = filenamesvoicePath + filenamesnonvoicePath

# it contains each feature as a list [lengthContours, meanPitchContour ... mfcc1, mfcc2 ...]
featureList = []
for ii in range(19):
    featureList.append([])

featureVec = []
target = []
featureMeanSd = []

def featureListCreate(featureList, lengthContour, meanPitchContour, sdPitchContour, totalSalience, meanSalience, sdSalience, mfccs, voicing):
    '''
    reshape features into featureList variable
    '''
    for ii in range(len(lengthContour)):
        featureList[0].append(lengthContour[ii])