浏览代码

analysis of anomalies @TODO: road test

Steve L. Nyemba 8 年之前
父节点
当前提交
2c0905091b
共有 2 个文件被更改,包括 35 次插入21 次删除
  1. 0 9
      src/api/ml-index.py
  2. 35 12
      src/utils/ml.py

+ 0 - 9
src/api/ml-index.py

@@ -1,9 +0,0 @@
-from flask import Flask, render_template
-from flask_socketio import SocketIO
-
-app = Flask(__name__)
-app.config['SECRET_KEY'] = '[0v8-247]-4qdm-h8r5!'
-socketio = SocketIO(app)
-
-if __name__ == '__main__':
-    socketio.run(app)

+ 35 - 12
src/utils/ml.py

@@ -25,9 +25,18 @@ class ML:
 		#
 		r = []
 		for row in data :
-			for item in row :
-				if attr in item and item[attr] == value:
-					r.append(item)
+			if isinstance(row,list) :
+				for item in row :
+					
+					if attr in item and item[attr] == value:
+						r.append(item)
+			else:
+				#
+				# We are dealing with a vector of objects
+				# 
+				if attr in row and row[attr] == value:
+					r.append(row)
+
 		return r
 	@staticmethod
 	def Extract(lattr,data):
@@ -43,7 +52,8 @@ class ML:
 	@TODO: determine computationally determine epsilon
 """
 class AnomalyDetection:
-		
+	def __init__(self):
+		pass	
 	def split(self,data,index=-1,threshold=0.65) :
 		N	= len(data)
 		# if N < LIMIT:
@@ -226,7 +236,9 @@ class AnomalyDetection:
 		sigma = [ list(row) for row in sigma]
 		return {"cov":sigma,"mean":list(u)}
 
-class AnalyzeAnomalies(AnomalyDetection):
+class AnalyzeAnomaly(AnomalyDetection):
+	def __init__(self):
+		AnomalyDetection.__init__(self)
 	"""
 		This analysis function will include a predicted status because an anomaly can either be 
 			- A downtime i.e end of day 
@@ -236,17 +248,28 @@ class AnalyzeAnomalies(AnomalyDetection):
 	"""
 	def predict(self,xo,info):
 		x = xo[len(xo)-1]
-		r = AnomalyDetection.predict(x,info)
+		r = AnomalyDetection.predict(self,[x],info)
 		#
 		# In order to determine what the anomaly is we compute the slope (idle or crash)
 		# The slope is computed using the covariance / variance of features
 		#
-		N = len(info['features'])
-		xy = ML.Extract(info['features'],xo)
-		xy = np.matrix(xy)
-		vxy= [xy[:,i] for i in range(0,N)]
-		print N,vxy.shape
-		alpha = info['cov'] / vxy
+		if r is not None:
+			N = len(info['features'])
+			xy = ML.Extract(info['features'],xo)
+			xy = np.array(xy)
+				
+			vxy= np.array([ np.var(xy[:,i]) for i in range(0,N)])
+			cxy=np.array(info['parameters']['cov'])
+			#cxy=np.cov(np.transpose(xy))
+			if np.sum(vxy) == 0:
+				vxy = cxy
+			
+			alpha = cxy/vxy
+			
+			
+
+			r =  {"anomaly":r[0][1],"slope":list(alpha[:,0])}
+			
 		return r
 class Regression:
 	parameters = {}