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

tp.begin and tp.end operators #148

Merged
merged 4 commits into from
Jun 8, 2023
Merged
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
167 changes: 96 additions & 71 deletions docs/src/tutorials/heart_rate_analysis.ipynb

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions docs/src/tutorials/m5_competition.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@
print("Export to csv file")
print("==================")

tabular_dataset_data.to_pandas().to_csv(
os.path.join(work_directory, "tabular_dataset.csv"), index=False
tp.to_csv(
tabular_dataset_data,
path=os.path.join(work_directory, "tabular_dataset.csv"),
)

print("The artefacts are available in:", work_directory)
28 changes: 28 additions & 0 deletions temporian/core/operators/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ py_library(
":since_last",
":unary",
":unique_timestamps",
":begin",
":end",
"//temporian/core/operators/binary",
"//temporian/core/operators/calendar:day_of_month",
"//temporian/core/operators/calendar:day_of_week",
Expand Down Expand Up @@ -250,3 +252,29 @@ py_library(
"//temporian/proto:core_py_proto",
],
)

py_library(
name = "begin",
srcs = ["begin.py"],
srcs_version = "PY3",
deps = [
":base",
"//temporian/core:operator_lib",
"//temporian/core/data:node",
"//temporian/core/data:schema",
"//temporian/proto:core_py_proto",
],
)

py_library(
name = "end",
srcs = ["end.py"],
srcs_version = "PY3",
deps = [
":base",
"//temporian/core:operator_lib",
"//temporian/core/data:node",
"//temporian/core/data:schema",
"//temporian/proto:core_py_proto",
],
)
2 changes: 2 additions & 0 deletions temporian/core/operators/all_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@
from temporian.core.operators.window.moving_max import moving_max
from temporian.core.operators.unique_timestamps import unique_timestamps
from temporian.core.operators.since_last import since_last
from temporian.core.operators.begin import begin
from temporian.core.operators.end import end
70 changes: 70 additions & 0 deletions temporian/core/operators/begin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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.


"""Begin operator class and public API function definitions."""

from temporian.core import operator_lib
from temporian.core.data.node import Node, create_node_new_features_new_sampling
from temporian.core.operators.base import Operator
from temporian.proto import core_pb2 as pb


class Begin(Operator):
def __init__(self, input: Node):
super().__init__()

self.add_input("input", input)

self.add_output(
"output",
create_node_new_features_new_sampling(
features=[],
indexes=input.schema.indexes,
is_unix_timestamp=input.schema.is_unix_timestamp,
creator=self,
),
)
self.check()

@classmethod
def build_op_definition(cls) -> pb.OperatorDef:
return pb.OperatorDef(
key="BEGIN",
attributes=[],
inputs=[pb.OperatorDef.Input(key="input")],
outputs=[pb.OperatorDef.Output(key="output")],
)


operator_lib.register_operator(Begin)


def begin(input: Node) -> Node:
"""Generates a single timestamp at the beginning of the input.

Args:
input: Guide input
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing full stop after description. also not sure what "Guide" means here? same goes for end op


Example:
Input
timestamps: [1, 5, 10]
Output
timestamps: [1]

Returns:
A feature-less node with a single timestamp.
"""

return Begin(input=input).outputs["output"]
70 changes: 70 additions & 0 deletions temporian/core/operators/end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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.


"""End operator class and public API function definitions."""

from temporian.core import operator_lib
from temporian.core.data.node import Node, create_node_new_features_new_sampling
from temporian.core.operators.base import Operator
from temporian.proto import core_pb2 as pb


class End(Operator):
def __init__(self, input: Node):
super().__init__()

self.add_input("input", input)

self.add_output(
"output",
create_node_new_features_new_sampling(
features=[],
indexes=input.schema.indexes,
is_unix_timestamp=input.schema.is_unix_timestamp,
creator=self,
),
)
self.check()

@classmethod
def build_op_definition(cls) -> pb.OperatorDef:
return pb.OperatorDef(
key="END",
attributes=[],
inputs=[pb.OperatorDef.Input(key="input")],
outputs=[pb.OperatorDef.Output(key="output")],
)


operator_lib.register_operator(End)


def end(input: Node) -> Node:
"""Generates a single timestamp at the end of the input.

Args:
input: Guide input

Example:
Input
timestamps: [1, 5, 10]
Output
timestamps: [10]

Returns:
A feature-less node with a single timestamp.
"""

return End(input=input).outputs["output"]
2 changes: 2 additions & 0 deletions temporian/core/test/registered_operators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_base(self):
"ADDITION_SCALAR",
"ADD_INDEX",
"AND",
"BEGIN",
"CALENDAR_DAY_OF_MONTH",
"CALENDAR_DAY_OF_WEEK",
"CALENDAR_DAY_OF_YEAR",
Expand All @@ -42,6 +43,7 @@ def test_base(self):
"DIVISION",
"DIVISION_SCALAR",
"DROP_INDEX",
"END",
"EQUAL",
"EQUAL_SCALAR",
"FILTER",
Expand Down
3 changes: 3 additions & 0 deletions temporian/implementation/numpy/data/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ def get_num_plots(
def auto_style(uniform: bool, xs, ys) -> Style:
"""Finds the best plotting style."""

if len(ys) <= 1:
return Style.marker

if len(ys) == 0:
all_ys_are_equal = True
else:
Expand Down
33 changes: 33 additions & 0 deletions temporian/implementation/numpy/operators/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ py_library(
":since_last",
":unary",
":unique_timestamps",
":begin",
":end",
"//temporian/implementation/numpy/operators/binary:arithmetic",
"//temporian/implementation/numpy/operators/binary:logical",
"//temporian/implementation/numpy/operators/binary:relational",
Expand Down Expand Up @@ -250,3 +252,34 @@ py_library(
"//temporian/implementation/numpy/data:event_set",
],
)

py_library(
name = "begin",
srcs = ["begin.py"],
srcs_version = "PY3",
deps = [
# already_there/numpy
":base",
"//temporian/core/data:duration",
"//temporian/core/operators:begin",
"//temporian/implementation/numpy:implementation_lib",
"//temporian/implementation/numpy:utils",
"//temporian/implementation/numpy/data:event_set",
],
)


py_library(
name = "end",
srcs = ["end.py"],
srcs_version = "PY3",
deps = [
# already_there/numpy
":base",
"//temporian/core/data:duration",
"//temporian/core/operators:end",
"//temporian/implementation/numpy:implementation_lib",
"//temporian/implementation/numpy:utils",
"//temporian/implementation/numpy/data:event_set",
],
)
2 changes: 2 additions & 0 deletions temporian/implementation/numpy/operators/all_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@
from temporian.implementation.numpy.operators.calendar import second
from temporian.implementation.numpy.operators import since_last
from temporian.implementation.numpy.operators import unique_timestamps
from temporian.implementation.numpy.operators import begin
from temporian.implementation.numpy.operators import end
59 changes: 59 additions & 0 deletions temporian/implementation/numpy/operators/begin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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.


"""Implementation for the Begin operator."""


from typing import Dict
import numpy as np

from temporian.implementation.numpy.data.event_set import IndexData, EventSet
from temporian.core.operators.begin import Begin
from temporian.implementation.numpy import implementation_lib
from temporian.implementation.numpy.operators.base import OperatorImplementation


class BeginNumpyImplementation(OperatorImplementation):
def __init__(self, operator: Begin) -> None:
assert isinstance(operator, Begin)
super().__init__(operator)

def __call__(self, input: EventSet) -> Dict[str, EventSet]:
assert isinstance(self.operator, Begin)
output_schema = self.output_schema("output")

# create output event set
output_evset = EventSet(data={}, schema=output_schema)

# fill output event set data
for index_key, index_data in input.data.items():
if len(index_data.timestamps) == 0:
dst_timestamps = np.array([], dtype=np.float64)
else:
dst_timestamps = np.array(
[index_data.timestamps[0]], dtype=np.float64
)
output_evset[index_key] = IndexData(
[],
dst_timestamps,
schema=output_schema,
)

return {"output": output_evset}


implementation_lib.register_operator_implementation(
Begin, BeginNumpyImplementation
)
57 changes: 57 additions & 0 deletions temporian/implementation/numpy/operators/end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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.


"""Implementation for the End operator."""


from typing import Dict
import numpy as np

from temporian.implementation.numpy.data.event_set import IndexData, EventSet
from temporian.core.operators.end import End
from temporian.implementation.numpy import implementation_lib
from temporian.implementation.numpy.operators.base import OperatorImplementation


class EndNumpyImplementation(OperatorImplementation):
def __init__(self, operator: End) -> None:
assert isinstance(operator, End)
super().__init__(operator)

def __call__(self, input: EventSet) -> Dict[str, EventSet]:
assert isinstance(self.operator, End)
output_schema = self.output_schema("output")

# create output event set
output_evset = EventSet(data={}, schema=output_schema)

# fill output event set data
for index_key, index_data in input.data.items():
if len(index_data.timestamps) == 0:
dst_timestamps = np.array([], dtype=np.float64)
else:
dst_timestamps = np.array(
[index_data.timestamps[-1]], dtype=np.float64
)
output_evset[index_key] = IndexData(
[],
dst_timestamps,
schema=output_schema,
)

return {"output": output_evset}


implementation_lib.register_operator_implementation(End, EndNumpyImplementation)
Loading