Example #1
0
def compavg(clients):
	signal.signal(signal.SIGINT, signal.SIG_DFL)
	y = np.zeros(shape=(6,8)) # observation matrix
	m = 8
	n = 8
	dg = DataGenerator(n=n, m=m);
	se = StateEstimator(dg.EstimationMatrix)

	while True:
		global globcount
		totals = [0]*8
		averages = [0]*8 # [avg1, avg2, avg3,..., avg8]
		
		for c in clients:
			line = c.getLine().strip().split(",")
			if len(line) != 8:
				line = [0.0, time.time(), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
			for i in range(len(line)):
				if  i == 1:
					continue
				if i > 1:
					#print line[i]
					totals[i-1] += float(line[i])
				else:
					totals[0] += float(line[0])
		averages = map(lambda x: x / len(clients), totals)
		#with comp_state_est_cv:
		with cv:
			y[globcount,:]=averages
			globcount += 1
			if globcount == 6:
				times = y[:,1].reshape(-1, 1)
				count = 0
				timeno = 0
				observation = sp.zeros((48, 3))
				for t, row in zip(times, y):
					for id, val in enumerate(row):
						if timeno == 1:
							timeno += 1
							continue
						observation[count, :] = sp.array([id, t[0] , val ] ).reshape(1, -1)
						count += 1		
						timeno += 1    			
				xhat = sp.zeros( (6, 8) )
				T = 6
				stepsize = 1
				
				for t in range( 0, T + 1 - stepsize, stepsize ):
					newstate,t0 = se.estimate( observation[t*m:(t+stepsize)*m,:] )	
					xhat[t:(t+stepsize),:] = se.interpolate(newstate,t0,times[t:(t+stepsize),:])
				
				global state					
				state = xhat.reshape(-1).tolist()
				globcount=0
				cv.notifyAll()
				
	print "interrupted"