def test_classifier(paramsClassifier, settingsClassifier, baselineValues):
    '''Performs network testing.
    Input:
        -classifier parameters
        -classifier settings values
        -sensor baseline values
    '''

    nrInputNeurons = paramsClassifier['NUM_INPUT_NEURONS']
    nrVR = paramsClassifier['NUM_VR']
    alpha = paramsClassifier['RATE_ALPHA']
    
    logFolder = settingsClassifier['TEST_LOGGER_FOLDER'] 
    masterPath = settingsClassifier['MASTER_PATH'] 
    spikeSourceVRTest = settingsClassifier['SPIKE_SOURCE_VR_RESPONSE_TEST']
    classLabelsTestFile = settingsClassifier['CLASS_LABELS_TEST']
    spikeLengthSample = settingsClassifier['SPIKE_TRAIN_LENGTH']
    nrSamples = settingsClassifier['NUM_LOG_SAMPLES']

    
    logPath = os.path.join(masterPath, logFolder)  
    files = os.listdir(logPath)
    pattern = '*.csv'
    fileList = [name for name in files if fnmatch.fnmatch(name, pattern)]   
    fileArray = np.ravel(np.array(fileList))
    np.random.shuffle(fileArray)
    odourClassesTest = [int(fileName[0]) for fileName in fileArray]
    concatenate_recordings(logPath, fileArray, spikeSourceVRTest,
                             odourClassesTest, nrSamples, nrInputNeurons,
                             nrVR, spikeLengthSample, alpha, baselineValues)
    make_labels_file(classLabelsTestFile, odourClassesTest)
    
    settingsClassifier['LEARNING'] = False
    eNoseClassifier.runClassifier(paramsClassifier, settingsClassifier, None)
def train_classifier(paramsClassifier, settingsClassifier, baselineValues):
    '''Performs network training.
    Input:
        -classifier parameters
        -classifier settings values
        -sensor baseline values
    Output:
        -saves the trained PN-AN connection weights to file
    '''
    nrInputNeurons = paramsClassifier['NUM_INPUT_NEURONS']
    nrVR = paramsClassifier['NUM_VR']
    spikeLengthSample = settingsClassifier['SPIKE_TRAIN_LENGTH']
    alpha = paramsClassifier['RATE_ALPHA']
    
    logFolder = settingsClassifier['TRAIN_LOGGER_FOLDER'] 
    masterPath = settingsClassifier['MASTER_PATH']
    spikeSourceVRTrain = settingsClassifier['SPIKE_SOURCE_VR_RESPONSE_TRAIN']
    classLabelsTrainFile = settingsClassifier['CLASS_LABELS_TRAIN']
    classActivationsFile = settingsClassifier['SPIKE_SOURCE_CLASS_ACTIVATIONS']
    nrRepetitions = settingsClassifier['NUM_REPETITIONS']
    nrSamples = settingsClassifier['NUM_LOG_SAMPLES']

    maxSimTime = nrSamples*spikeLengthSample
    logPath = os.path.join(masterPath, logFolder)  
    
    files = os.listdir(logPath)
    pattern = '*.csv'
    fileList = [name for name in files if fnmatch.fnmatch(name, pattern)]       
    fileArray = np.ravel(fileList * nrRepetitions)
    np.random.shuffle(fileArray)
    odourClassesTrain = [int(fileName[0]) for fileName in fileArray]
    
    concatenate_recordings(logPath, fileArray, spikeSourceVRTrain,
                            odourClassesTrain, nrSamples, nrInputNeurons,
                            nrVR, spikeLengthSample, alpha, baselineValues)
    make_class_activation_spikes(classActivationsFile,
                                 odourClassesTrain,
                                maxSimTime)
    make_labels_file(classLabelsTrainFile, odourClassesTrain)
    
    settingsClassifier['LEARNING'] = True
    eNoseClassifier.runClassifier(paramsClassifier, settingsClassifier, None)
