-
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
tp.begin and tp.end operators #148
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,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 | ||
|
||
Example: | ||
Input | ||
timestamps: [1, 5, 10] | ||
Output | ||
timestamps: [1] | ||
|
||
Returns: | ||
A feature-less node with a single timestamp. | ||
""" | ||
|
||
return Begin(input=input).outputs["output"] |
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,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"] |
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
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,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 | ||
) |
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,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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
missing full stop after description. also not sure what "Guide" means here? same goes for end op