-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #5309 for details. Signed-off-by: Christian Bourjau <christian.bourjau@quantco.com>
- Loading branch information
Showing
8 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
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,58 @@ | ||
# Copyright (c) ONNX Project Contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from datetime import datetime | ||
|
||
import numpy as np | ||
|
||
import onnx | ||
from onnx import numpy_helper | ||
from onnx.backend.sample.ops.abs import abs | ||
Check notice Code scanning / CodeQL Unused import Note
Import of 'abs' is not used.
|
||
from onnx.backend.test.case.base import Base | ||
from onnx.backend.test.case.node import expect | ||
|
||
|
||
class ParseDateTime(Base): | ||
@staticmethod | ||
def export_float_nan_default() -> None: | ||
fmt = "%d/%m/%y %H:%M" | ||
default = float("NaN") | ||
node = onnx.helper.make_node( | ||
"ParseDateTime", | ||
inputs=["x"], | ||
outputs=["y"], | ||
format=fmt, | ||
unit="s", | ||
default=numpy_helper.from_array(np.array(default, np.float64)) | ||
) | ||
x = np.array(["21/11/06 16:30", "foobar"], dtype=object) | ||
y = [] | ||
for s in x: | ||
try: | ||
# datetime.timestamp() returns a float | ||
y.append(datetime.strptime(s, fmt).timestamp()) | ||
except ValueError: | ||
y.append(default) | ||
expect(node, inputs=[x], outputs=[np.array(y)], name="test_parsedatetime") | ||
|
||
|
||
@staticmethod | ||
def export_int_default() -> None: | ||
fmt = "%d/%m/%y %H:%M" | ||
default = np.iinfo(np.int64).min | ||
node = onnx.helper.make_node( | ||
"ParseDateTime", | ||
inputs=["x"], | ||
outputs=["y"], | ||
format=fmt, | ||
unit="s", | ||
default=numpy_helper.from_array(np.array(default, np.int64)) | ||
) | ||
x = np.array(["21/11/06 16:30", "foobar"], dtype=object) | ||
y = [] | ||
for s in x: | ||
try: | ||
y.append(datetime.strptime(s, fmt).timestamp()) | ||
except ValueError: | ||
y.append(default) | ||
expect(node, inputs=[x], outputs=[np.array(y, np.int64)], name="test_parsedatetime") |
Binary file not shown.
1 change: 1 addition & 0 deletions
1
onnx/backend/test/data/node/test_parsedatetime/test_data_set_0/input_0.pb
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 @@ | ||
221/11/06 16:302foobarBx |
Binary file added
BIN
+25 Bytes
onnx/backend/test/data/node/test_parsedatetime/test_data_set_0/output_0.pb
Binary file not shown.
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,17 @@ | ||
# Copyright (c) ONNX Project Contributors | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# pylint: disable=W0221 | ||
from datetime import datetime | ||
|
||
import numpy as np | ||
|
||
from onnx.reference.ops._op import OpRunUnaryNum | ||
|
||
|
||
class ParseDateTime(OpRunUnaryNum): | ||
def _run(self, x, format: str, unit: str, default=None): # type: ignore | ||
out = np.array([datetime.strptime(s, format).timestamp() for s in x]) | ||
out[np.isnan(out)] = default | ||
out = out.astype(default.dtype) | ||
return (out,) |