-
Notifications
You must be signed in to change notification settings - Fork 0
/
processes.py
148 lines (123 loc) · 5.76 KB
/
processes.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
"""
Processes of the diagnostics of the monitored devices.
"""
import os
import numpy as np
import configparser
from datetime import date, timedelta
from src.data_handling.preprocessing import create_signal_windows
from src.data_handling.influxdb_handler import InfluxDBHandler
from src.predictive_maintenance.anomaly_detector import AnomalyDetector
from src.predictive_maintenance.anomaly_classifier import AnomalyClassifier
from src.predictive_maintenance.segment_comparator import SegmentComparator
from src.predictive_maintenance.maintenance_planner import get_maintenance_plan
def diagnose_robot_health(robot_arm_tag):
"""
Diagnose robot health in last hour.
:param robot_arm_tag: Robot InfluxDB arm tag.
:return: Diagnosis results as dictionary.
"""
diagnosis = {
'anomalies_number': None,
'anomalies_labels': None,
'anomalies_labels_count': None,
'number_of_segments': None,
'segments_comparison': None
}
config = configparser.ConfigParser()
config.read('config/configurations.ini')
pause_duration = int(config.get('segment-comparator', 'min_pause_duration'))
similarity_threshold = float(config.get('segment-comparator', 'similarity_threshold'))
influxdb_handler = InfluxDBHandler()
segment_comparator = SegmentComparator(min_pause_duration=pause_duration, similarity_threshold=similarity_threshold)
anomaly_detector = AnomalyDetector()
anomaly_detector.load_model()
anomaly_classifier = AnomalyClassifier()
anomaly_classifier.load_model()
try:
robot_energy_consumption = influxdb_handler.query_energy_consumption_by_hours(hours=1, arm_tag=robot_arm_tag)
except:
# remove historical signal
if os.path.exists('data/historical_signals/' + str(robot_arm_tag) + '.npy'):
os.remove('data/historical_signals/' + str(robot_arm_tag) + '.npy')
return diagnosis
try:
robot_energy_consumption_windows = create_signal_windows(robot_energy_consumption, 288, 0)
detection_results = anomaly_detector.detect(robot_energy_consumption_windows)
abnormal_windows = robot_energy_consumption_windows[detection_results == 1]
classification_results = np.argmax(anomaly_classifier.classify(abnormal_windows), axis=1)
unique_labels, label_counts = np.unique(classification_results, return_counts=True)
diagnosis['anomalies_number'] = len(abnormal_windows)
diagnosis['anomalies_labels'] = np.array(unique_labels)
diagnosis['anomalies_labels_count'] = np.array(label_counts)
except:
pass
try:
num_of_segments = len(segment_comparator.get_motions(robot_energy_consumption))
diagnosis['number_of_segments'] = num_of_segments
historical_energy_consum = np.load('data/historical_signals/' + str(robot_arm_tag) + '.npy')
segments_comparison = segment_comparator.compare_signals(robot_energy_consumption, historical_energy_consum)
diagnosis['segments_comparison'] = segments_comparison
np.save('data/historical_signals/' + str(robot_arm_tag) + '.npy', robot_energy_consumption)
except:
pass
# TODO save results into database
maintenance_plan = get_maintenance_plan(diagnosis)
print(maintenance_plan)
return diagnosis
def diagnose_robot_health_history(robot_arm_tag):
"""
Diagnose robot health with historical data.
:param robot_arm_tag: Robot InfluxDB arm tag.
:return: Diagnosis results as dictionary.
"""
diagnosis = {
'day_comparison': None,
'week1_comparison': None,
'week2_comparison': None,
'week3_comparison': None,
'week4_comparison': None
}
config = configparser.ConfigParser()
config.read('config/configurations.ini')
pause_duration = int(config.get('segment-comparator', 'min_pause_duration'))
similarity_threshold = float(config.get('segment-comparator', 'similarity_threshold'))
influxdb_handler = InfluxDBHandler()
segment_comparator = SegmentComparator(min_pause_duration=pause_duration, similarity_threshold=similarity_threshold)
today_date = date.today().strftime('%Y-%m-%d')
yestr_date = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d')
week1_date = (date.today() - timedelta(days=7)).strftime('%Y-%m-%d')
week2_date = (date.today() - timedelta(days=14)).strftime('%Y-%m-%d')
week3_date = (date.today() - timedelta(days=21)).strftime('%Y-%m-%d')
week4_date = (date.today() - timedelta(days=28)).strftime('%Y-%m-%d')
try:
today_ec = influxdb_handler.query_energy_consumption_by_day(today_date, robot_arm_tag)
except:
return diagnosis
try:
yestr_ec = influxdb_handler.query_energy_consumption_by_day(yestr_date, robot_arm_tag)
diagnosis['day_comparison'] = segment_comparator.compare_signals(today_ec, yestr_ec)
except:
pass
try:
week1_ec = influxdb_handler.query_energy_consumption_by_day(week1_date, robot_arm_tag)
diagnosis['week1_comparison'] = segment_comparator.compare_signals(today_ec, week1_ec)
except:
pass
try:
week2_ec = influxdb_handler.query_energy_consumption_by_day(week2_date, robot_arm_tag)
diagnosis['week2_comparison'] = segment_comparator.compare_signals(today_ec, week2_ec)
except:
pass
try:
week3_ec = influxdb_handler.query_energy_consumption_by_day(week3_date, robot_arm_tag)
diagnosis['week3_comparison'] = segment_comparator.compare_signals(today_ec, week3_ec)
except:
pass
try:
week4_ec = influxdb_handler.query_energy_consumption_by_day(week4_date, robot_arm_tag)
diagnosis['week4_comparison'] = segment_comparator.compare_signals(today_ec, week4_ec)
except:
pass
# TODO save results into database
return diagnosis