Fix delete_topics when custom labels are set - #2540
Open
CaptainAni187 wants to merge 1 commit into
Open
Conversation
`custom_labels_` is stored as a list ordered by topic, but `delete_topics` treated it as a mapping, so deleting a topic on a model with custom labels raised `AttributeError: 'list' object has no attribute 'items'`. Remap the list positionally against the topics it was built from. Also prepend the label for a newly created outlier topic rather than assigning to index -1, which silently overwrote the last topic's label. Fixes MaartenGr#2529
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2529
Problem
custom_labels_is stored as a list ordered by topic.set_topic_labelsconverts a dict argument into a list before assigning it, so the attribute is never a mapping:https://github.com/MaartenGr/BERTopic/blob/master/bertopic/_bertopic.py#L2028-L2040
delete_topicshowever iterated it as one:so calling
delete_topics()on a model with custom labels raisedAttributeError: 'list' object has no attribute 'items'.A second instance of the same assumption sits in the branch that initialises the
-1topic.self.custom_labels_[-1] = ""overwrites the last topic's label instead of adding one for the newly created outlier topic. That one is silent, and currently masked because the crash above always happens first — fixing only the reported error would have exposed it.Fix
custom_labels_is ordered by before the topic mapping is rebuilt, then remap the list positionally and re-emit it in new-topic order.insert(0, ""), since-1sorts first, and keep the tracked topic list in step.get_topic_info.The rest of the surrounding code is unchanged — the other attributes really are dicts, so their comprehensions still apply.
Tests
Adds
test_delete_with_custom_labelstotests/test_reduction/test_delete.py, parametrised over the same six fixtures as the existingtest_delete. Each topic is labelled after itself, so misalignment is detectable: the test asserts that every surviving topic keeps its own label across renumbering, that a newly created-1receives an empty label rather than stealing another topic's, and that the list length tracks the topic count.12 passedruff checkandruff formatare clean at the pinned v0.14.0.