Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/src/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Collaborator

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

| [`EventSet.add_index()`][temporian.EventSet.add_index] | Adds indexes to an [`EventSet`][temporian.EventSet]. |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_index is also repeated below

| [`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.
Expand All @@ -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].
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference/temporian/operators/unary/round.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: temporian.EventSet.round
32 changes: 32 additions & 0 deletions temporian/core/event_set_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:
Expand Down
63 changes: 62 additions & 1 deletion temporian/core/operators/test/test_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_round_float64 and test_round_float32 are doing the same thing. You can use functions f64 and f32 to create an array with that type (link).
f64 is used above in test_correct_isnan

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
def test_correct_sin(self) -> None:
def test_correct_sin(self):

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should run black . on the root of the repository (might need to call poetry shell or pre-ped poetry run) to fix the other formatting issues

evset = event_set(
timestamps=[1, 2, 3, 4],
features={"f": [0, np.pi / 2, np.pi, 3 * np.pi / 2]},
Expand Down
18 changes: 18 additions & 0 deletions temporian/core/operators/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -272,6 +277,7 @@ class ArcTanOperator(BaseUnaryOperator):
def op_key_definition(cls) -> str:
return "ARCTAN"


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary empty line

@classmethod
def allowed_dtypes(cls) -> List[DType]:
return [
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -414,5 +431,6 @@ def arctan(
assert isinstance(input, EventSetNode)

return ArcTanOperator(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary empty line

input=input,
).outputs["output"]
9 changes: 9 additions & 0 deletions temporian/implementation/numpy/operators/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
NotNanOperator,
IsNanOperator,
LogOperator,
RoundOperator,
SinOperator,
CosOperator,
TanOperator,
Expand Down Expand Up @@ -83,6 +84,11 @@ def _do_operation(self, feature: np.ndarray) -> np.ndarray:
return np.log(feature)



class RoundNumpyImplementation(BaseUnaryNumpyImplementation):
def _do_operation(self, feature: np.ndarray) -> np.ndarray:
return np.round(feature)

class SinNumpyImplementation(BaseUnaryNumpyImplementation):
def _do_operation(self, feature: np.ndarray) -> np.ndarray:
return np.sin(feature)
Expand Down Expand Up @@ -129,6 +135,9 @@ def _do_operation(self, feature: np.ndarray) -> np.ndarray:
LogOperator, LogNumpyImplementation
)
implementation_lib.register_operator_implementation(

RoundOperator, RoundNumpyImplementation

SinOperator, SinNumpyImplementation
)
implementation_lib.register_operator_implementation(
Expand Down
Loading