-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathbound.py
50 lines (40 loc) · 1.42 KB
/
bound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# Copyright (c) 2023 salesforce.com, inc.
# All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
#
"""
Transforms that clip the input.
"""
from collections import OrderedDict
import logging
import numpy as np
from merlion.transform.base import TransformBase
from merlion.utils import UnivariateTimeSeries, TimeSeries
logger = logging.getLogger(__name__)
class LowerUpperClip(TransformBase):
"""
Clips the values of a time series to lie between lower and upper.
"""
def __init__(self, lower=None, upper=None):
super().__init__()
assert not (lower is None and upper is None), "Must provide at least one of lower or upper"
if lower is not None and upper is not None:
assert lower < upper
self.lower = lower
self.upper = upper
@property
def requires_inversion_state(self):
"""
``False`` because "inverting" value clipping is stateless.
"""
return False
def train(self, time_series: TimeSeries):
pass
def __call__(self, time_series: TimeSeries) -> TimeSeries:
new_vars = OrderedDict()
for name, var in time_series.items():
x = np.clip(var.np_values, self.lower, self.upper)
new_vars[name] = UnivariateTimeSeries(var.index, x)
return TimeSeries(new_vars)