File size: 1,369 Bytes
09363f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #!/usr/bin/python
import scipy.io as sio
import matplotlib
matplotlib.use('Agg')
import pylab
import numpy as np
import random
import json
import itertools
lastIndex = 0
symbolIndices = dict()
def get_symbol_index(symbolName):
global lastIndex
global symbolIndices
if (not symbolIndices.has_key(symbolName)):
symbolIndices[symbolName] = lastIndex
lastIndex = lastIndex + 1
return symbolIndices[symbolName]
def one_hot(index, size):
vector = [0 for i in range(size)]
vector[index] = 1
return vector
def load_json_data(fileName):
global lastIndex
input = []
lastTime = 0
with open(fileName, 'r') as inputData:
jsonData = json.loads(inputData.read())
for item in jsonData:
symbolIndex = get_symbol_index(item['type'])
numberOfSymbols = lastIndex
with open(fileName, 'r') as inputData:
print("Number of symbols: ", numberOfSymbols)
jsonData = json.loads(inputData.read())
for item in jsonData:
timestamp = item['timestamp']
symbolIndex = get_symbol_index(item['type'])
deltaTime = (timestamp - lastTime) / 1000
lastTime = timestamp
inputVector = one_hot(symbolIndex, numberOfSymbols) + [ deltaTime ]
input.append(inputVector)
return (np.asfarray(input), lastIndex)
|