AtthalaricNero commited on
Commit
0da1da8
·
1 Parent(s): cea3342

feat(anomaly): add functions for preparing sensor data and calculating risk scores

Browse files
Files changed (1) hide show
  1. utils.py +73 -0
utils.py CHANGED
@@ -141,3 +141,76 @@ def create_timestamp_from_predictions(predictions, sensor_timestamp=None):
141
  except Exception as e:
142
  print(f"Error creating timestamp: {str(e)}")
143
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  except Exception as e:
142
  print(f"Error creating timestamp: {str(e)}")
143
  return None
144
+
145
+
146
+ def prepare_sensor_data_for_anomaly(sensor_data, preprocessor):
147
+ try:
148
+ sensor_features = {
149
+ "Air temperature [K]": sensor_data.get("air_temp"),
150
+ "Process temperature [K]": sensor_data.get("process_temp"),
151
+ "Rotational speed [rpm]": sensor_data.get("rotational_speed"),
152
+ "Torque [Nm]": sensor_data.get("torque"),
153
+ "Tool wear [min]": sensor_data.get("tool_wear"),
154
+ }
155
+ if None in sensor_features.values():
156
+ print("Error: Missing sensor data!")
157
+ return None
158
+
159
+ X = pd.DataFrame([sensor_features])
160
+
161
+ if preprocessor is None:
162
+ raise ValueError("preprocessor not loaded.")
163
+
164
+ X_transormed = preprocessor.transform(X)
165
+
166
+ return X_transormed
167
+
168
+ except Exception as e:
169
+ print(f"Error preparing prediction data: {str(e)}")
170
+ import traceback
171
+
172
+ traceback.print_exc()
173
+ return None
174
+
175
+
176
+ def calculate_risk_score(confidence, severity):
177
+ severity_weight = severity / 5.0
178
+ risk_score = confidence * severity_weight * 100
179
+
180
+ return risk_score
181
+
182
+
183
+ def get_risk_level(risk_score):
184
+ if risk_score >= 80:
185
+ return "Critical"
186
+ elif risk_score >= 60:
187
+ return "High"
188
+ elif risk_score >= 40:
189
+ return "Medium"
190
+ elif risk_score >= 20:
191
+ return "Low"
192
+ else:
193
+ return "Very Low"
194
+
195
+
196
+ def get_failure_severity(prediction_index):
197
+ severity_mapping = {
198
+ 0: 4, # Heat Dissipation Failure - High
199
+ 1: 4, # Overstrain Failure - High
200
+ 2: 5, # Power Failure - Critical
201
+ 3: 2, # Random Failures - Low-Medium
202
+ 4: 3, # Tool Wear Failure - Medium
203
+ }
204
+
205
+ return severity_mapping.get(prediction_index, 3)
206
+
207
+
208
+ def get_failure_type_name(prediction_index):
209
+ failure_types = {
210
+ 0: "Heat Dissipation Failure",
211
+ 1: "Overstrain Failure",
212
+ 2: "Power Failure",
213
+ 3: "Random Failures",
214
+ 4: "Tool Wear Failure",
215
+ }
216
+ return failure_types.get(prediction_index, "Unknown Failure")