bibraeval.Evaluator¶
- class bibraeval.Evaluator(gt_path, pred_path, schema, mode='field-avg', field_metric_config=None, drop_mismatched_doc_ids=False)¶
Bases:
objectLoads record collections, compares them and returns summarised metrics.
Without a field_metric_config, all fields are scored by exact matching with uniform weights.
Configure a one-call evaluation workflow.
- Parameters:
gt_path (str | Path) – Directory of JSON records or a JSONL file for ground truth.
pred_path (str | Path) – Directory of JSON records or a JSONL file for predictions.
schema (type[BaseModel]) – Pydantic model used to validate each record’s metadata.
mode (Literal['doc-avg', 'field-avg', 'micro-avg']) – Summary mode:
doc-avg,field-avg(the default), ormicro-avg.field_metric_config (FieldMetricMapping | dict[str, Any] | str | Path | None) – Per-field configuration as a
FieldMetricMapping, flat mapping, or YAML path. With no configuration all model fields use exact matching and weight 1.0.drop_mismatched_doc_ids (bool) – If true, evaluate only IDs present in both datasets; otherwise mismatched IDs raise
ValueError.
- Raises:
ValueError – If
modeis unsupported.
- compute_metrics(results_dir=None)¶
Load records, compare them, and return summary metrics.
- Parameters:
results_dir (str | Path | None) – If specified, save the evaluation results as YAML and CSV
directory. (files in this)
- Returns:
A polars dataframe with one row per
metric, summarised according to the specifiedmode. The dataframe’s columns are:metriccontaining the metric name,modecontaining the aggregation mode,valuecontaining the aggregated metric value,supportcontaining the number or weighted average of observations contributing to the metric. Its interpretation depends on the aggregation mode and metric.
- Return type:
DataFrame
Notes
Each call starts a new evaluation and clears results from previous runs. If the computation fails, no results from the failed run are available through the getter methods. Failures during results export do not invalidate the computed results.
- get_comparison_matrix()¶
Return the comparison matrix for the current evaluation.
- Returns:
A Polars DataFrame with one row per document and metadata field, containing the following columns:
doc_id: the document ID,field_name: the name of the metadata field,gt_present: whether the ground truth value is present,pred_present: whether the predicted value is present,gt_value: the ground truth value for the field,pred_value: the predicted value for the field,cell_agreement: how much the ground truth and predicted values agreeaccording to the field’s metric,
weight: the weight assigned to the field for metric computation.
- Raises:
ValueError – If the comparison matrix is not available, which occurs if
compute_metrics()has not been called yet.- Return type:
DataFrame
- get_intermediate_results()¶
Return the intermediate results for the current evaluation.
- Returns:
A Polars DataFrame containing precision, recall and F1 per group. The exact structure of the dataframe depends on the aggregation mode.
- Raises:
ValueError – If the intermediate results are not available, which occurs if
compute_metrics()has not been called yet.- Return type:
DataFrame
- save_results(target_dir)¶
Save evaluation results in the given target directory: - Summary as a YAML file under summary_metrics.yaml. - Intermediate results as a CSV file under intermediate_results.csv. Note: intermediate results are not saved in micro-avg mode since this mode does not have intermediate results. - Comparison matrix as a CSV file under comparison_matrix.csv.
- Parameters:
target_dir (str | Path)
- Return type:
None