swarmsim.metrics.JinjaMetric#
- class swarmsim.metrics.JinjaMetric(name='JinjaMetric', history=None, metric=None, metrics=None, template=None, expression=None, eval_condition=None, save_condition=None, default='__unset__', default_aggregation=None)[source]#
Combine multiple metrics programmatically using a Jinja expression.
Evaluation Order on
JinjaMetric.calculate():Evaluate the
templateandeval_conditionand return if False.Call
Metric.calculate()on the submetrics.Render the
templateand save the context.Evaluate the
expression.Evaluate the
save_conditionand return if False.Call
Metric.set_value().
You may reference
self,world,metric, andmetricsin thetemplate,expression,eval_condition, andsave_condition.. Additionally, any variables set in thetemplatewill be available in the expressions.Note that metrics in
world.metricsare calculated in order (i.e. the order specified/added). Thus, any metrics before thisJinjaMetricwill be up-to-date when this metric is calculated, but any metrics ordered afterwards may be at least one time step behind.- Parameters:
name (str) – The name of the JinjaMetric.
metric (Metric | dict) – A
Metricor a dict that can be used to initialize aMetric. Not added toworld.metrics. Can be referenced asself.metricormetricin the Jinja expression.metrics (Iterable[Metric | dict]) – Additional metrics to use in the Jinja expression. Not added to
world.metrics. This becomes a list which can be referenced asself.metricsormetricsin the Jinja expression.template (str) – A Jinja template that can be used to set variables that can be used in the expression. The rendered result of the template is not used. The template is always evaluated, so variables exported can be used in
expression,eval_condition, andsave_condition.expression (str) – The Jinja expression to use. The return value becomes the value of the metric.
eval_condition (Callable | str | None) – A Callable or Jinja expression that evaluates to a boolean. If the condition evaluates to False, the submetrics and
self.expressionwill not be evaluated. If None, the Jinja expression will always be evaluated.save_condition (Callable | str | None) – A Callable or Jinja expression that evaluates to a boolean. If the condition evaluates to False,
self.set_valuewill not be called and the result ofself.expressionwill not be saved. If None, the metric will always be saved.default (Any, default='__unset__') – The initial value of the metric. If
'__unset__', the value will be inherited fromMetric.default_aggregation (str) – The default aggregation to use for the metric.
- Attributes:
- average
- default_aggregation
- module
- value
Methods
as_config_dict
attach_world
calculate
calculate_submetrics
draw
eval_template
make_module
out_average
out_current
reset
set_value
setup_submetrics
- __init__(name='JinjaMetric', history=None, metric=None, metrics=None, template=None, expression=None, eval_condition=None, save_condition=None, default='__unset__', default_aggregation=None)[source]#
Methods
__init__([name, history, metric, metrics, ...])as_config_dict()attach_world(world)calculate()calculate_submetrics()draw(screen[, zoom])eval_template(expression)make_module(**kwargs)out_average()out_current()reset()set_value(value)setup_submetrics()Attributes
averagedefault_aggregationmodulevalue