Skip to main content

Fact-level embedding metric for evaluating chest X-ray radiology report generation.

Project description

CXRFEScore

Fact-level embedding metric for evaluating chest X-ray radiology report generation.

CXRFEScore extracts factual statements from reports with a T5 fact extractor, embeds them with CXRFE (or compatible CXR-BERT models), and scores hypothesis/reference pairs via soft bipartite matching of fact embeddings.

Based on:

Messina et al., Extracting and Encoding: Leveraging Large Language Models and Medical Knowledge to Enhance Radiological Text Representation, Findings of ACL 2024.

Install

pip install cxrfescore

Optional visualization support:

pip install cxrfescore[viz]

Python: 3.10+

Runtime dependencies (installed automatically)

Package Role
torch Model inference
transformers Load Hugging Face models
numpy Arrays / aggregation
scikit-learn Cosine similarity
nltk Sentence splitting
tqdm Progress bars
platformdirs Default cache directory

Optional: matplotlib via cxrfescore[viz] for visualize_fact_similarity.

Additional requirements (not pip packages)

On first use the metric downloads:

Supported alternate encoders:

  • microsoft/BiomedVLP-CXR-BERT-specialized
  • microsoft/BiomedVLP-BioViL-T
  • StanfordAIMI/SRR-BERT-Leaves (CLS embeddings only; label head unused)

A GPU is recommended but not required. CPU works and is slower.

Known-good stack from development: PyTorch 2.x + transformers 4.x / 5.x.

Quick start

from cxrfescore import CXRFEScore

metric = CXRFEScore(device="cuda")  # or "cpu"

hyps = [
    "There is a small right pleural effusion. The heart size is normal.",
]
refs = [
    "Small right pleural effusion. Normal heart size.",
]

result = metric(hyps, refs)
print(result["mean_similarity"])
print(result["per_pair_similarity"])

Extract facts or embeddings only

extract_facts treats each input string as a full report: sentence-split → T5 fact extraction → unique-fact aggregation (same pipeline as scoring).

facts_per_report = metric.extract_facts(hyps)
embeddings = metric.embed_facts(facts_per_report[0])  # shape (num_facts, dim)

Caching

With use_cache=True (default), results are kept in memory during the session:

  • Sentence→facts: {cache_dir}/sent_to_facts.pkl (shared across encoders)
  • Fact→embedding: {cache_dir}/embeddings/<encoder_name>/fact_to_embedding.pkl (per encoder)

Disk writes are not automatic. Call save_cache() explicitly after a large batch or at the end of a job:

metric = CXRFEScore(use_cache=True, cache_dir="/path/to/cache")
# ... score / extract / embed ...
metric.save_cache()

Disable with use_cache=False.

Inspect what is already on disk:

from cxrfescore import inspect_cache

inspect_cache()                 # default cache location
# or: metric.inspect_cache()    # uses this instance's cache_dir

Visualization

# requires: pip install cxrfescore[viz]
metric.visualize_fact_similarity(ref_report=refs[0], cand_report=hyps[0])

Examples

File Purpose
examples/colab_smoke.md Full Colab smoke + adversarial pairs
examples/minimal_cxrfe.md Default CXRFE encoder
examples/minimal_cxr_bert_specialized.md CXR-BERT specialized
examples/minimal_biovil_t.md BioViL-T
examples/minimal_srr_bert_leaves.md SRR-BERT-Leaves CLS
examples/minimal_extract_embed.md extract_facts / embed_facts

Try it on Google Colab

After publishing, install in Colab (GPU runtime recommended):

!pip install cxrfescore

For a pre-release check against TestPyPI:

!pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ "cxrfescore==0.2.3"

Citation

If you use CXRFEScore, please cite:

Pablo Messina, Rene Vidal, Denis Parra, Alvaro Soto, and Vladimir Araujo. 2024. Extracting and Encoding: Leveraging Large Language Models and Medical Knowledge to Enhance Radiological Text Representation. In Findings of the Association for Computational Linguistics: ACL 2024, pages 3955–3986, Bangkok, Thailand. Association for Computational Linguistics.

@inproceedings{messina-etal-2024-extracting,
    title = "Extracting and Encoding: Leveraging Large Language Models and Medical Knowledge to Enhance Radiological Text Representation",
    author = "Messina, Pablo  and
      Vidal, Rene  and
      Parra, Denis  and
      Soto, Alvaro  and
      Araujo, Vladimir",
    editor = "Ku, Lun-Wei  and
      Martins, Andre  and
      Srikumar, Vivek",
    booktitle = "Findings of the Association for Computational Linguistics: ACL 2024",
    month = aug,
    year = "2024",
    address = "Bangkok, Thailand",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2024.findings-acl.236/",
    doi = "10.18653/v1/2024.findings-acl.236",
    pages = "3955--3986"
}

Paper: ACL Anthology · PDF · arXiv

Development

pip install -e ".[dev]"
pytest                          # unit + mocked tests (default)
pytest -m integration           # downloads HF models; needs network / GPU optional
python -m build

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cxrfescore-0.2.3.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cxrfescore-0.2.3-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file cxrfescore-0.2.3.tar.gz.

File metadata

  • Download URL: cxrfescore-0.2.3.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for cxrfescore-0.2.3.tar.gz
Algorithm Hash digest
SHA256 3bc7ee6b36657fb122bad9fea94b84ec04d74e04b94cf0c517d0ed83c3e9e85f
MD5 08b62beb3e5cffab35078aa47ccb1216
BLAKE2b-256 639737afdb68528bea6cb7caa9056820adbe52936e8f86c6b73c64afe3d2e087

See more details on using hashes here.

File details

Details for the file cxrfescore-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: cxrfescore-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for cxrfescore-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 465918347b21a664acd3ab804fb902d12b6ce59dd74a3446a706593c0b20651f
MD5 bac048d0015c4a39b05a5f3000699187
BLAKE2b-256 6931006130456708ba05c799d968e75048461615cadd073ed3b100a63499b44e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page