Skip to content

Commit

Permalink
add handling unsupported nans in stringifying_unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
asledz authored and Anita Śledź committed Oct 23, 2023
1 parent bbee392 commit 1a3b7b8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- Minor tweaks to `neptune.cli` and cleaning leftovers after async Experiments ([#1529](https://github.com/neptune-ai/neptune-client/pull/1529))
- Pin `simplejson` required version to below `3.19` ([#1535](https://github.com/neptune-ai/neptune-client/pull/1535))
- Added `experimental` mode that supports partitioned operations queue ([#1524](https://github.com/neptune-ai/neptune-client/pull/1524))

- Added support for unsupported float values in `stringify_unsupported()` ([#1543](https://github.com/neptune-ai/neptune-client/pull/1543))

## neptune 1.8.2

Expand Down
5 changes: 5 additions & 0 deletions src/neptune/common/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"NeptuneDeprecationWarning",
"NeptuneWarning",
"NeptuneUnsupportedType",
"NeptuneUnsupportedValue",
]

import os
Expand All @@ -40,6 +41,10 @@ class NeptuneUnsupportedType(Warning):
pass


class NeptuneUnsupportedValue(Warning):
pass


warnings.simplefilter("always", category=NeptuneDeprecationWarning)

warned_once = set()
Expand Down
16 changes: 15 additions & 1 deletion src/neptune/internal/types/stringify_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__all__ = ["StringifyValue", "extract_if_stringify_value"]
__all__ = ["StringifyValue", "extract_if_stringify_value", "is_unsupported_float"]

import math
from typing import Any

from neptune.constants import (
Expand All @@ -40,6 +41,12 @@
from neptune.internal.utils.logger import logger


def is_unsupported_float(value) -> bool:
if isinstance(value, float):
return math.isinf(value) or math.isnan(value)
return False


class StringifyValue:
def __init__(self, value: Any):
# check if it's an integer outside 32bit range and cast it to float
Expand All @@ -51,6 +58,13 @@ def __init__(self, value: Any):
MAX_32_BIT_INT,
)
value = float(value)
if is_unsupported_float(value):
logger.info(
"The value you're trying to log is an unsupported float value (%s) and will be logged as string. "
"We'll add support for these types of values in the future.",
str(value),
)
value = str(value)

self.__value = value

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/neptune/new/test_stringify_unsupported.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import math
from contextlib import contextmanager
from datetime import datetime
from typing import (
Expand Down Expand Up @@ -531,3 +532,14 @@ def test_integers_outside_32bits(self, run):

with assert_no_warnings():
run["data"] = data

def test_stringifying_unsupported_floats(self, run):
with assert_no_warnings():
run["infinity"] = stringify_unsupported(float("inf"))
run["neg_infinity"] = stringify_unsupported(float("-inf"))
run["nan"] = stringify_unsupported(float("nan"))

assert run["infinity"].fetch() == "inf"
assert run["neg_infinity"].fetch() == "-inf"

assert math.isnan(float(run["nan"].fetch()))

0 comments on commit 1a3b7b8

Please sign in to comment.