Repository navigation
fix(medcat): Keep context vectors of concepts trained in only one CDB in merge_cdb - #679
Merged
Merged
Conversation
…erge_cdb When a concept is in both CDBs but only one has context vectors, merge_cdb dropped cdb2's vectors (cdb1 untrained, any overwrite mode) or replaced cdb1's vectors with zeros (cdb2 untrained, overwrite_training=2), while count_train kept the trained count. Copy the trained CDB's vectors in both cases.
chenyangsu
requested review from
adam-sutton-1992,
mart-r and
vladd-bit
as code owners
November 26, 2026 13:04
mart-r
approved these changes
Nov 26, 2026
mart-r
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the contribution and the thorough description!
Good catch! And good PR!
Thanks.
PS: We haven't used CDB merge in production too much so we haven't been able to catch this. Thanks again!
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.
When a CUI is in both CDBs but only one of them has context vectors for it,
merge_cdbkeeps the trainedcount_trainbut not the vectors:In the reverse case (cdb1 trained, cdb2 not) with
overwrite_training=2, the vectors come out as zeros, because the weighted average uses[0.0, 1.0]againstnp.zeros(300).With this change, a concept trained in only one CDB keeps that CDB's vectors; when both have vectors the merge is unchanged. This follows what the
count_trainmerge already does in the same cases. If you would rather keep cdb1 strictly first foroverwrite_training=1even when cdb1 has no vectors, I can change that.Testing: the new
test_merge_cdb_keeps_vectors_trained_in_one_cdbcovers both directions for eachoverwrite_trainingvalue; 4 of its 6 subtests fail on main.python -m unittest tests.utils.test_cdb_utils: 16 tests OK.ruff check medcat --previewand mypy pass.