Skip to content

fix: use cast_column for faster string conversion of label columns (Fixes #145) - #251

Open
rtmalikian wants to merge 1 commit into
Machine-Learning-for-Medical-Language:mainfrom
rtmalikian:fix/issue-145-slow-string-conversion
Open

fix: use cast_column for faster string conversion of label columns (Fixes #145)#251
rtmalikian wants to merge 1 commit into
Machine-Learning-for-Medical-Language:mainfrom
rtmalikian:fix/issue-145-slow-string-conversion

Conversation

@rtmalikian

Copy link
Copy Markdown

Fixes #145

Problem

The AutoProcessor in cnlp_processors.py converts classification label columns from integers to strings using a slow pattern:

self.dataset[split] = self.dataset[split].add_column(task_str, [str(x) for x in self.dataset[split][task]])
self.dataset[split] = self.dataset[split].remove_columns(task)
self.dataset[split] = self.dataset[split].rename_column(task_str, task)

This creates a new column with a Python list comprehension (str(x) for every element), removes the old column, and renames the new one. For large datasets, this is extremely slow because it iterates through every element in Python.

Solution

Replace the slow pattern with datasets.cast_column(task, datasets.Value("string")), which operates at the Apache Arrow level:

self.dataset[split] = self.dataset[split].cast_column(task, datasets.Value("string"))

This is dramatically faster because:

  1. No intermediate column creation
  2. No column removal or renaming
  3. Arrow's native type casting is orders of magnitude faster than Python list comprehensions

Verification

# All existing tests pass
python -m pytest test/data/test_cnlp_dataset.py -v
# 3 passed in 5.07s

# Manual verification with AutoProcessor
python3 -c "
from cnlpt.legacy.cnlp_processors import AutoProcessor
# ... creates processor with classification tasks, labels are correctly converted to strings
# SUCCESS: AutoProcessor works with cast_column fix!
"

About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.

📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a


Disclosure: This code was developed with assistance from mimo-2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.

Replace the slow add_column/remove_columns/rename_column pattern with
datasets.cast_column(task, datasets.Value("string")) for converting
classification label columns to strings.

The previous approach created a new column with str(x) for every element
via a Python list comprehension, then removed the old column and renamed
the new one. This is O(n) Python operations per column.

cast_column operates at the Apache Arrow level, which is dramatically
faster for large datasets because it avoids creating intermediate columns
and leverages Arrow's native type casting.

Fixes Machine-Learning-for-Medical-Language#145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

converting every single value to string in cnlp_processors is slow

1 participant