1 Star 0 Fork 0

李贞/OmniAnomaly

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
data_preprocess.py 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
smallcowbaby 提交于 2019-02-02 20:03 . init
import ast
import csv
import os
import sys
from pickle import dump
import numpy as np
from tfsnippet.utils import makedirs
output_folder = 'processed'
makedirs(output_folder, exist_ok=True)
def load_and_save(category, filename, dataset, dataset_folder):
temp = np.genfromtxt(os.path.join(dataset_folder, category, filename),
dtype=np.float32,
delimiter=',')
print(dataset, category, filename, temp.shape)
with open(os.path.join(output_folder, dataset + "_" + category + ".pkl"), "wb") as file:
dump(temp, file)
def load_data(dataset):
if dataset == 'SMD':
dataset_folder = 'ServerMachineDataset'
file_list = os.listdir(os.path.join(dataset_folder, "train"))
for filename in file_list:
if filename.endswith('.txt'):
load_and_save('train', filename, filename.strip('.txt'), dataset_folder)
load_and_save('test', filename, filename.strip('.txt'), dataset_folder)
load_and_save('test_label', filename, filename.strip('.txt'), dataset_folder)
elif dataset == 'SMAP' or dataset == 'MSL':
dataset_folder = 'data'
with open(os.path.join(dataset_folder, 'labeled_anomalies.csv'), 'r') as file:
csv_reader = csv.reader(file, delimiter=',')
res = [row for row in csv_reader][1:]
res = sorted(res, key=lambda k: k[0])
label_folder = os.path.join(dataset_folder, 'test_label')
makedirs(label_folder, exist_ok=True)
data_info = [row for row in res if row[1] == dataset and row[0] != 'P-2']
labels = []
for row in data_info:
anomalies = ast.literal_eval(row[2])
length = int(row[-1])
label = np.zeros([length], dtype=np.bool)
for anomaly in anomalies:
label[anomaly[0]:anomaly[1] + 1] = True
labels.extend(label)
labels = np.asarray(labels)
print(dataset, 'test_label', labels.shape)
with open(os.path.join(output_folder, dataset + "_" + 'test_label' + ".pkl"), "wb") as file:
dump(labels, file)
def concatenate_and_save(category):
data = []
for row in data_info:
filename = row[0]
temp = np.load(os.path.join(dataset_folder, category, filename + '.npy'))
data.extend(temp)
data = np.asarray(data)
print(dataset, category, data.shape)
with open(os.path.join(output_folder, dataset + "_" + category + ".pkl"), "wb") as file:
dump(data, file)
for c in ['train', 'test']:
concatenate_and_save(c)
if __name__ == '__main__':
datasets = ['SMD', 'SMAP', 'MSL']
commands = sys.argv[1:]
load = []
if len(commands) > 0:
for d in commands:
if d in datasets:
load_data(d)
else:
print("""
Usage: python data_preprocess.py <datasets>
where <datasets> should be one of ['SMD', 'SMAP', 'MSL']
""")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lizhen_hbu/OmniAnomaly.git
git@gitee.com:lizhen_hbu/OmniAnomaly.git
lizhen_hbu
OmniAnomaly
OmniAnomaly
master

搜索帮助