plt.savefig('Crossvalidation_scores.pdf')
        plt.close()
    else:
        settingsClassifier['NUM_REPETITIONS'] = 1
        print 'Training classifier...........................................'
        train_classifier(paramsClassifier, settingsClassifier, baselineValues)
        print 'Training completed'
        raw_input("Press Enter to proceed to testing...")
        
        #baselineValues = make_eNose_baseline.baseline_init_eNose()
        test_classifier(paramsClassifier, settingsClassifier, baselineValues)
    
    

    
    # plot VR spike source for training
    totalSimulationTime = float(observationTime*nrObsTrain)                                                        
    classLabels = utils.loadListFromCsvFile(classLabelsTrain,True)
    plot_spike_sources(masterPath, spikeSourceVRTrain, nrInputNeurons,
                         nrVR, observationTime, totalSimulationTime,
                         classLabels, odourNames)
    
    # plot VR spike source for testing
    totalSimulationTime = float(observationTime*nrObsTest)                                                        
    classLabels = utils.loadListFromCsvFile(classLabelsTest,True)
    plot_spike_sources(masterPath, spikeSourceVRTest, nrInputNeurons,
                         nrVR, observationTime, totalSimulationTime,
                         classLabels, odourNames)
    

Пример #2
0
def runClassifier(params, settings, fold):
    classifier.printParameters('Model Parameters', params)
    classifier.printParameters('Classifier Settings', settings)

    populationsInput = list()
    populationsNoiseSource = list()
    populationsRN = list()
    populationsPN = list()
    populationsAN = list()
    projectionsPNAN = list()  #keep handle to these for saving learnt weights

    if settings['LEARNING']:
        totalSimulationTime = float(settings['OBSERVATION_EXPOSURE_TIME_MS'] *
                                    settings['NUM_OBSERVATIONS'])
    else:
        totalSimulationTime = float(settings['OBSERVATION_EXPOSURE_TIME_MS'] *
                                    settings['NUM_OBSERVATIONS_TEST'])

    print 'Total Simulation Time will be', totalSimulationTime

    DT = 1.0  #ms Integration timestep for simulation

    classifier.setupModel(params, settings, DT, totalSimulationTime,
                          populationsInput, populationsNoiseSource,
                          populationsRN, populationsPN, populationsAN,
                          projectionsPNAN)

    utils.recordPopulations(populationsInput, settings['RECORD_POP_INPUT'])
    utils.recordPopulations(populationsNoiseSource,
                            settings['RECORD_POP_NOISE_SOURCE'])
    utils.recordPopulations(populationsRN, settings['RECORD_POP_RN'])
    utils.recordPopulations(populationsPN, settings['RECORD_POP_PN'])
    utils.recordPopulations(populationsAN, settings['RECORD_POP_AN'])

    #run the model for the whole learning or the whole testing period
    classifier.run(totalSimulationTime)

    fig1 = plt.figure(figsize=(20, 20))
    plt.xlabel('Time[ms]', fontsize=16)
    plt.ylabel('Neurons', fontsize=16)
    title = 'Testing'
    if settings['LEARNING']:
        title = 'Training'
    title = title + ' - Odour Classification - ' + str(params['NUM_VR']) + \
                                                    ' Virtual Receptors'
    fig1.suptitle(title, fontsize=18)

    indexOffset = 0
    indexOffset = 1 + utils.plotAllSpikes(populationsInput,
                                          totalSimulationTime, indexOffset,
                                          settings['RECORD_POP_INPUT'])

    indexOffset = 1 + utils.plotAllSpikes(populationsNoiseSource,
                                          totalSimulationTime, indexOffset,
                                          settings['RECORD_POP_NOISE_SOURCE'])

    indexOffset = 1 + utils.plotAllSpikes(populationsRN, totalSimulationTime,
                                          indexOffset,
                                          settings['RECORD_POP_RN'])

    indexOffset = 1 + utils.plotAllSpikes(populationsPN, totalSimulationTime,
                                          indexOffset,
                                          settings['RECORD_POP_PN'])

    indexOffset = 1 + utils.plotAllSpikes(populationsAN, totalSimulationTime,
                                          indexOffset,
                                          settings['RECORD_POP_AN'])

    filename = 'RasterPlot-Testing-fold' + str(fold) + '.pdf'
    if settings['LEARNING']:
        filename = 'RasterPlot-Training-fold' + str(fold) + '.pdf'
    plt.savefig(filename)
    plt.close()

    (fig2, (ax1, ax2, ax3)) = plt.subplots(3, 1, figsize=(20, 20), sharex=True)
    plt.axes(ax1)
    utils.plotAllSpikes(populationsRN, totalSimulationTime, 0,
                        settings['RECORD_POP_RN'])
    plt.axes(ax2)
    utils.plotAllSpikes(populationsPN, totalSimulationTime, 0,
                        settings['RECORD_POP_PN'])
    plt.axes(ax3)
    utils.plotAllSpikes(populationsAN, totalSimulationTime, 0,
                        settings['RECORD_POP_AN'])
    ax1.set_title('RN layer spikes', fontsize=30)
    ax2.set_title('PN layer spikes', fontsize=30)
    ax3.set_title('AN layer spikes', fontsize=30)
    ax3.set_xlabel('Simulation time[ms]', fontsize=30)
    ax3.set_ylabel('Neuron indices', fontsize=30)
    ax3.tick_params(labelsize=20)
    ax2.tick_params(labelsize=20)
    ax1.tick_params(labelsize=20)

    filename = 'Separated_RasterPlot-Testing-fold' + str(fold) + '.pdf'
    if settings['LEARNING']:
        filename = 'Separated_RasterPlot-Training-fold' + str(fold) + '.pdf'
    plt.savefig(filename)
    plt.close()

    #        fig.add_subplot(2,1,2)
    #        utils.plotAllSpikes(populationsAN,totalSimulationTime, 0, settings['RECORD_POP_AN'])

    #if in the learning stage
    if settings['LEARNING']:
        #store the weight values learnt via plasticity, these will be reloaded as
        #static weights for test stage
        classLabels = utils.loadListFromCsvFile(settings['CLASS_LABELS_TRAIN'],
                                                True)
        classifier.saveLearntWeightsPNAN(settings, params, projectionsPNAN,
                                         len(populationsPN),
                                         len(populationsAN))
        winningClassesByObservation, winningSpikeCounts = classifier.calculateWinnersAN(
            settings, populationsAN, classLabels)
        scorePercent = classifier.calculateScore(winningClassesByObservation,
                                                 classLabels)

    else:
        #save the AN layer spike data from the testing run.
        #This data will be interrogated to find the winning class (most active AN pop)
        #during the presentation of each test observation
        #classifier.saveSpikesAN(settings,populationsAN)
        classLabels = utils.loadListFromCsvFile(settings['CLASS_LABELS_TEST'],
                                                True)
        winningClassesByObservation, winningSpikeCounts = classifier.calculateWinnersAN(
            settings, populationsAN, classLabels)
        scorePercent = classifier.calculateScore(winningClassesByObservation,
                                                 classLabels)
        utils.saveListAsCsvFile(winningClassesByObservation,
                                settings['CLASSIFICATION_RESULTS_PATH'])
        utils.saveListAsCsvFile(winningSpikeCounts,
                                settings['SPIKE_COUNT_RESULTS_PATH'])

    classifier.end()

    #write a marker file to allow invoking programs to know that the Python/Pynn run completed
    utils.saveListToFile(['Pynn Run complete'], settings['RUN_COMPLETE_FILE'])

    print 'PyNN run completed.'
    return scorePercent
