Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_one_unit.py 6.8 KB

You have to be logged in to leave a comment. Sign In
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  1. # Created by: leo
  2. # Created on: 2018.10.26
  3. from unittest import TestCase, defaultTestLoader, TextTestRunner
  4. from datetime import datetime, timedelta
  5. from sklearn.cluster import KMeans
  6. import pandas as pd
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. from max_current import (coef_maxcur, string_coef_maxcur, max_current_monthly,
  10. calc_maxcur_coefs, train_thresholds, classifier)
  11. from coef_matrix import (train_median_thresholds, string_coef_matrix,
  12. calc_matrix_coefs, coef_median)
  13. import logging
  14. logging.basicConfig(level=logging.DEBUG)
  15. class ForecastTest(TestCase):
  16. def setUp(self):
  17. self.raw = pd.read_csv('ycz6502.csv', usecols=[0, 4, 6],
  18. index_col=0).dropna()
  19. self.raw.index = pd.to_datetime(self.raw.index)
  20. self.daily = self.raw.groupby(pd.Grouper(freq='D'))
  21. def test_zero_timeseries(self):
  22. pass
  23. class AnomalyDetectionTest(TestCase):
  24. def setUp(self):
  25. self.raw = pd.read_csv('ycz6502.csv', usecols=[0, 4, 6],
  26. index_col=0).dropna()
  27. self.raw.index = pd.to_datetime(self.raw.index)
  28. self.daily = self.raw.groupby(pd.Grouper(freq='D'))
  29. def test_coef_of_day(self):
  30. daydata = self.daily.get_group('2017-04-08')
  31. logging.info(coef_maxcur(daydata))
  32. # logging.info(coef_median(daydata))
  33. def test_coef_of_days(self):
  34. wins = ['2017-05-28', '2017-05-29', '2017-06-03', '2017-06-04']
  35. for win, coefs in zip(wins, map(lambda x: coef_maxcur(self.daily.get_group(x)), wins)):
  36. logging.info('%s\n%s' % (win, coefs))
  37. def test_str_coef_some_days(self):
  38. wins = ['2017-05-24', '2017-05-25', '2017-05-26', '2017-05-27',
  39. '2017-05-28', '2017-05-29', '2017-05-30', '2017-05-31',
  40. '2017-06-01', '2017-06-02', '2017-06-03', '2017-06-04',
  41. '2017-06-06', '2017-06-07']
  42. cluster_id = 1
  43. cluster_status = {}
  44. for item in zip(wins, map(lambda x: coef_maxcur(self.daily.get_group(x)), wins)):
  45. cluster_status[item[0]] = item[1][cluster_id]
  46. lists = sorted(cluster_status.items())
  47. x, y = zip(*lists)
  48. plt.scatter(x, y)
  49. plt.xticks(rotation=70)
  50. plt.title('Coefficients of String in Specific Days')
  51. plt.show()
  52. def test_maxcur_coef_all_days(self):
  53. string_id = 1
  54. coefs = self.daily.apply(string_coef_maxcur, string_id)
  55. plt.scatter(coefs.index, coefs, c='g', alpha=0.5)
  56. plt.xlabel('Date')
  57. plt.ylabel('String Coefficients')
  58. plt.title('Coefficients of String %d in All Days(Max Current)' % string_id)
  59. plt.grid()
  60. plt.show()
  61. def test_matrix_coef_all_days(self):
  62. string_id = 1
  63. coefs = self.daily.apply(string_coef_matrix, string_id)
  64. plt.scatter(coefs.index, coefs, c='g', alpha=0.5)
  65. plt.xlabel('Date')
  66. plt.ylabel('String Coefficients')
  67. plt.title('Coefficients of String %d in All Days (Matrix Median)' % string_id)
  68. plt.grid()
  69. plt.show()
  70. def test_daily_plot(self):
  71. cur528 = self.raw['2017-05-28']
  72. cur528_c1 = cur528[cur528['zuchuan'] == 1]['value']
  73. start = datetime(2017, 5, 28, 6, 0)
  74. end = datetime(2017, 5, 28, 19, 45)
  75. step = timedelta(minutes = 15)
  76. plt.plot(cur528_c1.index, cur528_c1)
  77. plt.title('Coeffients of String in a Time Span')
  78. plt.show()
  79. def test_groups(self):
  80. self.daily.aggregate(coef_maxcur)
  81. self.daily.aggregate(np.sum)
  82. for name, grp in self.daily:
  83. logging.info('%s\n%s' % (name, coef_maxcur(grp)))
  84. class TrainerTest(TestCase):
  85. def setUp(self):
  86. self.raw = pd.read_csv('ycz6502.csv', usecols=[0, 4, 6],
  87. index_col=0).dropna()
  88. def test_monthly_max(self):
  89. res = max_current_monthly(self.raw)
  90. self.assertAlmostEqual(res['2017-03'][0], 9.75)
  91. def test_trainer_03(self):
  92. logging.info('Thresholds between statuses:\n%s' % train_thresholds(self.raw, 0.3))
  93. def test_trainer_08(self):
  94. logging.info('Thresholds between statuses:\n%s' % train_thresholds(self.raw, 0.8))
  95. def test_trainer_095(self):
  96. logging.info('Thresholds between statuses:\n%s' % train_thresholds(self.raw, 0.95))
  97. def test_matrix_trainer(self):
  98. logging.info('Thresholds between statuses:\n%s' % train_median_thresholds(self.raw))
  99. def test_maxcur_all_coefs_plot(self):
  100. coefs = calc_maxcur_coefs(self.raw, 0.8)
  101. for col in coefs.columns:
  102. plt.scatter(coefs.index, coefs[col], alpha=0.95, s=10, marker='x')
  103. plt.xlabel('Date')
  104. plt.ylabel('Coefficients')
  105. plt.title('Coefficients of all strings with max-current method')
  106. plt.grid()
  107. plt.show()
  108. def test_matrix_all_coefs_plot(self):
  109. coefs = calc_matrix_coefs(self.raw)
  110. for col in coefs.columns:
  111. plt.scatter(coefs.index, coefs[col], alpha=0.95, s=10, marker='x')
  112. plt.xlabel('Date')
  113. plt.ylabel('Coefficients')
  114. plt.title('Coefficients of all strings with matrix-median method')
  115. plt.grid()
  116. plt.show()
  117. class ClassifierTest(TestCase):
  118. def setUp(self):
  119. self.raw = pd.read_csv('ycz6502.csv', usecols=[0, 4, 6],
  120. index_col=0).dropna()
  121. self.raw.index = pd.to_datetime(self.raw.index)
  122. self.daily = self.raw.groupby(pd.Grouper(freq='D'))
  123. def test_low_current(self):
  124. data0323 = self.daily.get_group('2017-03-24')
  125. logging.info(classifier(data0323, 8))
  126. def test_normal_current(self):
  127. data0605 = self.daily.get_group('2017-06-05')
  128. logging.info(classifier(data0605, 4))
  129. def test_warn_status(self):
  130. data0528 = self.daily.get_group('2017-05-28')
  131. logging.info(classifier(data0528, 8))
  132. def test_watch_status(self):
  133. data0604 = self.daily.get_group('2017-06-04')
  134. logging.info(classifier(data0604, 8))
  135. class UtilTest(TestCase):
  136. def test_combine_sets(self):
  137. inp = {1: {3, 2, 5}, 2: {5, 8, 7}, 3: {23, 34, 2}}
  138. res = set().union(*inp.values())
  139. self.assertEqual(res, {2, 7, 3, 34, 5, 23, 8})
  140. def test_kmeans(self):
  141. inp = {1, 0.9, 0.92, 0.89, 0.4, 0.2, 0.6, -0.2, 0.84, -0.05}
  142. data = np.array(list(inp)).reshape(-1, 1)
  143. kmeans = KMeans(n_clusters=3, random_state=1) # the random_state make the group ID in clustering result fixed
  144. kmeans.fit(data)
  145. res = kmeans.predict(data)
  146. np.testing.assert_array_equal(res, [1, 1, 1, 1, 2, 2, 2, 0, 1, 0])
  147. if __name__ == '__main__':
  148. # tests = defaultTestLoader.loadTestsFromTestCase(AnomalyDetectionTest)
  149. # logging.info(tests.countTestCases())
  150. test_trainer = defaultTestLoader.loadTestsFromName(
  151. 'test_app.TrainerTest.test_trainer_08')
  152. TextTestRunner(verbosity=1).run(test_trainer)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...