Skip to content

Commit

Permalink
Added list, set, and tuple support for log_configs(), added tuple s…
Browse files Browse the repository at this point in the history
…upport for tags (#67)

* Revert "Remove iterables from `log_config` value type hints (#53)"

This reverts commit 0988dff.

* Enhance value serialization to convert non-string list elements to strings and expand log_configs tests for various data types

* Added tuple support

* Add tuple support to log_configs and metadata fields; update tests for tag handling

* Added `tuple` to `add_tags()` and `remove_tags()` typehints

* Updated readme

* Applied PR review suggestions

* Merge commit '85bb57efbe10ad5eec4caa74ff3a6b33ca4f626c' into ss/log_configs_series_support

* Revert "Merge commit '85bb57efbe10ad5eec4caa74ff3a6b33ca4f626c' into ss/log_configs_series_support"

This reverts commit 2f2226d.
  • Loading branch information
SiddhantSadangi authored Nov 5, 2024
1 parent a91f12c commit ae56869
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ __Parameters__

| Name | Type | Default | Description |
|---------------|----------------------------------------------------|---------|---------------------------------------------------------------------------|
| `data` | `Dict[str, Union[float, bool, int, str, datetime]]`, optional | `None` | Dictionary of configs or other values to log. Available types: float, integer, Boolean, string, and datetime. |
| `data` | `Dict[str, Union[float, bool, int, str, datetime, list, set, tuple]]`, optional | `None` | Dictionary of configs or other values to log. Available types: float, integer, Boolean, string, and datetime. |

__Examples__

Expand Down Expand Up @@ -295,7 +295,7 @@ __Parameters__

| Name | Type | Default | Description |
|---------------|----------------------------------------------------|---------|---------------------------------------------------------------------------|
| `tags` | `Union[List[str], Set[str]]` | - | List or set of tags to add to the run. |
| `tags` | `Union[List[str], Set[str], Tuple[str]]` | - | List or set of tags to add to the run. |
| `group_tags` | `bool`, optional | `False` | Add group tags instead of regular tags. |

__Example__
Expand All @@ -313,7 +313,7 @@ __Parameters__

| Name | Type | Default | Description |
|---------------|----------------------------------------------------|---------|---------------------------------------------------------------------------|
| `tags` | `Union[List[str], Set[str]]` | - | List or set of tags to remove from the run. |
| `tags` | `Union[List[str], Set[str], Tuple[str]]` | - | List or set of tags to remove from the run. |
| `group_tags` | `bool`, optional | `False` | Remove group tags instead of regular tags. |

__Example__
Expand Down
23 changes: 14 additions & 9 deletions src/neptune_scale/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Literal,
Optional,
Set,
Tuple,
Union,
)

