<jupyter_start><jupyter_text>随机找出25种根因<jupyter_code>total_attrs = find_total_attrs('../data/result/')
root_causes = find_root_causes(total_attrs)
np.save('../data/root_causes.npy', root_causes)

for root_cause in root_causes:
	print(root_cause)<jupyter_output>[['l2']]
[['l2'], ['e01']]
[['l2'], ['e01'], ['e06']]
[['l2'], ['e01'], ['e06'], ['e07']]
[['l2'], ['e01'], ['e06'], ['e07'], ['l3']]
[['l4', 'c8']]
[['l4', 'c8'], ['p26', 'c1']]
[['l4', 'c8'], ['p26', 'c1'], ['c8', 'l2']]
[['l4', 'c8'], ['p26', 'c1'], ['c8', 'l2'], ['p12', 'i147']]
[['l4', 'c8'], ['p26', 'c1'], ['c8', 'l2'], ['p12', 'i147'], ['i61', 'l3']]
[['i123', 'l1', 'c6']]
[['i123', 'l1', 'c6'], ['p02', 'l2', 'e02']]
[['i123', 'l1', 'c6'], ['p02', 'l2', 'e02'], ['p14', 'c6', 'i106']]
[['i123', 'l1', 'c6'], ['p02', 'l2', 'e02'], ['p14', 'c6', 'i106'], ['c6', 'p20', 'i141']]
[['i123', 'l1', 'c6'], ['p02', 'l2', 'e02'], ['p14', 'c6', 'i106'], ['c6', 'p20', 'i141'], ['i118', 'c1', 'l2']]
[['p32', 'l2', 'e12', 'c6']]
[['p32', 'l2', 'e12', 'c6'], ['e01', 'p08', 'c1', 'l4']]
[['p32', 'l2', 'e12', 'c6'], ['e01', 'p08', 'c1', 'l4'], ['c7', 'e09', 'p32', 'l3']]
[['p32', 'l2', 'e12', 'c6'], ['e01', 'p08', 'c1', 'l4'], ['c7', 'e09', 'p32', 'l3'], ['c4', 'l4', 'p29', 'e05']]
[['p32', 'l2', '[...]<jupyter_text>随机找出25个timestamp注入异常<jupyter_code># 计算异常
def compute_anomaly(path):
    o = open(path, 'rb')
    df = pd.read_csv(o)
    length = len(df)

    # 随机选取一个时间戳(为方便计算不选头尾处时间戳)
    rand_idx = random.randint(150, length - 150)
    timestamp = df.loc[rand_idx][0]						# 随机选择的timestamp
    temp_df = df[rand_idx - 144 : rand_idx + 144]		# 时间间隔为5分钟，共14天数据，选取一天的数据
    kpi = temp_df['KPI'].as_matrix()
    kpi = [float(i) for i in kpi]
    # print(kpi)
    mean = np.mean(kpi)
    std = np.std(kpi)
    total_anomaly = int(mean + 5*std)					# 此timestamp上的总KPI异常
    # print(total_anomaly)
    return timestamp, total_anomaly


# 分发异常
def distribute_anomaly(root_cause, path):
    timestamp, total_anomaly = compute_anomaly(path)
    print(root_cause, timestamp)
    path2 = '../data/origin/' + str(timestamp) + '.csv'
    path3 = '../data/anomaly/' + str(timestamp) + '.csv'
    locs = []           # 后代叶子元素的索引
    o = open(path2, 'rb')
    df = pd.read_csv(o, names  = ['i', 'e', 'c', 'p', 'l', 'kpi'])
    for x in root_cause:
        for idx in df.index:
            i, e, c, p, l = df.loc[idx]['i'], df.loc[idx]['e'], df.loc[idx]['c'], df.loc[idx]['p'], df.loc[idx]['l']
            if i in x or e in x or c in x or p in x or l in x:
                locs.append(idx)

    # 若此时间戳不含此属性值，则重新找一个时间戳
    if len(locs) == 0:
        distribute_anomaly(root_cause, path)
        return

    print(locs)
    anomaly = int(total_anomaly / len(locs))
    df_out = pd.DataFrame(columns = ['i', 'e', 'c', 'p', 'l', 'kpi'])
    for idx in locs:
        i, e, c, p, l = df.loc[idx]['i'], df.loc[idx]['e'], df.loc[idx]['c'], df.loc[idx]['p'], df.loc[idx]['l']
#                 print(i, e, c, p, l)
        df_out.loc[0] = {'i':i, 'e':e, 'c':c, 'p':p, 'l':l, 'kpi':df.loc[idx]['kpi'] + anomaly}
        df = pd.concat([df[:idx], df_out, df[idx+1:]], ignore_index=True)
    csvfile = open(path3, 'w')
    df.to_csv(path3, header=None, index=None)
# 使用25种根因随机选择timestamp注入异常
path = '../data/KPI_new.csv'
for root_cause in root_causes:
    distribute_anomaly(root_cause, path)<jupyter_output>[['l2']] 1536895800000
[2, 8, 11, 15, 23, 25, 31, 38, 59, 95, 101, 119, 120, 121, 153, 182, 184, 185, 186, 188, 195, 242, 258, 259, 270, 271, 273, 278, 296, 297, 302, 324, 336, 342, 349, 355, 358, 387, 389, 410, 415, 421, 426, 432, 434, 436, 445, 452, 475, 476, 509, 511, 512, 521, 522, 527, 532, 541, 542, 545, 553, 571, 579, 582, 591, 592, 594, 597, 608, 624, 626, 642, 645, 646, 661, 666, 667, 676, 681, 716, 733, 747, 748, 750, 751, 752, 753, 754, 756, 761, 762, 769, 793, 797, 800, 805, 817, 827, 835, 883, 887, 901, 910, 912, 914, 915, 919, 922, 923, 929, 954, 960, 964, 970, 971, 972, 979, 991, 1003, 1049, 1055, 1056, 1062, 1064, 1068, 1069, 1081, 1088, 1092, 1093, 1094, 1095, 1108, 1115, 1116, 1126, 1127, 1150, 1162, 1164, 1169, 1171, 1179, 1180, 1184, 1187, 1197, 1206, 1232, 1242, 1252, 1259, 1274, 1281, 1288, 1295, 1296, 1297, 1298, 1304, 1317, 1325, 1341, 1345, 1348, 1362, 1366, 1387, 1393, 1399, 1409, 1411, 1415, 1431, 1435, 1440, 1468, 1472, 1473, 1474, 1480, 1488, 1502, 1503, 15[...]
{'fraction_non_alphanumeric': 0.2638473914042749, 'fraction_numerical': 0.19788554355320617, 'mean_word_length': 3.923076923076923, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '574241', 'license': 'Apache-2.0', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2143, 'n_words': 510, 'path': 'anomaly_injection/code/anomaly injection.ipynb', 'repo_name': 'FudanYuan/faultLocalization', 'size': '4351'}