def runClassifier(params, settings, fold):    
    classifier.printParameters('Model Parameters',params)
    classifier.printParameters('Classifier Settings',settings)
    
    populationsInput = list()
    populationsNoiseSource = list()
    populationsRN = list()
    populationsPN = list()
    populationsAN = list()
    projectionsPNAN = list() #keep handle to these for saving learnt weights
    
    if settings['LEARNING']:
        totalSimulationTime = float(settings['OBSERVATION_EXPOSURE_TIME_MS'] * 
                                settings['NUM_OBSERVATIONS'])
    else:
        totalSimulationTime = float(settings['OBSERVATION_EXPOSURE_TIME_MS'] *
                                settings['NUM_OBSERVATIONS_TEST'])
        
    print 'Total Simulation Time will be', totalSimulationTime
    
    DT = 1.0 #ms Integration timestep for simulation
    
    classifier.setupModel(params, settings, DT, totalSimulationTime, 
                          populationsInput, populationsNoiseSource,
                          populationsRN,populationsPN,populationsAN,projectionsPNAN)
    
    utils.recordPopulations(populationsInput,settings['RECORD_POP_INPUT'])
    utils.recordPopulations(populationsNoiseSource,settings['RECORD_POP_NOISE_SOURCE'])
    utils.recordPopulations(populationsRN,settings['RECORD_POP_RN'])
    utils.recordPopulations(populationsPN,settings['RECORD_POP_PN'])
    utils.recordPopulations(populationsAN,settings['RECORD_POP_AN'])
    
    #run the model for the whole learning or the whole testing period
    classifier.run(totalSimulationTime)
    
    fig1 = plt.figure(figsize=(20,20))
    plt.xlabel('Time[ms]', fontsize = 16)
    plt.ylabel('Neurons', fontsize = 16)
    title = 'Testing'
    if settings['LEARNING']:
        title = 'Training'
    title = title + ' - Odour Classification - ' + str(params['NUM_VR']) + \
                                                    ' Virtual Receptors'
    fig1.suptitle(title, fontsize = 18)
    
    indexOffset = 0
    indexOffset = 1 + utils.plotAllSpikes(populationsInput,
                            totalSimulationTime, indexOffset,
                            settings['RECORD_POP_INPUT'])
                            
    indexOffset = 1 + utils.plotAllSpikes(populationsNoiseSource,
                            totalSimulationTime, indexOffset,
                            settings['RECORD_POP_NOISE_SOURCE'])
                            
    indexOffset = 1 + utils.plotAllSpikes(populationsRN,
                                          totalSimulationTime,
                                          indexOffset,settings['RECORD_POP_RN'])
                                          
    indexOffset = 1 + utils.plotAllSpikes(populationsPN,
                                          totalSimulationTime,
                                          indexOffset,settings['RECORD_POP_PN'])
                                          
    indexOffset = 1 + utils.plotAllSpikes(populationsAN,
                                          totalSimulationTime,
                                          indexOffset,settings['RECORD_POP_AN'])
                                          
    
        
    filename = 'RasterPlot-Testing-fold' + str(fold)+'.pdf'
    if settings['LEARNING']:
        filename = 'RasterPlot-Training-fold' + str(fold)+'.pdf'
    plt.savefig(filename)
    plt.close()
    
    
    (fig2, (ax1, ax2, ax3)) = plt.subplots(3, 1, figsize=(20,20), sharex=True)
    plt.axes(ax1)
    utils.plotAllSpikes(populationsRN,totalSimulationTime,0, settings['RECORD_POP_RN'])
    plt.axes(ax2)
    utils.plotAllSpikes(populationsPN,totalSimulationTime,0, settings['RECORD_POP_PN'])
    plt.axes(ax3)
    utils.plotAllSpikes(populationsAN,totalSimulationTime,0, settings['RECORD_POP_AN'])
    ax1.set_title('RN layer spikes', fontsize = 30)
    ax2.set_title('PN layer spikes', fontsize = 30)
    ax3.set_title('AN layer spikes', fontsize = 30)
    ax3.set_xlabel('Simulation time[ms]', fontsize = 30)
    ax3.set_ylabel('Neuron indices', fontsize = 30)
    ax3.tick_params(labelsize=20)
    ax2.tick_params(labelsize=20)
    ax1.tick_params(labelsize=20)



    
    filename = 'Separated_RasterPlot-Testing-fold' + str(fold)+'.pdf'
    if settings['LEARNING']:
        filename = 'Separated_RasterPlot-Training-fold' + str(fold)+'.pdf'
    plt.savefig(filename)
    plt.close()
                                              
