Skip to content
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

[Tune] Add repr for ResultGrid class #31941

Merged
merged 7 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions python/ray/air/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Result:
log_dir: Optional[Path]
metrics_dataframe: Optional["pd.DataFrame"]
best_checkpoints: Optional[List[Tuple[Checkpoint, Dict[str, Any]]]]
_items_to_repr = ["metrics", "error", "log_dir"]
_items_to_repr = ["error", "metrics", "log_dir", "checkpoint"]

@property
def config(self) -> Optional[Dict[str, Any]]:
Expand All @@ -53,14 +53,29 @@ def config(self) -> Optional[Dict[str, Any]]:
return None
return self.metrics.get("config", None)

def __repr__(self):
def _repr_with_indent(self, offset: int = 0) -> str:
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
"""Construct the representation with specified number of space indent."""
from ray.tune.result import AUTO_RESULT_KEYS

shown_attributes = {k: self.__dict__[k] for k in self._items_to_repr}
if self.error:
shown_attributes["error"] = type(self.error).__name__
else:
shown_attributes.pop("error")

if self.metrics:
shown_attributes["metrics"] = {
k: v for k, v in self.metrics.items() if k not in AUTO_RESULT_KEYS
}
kws = [f"{key}={value!r}" for key, value in shown_attributes.items()]
return "{}({})".format(type(self).__name__, ", ".join(kws))

cls_indent = " " * offset
kws_indent = " " * (offset + 2)

kws = [
f"{kws_indent}{key}={value!r}" for key, value in shown_attributes.items()
]
kws_repr = ",\n".join(kws)
return "{0}{1}(\n{2}\n{0})".format(cls_indent, type(self).__name__, kws_repr)

def __repr__(self) -> str:
return self._repr_with_indent(offset=0)
18 changes: 14 additions & 4 deletions python/ray/tune/result_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def __init__(
experiment_analysis: ExperimentAnalysis,
):
self._experiment_analysis = experiment_analysis
self._results = list()

if experiment_analysis:
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
self._results = [
self._trial_to_result(trail)
for trail in self._experiment_analysis.trials
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
]

def get_best_result(
self,
Expand Down Expand Up @@ -178,13 +185,11 @@ def get_dataframe(
)

def __len__(self) -> int:
return len(self._experiment_analysis.trials)
return len(self._results)

def __getitem__(self, i: int) -> Result:
"""Returns the i'th result in the grid."""
return self._trial_to_result(
self._experiment_analysis.trials[i],
)
return self._results[i]

@property
def errors(self):
Expand Down Expand Up @@ -240,3 +245,8 @@ def _trial_to_result(self, trial: Trial) -> Result:
best_checkpoints=best_checkpoints,
)
return result

def __repr__(self) -> str:
all_results_repr = [result._repr_with_indent(2) for result in self]
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
all_results_repr = ",\n".join(all_results_repr)
return f"ResultGrid<[\n{all_results_repr}\n]>"
56 changes: 56 additions & 0 deletions python/ray/tune/tests/test_result_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,62 @@ def f(config):
assert not any(key in representation for key in AUTO_RESULT_KEYS)


def test_result_grid_repr(ray_start_2_cpus, tmpdir):
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
from ray.air.result import Result
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved

results = list()
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
results.append(
Result(
metrics={"loss": 1.0},
checkpoint=Checkpoint(data_dict={"weight": 1.0}),
log_dir=Path("./log_1"),
error=None,
metrics_dataframe=None,
best_checkpoints=None,
justinvyu marked this conversation as resolved.
Show resolved Hide resolved
)
)
results.append(
Result(
metrics={"loss": 2.0},
checkpoint=Checkpoint(data_dict={"weight": 2.0}),
log_dir=Path("./log_2"),
error=RuntimeError(),
metrics_dataframe=None,
best_checkpoints=None,
justinvyu marked this conversation as resolved.
Show resolved Hide resolved
)
)

result_grid = ResultGrid(experiment_analysis=None)
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
result_grid._results = results

representation = result_grid.__repr__()

from ray.tune.result import AUTO_RESULT_KEYS

assert len(result_grid) == 2
assert not any(key in representation for key in AUTO_RESULT_KEYS)
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved

gold_representation = """ResultGrid<[
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved
Result(
metrics={'loss': 1.0},
log_dir=PosixPath('log_1'),
checkpoint=Checkpoint(data_dict={'weight': 1.0})
),
Result(
error='RuntimeError',
metrics={'loss': 2.0},
log_dir=PosixPath('log_2'),
checkpoint=Checkpoint(data_dict={'weight': 2.0})
)
]>"""

assert representation == gold_representation
assert representation.count("metrics=") == 2
assert representation.count("log_dir=") == 2
assert representation.count("checkpoint=") == 2
assert representation.count("error=") == 1 and "RuntimeError" in representation
woshiyyya marked this conversation as resolved.
Show resolved Hide resolved


def test_no_metric_mode(ray_start_2_cpus):
def f(config):
tune.report(x=1)
Expand Down