forked from jsoref/temporian
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
380: Initial implementation of moving_product operation (google#383)
* WIP: Initial implementation of moving_product operation * Remove the wrong use of override keyword at Reset Method in the MovingProductAccumulator class * Implement On-Demand Product Calculation in MovingProductAccumulator Key Changes: - Start and end indices (`start_idx` and `end_idx`) introduced to track the window. - `Add` and `Remove` methods updated to adjust the window indices without affecting the product. - `Result` method now calculates the product on-demand, considering zeros and ignoring NaN values for accuracy. - Added a TODO comment to explore future optimizations for the Result method to enhance calculation efficiency. * add test and doc for moving_product * Optimize moving_product logic to directly return zero for windows with zero values * fix(docs): Update docs and adjust example values for clarity in event_set_ops.py fix(moving_product): Update the moving_product logic to return NaN when the input only contains NaN. * feat(window_op): Add cumprod operator * style: format cumprod related files with Black * docs: remove un-wanted function description from moving_product at event_set_ops.py * fix(test): Pass the correct dtype at docstring example for moving_product in event_set_ops.py * fix(test_moving_product): Correct calculation errors in test_with_sampling and test_with_variable_winlen_same_sampling.
- Loading branch information
1 parent
6e8cd65
commit 9ccd08f
Showing
17 changed files
with
682 additions
and
13 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
::: temporian.EventSet.cumprod |
1 change: 1 addition & 0 deletions
1
docs/src/reference/temporian/operators/window/moving_product.md
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
::: temporian.EventSet.moving_product |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2021 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Moving Product operator class and public API function definition..""" | ||
|
||
from typing import Optional | ||
|
||
import numpy as np | ||
|
||
from temporian.core import operator_lib | ||
from temporian.core.compilation import compile | ||
from temporian.core.data.dtype import DType | ||
from temporian.core.data.node import EventSetNode | ||
from temporian.core.data.schema import FeatureSchema | ||
from temporian.core.operators.window.base import BaseWindowOperator | ||
from temporian.utils.typecheck import typecheck | ||
from temporian.core.typing import EventSetOrNode, WindowLength | ||
|
||
|
||
class MovingProductOperator(BaseWindowOperator): | ||
""" | ||
Window operator to compute the moving product. | ||
""" | ||
|
||
@classmethod | ||
def operator_def_key(cls) -> str: | ||
return "MOVING_PRODUCT" | ||
|
||
def get_feature_dtype(self, feature: FeatureSchema) -> DType: | ||
if not feature.dtype.is_float: | ||
raise ValueError( | ||
"moving_product requires the input EventSet to contain" | ||
" floating point features only, but received feature" | ||
f" {feature.name!r} with type {feature.dtype}. Note: You can" | ||
" cast features e.g. `.cast(tp.float32)`" | ||
) | ||
return ( | ||
DType.FLOAT32 if feature.dtype == DType.FLOAT32 else DType.FLOAT64 | ||
) | ||
|
||
|
||
operator_lib.register_operator(MovingProductOperator) | ||
|
||
|
||
@typecheck | ||
@compile | ||
def moving_product( | ||
input: EventSetOrNode, | ||
window_length: WindowLength, | ||
sampling: Optional[EventSetOrNode] = None, | ||
) -> EventSetOrNode: | ||
assert isinstance(input, EventSetNode) | ||
if sampling is not None: | ||
assert isinstance(sampling, EventSetNode) | ||
|
||
return MovingProductOperator( | ||
input=input, | ||
window_length=window_length, | ||
sampling=sampling, | ||
).outputs["output"] | ||
|
||
|
||
@compile | ||
def cumprod( | ||
input: EventSetOrNode, | ||
sampling: Optional[EventSetOrNode] = None, | ||
) -> EventSetOrNode: | ||
assert isinstance(input, EventSetNode) | ||
if sampling is not None: | ||
assert isinstance(sampling, EventSetNode) | ||
|
||
return MovingProductOperator( | ||
input=input, window_length=np.inf, sampling=sampling | ||
).outputs["output"] |
Oops, something went wrong.