-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathalgorithm.py
93 lines (78 loc) · 2.89 KB
/
algorithm.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
import pandas as pd
import numpy as np
import json
import sys
from dataclasses import dataclass
import argparse
from srcnn.generate_data import generate_data
from srcnn.train import train as train_srcnn
from srcnn.evalue import evaluate
@dataclass
class CustomParameters:
window_size: int = 128
step: int = 64
random_state: int = 42
num: int = 10
learning_rate: float = 1e-6
epochs: int = 1
batch_size: int = 256
n_jobs: int = 1
split: float = 0.8
early_stopping_delta: float = 0.05
early_stopping_patience: int = 10
class AlgorithmArgs(argparse.Namespace):
@property
def df(self) -> pd.DataFrame:
df = pd.read_csv(self.dataInput)
df = df.iloc[:, [0, 1, df.shape[1]-1]]
df.columns = ["timestamp", "value", "is_anomaly"]
return df
@staticmethod
def from_sys_args() -> 'AlgorithmArgs':
args: dict = json.loads(sys.argv[1])
custom_parameter_keys = dir(CustomParameters())
filtered_parameters = dict(
filter(lambda x: x[0] in custom_parameter_keys, args.get("customParameters", {}).items()))
args["customParameters"] = CustomParameters(**filtered_parameters)
return AlgorithmArgs(**args)
def train(args: AlgorithmArgs):
data_path = generate_data(args.dataInput,
args.customParameters.window_size,
args.customParameters.step,
args.customParameters.random_state,
args.customParameters.num)
train_srcnn(data_path,
args.customParameters.window_size,
args.customParameters.learning_rate,
args.customParameters.step,
args.customParameters.random_state,
False,
args.modelOutput,
args.customParameters.epochs,
args.customParameters.batch_size,
args.customParameters.n_jobs,
"sr_cnn",
args.customParameters.split,
args.customParameters.early_stopping_delta,
args.customParameters.early_stopping_patience)
def execute(args: AlgorithmArgs):
scores = evaluate(args.modelInput, args.df, args.customParameters.window_size)
scores = np.array(scores[0][1])
scores.tofile(args.dataOutput, sep="\n")
def set_random_state(config: AlgorithmArgs) -> None:
seed = config.customParameters.random_state
import random
import numpy as np
import torch
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if __name__ == "__main__":
args = AlgorithmArgs.from_sys_args()
set_random_state(args)
if args.executionType == "train":
train(args)
elif args.executionType == "execute":
execute(args)
else:
raise ValueError(f"No executionType '{args.executionType}' available! Choose either 'train' or 'execute'.")