original_docstring: Create affinity matrix for the mutual kNN graph of the whole dataset
          Args:
              sims: similarities of kNN
              ids: indexes of kNN
          Returns:
              affinity: affinity matrix
          
docstring: Create affinity matrix for the mutual kNN graph of the whole dataset
docstring_tokens: Create affinity matrix for the mutual kNN graph of the whole dataset
code: def _compute_affinity_matrix(self, sims, ids):
          # TODO: Refactor this method!!!
          """Create affinity matrix for the mutual kNN graph of the whole dataset
          Args:
              sims: similarities of kNN
              ids: indexes of kNN
          Returns:
              affinity: affinity matrix
          """
          sims = sims[:, : self.k]
          ids = ids[:, : self.k]
  
          num = sims.shape[0]
          sims[sims < 0] = 0  # similarity should be non-negative
          sims = sims ** self.gamma
          # vec_ids: feature vectors' ids
          # mut_ids: mutual (reciprocal) nearest neighbors' ids
          # mut_sims: similarites between feature vectors and their mutual nearest neighbors
          vec_ids, mut_ids, mut_sims = [], [], []
          for i in range(num):
              i_nn = ids[i]
              j_nn = ids[i_nn]
              # check reciprocity: i is in j's kNN and j is in i's kNN when i != j
              ismutual = np.isin(j_nn, i).any(axis=1)
              ismutual[0] = False  # False where i = j
              if ismutual.any():
                  vec_ids.append(i * np.ones(ismutual.sum(), dtype=int))
                  mut_ids.append(i_nn[ismutual])
                  mut_sims.append(sims[i, ismutual])
          vec_ids, mut_ids, mut_sims = map(np.concatenate, [vec_ids, mut_ids, mut_sims])
          affinity = sparse.csc_matrix(
              (mut_sims, (vec_ids, mut_ids)), shape=(num, num), dtype=np.float32
          )
          return affinity
code_tokens: def _compute_affinity_matrix ( self , sims , ids ) : sims = sims [ : , : self . k ] ids = ids [ : , : self . k ] num = sims . shape [ 0 ] sims [ sims < 0 ] = 0 sims = sims ** self . gamma vec_ids , mut_ids , mut_sims = [ ] , [ ] , [ ] for i in range ( num ) : i_nn = ids [ i ] j_nn = ids [ i_nn ] ismutual = np . isin ( j_nn , i ) . any ( axis = 1 ) ismutual [ 0 ] = False if ismutual . any ( ) : vec_ids . append ( i * np . ones ( ismutual . sum ( ) , dtype = int ) ) mut_ids . append ( i_nn [ ismutual ] ) mut_sims . append ( sims [ i , ismutual ] ) vec_ids , mut_ids , mut_sims = map ( np . concatenate , [ vec_ids , mut_ids , mut_sims ] ) affinity = sparse . csc_matrix ( ( mut_sims , ( vec_ids , mut_ids ) ) , shape = ( num , num ) , dtype = np . float32 ) return affinity
short_docstring: Create affinity matrix for the mutual kNN graph of the whole dataset
short_docstring_tokens: Create affinity matrix for the mutual kNN graph of the whole dataset
comment: # TODO: Refactor this method!!!$SEP$"""Create affinity matrix for the mutual kNN graph of the whole dataset
          Args:
              sims: similarities of kNN
              ids: indexes of kNN
          Returns:
              affinity: affinity matrix
          """$SEP$# similarity should be non-negative$SEP$# vec_ids: feature vectors' ids$SEP$# mut_ids: mutual (reciprocal) nearest neighbors' ids$SEP$# mut_sims: similarites between feature vectors and their mutual nearest neighbors$SEP$# check reciprocity: i is in j's kNN and j is in i's kNN when i != j$SEP$# False where i = j