Upload core/window.py with huggingface_hub
Browse files- core/window.py +91 -0
core/window.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
import numpy as np
|
| 5 |
+
from typing import TypeVar, Generic
|
| 6 |
+
|
| 7 |
+
_T = TypeVar('_T')
|
| 8 |
+
|
| 9 |
+
class Window(Generic[_T]):
|
| 10 |
+
def __init__(self, window_length:int, window:list[_T]=None):
|
| 11 |
+
self.window_length = window_length
|
| 12 |
+
self.window:list[_T] = [] if window is None else window
|
| 13 |
+
|
| 14 |
+
def push(self, data:_T):
|
| 15 |
+
self.window.append(data)
|
| 16 |
+
if len(self.window) > self.window_length:
|
| 17 |
+
self.window.pop(0)
|
| 18 |
+
|
| 19 |
+
def clear(self):
|
| 20 |
+
self.window.clear()
|
| 21 |
+
|
| 22 |
+
def first(self) -> _T:
|
| 23 |
+
return self.window[0]
|
| 24 |
+
|
| 25 |
+
def last(self) -> _T:
|
| 26 |
+
return self.window[-1]
|
| 27 |
+
|
| 28 |
+
def get(self, index:int) -> _T:
|
| 29 |
+
return self.window[index]
|
| 30 |
+
|
| 31 |
+
def head(self, length:int) -> Window[_T]:
|
| 32 |
+
return Window[_T](self.window_length, self.window[:length])
|
| 33 |
+
|
| 34 |
+
def tail(self, length:int) -> Window[_T]:
|
| 35 |
+
return Window[_T](self.window_length, self.window[-length:])
|
| 36 |
+
|
| 37 |
+
def capacity(self):
|
| 38 |
+
return len(self.window)
|
| 39 |
+
|
| 40 |
+
def empty(self):
|
| 41 |
+
return len(self.window) == 0
|
| 42 |
+
|
| 43 |
+
def full(self):
|
| 44 |
+
return len(self.window) == self.window_length
|
| 45 |
+
|
| 46 |
+
def sum(self, func:function=lambda x:x):
|
| 47 |
+
return sum(map(func, self.window))
|
| 48 |
+
|
| 49 |
+
def count(self, func:function=lambda x:x):
|
| 50 |
+
return len(list(filter(lambda x:x == True, map(func, self.window))))
|
| 51 |
+
|
| 52 |
+
def map(self, func:function=lambda x:x) -> Window:
|
| 53 |
+
return Window(self.window_length, list(map(func, self.window)))
|
| 54 |
+
|
| 55 |
+
def argmax(self) -> tuple[int, _T]:
|
| 56 |
+
if self.capacity() == 0:
|
| 57 |
+
return 0
|
| 58 |
+
index = 0
|
| 59 |
+
value = self.window[0]
|
| 60 |
+
for i, v in enumerate(self.window):
|
| 61 |
+
if v > value:
|
| 62 |
+
value = v
|
| 63 |
+
index = i
|
| 64 |
+
return index, value
|
| 65 |
+
|
| 66 |
+
def to_numpy(self):
|
| 67 |
+
return np.concatenate(self.window, axis=0)
|
| 68 |
+
|
| 69 |
+
def to_numpy_inside(self):
|
| 70 |
+
return np.array([x.to_numpy() for x in self.window])
|
| 71 |
+
|
| 72 |
+
def feature(self) -> list[float]:
|
| 73 |
+
x = np.array(self.window)
|
| 74 |
+
std = np.std(x)
|
| 75 |
+
min = np.min(x)
|
| 76 |
+
max = np.max(x)
|
| 77 |
+
mean = np.mean(x)
|
| 78 |
+
sc = np.mean((x - mean) ** 3) / pow(std, 3)
|
| 79 |
+
ku = np.mean((x - mean) ** 4) / pow(std, 4)
|
| 80 |
+
if math.isnan(ku):
|
| 81 |
+
sc = 0
|
| 82 |
+
ku = 0
|
| 83 |
+
return [mean, min, max, sc, ku]
|
| 84 |
+
|
| 85 |
+
def set_to_last_value(self):
|
| 86 |
+
for i in range(self.window_length - 1):
|
| 87 |
+
if hasattr(self.window[i], "assigned_by"):
|
| 88 |
+
self.window[i].assigned_by(self.window[-1])
|
| 89 |
+
else:
|
| 90 |
+
self.window[i] = self.window[-1]
|
| 91 |
+
return self
|