File size: 2,354 Bytes
1de88a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import math
import numpy as np
from typing import TypeVar, Generic

_T = TypeVar('_T')

class Window(Generic[_T]):
  def __init__(self, window_length:int, window:list[_T]=None):
    self.window_length = window_length
    self.window:list[_T] = [] if window is None else window
  
  def push(self, data:_T):
    self.window.append(data)
    if len(self.window) > self.window_length:
      self.window.pop(0)

  def clear(self):
    self.window.clear()

  def first(self) -> _T:
    return self.window[0]

  def last(self) -> _T:
    return self.window[-1]

  def get(self, index:int) -> _T:
    return self.window[index]

  def head(self, length:int) -> Window[_T]:
    return Window[_T](self.window_length, self.window[:length])

  def tail(self, length:int) -> Window[_T]:
    return Window[_T](self.window_length, self.window[-length:])

  def capacity(self):
    return len(self.window)

  def empty(self):
    return len(self.window) == 0

  def full(self):
    return len(self.window) == self.window_length

  def sum(self, func:function=lambda x:x):
    return sum(map(func, self.window))

  def count(self, func:function=lambda x:x):
    return len(list(filter(lambda x:x == True, map(func, self.window))))

  def map(self, func:function=lambda x:x) -> Window:
    return Window(self.window_length, list(map(func, self.window)))

  def argmax(self) -> tuple[int, _T]:
    if self.capacity() == 0:
      return 0
    index = 0
    value = self.window[0]
    for i, v in enumerate(self.window):
      if v > value:
        value = v
        index = i
    return index, value

  def to_numpy(self):
    return np.concatenate(self.window, axis=0)
  
  def to_numpy_inside(self):
    return np.array([x.to_numpy() for x in self.window])
  
  def feature(self) -> list[float]:
    x = np.array(self.window)
    std = np.std(x)
    min = np.min(x)
    max = np.max(x)
    mean = np.mean(x)
    sc = np.mean((x - mean) ** 3) / pow(std, 3)
    ku = np.mean((x - mean) ** 4) / pow(std, 4)
    if math.isnan(ku):
      sc = 0
      ku = 0
    return [mean, min, max, sc, ku]
  
  def set_to_last_value(self):
    for i in range(self.window_length - 1):
      if hasattr(self.window[i], "assigned_by"):
        self.window[i].assigned_by(self.window[-1])
      else:
        self.window[i] = self.window[-1]
    return self