2019-03-26 00:27:59 -07:00
|
|
|
import numpy as np
|
|
|
|
import pprint
|
2022-04-12 07:50:09 +02:00
|
|
|
from typing import Any, Mapping
|
2019-03-26 00:27:59 -07:00
|
|
|
|
2019-05-20 16:46:05 -07:00
|
|
|
from ray.rllib.policy.sample_batch import SampleBatch, MultiAgentBatch
|
2022-05-24 22:14:25 -07:00
|
|
|
from ray.rllib.utils.annotations import DeveloperAPI
|
2019-03-26 00:27:59 -07:00
|
|
|
|
|
|
|
_printer = pprint.PrettyPrinter(indent=2, width=60)
|
|
|
|
|
|
|
|
|
2022-05-24 22:14:25 -07:00
|
|
|
@DeveloperAPI
|
2021-11-01 21:46:02 +01:00
|
|
|
def summarize(obj: Any) -> Any:
|
2019-03-26 00:27:59 -07:00
|
|
|
"""Return a pretty-formatted string for an object.
|
|
|
|
|
|
|
|
This has special handling for pretty-formatting of commonly used data types
|
|
|
|
in RLlib, such as SampleBatch, numpy arrays, etc.
|
2021-11-01 21:46:02 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
obj: The object to format.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The summarized object.
|
2019-03-26 00:27:59 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
return _printer.pformat(_summarize(obj))
|
|
|
|
|
|
|
|
|
|
|
|
def _summarize(obj):
|
2020-06-11 17:37:15 +10:00
|
|
|
if isinstance(obj, Mapping):
|
2019-03-26 00:27:59 -07:00
|
|
|
return {k: _summarize(v) for k, v in obj.items()}
|
|
|
|
elif hasattr(obj, "_asdict"):
|
|
|
|
return {
|
|
|
|
"type": obj.__class__.__name__,
|
|
|
|
"data": _summarize(obj._asdict()),
|
|
|
|
}
|
|
|
|
elif isinstance(obj, list):
|
|
|
|
return [_summarize(x) for x in obj]
|
|
|
|
elif isinstance(obj, tuple):
|
|
|
|
return tuple(_summarize(x) for x in obj)
|
|
|
|
elif isinstance(obj, np.ndarray):
|
2019-07-16 22:37:57 -07:00
|
|
|
if obj.size == 0:
|
|
|
|
return _StringValue("np.ndarray({}, dtype={})".format(obj.shape, obj.dtype))
|
2022-01-10 11:22:55 +01:00
|
|
|
elif obj.dtype == object or obj.dtype.type is np.str_:
|
2019-03-26 00:27:59 -07:00
|
|
|
return _StringValue(
|
|
|
|
"np.ndarray({}, dtype={}, head={})".format(
|
|
|
|
obj.shape, obj.dtype, _summarize(obj[0])
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
|
|
|
)
|
2019-03-26 00:27:59 -07:00
|
|
|
else:
|
|
|
|
return _StringValue(
|
|
|
|
"np.ndarray({}, dtype={}, min={}, max={}, mean={})".format(
|
|
|
|
obj.shape,
|
|
|
|
obj.dtype,
|
|
|
|
round(float(np.min(obj)), 3),
|
|
|
|
round(float(np.max(obj)), 3),
|
|
|
|
round(float(np.mean(obj)), 3),
|
|
|
|
)
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
2019-03-26 00:27:59 -07:00
|
|
|
elif isinstance(obj, MultiAgentBatch):
|
|
|
|
return {
|
|
|
|
"type": "MultiAgentBatch",
|
|
|
|
"policy_batches": _summarize(obj.policy_batches),
|
|
|
|
"count": obj.count,
|
|
|
|
}
|
|
|
|
elif isinstance(obj, SampleBatch):
|
|
|
|
return {
|
|
|
|
"type": "SampleBatch",
|
|
|
|
"data": {k: _summarize(v) for k, v in obj.items()},
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2020-01-02 17:42:13 -08:00
|
|
|
class _StringValue:
|
2019-03-26 00:27:59 -07:00
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.value
|