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 2 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
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(
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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Add another line specifying that only float types are allowed, and the output type will always be the same as the input's

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
47 changes: 47 additions & 0 deletions temporian/core/operators/test/test_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,53 @@ def test_correct_notnan(self) -> None:
)
assertOperatorResult(self, evset.notnan(), expected)

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, 12], "b": [1, 3]},
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think these will pass, since a list of ints will yield an eventset with int features, which won't be equal to the float results of round(). Ensure your new tests are passing by running bazel test //temporian/core/operators/test:test_unary --config=macos --test_output=errors or bazel test //temporian/core/operators/test:test_unary --config=linux --test_output=errors depending on your OS

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

Choose a reason for hiding this comment

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

missing calling round() on the evset here - this shouldn't be raising anything? did you manage to run the tests locally to ensure they pass?


def test_round_float32_and_float64_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,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

which of these are being defined as f32 and which as f64? see the f64 and f32 methods in temporian/test/utils.py to explicitly create an eventset with the desired feature types

Copy link
Author

Choose a reason for hiding this comment

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

I need a clarification. Can I use the numpy for specifying the float32 and float64 separately as

float32

def test_round_float32(self):
    evset = event_set(
        timestamps=[1, 2],
        features={"a": np.array([10.5, 11.7], dtype=np.float32), "b": np.array([1.2, 2.9], dtype=np.float32)},
    )
    expected = event_set(
        timestamps=[1, 2],
        features={"a": np.array([11.0, 12.0], dtype=np.float32), "b": np.array([1.0, 3.0], dtype=np.float32)},
        same_sampling_as=evset,
    )
    assertOperatorResult(self, evset.round(), expected)
    assertOperatorResult(self, round(evset), expected)  # __round__ magic

float64

def test_round_float64(self):
    evset = event_set(
        timestamps=[1, 2],
        features={"a": np.array([10.5, 11.7], dtype=np.float64), "b": np.array([1.2, 2.9], dtype=np.float64)},
    )
    expected = event_set(
        timestamps=[1, 2],
        features={"a": np.array([11.0, 12.0], dtype=np.float64), "b": np.array([1.0, 3.0], dtype=np.float64)},
        same_sampling_as=evset,
    )
    assertOperatorResult(self, evset.round(), expected)
    assertOperatorResult(self, round(evset), expected)  # __round__ magic

assertOperatorResult(self, evset.round(), expected)
assertOperatorResult(self, round(evset), expected) # __round__ magic


if __name__ == "__main__":
absltest.main()
31 changes: 31 additions & 0 deletions temporian/core/operators/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,31 @@ def get_output_dtype(cls, feature_dtype: DType) -> DType:
return feature_dtype


class RoundOperator(BaseUnaryOperator):
@classmethod
def op_key_definition(cls) -> str:
return "ROUND"

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 [
DType.FLOAT32,
DType.FLOAT64,
DType.INT32,
DType.INT64,
Copy link
Collaborator

Choose a reason for hiding this comment

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

ints shouldn't be allowed

]

@classmethod
def get_output_dtype(cls, feature_dtype: DType) -> DType:
return feature_dtype


operator_lib.register_operator(InvertOperator)
operator_lib.register_operator(IsNanOperator)
operator_lib.register_operator(NotNanOperator)
operator_lib.register_operator(AbsOperator)
operator_lib.register_operator(LogOperator)
operator_lib.register_operator(RoundOperator)

Copy link
Collaborator

Choose a reason for hiding this comment

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

extra newline here too


@compile
Expand Down Expand Up @@ -242,3 +262,14 @@ def log(
return LogOperator(
input=input,
).outputs["output"]


@compile
def round(
input: EventSetOrNode,
) -> EventSetOrNode:
assert isinstance(input, EventSetNode)

return RoundOperator(
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,
)
from temporian.implementation.numpy import implementation_lib
from temporian.implementation.numpy.data.event_set import IndexData
Expand Down Expand Up @@ -77,6 +78,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)


implementation_lib.register_operator_implementation(
AbsOperator, AbsNumpyImplementation
)
Expand All @@ -92,3 +98,6 @@ def _do_operation(self, feature: np.ndarray) -> np.ndarray:
implementation_lib.register_operator_implementation(
LogOperator, LogNumpyImplementation
)
implementation_lib.register_operator_implementation(
RoundOperator, RoundNumpyImplementation
)