SPATZ migration + proper directory structure

This commit is contained in:
dario
2023-12-10 14:11:54 +01:00
parent f819b24bfa
commit c60629b4c9
24 changed files with 938 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
from failures import *
from noise import *
from transform import *

View File

@@ -0,0 +1,29 @@
import numpy as np
from numpy.typing import ArrayLike
from typing import Any, Tuple
from spatz.transforms import Transform
class Downtime(Transform):
def __init__(self, mu_duration: float, sigma_duration: float) -> None:
super().__init__()
self.__mu = mu_duration
self.__sigma = sigma_duration
self.__state = 1
self.__until = abs(np.random.normal(mu_duration, sigma_duration))
def get_state(self) -> Tuple[int, float]:
return self.__state, self.__until
def __call__(self, t: float, x: ArrayLike) -> Any:
if t >= self.__until:
self.__state = 1 - self.__state
self.__until = t + abs(np.random.normal(self.__mu, self.__sigma))
if self.__state == 1:
return x
return np.zeros_like(x)

34
spatz/transforms/noise.py Normal file
View File

@@ -0,0 +1,34 @@
import numpy as np
from numpy.typing import ArrayLike
from typing import Any, Tuple
from spatz.transforms import Transform
class GaussianNoise(Transform):
def __init__(self, mu: ArrayLike, sigma: ArrayLike) -> None:
super().__init__()
self.__mu = mu
self.__sigma = sigma
def __call__(self, t: float, x: ArrayLike) -> ArrayLike:
assert np.shape(self.__mu) == np.shape(x), "Mu and x have to match in shape."
if np.isscalar(x):
noise = np.random.normal(0, 1)
x += self.__sigma * noise + self.__mu
else:
noise = np.random.normal(0, 1, np.shape(x))
x += self.__sigma @ noise + self.__mu
return x
class PinkNoise(Transform):
def __init__(self) -> None:
super().__init__()
def __call__(self, t: float, x: ArrayLike) -> Any:
pass

View File

@@ -0,0 +1,12 @@
import numpy as np
from numpy.typing import ArrayLike
from typing import Any, Tuple
class Transform:
def apply(self, t: float, x: np.array):
y = self(t, x)
assert x.shape == y.shape, "Transform has to maintain the array's shape."
return y