def cross_validate(paramsClassifier, settingsClassifier, baselineValues):
    '''Performs crossvalidation on the data set.
    Input:
        -classifier parameters
        -classifier settings values
        -sensor baseline values
    Output:
        -crossvalidation network performance scores vector
    '''
    
    nrInputNeurons = paramsClassifier['NUM_INPUT_NEURONS']
    nrVR = paramsClassifier['NUM_VR']
    nrClasses = paramsClassifier['NUM_CLASSES']
    alpha = paramsClassifier['RATE_ALPHA']
    
    logFolder = settingsClassifier['CROSSVALIDATION_LOGGER_FOLDER'] 
    nrFolds = settingsClassifier['NUM_FOLDS']
    masterPath = settingsClassifier['MASTER_PATH']
    spikeSourceVRTrain = settingsClassifier['SPIKE_SOURCE_VR_RESPONSE_TRAIN']
    spikeSourceVRTest = settingsClassifier['SPIKE_SOURCE_VR_RESPONSE_TEST']
    classLabelsTrainFile = settingsClassifier['CLASS_LABELS_TRAIN']
    classLabelsTestFile = settingsClassifier['CLASS_LABELS_TEST']
    classActivationsFile = settingsClassifier['SPIKE_SOURCE_CLASS_ACTIVATIONS']
    nrSamples = settingsClassifier['NUM_LOG_SAMPLES']
    spikeLengthSample = settingsClassifier['SPIKE_TRAIN_LENGTH']


    maxSimTime = nrSamples*spikeLengthSample
    logPath = os.path.join(masterPath, logFolder)  
    
    files = os.listdir(logPath)
    fileList = [[]] * nrClasses

    for cls in range(nrClasses):
        pattern = str(cls)+'-*.csv'
        tempFileList = [name for name in files if fnmatch.fnmatch(name, pattern)]
        fileList[cls] = tempFileList
    
    fileArray = np.array(fileList)
    nrTotObs = np.shape(fileArray)[1]
    scores = np.zeros((1, nrFolds))
    
    for fold in range(nrFolds):
        indices = range(0, nrTotObs)
        indxTest = range(nrTotObs/nrFolds*fold, nrTotObs/nrFolds*(fold+1))
        indxTrain = np.setdiff1d(indices,indxTest)
        
        trainFiles = np.ravel(fileArray[:, indxTrain])
        testFiles = np.ravel(fileArray[:, indxTest])
        
        np.random.shuffle(trainFiles)
        np.random.shuffle(testFiles)
        
        odourClassesTrain = [int(fileName[0]) for fileName in trainFiles]
        odourClassesTest = [int(fileName[0]) for fileName in testFiles]
       
        concatenate_recordings(logPath, trainFiles, spikeSourceVRTrain,
                                odourClassesTrain, nrSamples, nrInputNeurons,
                                nrVR, spikeLengthSample, alpha, baselineValues)
        concatenate_recordings(logPath, testFiles, spikeSourceVRTest,
                                odourClassesTest, nrSamples, nrInputNeurons,
                                nrVR, spikeLengthSample, alpha, baselineValues)
        
        make_class_activation_spikes(classActivationsFile,
                                     odourClassesTrain,
                                     maxSimTime)
        
        make_labels_file(classLabelsTrainFile, odourClassesTrain)
        make_labels_file(classLabelsTestFile, odourClassesTest)
        
        settingsClassifier['NUM_OBSERVATIONS'] = len(odourClassesTrain)
        settingsClassifier['NUM_OBSERVATIONS_TEST'] = len(odourClassesTest)

        settingsClassifier['LEARNING'] = True
        eNoseClassifier.runClassifier(paramsClassifier, settingsClassifier, fold)
        
        print 'Training completed'
        raw_input("Press Enter to proceed to testing...")
        
        settingsClassifier['LEARNING'] = False
        scores[0, fold] = eNoseClassifier.runClassifier(paramsClassifier,
                            settingsClassifier, fold)
        
        print 'Testing for fold ' + str(fold) + ' completed'
        
        raw_input("Press Enter to continue with the next fold...")
    return scores