-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.py
161 lines (134 loc) · 5.11 KB
/
predict.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#importing the necessary libraries
from features import Features
import pandas as pd
import time
import pickle
from os import listdir
from typing import Dict
import json
from datetime import datetime
def get_files(file_path: str) -> Dict[str, str]:
"""
Function to get the list of files inside a file path
:parameter file_path will contain the file path of the folder
:attrib onlyfiles is a list of all the files in file_path
:attrib files is a dictionary which will have the sound file as key,
and the file path as the value
This function returns the files as a dictionary
"""
onlyfiles = [f for f in listdir(file_path)]
files = {}
for i in onlyfiles:
files[i] = file_path + i
return files
def machine(machine_type: str) ->str:
"""
Function machine gets the type model depending on the machine type
:parameter machine_type will contain the type of machine
"""
if machine_type.lower() == "fan":
model = "Models/fan_model.sav"
return model
elif machine_type.lower() == "valve":
model = "Models/valve_model.sav"
return model
elif machine_type.lower() == "pump":
model = "Models/pump_model.sav"
return model
elif machine_type.lower() == "slider":
model = "Models/slider_model.sav"
return model
def predict(df, model: str) -> int:
"""
Function predict will predict the data
:parameter df contains the file with the features
:parameter model contains the model to be used in predicting
:attrib fm will contain the loaded prediction
:attrib prediction will contain the prediction from the model
This function returns the variable if 1, means abnormal, if 0 means normal
"""
with open(model, 'rb') as f:
fm = pickle.load(f)
prediction = fm.predict(df)
#print(fm.score(df, df))
#print(f"Machine is {prediction}.")
if prediction == 1:
print("Machine is Abnormal.")
return 1
#send an alert
elif prediction == 0:
print("Machine is Normal.")
return 0
def results(pred: Dict[str, int], file_path: str, anomalies: str, machine_type: str):
"""
Function to get the results and save it as a json file
:parameter pred contains the prediction in a dictionary format
:parameter file_path contains the file path of the folder
:parameter anomalies contains the number of anomalies
:parameter machine_type contains the type of machine
It creates a json file and saves the json file in the Test_Predictions_Output
"""
now = datetime.now()
date = now.strftime("%d/%m/%Y %H:%M:%S")
result = {
'Type of Machine': machine_type,
'File Path' : file_path,
'Date and Time' : date,
'Predictions' : pred,
'Total Anomalies' : anomalies
}
with open(f'Demo/Test_Predictions_output/{machine_type}_result.json', 'w') as json_file:
json.dump(result, json_file)
print("Results are now in your file!")
def get_prediction(dict_files: Dict[str,str], model: str) -> Dict[str, int]:
"""
Function to get the prediction of the files
:parameter dict_files will contain the file paths
:parameter model will contain the specified model for prediction
:attrib pred will contain a dictionary with the prediction
This function will return the prediction in a dictionary
"""
pred = {}
for key, value in dict_files.items():
print(value)
data = Features.get_features(value)
df = pd.DataFrame([data])
print(f"Predicting for {key}...")
prediction = predict(df, model)
pred[key] = prediction
return pred
def count_anomalies(pred: Dict[str, int]) -> int:
"""
Function to calculate the anomalies in the prediction
:parameter pred contains the prediction in a dictionary format
:attrib count will contain the total number of anomalies
This function returns the count as in integer
"""
count = 0
for i in pred.values():
if i == 1:
count +=1
return count
"""
This is where the program starts
:attrib machine_type will contain the input type of machine
:attrib file_path will contain the file path of the sound files
:attrib start_time will contain the time the program started
:attrib model will contain the model to predict the file
:attrib dict_files will contain the files in a dictionary
:attrib pred will contain the prediction in a dictionary
:attrib count will contain the total number of abnormal in the set
:attrib end_time will contain the time the program ended
"""
machine_type = input("Enter the machine type: ")
file_path = input("Enter the file path: ")
start_time = time.time()
model = machine(machine_type)
dict_files = get_files(file_path)
pred = get_prediction(dict_files, model)
count = count_anomalies(pred)
anomalies = str(count) + " out of " + str(len(pred.values())) + " samples."
print(f"Predicted Anomalies: {anomalies}")
results(pred, file_path, anomalies, machine_type)
end_time = time.time()
print(f"Program runs for {end_time - start_time} seconds.")