<jupyter_start><jupyter_text>Import Statements<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.lines import Line2D
from scipy.signal import butter, lfilter
from google.colab import files<jupyter_output><empty_output><jupyter_text>Auto-Cloud Implementation<jupyter_code>class DataCloud:
	N=0
	def __init__(self,x):
		self.n=1
		self.mean=x
		self.variance=0
		self.pertinency=1
		self.eccAn=0
		DataCloud.N+=1
	def addDataClaud(self,x):
		self.n=2
		self.mean=(self.mean+x)/2
		self.variance=((np.linalg.norm(self.mean-x))**2)
	def updateDataCloud(self,n,mean,variance):
		self.n=n
		self.mean=mean
		self.variance=variance
class AutoCloud:
	c= np.array([DataCloud(0)],dtype=DataCloud)
	alfa= np.array([0.0],dtype=float)
	intersection = np.zeros((1,1),dtype=int)
	listIntersection = np.zeros((1),dtype=int)
	matrixIntersection = np.zeros((1,1),dtype=int)
	relevanceList = np.zeros((1),dtype=int)
	k=1
	def __init__(self, m):
		AutoCloud.m = m
		AutoCloud.c= np.array([DataCloud(0)],dtype=DataCloud)
		AutoCloud.alfa= np.array([0.0],dtype=float)
		AutoCloud.intersection = np.zeros((1,1),dtype=int)
		AutoCloud.listIntersection = np.zeros((1),dtype=int)
		AutoCloud.relevanceList = np.zeros((1),dtype=int)
		AutoCloud.matrixIntersection = np.zeros((1,1),dtype=int)
		AutoCloud.classIndex = []
		AutoCloud.k = 1		

	def mergeClouds(self):
		i=0
		while(i<len(AutoCloud.listIntersection)-1):
			merge=False
			j=i+1
			while(j<len(AutoCloud.listIntersection)):
				if(AutoCloud.listIntersection[i] == 1 and AutoCloud.listIntersection[j] == 1):
					AutoCloud.matrixIntersection[i,j] = AutoCloud.matrixIntersection[i,j] + 1;
				nI = AutoCloud.c[i].n
				nJ = AutoCloud.c[j].n
				meanI = AutoCloud.c[i].mean
				meanJ = AutoCloud.c[j].mean
				varianceI = AutoCloud.c[i].variance
				varianceJ = AutoCloud.c[j].variance
				nIntersc = AutoCloud.matrixIntersection[i,j]
				if (nIntersc > (nI - nIntersc) or nIntersc > (nJ - nIntersc)):
					merge = True
					#update values
					n = nI + nJ - nIntersc
					mean = ((nI * meanI) + (nJ * meanJ))/(nI + nJ)
					variance = ((nI - 1) * varianceI + (nJ - 1) * varianceJ)/(nI + nJ - 2)
					newCloud = DataCloud(mean)
					newCloud.updateDataCloud(n,mean,variance)
					#updating intersection list
					AutoCloud.listIntersection = np.concatenate((AutoCloud.listIntersection[0 : i], np.array([1]), AutoCloud.listIntersection[i + 1 : j],AutoCloud.listIntersection[j + 1 : np.size(AutoCloud.listIntersection)]),axis=None)
					#updating data clouds list 
					AutoCloud.c = np.concatenate((AutoCloud.c[0 : i ], np.array([newCloud]), AutoCloud.c[i + 1 : j],AutoCloud.c[j + 1 : np.size(AutoCloud.c)]),axis=None)
					#update  intersection matrix
					M0 = AutoCloud.matrixIntersection
					#remove lines
					M1=np.concatenate((M0[0 : i , :],np.zeros((1,len(M0))),M0[i + 1 : j, :],M0[j + 1 : len(M0), :]))
					#remove columns
					M1=np.concatenate((M1[:, 0 : i ],np.zeros((len(M1),1)),M1[:, i+1 : j],M1[:, j+1 : len(M0)]),axis=1)
					#calculating new column
					col = (M0[:, i] + M0[:, j])*(M0[: , i]*M0[:, j] != 0)
					col = np.concatenate((col[0 : j], col[j + 1 : np.size(col)]))
					#calculating new line
					lin = (M0[i, :]+M0[j, :])*(M0[i, :]*M0[j, :] != 0)
					lin = np.concatenate((lin[ 0 : j], lin[j + 1 : np.size(lin)]))
					#updating column
					M1[:,i]=col
					#updating line
					M1[i,:]=lin
					M1[i, i + 1 : j] = M0[i, i + 1 : j] + M0[i + 1 : j, j].T;   
					AutoCloud.matrixIntersection = M1
				j += 1
			if(merge):
				i = 0
			else:
				i += 1
				
	def run(self,X):
		AutoCloud.listIntersection = np.zeros((np.size(AutoCloud.c)),dtype=int)
		if AutoCloud.k==1:
			AutoCloud.c[0]=DataCloud(X)
			AutoCloud.classIndex.append(0)
		elif AutoCloud.k==2:
			AutoCloud.c[0].addDataClaud(X)
			AutoCloud.classIndex.append(0)
		elif AutoCloud.k>=3:
			i=0
			createCloud = True
			AutoCloud.alfa = np.zeros((np.size(AutoCloud.c)),dtype=float)
			for data in AutoCloud.c:
				n= data.n +1
				mean = ((n-1)/n)*data.mean + (1/n)*X
				variance = ((n-1)/n)*data.variance +(1/n)*((np.linalg.norm(X-mean))**2)
				eccentricity=(1/n)+((mean-X).T.dot(mean-X))/(n*variance)
				typicality = 1 - eccentricity
				norm_eccentricity = eccentricity/2
				norm_typicality = typicality/(AutoCloud.k-2)
				data.eccAn = eccentricity
				if(norm_eccentricity<=(AutoCloud.m**2 +1)/(2*n)):
					data.updateDataCloud(n,mean,variance)
					AutoCloud.alfa[i] = norm_typicality
					createCloud= False
					AutoCloud.listIntersection.itemset(i,1)
				else:
					AutoCloud.alfa[i] = 0
					AutoCloud.listIntersection.itemset(i,0)
				i+=1
			
			if(createCloud):
				AutoCloud.c = np.append(AutoCloud.c,DataCloud(X))
				AutoCloud.listIntersection = np.insert(AutoCloud.listIntersection,i,1)
				AutoCloud.matrixIntersection = np.pad(AutoCloud.matrixIntersection, ((0,1),(0,1)), 'constant', constant_values=(0)) 
			self.mergeClouds()
			AutoCloud.relevanceList = AutoCloud.alfa /np.sum(AutoCloud.alfa)
			classIndex = np.argmax(AutoCloud.relevanceList)
			AutoCloud.classIndex.append(classIndex)

        
		AutoCloud.k=AutoCloud.k+1<jupyter_output><empty_output><jupyter_text>Time filter implementation<jupyter_code>def filter_time(signal_input,e1 = 20, e2 = 60):
  result = []
  for i in range(e1):
    result.append(0)
  for i in range(e1, len(signal_input)):
    if(signal_input[i] == 0 and np.sum(signal_input[i-e1:i] == signal_input[i]) == e1):
      result.append(signal_input[i])
    elif(signal_input[i] != 0 and  np.sum(signal_input[i-e2:i] == signal_input[i]) == e2):
      result.append(signal_input[i])
    else:
      result.append(result[len(result)-1])
  return np.array(result)<jupyter_output><empty_output><jupyter_text>First fault scenario Load data set<jupyter_code>data_fault_1 = pd.read_csv('f_01_06.csv')
