BUG: cluster.hierarchy.fcluster: reject t < 1 for maxclust criteria - #26100
Open
HuzaifaChaudary wants to merge 1 commit into
Open
BUG: cluster.hierarchy.fcluster: reject t < 1 for maxclust criteria#26100HuzaifaChaudary wants to merge 1 commit into
HuzaifaChaudary wants to merge 1 commit into
Conversation
cluster_maxclust_monocrit ends with MC[upper_idx], where MC has n-1 entries and upper_idx starts at n-1. upper_idx only moves down when some threshold gives nc <= max_nc, and testing i = n-2 always puts the whole tree in one cluster, so that test only fails when max_nc < 1. in that one case upper_idx is never lowered and MC[n-1] is read one past the end of the array. the file is compiled with boundscheck=False, so nothing catches it and the cutoff is whatever happens to sit in that memory. with t=0 on six points the result is six singleton clusters, and feeding a longer MC with a known value in slot n-1 changes the answer, which is what showed where it was reading from. no t can produce fewer than one cluster, so the input is rejected instead. this removes the only path that reaches the read. t >= 1 is unchanged, and distance and inconsistent still take t=0.
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.
Reference issue
Closes gh-21206.
What does this implement/fix?
fclusterwithcriterion="maxclust"andt=0gives a different answer depending on whatis in memory , instead of complaining .
cluster_maxclust_monocritends withMC[upper_idx], butMConly holdsn-1values ,so the last legal index is
n-2.upper_idxstarts atn-1and only comes down when athreshold gives few enough clusters . testing
i = n-2always puts the whole tree into onecluster , so that test only fails when
max_nc < 1. in that one caseupper_idxis neverlowered and
MC[n-1]is read one past the end of the array ._hierarchy.pyxis compiled withboundscheck=False, so nothing catches it and the cutoffis whatever happens to sit in that memory . with
t=0on six points the result is sixsingleton clusters . passing an
MCone slot longer with a known value in slotn-1changesthe answer , which is what showed where it was reading from .
no
tcan ever produce fewer than one cluster , so the input is rejected instead . thatremoves the only path that reaches the read rather than hiding it .
t >= 1is unchanged ,and
distanceandinconsistentstill taket=0.Additional information
t=0.5is rejected too , it truncated to zero and reached the same line .t=1.5stillworks .
six new cases cover
maxclustandmaxclust_monocritfortof 0 , -1 and 0.5 . they failon main with "DID NOT RAISE ValueError" .
pytest scipy/cluster/gives 225 passed , 6skipped .
AI Generation Disclosure
i used claude code ( claude opus 5 ) to help look into the cause and to draft the patch and
the test . i went over the change myself , confirmed the out of bounds read , and ran the
tests locally .