README
liblaf.cherries.core.metrics
¶
Type Aliases:
-
MetricsLike–Nested mapping accepted by batch metric logging.
Classes:
-
Metric–In-memory samples for one scalar metric.
-
MetricPluginProtocol–Hook surface for plugins that receive scalar metrics.
-
MetricsManager–Store scalar metrics and mirror them to plugins.
MetricsLike
¶
MetricsLike = Mapping[str, SupportsFloat | MetricsLike]
Nested mapping accepted by batch metric logging.
Metric
¶
In-memory samples for one scalar metric.
Values, steps, and timestamps are stored in compact arrays and materialized as a Polars dataframe only when requested.
Parameters:
-
name(str) –Metric name.
-
value(array[float], default:array('d')) –Metric values as doubles.
-
step(array[int], default:array('L')) –Metric steps as unsigned integers.
-
timestamp_ms(array[int], default:array('Q')) –Sample timestamps as milliseconds since the Unix epoch.
Methods:
-
append–Append one metric sample.
-
to_polars–Convert samples to a dataframe with
name,value,step, andtime.
Attributes:
-
name(str) –Metric name.
-
step(array[int]) –Metric steps as unsigned integers.
-
timestamp_ms(array[int]) –Sample timestamps as milliseconds since the Unix epoch.
-
value(array[float]) –Metric values as doubles.
step
class-attribute
instance-attribute
¶
Metric steps as unsigned integers.
timestamp_ms
class-attribute
instance-attribute
¶
Sample timestamps as milliseconds since the Unix epoch.
value
class-attribute
instance-attribute
¶
Metric values as doubles.
append
¶
Append one metric sample.
Parameters:
Source code in src/liblaf/cherries/core/metrics/_struct.py
to_polars
¶
Convert samples to a dataframe with name, value, step, and time.
Source code in src/liblaf/cherries/core/metrics/_struct.py
MetricPluginProtocol
¶
Bases: Protocol
flowchart TD
liblaf.cherries.core.metrics.MetricPluginProtocol[MetricPluginProtocol]
click liblaf.cherries.core.metrics.MetricPluginProtocol href "" "liblaf.cherries.core.metrics.MetricPluginProtocol"
Hook surface for plugins that receive scalar metrics.
Methods:
-
log_metric–Record one metric sample.
-
log_metrics–Record a batch of already-flattened metric samples.
MetricsManager
¶
Store scalar metrics and mirror them to plugins.
Metrics are kept in memory as named series and can be exported as Polars dataframes. Batch logging accepts nested mappings and stores them with slash-delimited names.
Parameters:
-
plugins(MetricPluginProtocol) –Plugin delegate that receives metric events.
-
metrics(dict[str, Metric], default:<class 'dict'>) –dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
-
step(int, default:0) –Default step used when a log call does not provide one.
Methods:
-
get_metric–Return one metric as a Polars dataframe.
-
get_metrics–Return selected metrics concatenated into one dataframe.
-
log_metric–Log one scalar metric.
-
log_metrics–Log multiple scalar metrics.
Attributes:
-
metrics(dict[str, Metric]) –Metric series by name.
-
plugins(MetricPluginProtocol) –Plugin delegate that receives metric events.
-
step(int) –Default step used when a log call does not provide one.
metrics
class-attribute
instance-attribute
¶
Metric series by name.
plugins
instance-attribute
¶
plugins: MetricPluginProtocol
Plugin delegate that receives metric events.
step
class-attribute
instance-attribute
¶
step: int = 0
Default step used when a log call does not provide one.
get_metric
¶
get_metric(name: str) -> DataFrame
get_metrics
¶
Return selected metrics concatenated into one dataframe.
Parameters:
-
names(Iterable[str] | None, default:None) –Metric names to include. When omitted, all known metrics are returned.
Raises:
-
KeyError–If any requested name has not been logged.
Source code in src/liblaf/cherries/core/metrics/_manager.py
log_metric
¶
log_metric(
name: str,
value: SupportsFloat,
*,
step: SupportsInt | None = None,
time: datetime | None = None,
) -> None
Log one scalar metric.
Parameters:
-
name(str) –Metric name.
-
value(SupportsFloat) –Numeric value convertible with
float(). -
step(SupportsInt | None, default:None) –Optional step override. Defaults to
step. -
time(datetime | None, default:None) –Optional timestamp. Defaults to the current local time.
Source code in src/liblaf/cherries/core/metrics/_manager.py
log_metrics
¶
log_metrics(
metrics: MetricsLike,
*,
step: SupportsInt | None = None,
time: datetime | None = None,
) -> None
Log multiple scalar metrics.
Nested mappings are flattened with /, so {"train": {"loss": 0.5}}
is stored as train/loss.
Parameters:
-
metrics(MetricsLike) –Metric values to log.
-
step(SupportsInt | None, default:None) –Optional step override shared by all values.
-
time(datetime | None, default:None) –Optional timestamp shared by all values.