constrained_vars = ["%0d" % v for v in [2,3]]
data_f = np.array(data_fault_1[constrained_vars])
df = pd.DataFrame(data_f, columns=['1','2'])<jupyter_output><empty_output><jupyter_text>Pre-processing<jupyter_code>import pandas as pd
from sklearn import preprocessing

x = data_f
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
data_f = x_scaled
plt.figure(figsize=(20,10)) 
plt.plot(data_f)
plt.show()
freq = 400
cutoff = 5

data = data_f[:,0]
plt.figure(figsize=(20,4)) 
plt.plot(data)
plt.show()
b, a = butter(5, (cutoff/freq), btype='low', analog=False)
data = lfilter(b, a, data)[1000:]
plt.figure(figsize=(20,4)) 
plt.plot(data)
plt.show()
freq = 500
cutoff = 5

data_1 = data_f[:,1]
plt.figure(figsize=(20,4)) 
plt.plot(data_1)
plt.show()
b, a = butter(5, (cutoff/freq), btype='low', analog=False)
data_1 = lfilter(b, a, data_1)[1000:]
plt.figure(figsize=(20,4))
plt.plot(data_1)
plt.show()
result_window = pd.DataFrame(data)
result_window = result_window.rolling(window=4).mean()
data = result_window.values


result_window = pd.DataFrame(data_1)
result_window = result_window.rolling(window=1).mean()
data_1 = result_window.values

data = data[3:,0]
data_1 = data_1[3:,0]

data_f = np.array([data,data_1]).T
data_f = np.concatenate((data_f[:42699],data_f))
plt.figure(figsize=(20,4)) 
plt.plot(data_f)
plt.show()
plt.figure(figsize=(10,6)) 
sns.set(style= 'whitegrid' )
plt.xlabel("Time (s)")
plt.ylabel("Value")
plt.plot(data_f[:,0])
plt.plot(data_f[:,1])
plt.legend(["Normalized Pressure","Normalized Level"])
plt.show()
teste = AutoCloud(2.48)
for t in data_f:
    teste.run(np.array(t))
