Skip to content

Commit

Permalink
Handles case where input was series with time index
Browse files Browse the repository at this point in the history
So we need to fix the inputs and introspecting them. This is
a quick fix to not fail when someone applies the SDK to the
quick start example.
  • Loading branch information
skrawcz committed Jun 12, 2024
1 parent 4771dd0 commit b8e9435
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ui/sdk/src/hamilton_sdk/tracking/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def make_json_safe(item: Union[dict, list, str, float, int, bool]) -> Any:
return make_json_safe(dataclasses.asdict(item))
elif isinstance(item, Enum):
return item.value # Convert enum to its corresponding value
elif hasattr(item, "to_json"):
# we convert to json string and then deserialize it so that
# it's not a string in the UI.
return json.loads(item.to_json())
elif hasattr(item, "to_dict"):
return make_json_safe(item.to_dict())
else:
Expand Down
23 changes: 20 additions & 3 deletions ui/sdk/tests/tracking/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,29 @@ def test_make_json_safe_with_pandas_dataframe():
"F": "foo",
}
)
utils.make_json_safe(input_dataframe)
actual = utils.make_json_safe(input_dataframe)
assert actual == {
"A": {"0": 1.0, "1": 1.0, "2": 1.0, "3": 1.0},
"B": {"0": 1357084800000, "1": 1357084800000, "2": 1357084800000, "3": 1357084800000},
"C": {"0": 1.0, "1": 1.0, "2": 1.0, "3": 1.0},
"D": {"0": 3, "1": 3, "2": 3, "3": 3},
"E": {"0": "test", "1": "train", "2": "test", "3": "train"},
"F": {"0": "foo", "1": "foo", "2": "foo", "3": "foo"},
}


def test_make_json_safe_with_pandas_series():
input_series = pd.Series(["a", "b", "c", "d"])
utils.make_json_safe(input_series)
index = pd.date_range("2022-01-01", periods=6, freq="w")
input_series = pd.Series([1, 10, 50, 100, 200, 400], index=index)
actual = utils.make_json_safe(input_series)
assert actual == {
"1641081600000": 1,
"1641686400000": 10,
"1642291200000": 50,
"1642896000000": 100,
"1643500800000": 200,
"1644105600000": 400,
}


def test_make_json_safe_with_file_object(tmp_path):
Expand Down

0 comments on commit b8e9435

Please sign in to comment.