public abstract class Metric<X>
extends java.lang.Object
Interface for common aspects of metrics, which are used to measure individual or per-user results. Each metric can measure results or users, and contribute aggregate values to the global aggregate measurement table.
This class should not be extended directly; rather, one of its subclasses should be extended. This hierarchy uses abstract classes instead of interfaces for two reasons: first, to provide useful default behavior, and second, because implementing multiple types of metrics in a single class is likely to be erroneous.
Metrics use contexts to track and accumulate data for experimental conditions. For each experimental condition (algorithm / data set pair), the evaluator will do the following:
createContext(AlgorithmInstance, DataSet, RecommenderEngine)
.getAggregateMeasurements(Object)
and add them
to the global output file.The context will generally consist of accumulators for the aggregate results reported by a metric over the entire experimental condition, such as the average of all user measurements. It may also contain additional relevant information, such as anything needed from the algorithm and data set for the measurements, or additional output tables for recording extra data.
Metrics themselves are generally stateless, with all state contained in the context. In this case, there is a single instance of the metric, or an instance per parameterization.
Metrics may be used from multiple threads. LensKit may use multiple threads with the same context.
Modifier | Constructor and Description |
---|---|
protected |
Metric(java.util.List<java.lang.String> labels,
java.util.List<java.lang.String> aggLabels) |
Modifier and Type | Method and Description |
---|---|
X |
createContext(AlgorithmInstance algorithm,
DataSet dataSet,
RecommenderEngine engine)
Create the context for an experimental condition (algorithm/data set pair).
|
java.util.List<java.lang.String> |
getAggregateColumnLabels()
Get labels for the aggregate columns output by this metric.
|
MetricResult |
getAggregateMeasurements(X context)
Get the aggregate results from an accumulator.
|
java.util.List<java.lang.String> |
getColumnLabels()
Get the labels for the per-user or per-result columns output by this metric.
|
java.util.Set<java.lang.Class<?>> |
getRequiredRoots()
Get the classes on which this metric depends.
|
protected Metric(java.util.List<java.lang.String> labels, java.util.List<java.lang.String> aggLabels)
public java.util.List<java.lang.String> getAggregateColumnLabels()
Get labels for the aggregate columns output by this metric.
public java.util.List<java.lang.String> getColumnLabels()
Get the labels for the per-user or per-result columns output by this metric.
public java.util.Set<java.lang.Class<?>> getRequiredRoots()
Get the classes on which this metric depends. These will be added to the roots of each algorithm configuration.
@Nullable public X createContext(AlgorithmInstance algorithm, DataSet dataSet, RecommenderEngine engine)
Create the context for an experimental condition (algorithm/data set pair). The default implementation returns null
.
Note: Contexts must be thread-safe, in that multiple concurrent calls to the appropriate user-measurement function with the same context must be safe. This can be handled either by the context itself, or by the user-measurement function.
algorithm
- The algorithm.dataSet
- The data set.engine
- The LensKit recommender engine, if applicable. This can be null for an external algorithm that does not provide a LensKit recommender.null
.@Nonnull public MetricResult getAggregateMeasurements(X context)
Get the aggregate results from an accumulator. The default implementation returns MetricResult.empty()
.
context
- The context for an experimental condition.