result_f = np.array(teste.classIndex)<jupyter_output><empty_output><jupyter_text>Results<jupyter_code>plt.figure(figsize=(10, 18))
plt.subplot(311)
plt.plot(data_f)
plt.xlabel("Time (s)")
plt.ylabel("Value")
plt.legend(["Normalized Pressure","Normalized Level"])
plt.title('a)')
plt.subplot(312)

plt.xlabel("Time (s)")
plt.ylabel("# group")
plt.plot(result_f)
plt.ylim(-0.2,4.2)
plt.title('b)')
plt.subplot(313)


plt.xlabel("Time (s)")
plt.ylabel("# group")
result_filter_time = filter_time(result_f, 180,  100)
plt.ylim(-0.2,4.2)
plt.title('c)')
plt.plot(result_filter_time)
target = np.concatenate((np.zeros(5419),np.ones(730),np.zeros(6184),np.ones(1046),np.zeros(6220),np.ones(1194),np.zeros(6206),2*np.ones(1200),np.zeros(6200),2*np.ones(1100),np.zeros(6100),2*np.ones(1100),np.zeros(5419),np.ones(730),np.zeros(6184),np.ones(1046),np.zeros(6220),np.ones(1194),np.zeros(6206),2*np.ones(1200),np.zeros(6200),2*np.ones(1100),np.zeros(6100),2*np.ones(1100),np.zeros(6185)),axis=0)<jupyter_output><empty_output><jupyter_text>Performance results for fault scenario<jupyter_code>from sklearn.metrics import classification_report
print(classification_report(target[3:],result_filter_time))<jupyter_output>precision    recall  f1-score   support

         0.0       0.96      0.99      0.97     78840
         1.0       0.85      0.76      0.80      5940
         2.0       0.89      0.74      0.81      6800

    accuracy                           0.95     91580
   macro avg       0.90      0.83      0.86     91580
weighted avg       0.95      0.95      0.95     91580<jupyter_text>Performance results with data cluster assessment metrics<jupyter_code>from sklearn import metrics
metrics.adjusted_rand_score(target[43003:],result_filter_time[43000:])
metrics.homogeneity_score(target[43003:],result_filter_time[43000:])
metrics.completeness_score(target[43003:],result_filter_time[43000:])
metrics.v_measure_score(target[43003:],result_filter_time[43000:])<jupyter_output><empty_output><jupyter_text>Second fault scenario Load data set<jupyter_code>data_fault_1 = pd.read_csv('f_07_12.csv')
constrained_vars = ["%0d" % v for v in [2,3]]
data_f = np.array(data_fault_1[constrained_vars])
df = pd.DataFrame(data_f, columns=['1','2'])<jupyter_output><empty_output><jupyter_text>Pre-processing<jupyter_code>import pandas as pd
from sklearn import preprocessing

x = data_f
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
data_f = x_scaled
freq = 400
cutoff = 5

data = data_f[:,0]
plt.figure(figsize=(20,4)) 
plt.plot(data)
plt.show()
b, a = butter(5, (cutoff/freq), btype='low', analog=False)
data = lfilter(b, a, data)[1000:]
plt.figure(figsize=(20,4)) 
plt.plot(data)
plt.show()
freq = 50
cutoff = 5

data_1 = data_f[:,1]
plt.figure(figsize=(20,4)) 
plt.plot(data_1)
plt.show()
b, a = butter(5, (cutoff/freq), btype='low', analog=False)
data_1 = lfilter(b, a, data_1)[1000:]
plt.figure(figsize=(20,4))
plt.plot(data_1)
plt.show()

result_window = pd.DataFrame(data)
result_window = result_window.rolling(window=4).mean()
data = result_window.values

result_window = pd.DataFrame(data_1)
result_window = result_window.rolling(window=1).mean()
data_1 = result_window.values

data = data[3:,0]
data_1 = data_1[3:,0]

data_f = np.array([data,data_1]).T
teste = AutoCloud(2.9)
for t in data_f:
    teste.run(np.array(t))
result_f = teste.classIndex<jupyter_output><empty_output><jupyter_text>Result<jupyter_code>plt.figure(figsize=(10,18)) 
sns.set(style= 'whitegrid' )
plt.subplot(311)

plt.xlabel("Time (s)")
plt.ylabel("Value")
plt.plot(data_f[:,0])
plt.plot(data_f[:,1])
plt.legend(["Normalized Pressure","Normalized Level"])
plt.title('a)')
plt.subplot(312)