#        fig.add_subplot(2,1,2)
#        utils.plotAllSpikes(populationsAN,totalSimulationTime, 0, settings['RECORD_POP_AN'])
    
    #if in the learning stage
    if settings['LEARNING']:
        #store the weight values learnt via plasticity, these will be reloaded as 
        #static weights for test stage
        classLabels = utils.loadListFromCsvFile(settings['CLASS_LABELS_TRAIN'],True)
        classifier.saveLearntWeightsPNAN(settings, params, projectionsPNAN,
                                         len(populationsPN),len(populationsAN))
        winningClassesByObservation, winningSpikeCounts = classifier.calculateWinnersAN(settings,populationsAN, classLabels)
        scorePercent = classifier.calculateScore(winningClassesByObservation,classLabels)
    
                           
    else:
        #save the AN layer spike data from the testing run.
        #This data will be interrogated to find the winning class (most active AN pop)
        #during the presentation of each test observation
        #classifier.saveSpikesAN(settings,populationsAN)
        classLabels = utils.loadListFromCsvFile(settings['CLASS_LABELS_TEST'],True)
        winningClassesByObservation, winningSpikeCounts = classifier.calculateWinnersAN(settings,populationsAN, classLabels)
        scorePercent = classifier.calculateScore(winningClassesByObservation, classLabels)
        utils.saveListAsCsvFile(winningClassesByObservation,settings['CLASSIFICATION_RESULTS_PATH'])
        utils.saveListAsCsvFile(winningSpikeCounts,settings['SPIKE_COUNT_RESULTS_PATH'])

    classifier.end()
    
    #write a marker file to allow invoking programs to know that the Python/Pynn run completed
    utils.saveListToFile(['Pynn Run complete'],settings['RUN_COMPLETE_FILE'])
    
    print 'PyNN run completed.'
    return scorePercent  
    indexOffset = 1 + utils.plotAllSpikes(populationsNoiseSource, totalSimulationTime, indexOffset,settings['RECORD_POP_NOISE_SOURCE'])
    indexOffset = 1 + utils.plotAllSpikes(populationsRN, totalSimulationTime, indexOffset,settings['RECORD_POP_RN'])
    indexOffset = 1 + utils.plotAllSpikes(populationsPN, totalSimulationTime, indexOffset,settings['RECORD_POP_PN'])
    indexOffset = 1 + utils.plotAllSpikes(populationsAN, totalSimulationTime, indexOffset,settings['RECORD_POP_AN'])

