def __init__(self): self.outData = [] self.tuplesCovered = [] self.aqiTrainingLUT = {} self.aqiTestLUT = {} self.dateTimeLUT = {} self.dateTimeCE = Tesla(order=2, numInputs=2); self.coarseLatCE = Tesla(order=3, numInputs=1); self.coarseLngCE = Tesla(order=3, numInputs=1); self.aqiCE = Tesla(order=3, numInputs=3);
def __init__(self, funcOrder=3, numOfInputs=2, numHistoryStates=0): self.inputData = [] self.outputData = [] self.flexCE = Tesla(order=funcOrder, numInputs=(numOfInputs+numHistoryStates)); self.aggregatedError = 0.0; self.aggregatedCount = 0; self.errorNumerator = 0.0; self.errorDenominator = 0; self.predictedList = []; self.actualList = []; self.testSetPredictedList = []; self.testSetActualList = []; self.numInputs = numOfInputs; self.numHistoryStates = numHistoryStates;
import numpy as np import os.path import pickle import math from Tesla import Tesla # Tesla training objects kitchOut2Trainer = Tesla(order=2,numInputs=1); kitchOut3Trainer = Tesla(order=2,numInputs=1); kitchOut4Trainer = Tesla(order=2,numInputs=1); microwaveTrainer = Tesla(order=2,numInputs=1); oven01Trainer = Tesla(order=2,numInputs=1); oven02Trainer = Tesla(order=2,numInputs=1); refrigeratorTrainer = Tesla(order=2,numInputs=1); stoveTrainer = Tesla(order=2,numInputs=1); # Pickle files for trained data kitchOut2PickleFilename = 'traces/kitch_out2_trained_order' + str(kitchOut2Trainer.functionOrder) + '.dat'; kitchOut3PickleFilename = 'traces/kitch_out3_trained_order' + str(kitchOut3Trainer.functionOrder) + '.dat'; kitchOut4PickleFilename = 'traces/kitch_out4_trained_order' + str(kitchOut4Trainer.functionOrder) + '.dat'; microwavePickleFilename = 'traces/microwave_trained_order' + str(microwaveTrainer.functionOrder) + '.dat'; oven01PickleFilename = 'traces/oven01_trained_order' + str(oven01Trainer.functionOrder) + '.dat'; oven02PickleFilename = 'traces/oven02_trained_order' + str(oven02Trainer.functionOrder) + '.dat'; refrigeratorPickleFilename = 'traces/refrigerator_trained_order' + str(refrigeratorTrainer.functionOrder) + '.dat'; stovePickleFilename = 'traces/stove_trained_order' + str(stoveTrainer.functionOrder) + '.dat'; # Open the files needed: input trace and output observations kitchOut2Trace = open('traces/kitch_out2.dat', mode='r'); kitchOut3Trace = open('traces/kitch_out3.dat', mode='r');
import numpy as np import os.path import pickle from Tesla import Tesla # Tesla training objects refrigeratorTrainer1 = Tesla(order=2,numInputs=1); refrigeratorTrainer2 = Tesla(order=2,numInputs=2); refrigeratorTrainer3 = Tesla(order=2,numInputs=3); refrigeratorTrainer4 = Tesla(order=2,numInputs=4); # Pickle files for trained data refrigeratorPickleFile1 = 'traces/refrigeratorTrained1.dat'; refrigeratorPickleFile2 = 'traces/refrigeratorTrained2.dat'; refrigeratorPickleFile3 = 'traces/refrigeratorTrained3.dat'; refrigeratorPickleFile4 = 'traces/refrigeratorTrained4.dat'; # Open the files needed: input trace and output observations refrigeratorTrace = open('traces/refrigerator.dat', mode='r'); refrigeratorOutputTrace = open('traces/refrigerator_obs.dat', mode='r'); # First, parse the first 3 elements, needed as additional inputs # Add observations to the appropriate tesla training objects# First observation # First observation (timestamp, refrigeratorEntry1) = refrigeratorTrace.readline().split(); refrigeratorEntry1 = float(refrigeratorEntry1); (timestamp, refrigeratorOutput) = refrigeratorOutputTrace.readline().split(); refrigeratorOutput = int(refrigeratorOutput); refrigeratorTrainer1.addSingleObservation([refrigeratorEntry1], refrigeratorOutput);
from ContextEngineBase import Complexity ## For different tests, these values will vary. inputFilePath = "pythonBaseTestInput.csv" outputFilePath = "pythonBaseTestOutput.csv" complexity = Complexity.firstOrder; numTrainingSamples = 5; numExecuteSamples = 5; inputFile = open(inputFilePath); outputFile = open(outputFilePath); inputReader = csv.reader(inputFile); outputReader = csv.reader(outputFile); ## Change the name of the algorithm to test it out. algorithmTest = Tesla(Complexity.firstOrder, 2, 0, [0, 0], {}); teslaTimestamps = {}; knnTimestamps = {}; for trainingSample in range(numTrainingSamples): inputRow = next(inputReader); outputRow = next(outputReader); if (len(inputRow) > 0): input1 = float(inputRow[0]); input2 = float(inputRow[1]); output = float(outputRow[0]); firstTS = time.time(); algorithmTest.addSingleObservation([input1, input2], output); secondTS = time.time(); teslaTimestamps["load" + str(trainingSample)] = secondTS - firstTS;
## For different tests, these values will vary. inputFilePath = "3192_bathroomPrescence_Input.csv" outputFilePath = "3192_bathroomPrescence_Output.csv" complexity = Complexity.thirdOrder numTrainingSamples = 5000 numExecuteSamples = 6320 numInputs = 2 inputFile = open(inputFilePath) outputFile = open(outputFilePath) inputReader = csv.reader(inputFile) outputReader = csv.reader(outputFile) ## Change the name of the algorithm to test it out. algorithmTest = Tesla(complexity, numInputs, 0, [0] * numInputs, {}) teslaTimestamps = {} knnTimestamps = {} for trainingSample in range(numTrainingSamples): inputRow = next(inputReader) outputRow = next(outputReader) if (len(inputRow) > 0): inputs = [float(x) for x in inputRow[0:numInputs]] output = float(outputRow[0]) firstTS = time.time() algorithmTest.addSingleObservation(inputs, output) secondTS = time.time() teslaTimestamps["load" + str(trainingSample)] = secondTS - firstTS print(str(trainingSample))
from Tesla import Tesla f = open("myTestFile.txt", 'r') x = f.readlines() print(x) Tesla(x[0], int(x[1]), x[2], x[3], x[4])
from ContextEngineBase import Complexity ## For different tests, these values will vary. inputFilePath = "../python/Tesla/teslaInput.csv" outputFilePath = "../python/Tesla/teslaOutput.csv" complexity = Complexity.firstOrder; numTrainingSamples = 5; numExecuteSamples = 5; inputFile = open(inputFilePath); outputFile = open(outputFilePath); inputReader = csv.reader(inputFile); outputReader = csv.reader(outputFile); ## Change the name of the algorithm to test it out. algorithmTest = Tesla(Complexity.firstOrder, 1, 0, [0, 0], {}); teslaTimestamps = {}; knnTimestamps = {}; for trainingSample in range(numTrainingSamples): inputRow = next(inputReader); outputRow = next(outputReader); if (len(inputRow) > 0): input1 = float(inputRow[0]); input2 = float(inputRow[1]); output = float(outputRow[0]); firstTS = time.time(); algorithmTest.addSingleObservation([input1, input2], output); secondTS = time.time(); teslaTimestamps["load" + str(trainingSample)] = secondTS - firstTS;
numTrainingSamples = int(numTotalSamples * trainSetProportion); numExecuteSamples = numTotalSamples - numTrainingSamples; print('Total samples: ',numTotalSamples, 'Training samples: ',numTrainingSamples, 'Test samples: ',numExecuteSamples) for complexity in complexityOptions: print(complexity, " (numTrials: ",numTrials, ")", sep="") for trial in range(numTrials): inputFile = open(inputFilePath); outputFile = open(outputFilePath); inputReader = csv.reader(inputFile); outputReader = csv.reader(outputFile); ## Change the name of the algorithm to test it out. algorithmTest = Tesla(complexity, numInputs, 0, [0] * numInputs, {}); teslaTimestamps = {}; teslaError = {}; knnTimestamps = {}; rowOffset = int(numTrainingSamples * trial % numTotalSamples); for i in range(rowOffset): try: inputRow = next(inputReader); outputRow = next(outputReader); except: inputFile.seek(0); outputFile.seek(0); inputRow = next(inputReader); outputRow = next(outputReader);
class PecanStreetCE(): def __init__(self, funcOrder=3, numOfInputs=2, numHistoryStates=0): self.inputData = [] self.outputData = [] self.flexCE = Tesla(order=funcOrder, numInputs=(numOfInputs+numHistoryStates)); self.aggregatedError = 0.0; self.aggregatedCount = 0; self.errorNumerator = 0.0; self.errorDenominator = 0; self.predictedList = []; self.actualList = []; self.testSetPredictedList = []; self.testSetActualList = []; self.numInputs = numOfInputs; self.numHistoryStates = numHistoryStates; def runTesla(self, csvFilepath): with open(csvFilepath) as csvFile: reader = csv.reader(csvFile); i = 0; for row in reader: inputList = []; historyList = []; outputValue = 0.0; if (i != 0 and i < self.numHistoryStates): self.predictedList.append(0.0); self.actualList.append(float(row[self.numInputs])); elif (i != 0): # Append the appropriate number of values from the CSV file to the input list. for j in range(0,self.numInputs): inputList.append(float(row[j])); # Generate a list of history states to append to the inputs for k in range(0, self.numHistoryStates): historyList.append(-1-k); # Append the history state as an additional inputs. inputList += historyList; outputValue = float(row[self.numInputs]); self.inputData.append(inputList); self.outputData.append(outputValue); if (i >= self.numHistoryStates and i <= 17500): self.flexCE.addSingleObservation(inputList, outputValue); # Append garbage data to predicted, since this is training. self.predictedList.append(0.0); self.actualList.append(outputValue); elif (i > 17500 and i <= 35000): predicted = self.flexCE.test(inputList); if (predicted < 0): predicted = 0.0; actual = outputValue; self.aggregatedError += pow(actual-predicted, 2); self.aggregatedCount += 1; # print(str(actual), "\t", str(predicted)) self.errorNumerator += abs((actual-predicted)); self.errorDenominator += 1; self.predictedList.append(predicted); self.actualList.append(actual); self.testSetPredictedList.append(predicted); self.testSetActualList.append(actual); elif (i > 35000): print(str(time.clock())); break; if (i == 17500): print("Training data loaded"); startTime = time.clock() self.flexCE.train(); elapsedTime = time.clock() - startTime; print("CE trained, elapsed time:", str(elapsedTime)); print(str(time.clock())); i+=1;
class CitisenseCE(): def __init__(self): self.outData = [] self.tuplesCovered = [] self.aqiTrainingLUT = {} self.aqiTestLUT = {} self.dateTimeLUT = {} self.dateTimeCE = Tesla(order=2, numInputs=2); self.coarseLatCE = Tesla(order=3, numInputs=1); self.coarseLngCE = Tesla(order=3, numInputs=1); self.aqiCE = Tesla(order=3, numInputs=3); def enumerateAQI(self, rawValue): if (rawValue < 0.0): return -1; elif (rawValue <= 50.0): return 0; elif (rawValue <= 100.0): return 1; elif (rawValue <= 150.0): return 2; elif (rawValue <= 200.0): return 3; elif (rawValue <= 300.0): return 4; else: return 5; def truncate(self, f, n): '''Truncates/pads a float f to n decimal places without rounding''' s = '%.12f' % f i, p, d = s.partition('.') return '.'.join([i, (d+'0'*n)[:n]]) def genLUTs(self): with open('../traces/aqiOnly.csv', 'r') as csvFile: reader = csv.reader(csvFile) index = 0 for row in reader: if (not index == 0) and (len(row) > 0): reading = float(row[6]) date = datetime.datetime.strptime(row[7]+"00", "%Y-%m-%d %H:%M:%S%z") coarseLat = float(self.truncate(float(row[8]), 6)) coarseLng = float(self.truncate(float(row[9]), 6)) dateInt = date.hour*60+date.minute if (index <= 1400): self.coarseLatCE.addSingleObservation([float(row[8])], coarseLat); self.coarseLngCE.addSingleObservation([float(row[9])], coarseLng); if (date.hour, date.minute) not in self.dateTimeLUT: self.dateTimeLUT[(date.hour, date.minute)] = dateInt self.dateTimeCE.addSingleObservation([date.hour, date.minute], dateInt); if (coarseLat, coarseLng, dateInt) in self.aqiTrainingLUT: print("Duplicate found! " + str(reading) + " vs. " + str(self.aqiTrainingLUT[(coarseLat, coarseLng, dateInt)])); continue; else: self.aqiTrainingLUT[(coarseLat, coarseLng, dateInt)] = reading; self.aqiCE.addSingleObservation([coarseLat, coarseLng, dateInt], reading); elif (index <= 2000): self.aqiTestLUT[(coarseLat, coarseLng, dateInt)] = reading; else: break; index += 1;
## For different tests, these values will vary. inputFilePath = "3192_clothesWasher1_0_input.csv" outputFilePath = "3192_clothesWasher1_0_output.csv" complexity = Complexity.firstOrder numTrainingSamples = 800 numExecuteSamples = 800 numInputsToUse = 5 inputFile = open(inputFilePath) outputFile = open(outputFilePath) inputReader = csv.reader(inputFile) outputReader = csv.reader(outputFile) ## Change the name of the algorithm to test it out. algorithmTest = Tesla(complexity, numInputsToUse, 0, [0] * numInputsToUse, {}) teslaTimestamps = {} knnTimestamps = {} print(algorithmTest.complexity) print(algorithmTest.functionOrder) for trainingSample in range(numTrainingSamples): inputRow = next(inputReader) outputRow = next(outputReader) if (len(inputRow) > 0): inputs = inputRow[:numInputsToUse] inputs = [float(x) for x in inputs] # input1 = float(inputRow[0]); # input2 = float(inputRow[1]); output = float(outputRow[0])
from ContextEngineBase import Complexity ## For different tests, these values will vary. inputFilePath = "accessorInput.csv" outputFilePath = "accessorOutput.csv" complexity = Complexity.firstOrder numTrainingSamples = 5 numExecuteSamples = 5 inputFile = open(inputFilePath) outputFile = open(outputFilePath) inputReader = csv.reader(inputFile) outputReader = csv.reader(outputFile) ## Change the name of the algorithm to test it out. algorithmTest = Tesla(Complexity.firstOrder, 1, 0, [0], {}) teslaTimestamps = {} knnTimestamps = {} for trainingSample in range(numTrainingSamples): inputRow = next(inputReader) outputRow = next(outputReader) if (len(inputRow) > 0): input1 = float(inputRow[0]) output = float(outputRow[0]) firstTS = time.time() algorithmTest.addSingleObservation([input1], output) secondTS = time.time() teslaTimestamps["load" + str(trainingSample)] = secondTS - firstTS print(str(trainingSample))
## For different tests, these values will vary. inputFilePath = "3192_clothesWasher1_0_input.csv" outputFilePath = "3192_clothesWasher1_0_output.csv" complexity = Complexity.firstOrder; numTrainingSamples = 800; numExecuteSamples = 800; numInputsToUse = 5; inputFile = open(inputFilePath); outputFile = open(outputFilePath); inputReader = csv.reader(inputFile); outputReader = csv.reader(outputFile); ## Change the name of the algorithm to test it out. algorithmTest = Tesla(complexity, numInputsToUse, 0, [0]*numInputsToUse, {}); teslaTimestamps = {}; knnTimestamps = {}; print(algorithmTest.complexity); print(algorithmTest.functionOrder); for trainingSample in range(numTrainingSamples): inputRow = next(inputReader); outputRow = next(outputReader); if (len(inputRow) > 0): inputs = inputRow[:numInputsToUse]; inputs = [float(x) for x in inputs]; # input1 = float(inputRow[0]); # input2 = float(inputRow[1]); output = float(outputRow[0]);