stdlib: Add SparseHist to PyStats
This is inclusive of tests to ensure they have implemented correctly. Change-Id: I5c84d5ffdb7b914936cfd86ca012a7b141eeaf42
This commit is contained in:
@@ -253,3 +253,31 @@ class Distribution(Vector):
|
||||
# These check some basic conditions of a distribution.
|
||||
assert self.bin_size >= 0
|
||||
assert self.num_bins >= 1
|
||||
|
||||
|
||||
class SparseHist(Vector):
|
||||
"""A Sparse Histogram of values. A sparse histogram simply counts the "
|
||||
frequency of each value in a sample. Ergo, it is, ineffect an disctionary
|
||||
of values mapped to their count"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
value: Dict[float, Scalar],
|
||||
description: Optional[str] = None,
|
||||
):
|
||||
super().__init__(
|
||||
value=value,
|
||||
type="SparseHist",
|
||||
description=description,
|
||||
)
|
||||
|
||||
def size(self) -> int:
|
||||
"""The number of unique sampled values."""
|
||||
return len(self.value)
|
||||
|
||||
def count(self) -> int:
|
||||
"""
|
||||
Returns the total number of samples.
|
||||
"""
|
||||
assert self.value != None
|
||||
return sum(self.value.values())
|
||||
|
||||
@@ -140,6 +140,8 @@ def __get_statistic(statistic: _m5.stats.Info) -> Optional[Statistic]:
|
||||
return __get_vector(statistic)
|
||||
elif isinstance(statistic, _m5.stats.Vector2dInfo):
|
||||
return __get_vector2d(statistic)
|
||||
elif isinstance(statistic, _m5.stats.SparseHistInfo):
|
||||
return __get_sparse_hist(statistic)
|
||||
|
||||
return None
|
||||
|
||||
@@ -268,6 +270,24 @@ def __get_vector2d(statistic: _m5.stats.Vector2dInfo) -> Vector2d:
|
||||
return Vector2d(value=vector_rep, type="Vector2d", description=description)
|
||||
|
||||
|
||||
def __get_sparse_hist(statistic: _m5.stats.SparseHistInfo) -> SparseHist:
|
||||
description = statistic.desc
|
||||
value = statistic.values
|
||||
|
||||
parsed_values = {}
|
||||
for val in value:
|
||||
parsed_values[val] = Scalar(
|
||||
value=value[val],
|
||||
unit=statistic.unit,
|
||||
datatype=StorageType["f64"],
|
||||
)
|
||||
|
||||
return SparseHist(
|
||||
value=parsed_values,
|
||||
description=description,
|
||||
)
|
||||
|
||||
|
||||
def _prepare_stats(group: _m5.stats.Group):
|
||||
"""
|
||||
Prepares the statistics for dumping.
|
||||
|
||||
@@ -78,6 +78,7 @@ cast_stat_info(const statistics::Info *info)
|
||||
TRY_CAST(statistics::VectorInfo);
|
||||
TRY_CAST(statistics::Vector2dInfo);
|
||||
TRY_CAST(statistics::DistInfo);
|
||||
TRY_CAST(statistics::SparseHistInfo);
|
||||
|
||||
return py::cast(info);
|
||||
|
||||
@@ -195,6 +196,15 @@ pybind_init_stats(py::module_ &m_native)
|
||||
.def_readonly("value", &statistics::Vector2dInfo::cvec)
|
||||
;
|
||||
|
||||
py::class_<statistics::SparseHistInfo, statistics::Info,
|
||||
std::unique_ptr<statistics::SparseHistInfo, py::nodelete>>(
|
||||
m, "SparseHistInfo")
|
||||
.def_property_readonly("values", //A Dict[float, int] of sample & count
|
||||
[](const statistics::SparseHistInfo &info) {
|
||||
return info.data.cmap;
|
||||
})
|
||||
;
|
||||
|
||||
py::class_<statistics::FormulaInfo, statistics::VectorInfo,
|
||||
std::unique_ptr<statistics::FormulaInfo, py::nodelete>>(
|
||||
m, "FormulaInfo")
|
||||
|
||||
Reference in New Issue
Block a user