#NEWLINE# def _find_K_cut( self , k ) : #NEWLINE# #INDENT# '\n        Find the lowest level cut that has k connected components. If there are\n        no levels that have k components, then find the lowest level that has\n        at least k components. If no levels have > k components, find the\n        lowest level that has the maximum number of components.\n\n        Parameters\n        ----------\n        k : int\n            Desired number of clusters/nodes/components.\n\n        Returns\n        -------\n        cut : float\n            Lowest density level where there are k nodes.\n        ' #NEWLINE# starts = [ v . start_level for v in self . nodes . itervalues ( ) ] #NEWLINE# ends = [ v . end_level for v in self . nodes . itervalues ( ) ] #NEWLINE# crits = _np . unique ( ( starts + ends ) ) #NEWLINE# nclust = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for c in crits : #NEWLINE# #INDENT# nclust [ c ] = len ( [ e for ( e , v ) in self . nodes . iteritems ( ) if ( ( v . start_level <= c ) and ( v . end_level > c ) ) ] ) #NEWLINE# #UNINDENT# width = _np . max ( nclust . values ( ) ) #NEWLINE# if ( k in nclust . values ( ) ) : #NEWLINE# #INDENT# cut = _np . min ( [ e for ( e , v ) in nclust . iteritems ( ) if ( v == k ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( width < k ) : #NEWLINE# #INDENT# cut = _np . min ( [ e for ( e , v ) in nclust . iteritems ( ) if ( v == width ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ktemp = _np . min ( [ v for v in nclust . itervalues ( ) if ( v > k ) ] ) #NEWLINE# cut = _np . min ( [ e for ( e , v ) in nclust . iteritems ( ) if ( v == ktemp ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# return width