-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
349: Implemented round() function #359
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ Check the index on the left for a more detailed description of any symbol. | |
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | | ||
| [`tp.combine()`][temporian.combine] | Combines events from [`EventSets`][temporian.EventSet] with different samplings. | | ||
| [`tp.glue()`][temporian.glue] | Concatenates features from [`EventSets`][temporian.EventSet] with the same sampling. | | ||
| [`EventSet.abs()`][temporian.EventSet.abs] | Computes the absolute value of the features. | ||
| [`EventSet.add_index()`][temporian.EventSet.add_index] | Adds indexes to an [`EventSet`][temporian.EventSet]. | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| [`EventSet.abs()`][temporian.EventSet.abs] | Computes the absolute value of the features. | | ||
| [`EventSet.add_index()`][temporian.EventSet.add_index] | Adds indexes to an [`EventSet`][temporian.EventSet]. | ||
| [`EventSet.arccos()`][temporian.EventSet.arccos] | Computes the inverse cosine of the features. | ||
|
@@ -70,6 +72,7 @@ Check the index on the left for a more detailed description of any symbol. | |
| [`EventSet.propagate()`][temporian.EventSet.propagate] | Propagates feature values over a sub index. | | ||
| [`EventSet.rename()`][temporian.EventSet.rename] | Renames an [`EventSet`][temporian.EventSet]'s features and index. | | ||
| [`EventSet.resample()`][temporian.EventSet.resample] | Resamples an [`EventSet`][temporian.EventSet] at each timestamp of another [`EventSet`][temporian.EventSet]. | | ||
| [`EventSet.round()`][temporian.EventSet.round] | Computes the round value of the features. | | ||
| [`EventSet.select()`][temporian.EventSet.select] | Selects a subset of features from an [`EventSet`][temporian.EventSet]. | | ||
| [`EventSet.select_index_values()`][temporian.EventSet.select_index_values] | Selects a subset of index values from an [`EventSet`][temporian.EventSet]. | | ||
| [`EventSet.set_index()`][temporian.EventSet.set_index] | Replaces the indexes in an [`EventSet`][temporian.EventSet]. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
::: temporian.EventSet.round |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1501,6 +1501,38 @@ def abs( | |
|
||
return abs(self) | ||
|
||
def __round__(self): | ||
from temporian.core.operators.unary import round | ||
|
||
return round(input=self) | ||
|
||
def round( | ||
javiber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self: EventSetOrNode, | ||
) -> EventSetOrNode: | ||
"""Rounds the values of an [`EventSet`][temporian.EventSet]'s features to the nearest integer. | ||
only float types are allowed and the output. Output type wil be same as the input type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation is incorrect, black should fix this |
||
Example: | ||
```python | ||
>>> a = tp.event_set( | ||
... timestamps=[1, 2, 3], | ||
... features={"M": [1.4, 2.6, 3.1], "N": [-1.9, -3.5, 5.8]}, | ||
... ) | ||
>>> a.round() | ||
indexes: ... | ||
'M': [1, 3, 3] | ||
'N': [-2, -4, 6] | ||
... | ||
|
||
``` | ||
|
||
Returns: | ||
EventSet with rounded feature values. | ||
""" | ||
from temporian.core.operators.unary import round | ||
|
||
return round(self) | ||
|
||
|
||
def add_index( | ||
self: EventSetOrNode, indexes: Union[str, List[str]] | ||
) -> EventSetOrNode: | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -99,7 +99,68 @@ def test_correct_notnan(self) -> None: | |||||
) | ||||||
assertOperatorResult(self, evset.notnan(), expected) | ||||||
|
||||||
def test_correct_sin(self) -> None: | ||||||
|
||||||
def test_round_single_feature(self): | ||||||
evset = event_set( | ||||||
timestamps=[1, 2, 3], | ||||||
features={"f": [1.1, -2.5, -3.9]}, | ||||||
) | ||||||
expected = event_set( | ||||||
timestamps=[1, 2, 3], | ||||||
features={"f": [1.0, -3.0, -4.0]}, | ||||||
same_sampling_as=evset, | ||||||
) | ||||||
assertOperatorResult(self, evset.round(), expected) | ||||||
assertOperatorResult(self, round(evset), expected) # __round__ magic | ||||||
|
||||||
def test_round_multiple_features(self): | ||||||
evset = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": [10.5, 11.7], "b": [1.2, 2.9]}, | ||||||
) | ||||||
expected = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": [11.0, 12.0], "b": [1.0, 3.0]}, | ||||||
same_sampling_as=evset, | ||||||
) | ||||||
assertOperatorResult(self, evset.round(), expected) | ||||||
assertOperatorResult(self, round(evset), expected) # __round__ magic | ||||||
|
||||||
def test_round_non_accepted_types(self): | ||||||
evset = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": ["10.5", 11.7], "b": [1, 2]}, | ||||||
) | ||||||
with self.assertRaises(TypeError): | ||||||
_ = evset.round() | ||||||
|
||||||
def test_round_float32(self): | ||||||
evset = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": [10.5, 11.7], "b": [1.2, 2.9]}, | ||||||
) | ||||||
expected = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": [11.0, 12.0], "b": [1.0, 3.0]}, | ||||||
same_sampling_as=evset, | ||||||
) | ||||||
assertOperatorResult(self, evset.round(), expected) | ||||||
assertOperatorResult(self, round(evset), expected) # __round__ magic | ||||||
|
||||||
def test_round_float64(self): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
evset = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": [10.5, 11.7], "b": [1.2, 2.9]}, | ||||||
) | ||||||
expected = event_set( | ||||||
timestamps=[1, 2], | ||||||
features={"a": [11.0, 12.0], "b": [1.0, 3.0]}, | ||||||
same_sampling_as=evset, | ||||||
) | ||||||
assertOperatorResult(self, evset.round(), expected) | ||||||
assertOperatorResult(self, round(evset), expected) # __round__ magic | ||||||
|
||||||
def test_correct_sin(self) -> None: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation of this line is off. Also no need for type annotations in these test
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should run |
||||||
evset = event_set( | ||||||
timestamps=[1, 2, 3, 4], | ||||||
features={"f": [0, np.pi / 2, np.pi, 3 * np.pi / 2]}, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,6 +182,11 @@ def get_output_dtype(cls, feature_dtype: DType) -> DType: | |
return feature_dtype | ||
|
||
|
||
class RoundOperator(BaseUnaryOperator): | ||
@classmethod | ||
def op_key_definition(cls) -> str: | ||
return "ROUND" | ||
|
||
class SinOperator(BaseUnaryOperator): | ||
@classmethod | ||
def op_key_definition(cls) -> str: | ||
|
@@ -272,6 +277,7 @@ class ArcTanOperator(BaseUnaryOperator): | |
def op_key_definition(cls) -> str: | ||
return "ARCTAN" | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary empty line |
||
@classmethod | ||
def allowed_dtypes(cls) -> List[DType]: | ||
return [ | ||
|
@@ -289,6 +295,7 @@ def get_output_dtype(cls, feature_dtype: DType) -> DType: | |
operator_lib.register_operator(NotNanOperator) | ||
operator_lib.register_operator(AbsOperator) | ||
operator_lib.register_operator(LogOperator) | ||
operator_lib.register_operator(RoundOperator) | ||
operator_lib.register_operator(SinOperator) | ||
operator_lib.register_operator(CosOperator) | ||
operator_lib.register_operator(TanOperator) | ||
|
@@ -353,6 +360,16 @@ def log( | |
|
||
|
||
@compile | ||
def round( | ||
input: EventSetOrNode, | ||
) -> EventSetOrNode: | ||
assert isinstance(input, EventSetNode) | ||
|
||
return RoundOperator( | ||
input=input, | ||
).outputs["output"] | ||
|
||
@compile | ||
def sin( | ||
input: EventSetOrNode, | ||
) -> EventSetOrNode: | ||
|
@@ -414,5 +431,6 @@ def arctan( | |
assert isinstance(input, EventSetNode) | ||
|
||
return ArcTanOperator( | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary empty line |
||
input=input, | ||
).outputs["output"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abs
is repeated on line 49 below