Skip to content

Commit

Permalink
Adding docs and fixing some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javiber committed May 8, 2024
1 parent f348888 commit 77d109b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: temporian.EventSet.moving_quantile
53 changes: 52 additions & 1 deletion temporian/core/event_set_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3224,7 +3224,58 @@ def moving_quantile(
quantile: float,
sampling: Optional[EventSetOrNode] = None,
) -> EventSetOrNode:
""" """
"""Computes the quantile in a sliding window over an
[`EventSet`][temporian.EventSet].
For each t in sampling, and for each feature independently, returns at
time t the appropiated quantile for the feature in the window
(t - window_length, t].
`sampling` can't be specified if a variable `window_length` is
specified (i.e. if `window_length` is an EventSet).
If `sampling` is specified or `window_length` is an EventSet, the moving
window is sampled at each timestamp in them, else it is sampled on the
input's.
Missing values (such as NaNs) are ignored.
If the window does not contain any values (e.g., all the values are
missing, or the window does not contain any sampling), outputs missing
values.
The quantile calculated in each window is equivalent to numpy's
`"averaged_inverted_cdf"` method.
Example:
```python
>>> a = tp.event_set(
... timestamps=[0, 1, 2, 5, 6, 7],
... features={"value": [np.nan, 1, 5, 10, 15, 20]},
... )
>>> a.moving_quantile(tp.duration.seconds(4), quantile=0.5)
indexes: ...
(6 events):
timestamps: [0. 1. 2. 5. 6. 7.]
'value': [ nan 1. 3. 7.5 12.5 15. ]
...
```
See [`EventSet.moving_count()`][temporian.EventSet.moving_count] for
examples of moving window operations with external sampling and indices.
Args:
window_length: Sliding window's length.
quantile: the desired quantile defined in the range (0, 1).
sampling: Timestamps to sample the sliding window's value at. If not
provided, timestamps in the input are used.
Returns:
EventSet containing the moving standard deviation of each feature in
the input.
"""
from temporian.core.operators.window.moving_quantile import (
moving_quantile,
)
Expand Down
1 change: 1 addition & 0 deletions temporian/core/test/registered_operators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_base(self):
"MOVING_MAX",
"MOVING_MIN",
"MOVING_PRODUCT",
"MOVING_QUANTILE",
"MOVING_STANDARD_DEVIATION",
"MOVING_SUM",
"MULTIPLICATION",
Expand Down
4 changes: 2 additions & 2 deletions temporian/implementation/numpy_cc/operators/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ py::array_t<OUTPUT> accumulate(const ArrayD &evset_timestamps,
auto v_window_length = window_length.unchecked<1>();

assert(v_timestamps.shape(0) == v_window_length.shape(0));
assert(v_timestamps.shape(0) == v_values.shape(0));
assert(v_timestamps.shape(0) == evset_values.shape(0));

// Index of the first value in the window.
size_t begin_idx = 0;
Expand Down Expand Up @@ -247,7 +247,7 @@ py::array_t<OUTPUT> accumulate(const ArrayD &evset_timestamps,
auto v_sampling = sampling_timestamps.unchecked<1>();
auto v_window_length = window_length.unchecked<1>();

assert(v_timestamps.shape(0) == v_values.shape(0));
assert(v_timestamps.shape(0) == evset_values.shape(0));
assert(v_sampling.shape(0) == v_window_length.shape(0));

size_t begin_idx = 0;
Expand Down

0 comments on commit 77d109b

Please sign in to comment.