-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: string representation base classes
- Loading branch information
Showing
2 changed files
with
45 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from popmon.base import Module | ||
|
||
|
||
def test_popmon_module(): | ||
class Scaler(Module): | ||
_input_keys = ("input_key",) | ||
_output_keys = ("output_key",) | ||
class Scaler(Module): | ||
_input_keys = ("input_key",) | ||
_output_keys = ("output_key",) | ||
|
||
def __init__(self, input_key, output_key, mean, std): | ||
super().__init__() | ||
self.input_key = input_key | ||
self.output_key = output_key | ||
self.mean = mean | ||
self.std = std | ||
def __init__(self, input_key, output_key, mean, std): | ||
super().__init__() | ||
self.input_key = input_key | ||
self.output_key = output_key | ||
self.mean = mean | ||
self.std = std | ||
|
||
def transform(self, input_array: np.ndarray): | ||
res = input_array - np.mean(input_array) | ||
res = res / np.std(res) | ||
res = res * self.std | ||
res = res + self.mean | ||
return res | ||
def transform(self, input_array: np.ndarray): | ||
res = input_array - np.mean(input_array) | ||
res = res / np.std(res) | ||
res = res * self.std | ||
res = res + self.mean | ||
return res | ||
|
||
test_module = Scaler(input_key="x", output_key="scaled_x", mean=2.0, std=0.3) | ||
|
||
@pytest.fixture | ||
def test_module(): | ||
return Scaler(input_key="x", output_key="scaled_x", mean=2.0, std=0.3) | ||
|
||
|
||
def test_popmon_module(test_module): | ||
datastore = {"x": np.arange(10)} | ||
datastore = test_module.transform(datastore) | ||
|
||
assert "x" in datastore # check if key 'x' is still in the datastore | ||
np.testing.assert_almost_equal(np.mean(datastore["scaled_x"]), 2.0, decimal=5) | ||
np.testing.assert_almost_equal(np.std(datastore["scaled_x"]), 0.3, decimal=5) | ||
|
||
|
||
def test_popmon_module_repr(test_module): | ||
assert str(test_module) == "Scaler(input_key='x', output_key='scaled_x')" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters