mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/SPATZ.git
synced 2025-09-28 21:17:33 +00:00
SPATZ migration + proper directory structure
This commit is contained in:
3
spatz/transforms/__init__.py
Normal file
3
spatz/transforms/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from failures import *
|
||||
from noise import *
|
||||
from transform import *
|
29
spatz/transforms/failures.py
Normal file
29
spatz/transforms/failures.py
Normal 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
34
spatz/transforms/noise.py
Normal 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
|
12
spatz/transforms/transform.py
Normal file
12
spatz/transforms/transform.py
Normal 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
|
Reference in New Issue
Block a user