fix(metrics): honor agg_name in get_mean_grouping

AN
3d ago
+21-3

Summary

get_mean_grouping() takes an agg_name argument, documented as the string used in the export filename, but the function overwrote it in every eval_name branch (agg_name = "cct", "element-type", "object-detection"). Whatever the caller passed was ignored and the file was always written as all-<group_by>-agg-<default>.tsv.

Minimal repro on main:

import os, tempfile
import pandas as pd
from unstructured.metrics.evaluate import get_mean_grouping

df = pd.DataFrame({
    "filename": ["a.pdf", "b.pptx"],
    "doctype": ["pdf", "pptx"],
    "connector": ["c1", "c1"],
    "cct-accuracy": [0.9, 0.8],
    "cct-%missing": [0.01, 0.02],
})
out = tempfile.mkdtemp()
get_mean_grouping("doctype", df, out, "text_extraction", agg_name="custom")
print(os.listdir(out))  # ['all-doctype-agg-cct.tsv'], expected ['all-doctype-agg-custom.tsv']

The fix keeps the per-metric value as the default and uses it only when agg_name is not given (agg_name = agg_name or "cct", and the same for the other two branches). Callers that do not pass agg_name get the same filenames as before.

Validation

Added test_get_mean_grouping_uses_agg_name_in_export_filename, parametrized over text_extraction and element_type. It writes to tmp_path and checks that the only exported file is all-doctype-agg-custom.tsv.

$ python -m pytest test_unstructured/metrics/test_evaluate.py -q -k agg_name    # main + new test only
2 failed, 26 deselected
$ MAX_PROCESSES=2 python -m pytest test_unstructured/metrics/test_evaluate.py -q    # this branch
28 passed
$ ruff check && ruff format --check    # ruff 0.15.10, both changed files
All checks passed!
2 files already formatted

The new test fails on main and passes with the fix. I ran the file on Windows with MAX_PROCESSES=2; with the default pool size the existing calculator tests sometimes hit MemoryError in worker processes on my machine, which is unrelated to this change.

Checklist

  • The PR title is a user-facing release-note entry with a conventional commit prefix.
  • Apply a category label (bug), or ask a maintainer to apply it.
  • Not a breaking change.

This change was prepared with the help of an AI assistant (Claude), and I reviewed and tested it.