Skip to main content

spacy-matching

py3.10

Background

In Germany's cancer registries, substances are reported in a free text field, e.g., "Interferon alpha-2a weekly i.v.". The reported text might include additional information such as dosage or labels or typos. For analysis, it is helpful to have substances reported in a harmonized way. The aim of this Python function is extracting the substance name from the free text field. The script uses the FuzzyMatcher from spaczz and spacy to scan each free text for potential matches.

Usage

Call add_substance() with these arguments as Series:

  • col_with_substances - original substance name (freetext)
  • col_with_ref_substances - reference list of substances

Returns a dataframe with the following columns:

  • Original - column with original value
  • Mapped_to - column with recoded value, i.e the substance name that is deemed most likely
  • Similarity - column with similarity score
Original                   Mapped_to         Similarity
0            Fluorouracil  Fluorouracil      1.00
1              Epirubicin  Epirubicin        1.00
2        PEG-Asparaginase  Pegaspargase      0.86
3             Trastuzumab  Trastuzumab       1.00
4  Temozolomid (Temodal®)  Temozolomid       1.00
from spacy_matching import recoding as rec

# * get substance reference table from public repo
URL_V2 = "https://gitlab.opencode.de/robert-koch-institut/zentrum-fuer-krebsregisterdaten/cancerdata-references/-/raw/main/data/v2/Klassifikationen/substanz.csv?ref_type=heads"
reference_list = pd.read_csv(URL_V2, sep=";")

# * create a pandaSeries with some test data
fake_data = pd.Series(["Interferon alpha 2a", "Paclitaxel (nab)", "Filgrastim", "Leuprorelin; Tamoxifen"])

# * add a column with recoded substances
results = rec.add_substance(
    col_with_substances=fake_data,
    col_with_ref_substances=reference_list["substanz"],
    threshold=0.85
)

Options and parameters

threshold

The function features some parameters: The threshold parameter defines the accuracy. The lower the more matches but higher values lead to more accurate matches. The graph below plots the number of substances extracted from free text fields against the corresponding threshold parameter. There is a tradeoff between the number of matches and their accuracy. A threshold value of 0.85 is set as default as it usually ensures sufficient accuracy.

show_num

only_first_match

The option only_first_match = True should be used if the user wants to allow only one match per free text field. Even if there are several substances in the free text field such as "Leuprorelin; Tamoxifen", the function will return only the first match. If the option is set to "False", the function can return multiple hits. For the input "Interferon alpha-2a weekly i.v.", the function might return two substances "Interferon alpha-2a" and "Interferon alpha-2b" (assuming they are both on the reference list). Results based on both options is shown below.

show_num

Credits

Thanks to @smeisegeier for helpful feedback on the code and for making the function available as a python package.

Metadata

Release files for spacy-matching 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spacy-matching 0.6.0
File Size Uploaded
spacy_matching-0.6.0.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spacy-matching 0.6.0
File Interpreter ABI Platform
spacy_matching-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.6 kB

Release files / spacy_matching-0.6.0.tar.gz

Download URL spacy_matching-0.6.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ca598f8e04f94a4abc8ba8578908657f773b300486309ff36c7371e725d0de5a
BLAKE2b-256 checksum
How to use checksums
7a76fefe72d0bbad6c7044a020fb516da7b4c6561e8f682869c181975c441f84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / spacy_matching-0.6.0-py3-none-any.whl

Download URL spacy_matching-0.6.0-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b86b599eabe5f89d3cc157115cedef3ce2310f8d77e53a29ba19e56938d8daf
BLAKE2b-256 checksum
How to use checksums
8eace4a642eac8672351c8b90fdedfbaf2707adea886874aa90cb633c5b9f4fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page