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
# -*- coding:utf-8 -*-
"""用电健康各项偏差"""
# from pot_libs.logger import log
dev_ranges = {
# 电压偏差
"v": [
[(80, 100), [(0, 0.05), (-0.05, 0)]],
[(70, 80), [(0.05, 0.07), (-0.07, -0.05)]],
[(60, 70), [(0.07, 0.1), (-0.1, -0.07)]],
[(0, 60), [(0.1, 0.2), (-0.2, -0.1)]],
[0, [(0.2, 1), (-1, -0.2)]]
],
# 频率
"freq": [
[(90, 100), [(0, 0.05), (-0.05, 0)]],
[(80, 90), [(0.05, 0.1), (-0.1, -0.05)]],
[(60, 80), [(0.1, 0.2), (-0.2, -0.1)]],
[(0, 60), [(0.2, 0.5), (-0.5, -0.2)]],
[0, [(0.5, 1), (-1, -0.5)]]
],
# 三相电压不平衡度
"ubl": [
[(90, 100), [(0, 0.005)]],
[(80, 90), [(0.005, 0.01)]],
[(60, 80), [(0.01, 0.02)]],
[(0, 60), [(0.02, 0.04)]],
[0, [(0.04, 1)]]
],
# 功率因数
"costtl": [
[(90, 100), [(0.95, 1.0), (-1.0, -0.95)]],
[(80, 90), [(0.9, 0.95), (-0.95, -0.9)]],
[(60, 80), [(0.85, 0.9), (-0.9, -0.85)]],
[(0, 60), [(0.65, 0.85), (-0.85, -0.65)]],
[0, [(0, 0.65), (-0.65, 0)]]
],
# 谐波畸变率
"thdu": [
[(90, 100), [(0, 0.02)]],
[(80, 90), [(0.02, 0.03)]],
[(60, 80), [(0.03, 0.05)]],
[(0, 60), [(0.05, 0.1)]],
[0, [(0.1, 1)]]
],
# 负载率
"lf": [
[100, [(-1, 0.8)]],
[60, [(0.8, 0.9)]],
[0, [(0.9, 1)]]
],
}
def get_dev_grade(dev_type, cur):
"""获取分数档数等级"""
if dev_type not in dev_ranges:
# log.error("dev_type error")
return None
if cur is None or isinstance(cur, str):
return None
for _score, ranges in dev_ranges[dev_type]:
for range in ranges:
if range[0] == -1 and cur <= range[1]:
return _score if isinstance(_score, int) else None
if range[1] == 1 and cur >= range[0]:
return _score if isinstance(_score, int) else _score[0]
if cur >= range[0] and cur <= range[1]:
return _score if isinstance(_score, int) else _score[0]
# log.error("data error")
return None
def get_dev_score(dev_type, cur):
""""获取评分"""
if dev_type not in dev_ranges:
# log.error("dev_type error")
return None
for _score, ranges in dev_ranges[dev_type]:
for range in ranges:
if cur >= range[0] and cur <= range[1]:
if isinstance(_score, int):
return _score
if range[1] <= 0:
if cur >= range[0] and cur <= range[1]:
if isinstance(_score, int):
return _score
ratio = (cur - range[0]) / (range[1] - range[0])
if dev_type in ["v", "freq", "ubl", "thdu", "lf"]:
# 负数区间,这个值成正比关系
return _score[0] + (_score[1] - _score[0]) * ratio
else:
return _score[1] - (_score[1] - _score[0]) * ratio
if range[0] >= 0:
if cur >= range[0] and cur <= range[1]:
if isinstance(_score, int):
return _score
ratio = (cur - range[0]) / (range[1] - range[0])
if dev_type in ["v", "freq", "ubl", "thdu", "lf"]:
# 正数区间,这个值成反比关系
return _score[1] - (_score[1] - _score[0]) * ratio
else:
return _score[0] + (_score[1] - _score[0]) * ratio
return None
if __name__ == '__main__':
res = get_dev_grade(dev_type="v", cur=0.01)
print(res)
res = get_dev_score(dev_type="thdu", cur=0.015)
print(res)
assert get_dev_score(dev_type="v", cur=0.01) == 96
assert get_dev_score(dev_type="v", cur=-0.01) == 96
assert get_dev_score(dev_type="v", cur=0.06) == 75
assert get_dev_score(dev_type="v", cur=-0.06) == 75
assert get_dev_score(dev_type="v", cur=0.08) == 70-10*(0.08-0.07)/(0.1-0.07)
assert get_dev_score(dev_type="v", cur=-0.09) == 60 + 10*(0.01/0.03)
assert get_dev_score(dev_type="v", cur=0.15) == 60 - 60 * ((0.15-0.1) / (0.2-0.1))
assert get_dev_score(dev_type="v", cur=-0.15) == 0 + 60 * ((-0.15 + 0.2) / (0.2 - 0.1))
assert get_dev_score(dev_type="v", cur=0.3) == 0
assert get_dev_score(dev_type="v", cur=0.8) == 0
assert get_dev_score(dev_type="v", cur=-0.2) == 0
# 测试功率因数
assert get_dev_score(dev_type="costtl", cur=0.98) == 90 + 10 * ((0.98-0.95)/(1-0.95))
assert get_dev_score(dev_type="costtl", cur=-0.98) == 100 - 10 * ((-0.98+1)/(-0.95+1))
assert get_dev_score(dev_type="costtl", cur=0.4) == 0
assert get_dev_score(dev_type="costtl", cur=-0.4) == 0