#if in the learning stage
if settings['LEARNING']:
    #store the weight values learnt via plasticity, these will be reloaded as static weights for test stage
    classifier.saveLearntWeightsPNAN(settings,params,projectionsPNAN,len(populationsPN),len(populationsAN))
else:
    #save the AN layer spike data from the testing run. 
    #This data will be interrogated to find the winning class (most active AN pop) 
    #during the presentation of each test observation
    #classifier.saveSpikesAN(settings,populationsAN)
    winningClassesByObservation = classifier.calculateWinnersAN(settings,populationsAN)
    classLabels = utils.loadListFromCsvFile(settings['CLASS_LABELS_PATH'],True)
    scorePercent = classifier.calculateScore(winningClassesByObservation,classLabels)
    utils.saveListAsCsvFile(winningClassesByObservation,settings['CLASSIFICATION_RESULTS_PATH'])
        
#make sure spinnaker shut down before blocking figure is put up
classifier.end()

if settings['SAVE_RASTER_PLOT_AS_PNG']:
    filename = 'RasterPlot-Testing.png'
    if settings['LEARNING']:
        filename = 'RasterPlot-Training.png'
    plt.savefig(filename)
     
#write a marker file to allow invoking programs to know that the Python/Pynn run completed
utils.saveListToFile(['Pynn Run complete'],settings['RUN_COMPLETE_FILE'])