<s> from architecture_further_formulas import * <EOL> from processHDF5Data import * <EOL> from savedWeights import * <EOL> import re <EOL> import time <EOL> def initializeTrainedModelParameters ( dendObj ) : <EOL> if loadHD5ForTesting == True : <EOL> hdf5Data = processHDF5Data ( '<STR_LIT>' , permision = "<STR_LIT:r>" ) <EOL> start = <NUM_LIT> <EOL> end = <NUM_LIT> <EOL> weightCollection = np . array ( [ None ] * dictionaryLongitude ) <EOL> names = [ None ] * dictionaryLongitude <EOL> for weightIndex in range ( dictionaryLongitude ) : names [ weightIndex ] = '<STR_LIT>' + str ( weightIndex ) <EOL> totalWeightCollection = hdf5Data . incorperateData ( weightCollection , names , start , end ) <EOL> lastIndex = ( len ( totalWeightCollection [ <NUM_LIT:0> ] ) - <NUM_LIT:1> ) <EOL> testW = [ ] <EOL> for finalWeightIndex in range ( dictionaryLongitude ) : <EOL> testW . extend ( [ totalWeightCollection [ finalWeightIndex ] [ lastIndex ] ] ) <EOL> else : <EOL> testW = retreiveTestW ( ) <EOL> testW = np . array ( testW ) <EOL> testTauD = np . array ( tauMax - abs ( testW ) * ( tauMax - tauMin ) ) <EOL> tauMScaled = tauM * <NUM_LIT:1000> <EOL> testR = np . array ( ( ( testTauD * neuronFiringThreshold ) / Rm ) * ( ( tauMScaled / testTauD ) ** ( tauM / ( tauMScaled - testTauD ) ) ) ) <EOL> if loadHD5ForTesting == True : del ( totalWeightCollection ) <EOL> for indexOfDend in range ( dictionaryLongitude ) : <EOL> dendObj [ indexOfDend ] . w = testW [ indexOfDend ] * volt * weightScaling <EOL> dendObj [ indexOfDend ] . tau = testTauD [ indexOfDend ] * ms <EOL> dendObj [ indexOfDend ] . r = testR [ indexOfDend ] * mV <EOL> '''<STR_LIT>''' <EOL> def evaluateClassifierPerf ( ADDSObj , testRun ) : <EOL> if timeAndRefrac . refractoryPointSwitch == True : <EOL> if ( timeAndRefrac . spikeIntervalCounter >= <NUM_LIT:2> ) and ( timeAndRefrac . spikeIntervalCounter <= ( totalSpikeIntervals + <NUM_LIT:1> ) ) : <EOL> for neuronIndex in range ( dictionaryLongitude ) : <EOL> if ( ADDSObj . UmSpikeFired [ neuronIndex ] == <NUM_LIT:1> * mV ) : <EOL> if ( testRun . correctSpikes [ neuronIndex ] [ ( timeAndRefrac . spikeIntervalCounter - <NUM_LIT:1> ) ] == <NUM_LIT:1> ) : <EOL> testRun . truePositiveSpikeResults = testRun . truePositiveSpikeResults + <NUM_LIT:1> <EOL> print '<STR_LIT>' , '<STR_LIT>' , timeAndRefrac . spikeIntervalCounter - <NUM_LIT:1> , '<STR_LIT>' , neuronIndex <EOL> else : <EOL> testRun . falsePositiveSpikeResults = testRun . falsePositiveSpikeResults + <NUM_LIT:1> <EOL> elif ( ADDSObj . UmSpikeFired [ neuronIndex ] == <NUM_LIT:0> * mV ) : <EOL> if ( testRun . correctSpikes [ neuronIndex ] [ ( timeAndRefrac . spikeIntervalCounter - <NUM_LIT:1> ) ] == <NUM_LIT:1> ) : <EOL> testRun . falseNegativeSpikeResults = testRun . falseNegativeSpikeResults + <NUM_LIT:1> <EOL> else : <EOL> testRun . trueNegativeSpikeResults = testRun . trueNegativeSpikeResults + <NUM_LIT:1> <EOL> for neuronIndex in range ( dictionaryLongitude ) : <EOL> ADDSObj . UmSpikeFired [ neuronIndex ] = <NUM_LIT:0> * mV <EOL> return ADDSObj . UmSpikeFired , testRun <EOL> def evaluateClassifierPerf2 ( ADDSObj , testRun ) : <EOL> currentSpikeIntervalTime = timeAndRefrac . spikeIntervalCounter <EOL> neuronsWithoutSpikes = <NUM_LIT:0> <EOL> currentWindow = math . floor ( Decimal ( format ( ( currentSpikeIntervalTime - <NUM_LIT:2> ) , '<STR_LIT>' ) ) / Decimal ( format ( testingSpikesPerChar , '<STR_LIT>' ) ) ) <EOL> if timeAndRefrac . refractoryPointSwitch == True : <EOL> if ( currentSpikeIntervalTime >= <NUM_LIT:2> ) and ( currentSpikeIntervalTime <= ( totalSpikeIntervals + <NUM_LIT:1> ) ) : <EOL> for neuronIndex in range ( dictionaryLongitude ) : <EOL> if ( ADDSObj . UmSpikeFired [ neuronIndex ] == <NUM_LIT:1> * mV ) : <EOL> if ( testRun . firedSpikes [ currentSpikeIntervalTime ] ) == None : <EOL> testRun . firedSpikes [ currentSpikeIntervalTime ] = np . array ( [ neuronIndex ] ) <EOL> else : <EOL> firedSpikes2 = [ ] <EOL> for elemIndex in range ( len ( testRun . firedSpikes ) ) : <EOL> if elemIndex == currentSpikeIntervalTime : <EOL> newElem = testRun . firedSpikes [ elemIndex ] . tolist ( ) <EOL> newElem . append ( [ neuronIndex ] ) <EOL> firedSpikes2 . append ( np . array ( newElem ) ) <EOL> else : <EOL> firedSpikes2 . append ( testRun . firedSpikes [ elemIndex ] ) <EOL> testRun . firedSpikes = np . array ( firedSpikes2 ) <EOL> neuronAssignedToWindow = False <EOL> for neuronWindowAssignment in testRun . assignedSpikeWindows : <EOL> if neuronWindowAssignment == currentWindow : <EOL> neuronAssignedToWindow = True <EOL> if neuronAssignedToWindow == False and testRun . assignedSpikeWindows [ neuronIndex ] == None : <EOL> testRun . assignedSpikeWindows [ neuronIndex ] = currentWindow <EOL> if testRun . assignedSpikeWindows [ neuronIndex ] == currentWindow : <EOL> testRun . truePositiveSpikeResults = testRun . truePositiveSpikeResults + <NUM_LIT:1> <EOL> else : <EOL> testRun . falsePositiveSpikeResults = testRun . falsePositiveSpikeResults + <NUM_LIT:1> <EOL> elif ( ADDSObj . UmSpikeFired [ neuronIndex ] == <NUM_LIT:0> * mV ) : <EOL> testRun . trueNegativeSpikeResults = testRun . trueNegativeSpikeResults + <NUM_LIT:1> <EOL> neuronsWithoutSpikes += <NUM_LIT:1> <EOL> if neuronsWithoutSpikes == dictionaryLongitude : <EOL> testRun . trueNegativeSpikeResults = testRun . trueNegativeSpikeResults - <NUM_LIT:1> <EOL> correctNeuronFound = False <EOL> for spikedNeurons in testRun . firedSpikes : <EOL> if len ( spikedNeurons ) > <NUM_LIT:1> or spikedNeurons != [ None ] : <EOL> for spikedNeuronNumber in spikedNeurons : <EOL> if spikedNeuronNumber != None and currentWindow == testRun . assignedSpikeWindows [ spikedNeuronNumber ] : <EOL> correctNeuronFound = True <EOL> if correctNeuronFound == False : <EOL> testRun . falseNegativeSpikeResults = testRun . falseNegativeSpikeResults + <NUM_LIT:1> <EOL> for neuronIndex in range ( dictionaryLongitude ) : <EOL> ADDSObj . UmSpikeFired [ neuronIndex ] = <NUM_LIT:0> * mV <EOL> return ADDSObj . UmSpikeFired , testRun <EOL> def totalCorrectPercentage ( ) : <EOL> totalCorrectPercentage = ( Decimal ( format ( testRun . truePositiveSpikeResults , '<STR_LIT>' ) ) + Decimal ( format ( testRun . trueNegativeSpikeResults , '<STR_LIT>' ) ) ) / ( Decimal ( format ( totalSpikeIntervals , '<STR_LIT>' ) ) * Decimal ( format ( dictionaryLongitude , '<STR_LIT>' ) ) ) <EOL> return totalCorrectPercentage <EOL> def precisionPercentage ( ) : <EOL> precisionPercentage = ( Decimal ( format ( testRun . truePositiveSpikeResults , '<STR_LIT>' ) ) / Decimal ( format ( totalSpikeIntervals , '<STR_LIT>' ) ) ) <EOL> return precisionPercentage <EOL> def OutputEvaluationResults ( dendObj , testRun , verbosePrint , evaluateClassifier ) : <EOL> '''<STR_LIT>''' <EOL> if ( evaluateClassifier == False ) : <EOL> weightsFile = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> weightsFile . close ( ) <EOL> weightsFile = open ( '<STR_LIT>' , '<STR_LIT:a>' ) <EOL> np . set_printoptions ( threshold = np . inf , linewidth = np . inf ) <EOL> outputWeightDesc = "<STR_LIT:[>" <EOL> for printIndex in range ( dictionaryLongitude ) : <EOL> outputWeightDescTemp = "<STR_LIT>" <EOL> outputWeightDescTemp += np . array2string ( ( dendObj [ printIndex ] . w [ : ] / volt ) , separator = '<STR_LIT:U+002CU+0020>' ) <EOL> outputWeightDescTemp = outputWeightDescTemp . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> outputWeightDescTemp = outputWeightDescTemp . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> outputWeightDescTemp = outputWeightDescTemp . replace ( "<STR_LIT:U+002CU+0020>" , "<STR_LIT:U+002CU+0020>" ) <EOL> outputWeightDescTemp = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , outputWeightDescTemp ) <EOL> outputWeightDescTemp = re . sub ( r"<STR_LIT>" , "<STR_LIT:]>" , outputWeightDescTemp ) <EOL> outputWeightDescTemp = re . sub ( r"<STR_LIT>" , "<STR_LIT:U+002C>" , outputWeightDescTemp ) <EOL> outputWeightDescTemp = re . sub ( r"<STR_LIT>" , "<STR_LIT:[>" , outputWeightDescTemp ) <EOL> outputWeightDescTemp = re . sub ( r"<STR_LIT>" , r"<STR_LIT>" , outputWeightDescTemp ) <EOL> outputWeightDesc += outputWeightDescTemp <EOL> if printIndex < ( dictionaryLongitude - <NUM_LIT:1> ) : <EOL> outputWeightDesc += "<STR_LIT:\n>" <EOL> else : <EOL> outputWeightDesc += "<STR_LIT>" <EOL> outputWeightDesc = outputWeightDesc . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> outputWeightDesc = outputWeightDesc . replace ( "<STR_LIT:[>" , "<STR_LIT>" ) <EOL> outputWeightDesc = outputWeightDesc . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> outputWeightDesc = outputWeightDesc . replace ( "<STR_LIT:]>" , "<STR_LIT>" ) <EOL> weightsFile . write ( outputWeightDesc ) <EOL> weightsFile . close ( ) <EOL> totalCorrectPercentage = ( Decimal ( format ( testRun . truePositiveSpikeResults , '<STR_LIT>' ) ) + Decimal ( format ( testRun . trueNegativeSpikeResults , '<STR_LIT>' ) ) ) / ( Decimal ( format ( totalSpikeIntervals , '<STR_LIT>' ) ) * Decimal ( format ( dictionaryLongitude , '<STR_LIT>' ) ) ) <EOL> if ( verbosePrint == True ) : <EOL> print '<STR_LIT>' <EOL> for printIndex in range ( dictionaryLongitude ) : <EOL> print dendObj [ printIndex ] . w [ : ] / volt <EOL> print '<STR_LIT>' <EOL> for printIndex in range ( dictionaryLongitude ) : <EOL> print dendObj [ printIndex ] . tau [ : ] / ms <EOL> print '<STR_LIT>' <EOL> for printIndex in range ( dictionaryLongitude ) : <EOL> print dendObj [ printIndex ] . r [ : ] / mV <EOL> print '<STR_LIT:\n>' <EOL> print '<STR_LIT>' , testRun . firedSpikes <EOL> print '<STR_LIT>' , testRun . assignedSpikeWindows <EOL> print '<STR_LIT:\n>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' , testRun . truePositiveSpikeResults , '<STR_LIT>' , testRun . falsePositiveSpikeResults , '<STR_LIT>' , testRun . trueNegativeSpikeResults , '<STR_LIT>' , testRun . falseNegativeSpikeResults <EOL> print '<STR_LIT>' , totalSpikeIntervals , '<STR_LIT>' , dictionaryLongitude <EOL> print '<STR_LIT>' , Decimal ( format ( testRun . truePositiveSpikeResults , '<STR_LIT>' ) ) / Decimal ( format ( totalSpikeIntervals , '<STR_LIT>' ) ) , '<STR_LIT>' <EOL> print '<STR_LIT>' , totalCorrectPercentage <EOL> print '<STR_LIT>' <EOL> if ( evaluateClassifier == True ) : <EOL> latestResultsFile = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> latestResultsFile . write ( str ( totalCorrectPercentage ) ) <EOL> latestResultsFile . close ( ) <EOL> time . sleep ( <NUM_LIT:20> ) <EOL> def printWeights ( dendObj ) : <EOL> print '<STR_LIT>' <EOL> for printIndex in range ( dictionaryLongitude ) : <EOL> print dendObj [ printIndex ] . w [ : ] / volt <EOL> def writeOptimizationResults ( self , testRun , accuracyPerc ) : <EOL> if ( self . optResultsFile != "<STR_LIT>" ) : <EOL> optResults = open ( self . optResultsFile , '<STR_LIT:a>' ) <EOL> optResults . write ( "<STR_LIT>" + str ( self . minWeightsRand ) + "<STR_LIT>" + str ( self . maxWeightsRand ) + "<STR_LIT>" + str ( self . positiveWeightReinforcement ) + "<STR_LIT>" + str ( self . negativeWeightReinforcement ) ) <EOL> optResults . write ( "<STR_LIT>" + str ( Decimal ( format ( testRun . truePositiveSpikeResults , '<STR_LIT>' ) ) / Decimal ( format ( self . totalSpikeIntervals , '<STR_LIT>' ) ) ) + "<STR_LIT:\t>" + str ( testRun . truePositiveSpikeResults ) + "<STR_LIT:/>" + str ( self . totalSpikeIntervals ) + "<STR_LIT>" ) <EOL> optResults . write ( '<STR_LIT>' + str ( accuracyPerc ) + '<STR_LIT>' + str ( testRun . truePositiveSpikeResults ) + '<STR_LIT>' + str ( testRun . falsePositiveSpikeResults ) + '<STR_LIT>' + str ( testRun . trueNegativeSpikeResults ) + '<STR_LIT>' + str ( testRun . falseNegativeSpikeResults ) ) <EOL> optResults . close ( ) </s>