Expand Down Expand Up @@ -449,7 +450,9 @@ def log_metrics(
"""
self.log(step=step, timestamp=timestamp, metrics=data)

def log_configs(self, data: Optional[Dict[str, Union[float, bool, int, str, datetime]]] = None) -> None:
def log_configs(
self, data: Optional[Dict[str, Union[float, bool, int, str, datetime, list, set, tuple]]] = None
) -> None:
"""
Logs the specified metadata to a Neptune run.
Expand Down Expand Up @@ -483,7 +486,7 @@ def log_configs(self, data: Optional[Dict[str, Union[float, bool, int, str, date
"""
self.log(configs=data)

def add_tags(self, tags: Union[List[str], Set[str]], group_tags: bool = False) -> None:
def add_tags(self, tags: Union[List[str], Set[str], Tuple[str]], group_tags: bool = False) -> None:
"""
Adds the list of tags to the run.
Expand All @@ -502,7 +505,7 @@ def add_tags(self, tags: Union[List[str], Set[str]], group_tags: bool = False) -
name = "sys/tags" if not group_tags else "sys/group_tags"
self.log(tags_add={name: tags})

def remove_tags(self, tags: Union[List[str], Set[str]], group_tags: bool = False) -> None:
def remove_tags(self, tags: Union[List[str], Set[str], Tuple[str]], group_tags: bool = False) -> None:
"""
Removes the specified tags from the run.
Expand All @@ -525,10 +528,10 @@ def log(
self,
step: Optional[Union[float, int]] = None,
timestamp: Optional[datetime] = None,
configs: Optional[Dict[str, Union[float, bool, int, str, datetime]]] = None,
configs: Optional[Dict[str, Union[float, bool, int, str, datetime, list, set, tuple]]] = None,
metrics: Optional[Dict[str, Union[float, int]]] = None,
tags_add: Optional[Dict[str, Union[List[str], Set[str]]]] = None,
tags_remove: Optional[Dict[str, Union[List[str], Set[str]]]] = None,
tags_add: Optional[Dict[str, Union[List[str], Set[str], Tuple[str]]]] = None,
tags_remove: Optional[Dict[str, Union[List[str], Set[str], Tuple[str]]]] = None,
) -> None:
"""
See one of the following instead:
Expand Down Expand Up @@ -557,10 +560,12 @@ def log(
verify_collection_type("`tags_add` keys", list(tags_add.keys()), str)
verify_collection_type("`tags_remove` keys", list(tags_remove.keys()), str)

verify_collection_type("`configs` values", list(configs.values()), (float, bool, int, str, datetime))
verify_collection_type(
"`configs` values", list(configs.values()), (float, bool, int, str, datetime, list, set, tuple)
)
verify_collection_type("`metrics` values", list(metrics.values()), (float, int))
verify_collection_type("`tags_add` values", list(tags_add.values()), (list, set))
verify_collection_type("`tags_remove` values", list(tags_remove.values()), (list, set))
verify_collection_type("`tags_add` values", list(tags_add.values()), (list, set, tuple))
verify_collection_type("`tags_remove` values", list(tags_remove.values()), (list, set, tuple))

# Don't log anything after we've been stopped. This allows continuing the training script
# after a non-recoverable error happened. Note we don't to use self._lock in this check,
Expand Down
6 changes: 3 additions & 3 deletions src/neptune_scale/core/metadata_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def __init__(
run_id: str,
step: Optional[Union[int, float]],
timestamp: datetime,
fields: Dict[str, Union[float, bool, int, str, datetime]],
fields: Dict[str, Union[float, bool, int, str, datetime, list, set, tuple]],
metrics: Dict[str, float],
add_tags: Dict[str, Union[List[str], Set[str]]],
remove_tags: Dict[str, Union[List[str], Set[str]]],
add_tags: Dict[str, Union[List[str], Set[str], Tuple[str]]],
remove_tags: Dict[str, Union[List[str], Set[str], Tuple[str]]],
max_message_bytes_size: int = 1024 * 1024,
):
self._step = None if step is None else make_step(number=step)
Expand Down
5 changes: 2 additions & 3 deletions src/neptune_scale/core/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ def make_value(value: Union[Value, float, str, int, bool, datetime, List[str], S
return Value(string=value)
elif isinstance(value, datetime):
return Value(timestamp=datetime_to_proto(value))
elif isinstance(value, (list, set)):
fv = Value(string_set=StringSet(values=value))
return fv
elif isinstance(value, (list, set, tuple)):
return Value(string_set=StringSet(values=value))
else:
raise ValueError(f"Unsupported ingest field value type: {type(value)}")

Expand Down
21 changes: 17 additions & 4 deletions tests/unit/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,18 @@ def test_tags(api_token):

# then
with Run(project=project, api_token=api_token, run_id=run_id, mode="disabled") as run:
run.add_tags(["tag1"])
run.add_tags(["tag2"], group_tags=True)
run.remove_tags(["tag3"])
run.remove_tags(["tag4"], group_tags=True)
run.add_tags(["tag1", "tag2"])
run.add_tags(["tag3", "tag4"], group_tags=True)
run.remove_tags(["tag1"])
run.remove_tags(["tag3"], group_tags=True)
run.add_tags(("tag5", "tag6"))
run.remove_tags(("tag5", "tag6"))
run.add_tags(("tag5", "tag6"), group_tags=True)
run.remove_tags(("tag5", "tag6"), group_tags=True)
run.add_tags({"tag7", "tag8"})
run.remove_tags({"tag7", "tag8"})
run.add_tags({"tag7", "tag8"}, group_tags=True)
run.remove_tags({"tag7", "tag8"}, group_tags=True)

# and
assert True
Expand Down Expand Up @@ -171,6 +179,11 @@ def test_log_configs(api_token):
# then
with Run(project=project, api_token=api_token, run_id=run_id, mode="disabled") as run:
run.log_configs({"int": 1})
run.log_configs({"float": 3.14})
run.log_configs({"bool": True})
run.log_configs({"string": "test"})
run.log_configs({"datetime": datetime.now()})
run.log_configs({"string_list": ["a", "b", "c"]})

# and
assert True
Expand Down

0 comments on commit ae56869

Please sign in to comment.