plt.xlabel("Time (s)")
plt.ylabel("# group")

plt.plot(result_f)
plt.title('b)')
plt.ylim(-0.2,6.2)

#Filter time
plt.subplot(313)
plt.xlabel("Time (s)")
plt.ylabel("# group")

plt.ylim(-0.2,6.2)
result_filter_time = filter_time(result_f,80,80)
plt.plot(result_filter_time)
target = np.concatenate((np.zeros(5459),np.ones(740),np.zeros(6134),np.ones(1060),np.zeros(6306),2*np.ones(1094),np.zeros(6206),3*np.ones(1200),np.zeros(6200),4*np.ones(1000),np.zeros(6800),5*np.ones(500),np.zeros(6185)),axis=0)
plt.title('c)')


from sklearn.metrics import classification_report
print(classification_report(target[3:],result_f))
from sklearn import metrics
metrics.adjusted_rand_score(target[3:],result_filter_time)
metrics.homogeneity_score(target[3:],result_filter_time)
metrics.completeness_score(target[3:],result_filter_time)
metrics.v_measure_score(target[3:],result_filter_time)<jupyter_output><empty_output><jupyter_text>Third fault scenario<jupyter_code>data_fault_1 = pd.read_csv('f_3_6_9_12.csv')
constrained_vars = ["%0d" % v for v in [2,3]]
data_f = np.array(data_fault_1[constrained_vars])
df = pd.DataFrame(data_f, columns=['1','2'])<jupyter_output><empty_output><jupyter_text>pre-prossing<jupyter_code>import pandas as pd
from sklearn import preprocessing

x = data_f
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
data_f = x_scaled
freq = 400
cutoff = 5

data = data_f[:,0]
plt.figure(figsize=(20,4)) 
plt.plot(data)
plt.show()
b, a = butter(5, (cutoff/freq), btype='low', analog=False)
data = lfilter(b, a, data)[1000:]
plt.figure(figsize=(20,4)) 
plt.plot(data)
plt.show()

freq = 400
cutoff = 5

data_1 = data_f[:,1]
plt.figure(figsize=(20,4)) 
plt.plot(data_1)
plt.show()
b, a = butter(5, (cutoff/freq), btype='low', analog=False)
data_1 = lfilter(b, a, data_1)[1000:]
plt.figure(figsize=(20,4))
plt.plot(data_1)
plt.show()
result_window = pd.DataFrame(data)
result_window = result_window.rolling(window=1).mean()
data = result_window.values

result_window = pd.DataFrame(data_1)
result_window = result_window.rolling(window=3).mean()
data_1 = result_window.values

data = data[20:,0]
data_1 = data_1[20:,0]

data_f = np.array([data,data_1]).T
teste = AutoCloud(2.35)
for t in data_f:
    teste.run(np.array(t))
result_f = teste.classIndex
result_filter_time = filter_time(result_f,80,80)
plt.figure(figsize=(10,18)) 
sns.set(style= 'whitegrid' )
plt.subplot(311)

plt.xlabel("Time (s)")
plt.ylabel("Value")
plt.plot(data_f[:,0])
plt.plot(data_f[:,1])
plt.legend(["Normalized Pressure","Normalized Level"])
plt.title('a)')
plt.subplot(312)

plt.xlabel("Time (s)")
plt.ylabel("# group")
plt.plot(result_f)

plt.title('b)')
plt.ylim(-0.2,6.2)
#Filter time
plt.subplot(313)
plt.xlabel("Time (s)")
plt.ylabel("# group")

plt.ylim(-0.2,6.2)
plt.plot(result_filter_time)
plt.title('c)')

target = np.concatenate((np.zeros(5360),np.ones(1290),np.zeros(6135),2*np.ones(1100),np.zeros(6162),3*np.ones(2300),np.zeros(5707),4*np.ones(1600),np.zeros(6111)),axis=0)
from sklearn.metrics import classification_report
print(classification_report(target,result_filter_time))
from sklearn import metrics
metrics.adjusted_rand_score(target,result_filter_time)
metrics.homogeneity_score(target,result_filter_time)
metrics.completeness_score(target,result_filter_time)
metrics.v_measure_score(target,result_filter_time)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1715423705534954, 'fraction_numerical': 0.0519159456118665, 'mean_word_length': 5.3649475524475525, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '251531', 'license': 'MIT', 'n_tokens_mistral': 6667, 'n_tokens_neox': 5922, 'n_words': 878, 'path': 'An Evolving Approach for Fault Diagnosis of Dynamic Systems.ipynb', 'repo_name': 'MailsonRibeiroSantos/An-Evolving-Approach-for-Fault-Diagnosis-of-Dynamic-Systems', 'size': '14562'}