-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathalgorithm.py
82 lines (61 loc) · 2.36 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
#!/usr/bin/env python3
import json
import sys
import argparse
import numpy as np
from dataclasses import dataclass
from typing import Union
from ptsa.models.SSA import SSA
@dataclass
class CustomParameters:
ep: int = 3
window_size: int = 720
rf_method: str = 'alpha'
alpha: Union[float, int, np.ndarray] = 0.2
random_state: int = 42
class AlgorithmArgs(argparse.Namespace):
@staticmethod
def from_sys_args() -> 'AlgorithmArgs':
if len(sys.argv) != 2:
raise ValueError("Wrong number of arguments specified! Single JSON-string pos. argument expected.")
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 set_random_state(config: AlgorithmArgs) -> None:
seed = config.customParameters.random_state
import random
random.seed(seed)
np.random.seed(seed)
def main():
config = AlgorithmArgs.from_sys_args()
set_random_state(config)
ts_filename = config.dataInput # "/data/dataset.csv"
score_filename = config.dataOutput # "/results/anomaly_window_scores.ts"
print(f"Configuration: {config}")
if config.executionType == "train":
print("No training required!")
exit(0)
if config.executionType != "execute":
raise ValueError("Unknown executionType specified!")
# read only single column from dataset
print(f"Reading data from {ts_filename}")
da = np.genfromtxt(ts_filename, skip_header=1, delimiter=",")
data = da[:, 1]
# run SSA
print("Executing SSA ...")
model = SSA(a=config.customParameters.alpha,
ep=config.customParameters.ep,
n=config.customParameters.window_size,
rf_method=config.customParameters.rf_method)
model.fit(data)
# get outlier scores
scores = model.decision_scores_
scores = np.roll(scores, -config.customParameters.window_size)
print(f"Input size: {len(data)}\nOutput size: {len(scores)}")
print("SSA result:", scores)
print(f"Writing results to {score_filename}")
np.savetxt(score_filename, scores, delimiter=",", fmt='%f')
if __name__ == "__main__":
main()