Skip to content

Commit

Permalink
fix: C contiguous error for exp mov avg tx
Browse files Browse the repository at this point in the history
Signed-off-by: Avik Basu <[email protected]>
  • Loading branch information
ab93 committed Apr 22, 2024
1 parent 88ccf66 commit 5593be3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
10 changes: 7 additions & 3 deletions numalogic/config/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PreprocessFactory(_ObjectFactory):
DifferenceTransform,
FlattenVector,
PercentileScaler,
ExpMovingAverage
ExpMovingAverage,
)

_CLS_MAP: ClassVar[dict] = {
Expand All @@ -68,7 +68,7 @@ class PreprocessFactory(_ObjectFactory):
"DifferenceTransform": DifferenceTransform,
"FlattenVector": FlattenVector,
"PercentileScaler": PercentileScaler,
"ExpMovingAverage": ExpMovingAverage
"ExpMovingAverage": ExpMovingAverage,
}

def get_pipeline_instance(self, objs_info: list[ModelInfo]):
Expand All @@ -88,7 +88,11 @@ class PostprocessFactory(_ObjectFactory):

from numalogic.transforms import TanhNorm, ExpMovingAverage, SigmoidNorm

_CLS_MAP: ClassVar[dict] = {"TanhNorm": TanhNorm, "ExpMovingAverage": ExpMovingAverage, "SigmoidNorm": SigmoidNorm}
_CLS_MAP: ClassVar[dict] = {
"TanhNorm": TanhNorm,
"ExpMovingAverage": ExpMovingAverage,
"SigmoidNorm": SigmoidNorm,
}


class ThresholdFactory(_ObjectFactory):
Expand Down
2 changes: 1 addition & 1 deletion numalogic/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
"DifferenceTransform",
"FlattenVector",
"PercentileScaler",
"SigmoidNorm"
"SigmoidNorm",
]
5 changes: 3 additions & 2 deletions numalogic/transforms/_movavg.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ def transform(self, input_: npt.NDArray[float], **__) -> npt.NDArray[float]:
------
InvalidDataShapeError: if input array is not single featured
"""
x_df = pd.DataFrame(input_)
return x_df.ewm(alpha=self.alpha).mean().to_numpy(dtype=np.float32)
x_df = pd.DataFrame(input_, dtype=np.float32, copy=True)
x_smoothed = x_df.ewm(alpha=self.alpha).mean().to_numpy(dtype=np.float32)
return np.ascontiguousarray(x_smoothed, dtype=np.float32)
4 changes: 2 additions & 2 deletions numalogic/transforms/_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def transform(self, input_: npt.NDArray[float], **__) -> npt.NDArray[float]:


class SigmoidNorm(StatelessTransformer):
def __init__(self, scale_factor: float = 10., smooth_factor: float = 0.5):
def __init__(self, scale_factor: float = 10.0, smooth_factor: float = 0.5):
super().__init__()
self.scale_factor = scale_factor
self.smooth_factor = smooth_factor

def transform(self, x: npt.NDArray[float], **__) -> npt.NDArray[float]:
return self.scale_factor / (1.0 + np.exp(5 - (self.smooth_factor * x)))
return self.scale_factor / (1.0 + np.exp(5 - (self.smooth_factor * x)))
9 changes: 6 additions & 3 deletions numalogic/transforms/_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ class PercentileScaler(BaseTransformer):
Default is None.
"""

def __init__(self, max_percentile: float = 99, min_percentile: Optional[float] = None, eps: float = 1e-2):
def __init__(
self, max_percentile: float = 99, min_percentile: Optional[float] = None, eps: float = 1e-2
):
self._max_px = max_percentile
self._min_px = min_percentile
self.tx = MinMaxScaler()
Expand Down Expand Up @@ -120,8 +122,9 @@ def fit(self, x: npt.NDArray[float]) -> Self:
for idx, _range in enumerate(p_ranges):
if _range <= self._eps:
LOGGER.warning(
"Max and Min percentile difference is less than "
"epsilon: %s for column %s", self._eps, idx
"Max and Min percentile difference is less than " "epsilon: %s for column %s",
self._eps,
idx,
)
data_max_px[idx] = data_max[idx]

Expand Down
27 changes: 7 additions & 20 deletions tests/transforms/test_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from sklearn.pipeline import make_pipeline

from numalogic.transforms import tanh_norm, TanhNorm, ExpMovingAverage, expmov_avg_aggregator
from numalogic.tools.exceptions import InvalidDataShapeError


class TestPostprocess(unittest.TestCase):
Expand All @@ -22,21 +21,17 @@ def test_tanh_norm_clf(self):
self.assertTupleEqual(arr.shape, scores.shape)
self.assertAlmostEqual(np.sum(scores), 39.52, places=2)

def test_exp_mov_avg_estimator_01(self):
def test_exp_mov_avg_estimator(self):
beta = 0.9
arr = np.arange(1, 11).reshape(-1, 1)
clf = ExpMovingAverage(0.9)
clf = ExpMovingAverage(beta)
out = clf.fit_transform(arr)
self.assertTupleEqual(arr.shape, out.shape)
self.assertAlmostEqual(expmov_avg_aggregator(arr, 0.9), out[-1].item(), places=3)

def test_exp_mov_avg_estimator_02(self):
arr = np.arange(1, 11).reshape(-1, 1)
clf = ExpMovingAverage(0.9, bias_correction=False)
out = clf.fit_transform(arr)
expected = expmov_avg_aggregator(arr, beta)

self.assertTupleEqual(arr.shape, out.shape)
self.assertAlmostEqual(
expmov_avg_aggregator(arr, 0.9, bias_correction=False), out[-1].item(), places=3
)
self.assertAlmostEqual(expected, out[-1].item(), places=2)
self.assertTrue(out.data.c_contiguous)

def test_exp_mov_avg_estimator_err(self):
with self.assertRaises(ValueError):
Expand All @@ -48,14 +43,6 @@ def test_exp_mov_avg_estimator_err(self):
with self.assertRaises(ValueError):
ExpMovingAverage(1.0)

with self.assertRaises(InvalidDataShapeError):
clf = ExpMovingAverage(0.1)
clf.fit_transform(np.arange(10).reshape(1, -1))

with self.assertRaises(InvalidDataShapeError):
clf = ExpMovingAverage(0.1)
clf.fit_transform(np.arange(12).reshape((2, 3, -1)))

def test_exp_mov_avg_agg(self):
arr = np.arange(1, 11)
val = expmov_avg_aggregator(arr, 0.9)
Expand Down

0 comments on commit 5593be3

Please sign in to comment.