def findDiffSeries(series1,diffInterval=1): series1 = smoothArray(series1) diffSeries =[] lenSeries=len(series1) if (diffInterval>lenSeries): print "diffInterval can not be greater than the length of series" return i=diffInterval while(i<lenSeries): diffSeries.append((i-diffInterval,i,((series1[i]-series1[i-diffInterval])/series1[i-diffInterval])*100)) i=i+1 (N,P) = MADThreshold(diffSeries) print "N & P ::::::::"+ str(N)+":::"+ str(P) return diffSeries
def slopeBasedDetection(series1,smoothed1,series2,smoothed2,next_val_to_consider = 7, default_threshold = True, threshold = 0, what_to_consider = 1): if(smoothed1 == False): series1 = smoothArray(series1) if(smoothed2 == False): series2 = smoothArray(series2) n = len(series1) positive_slopes = [] anomalous_pts = [] negative_slopes = [] i = 0 while(i<(n-next_val_to_consider+1)): if((series2[i+next_val_to_consider-1] - series2[i]) == 0): i= i+ next_val_to_consider continue diff = ((series1[i+next_val_to_consider-1] - series1[i]) * series2[i] )/ ((series2[i+next_val_to_consider-1] - series2[i]) * series1[i]) if(diff < 0): negative_slopes.append((i,i+next_val_to_consider-1,diff)) else: positive_slopes.append((i,i+next_val_to_consider-1,diff)) i= i+ next_val_to_consider if(default_threshold == True): temp = [] for x in positive_slopes: temp.append(x[2]) if(len(temp)>0): (_,positive_threshold) = MADThreshold(temp) # print "Positive Threshold Value:" + str(positive_threshold) if(default_threshold == True): temp = [] for x in negative_slopes: temp.append(x[2]) if(len(temp)>0): (negative_threshold,_) = MADThreshold(temp) # print "Negative Threshold Value:" + str(negative_threshold) if(what_to_consider == 1): positive_anomalous_pts = [] if(default_threshold): print "Possitive Threshold of Slope Based : " + str(positive_threshold) for i in range(0,len(positive_slopes)): if(positive_slopes[i][2] > positive_threshold): positive_anomalous_pts.append(positive_slopes[i]) else: print "Possitive Threshold of Slope Based User Given: " + str(threshold) for i in range(0,len(positive_slopes)): if(positive_slopes[i][2] > threshold): positive_anomalous_pts.append(positive_slopes[i]) return positive_anomalous_pts elif(what_to_consider == -1): negative_anomalous_pts = [] if(default_threshold): print "Negative Threshold of Slope Based : " + str(negative_threshold) for i in range(0,len(negative_slopes)): if(negative_slopes[i][2] < negative_threshold): negative_anomalous_pts.append(negative_slopes[i]) else: print "Negative Threshold of Slope Based User Given: " + str(threshold) for i in range(0,len(negative_slopes)): if(negative_slopes[i][2] < threshold): negative_anomalous_pts.append(negative_slopes[i]) return negative_anomalous_pts elif(what_to_consider == 0): if(default_threshold): print "Possitive Threshold of Slope Based : " + str(positive_threshold) print "Negative Threshold of Slope Based : " + str(negative_threshold) positive_anomalous_pts = [] for i in range(0,len(positive_slopes)): if(positive_slopes[i][2] > positive_threshold): positive_anomalous_pts.append(positive_slopes[i]) negative_anomalous_pts = [] for i in range(0,len(negative_slopes)): if(negative_slopes[i][2] < negative_threshold): negative_anomalous_pts.append(negative_slopes[i]) anomalous_pts = positive_anomalous_pts + negative_anomalous_pts # Sort array according to start of window sorted(anomalous_pts, key=lambda x: x[0]) return anomalous_pts else: print "Threshold of Slope Based User Given: " + str(threshold) positive_anomalous_pts = [] for i in range(0,len(positive_slopes)): if(positive_slopes[i][2] > threshold): positive_anomalous_pts.append(positive_slopes[i]) negative_anomalous_pts = [] for i in range(0,len(negative_slopes)): if(abs(negative_slopes[i][2]) > threshold): negative_anomalous_pts.append(negative_slopes[i]) anomalous_pts = positive_anomalous_pts + negative_anomalous_pts # Sort array according to start of window sorted(anomalous_pts, key=lambda x: x[0]) return anomalous_pts pass