nodes: 0, module; 1, function_definition; 2, function_name:dispersion_ranking_NN; 3, parameters; 4, block; 5, identifier:self; 6, identifier:nnm; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, return_statement; 20, identifier:num_norm_avg; 21, integer:50; 22, comment:"""Computes the spatial dispersion factors for each gene.
  
          Parameters
          ----------
          nnm - scipy.sparse, float
              Square cell-to-cell nearest-neighbor matrix.
  
          num_norm_avg - int, optional, default 50
              The top 'num_norm_avg' dispersions are averaged to determine the
              normalization factor when calculating the weights. This ensures
              that outlier genes do not significantly skew the weight
              distribution.
  
          Returns:
          -------
          indices - ndarray, int
              The indices corresponding to the gene weights sorted in decreasing
              order.
  
          weights - ndarray, float
              The vector of gene weights.
          """; 23, call; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, assignment; 32, assignment; 33, identifier:weights; 34, attribute; 35, argument_list; 36, identifier:D_avg; 37, subscript; 38, pattern_list; 39, call; 40, identifier:dispersions; 41, call; 42, subscript; 43, binary_operator:var[mu > 0] / mu[mu > 0]; 44, subscript; 45, call; 46, identifier:ma; 47, call; 48, subscript; 49, identifier:ma; 50, identifier:weights; 51, call; 52, subscript; 53, identifier:weights; 54, identifier:self; 55, identifier:knn_avg; 56, identifier:nnm; 57, attribute; 58, string; 59, identifier:mu; 60, identifier:var; 61, attribute; 62, argument_list; 63, attribute; 64, argument_list; 65, identifier:dispersions; 66, comparison_operator:mu > 0; 67, subscript; 68, subscript; 69, attribute; 70, string; 71, attribute; 72, argument_list; 73, attribute; 74, argument_list; 75, identifier:dispersions; 76, comparison_operator:dispersions >= ma; 77, attribute; 78, argument_list; 79, attribute; 80, string; 81, attribute; 82, identifier:layers; 83, string_content:X_knn_avg; 84, identifier:sf; 85, identifier:mean_variance_axis; 86, identifier:D_avg; 87, keyword_argument; 88, identifier:np; 89, identifier:zeros; 90, attribute; 91, identifier:mu; 92, integer:0; 93, identifier:var; 94, comparison_operator:mu > 0; 95, identifier:mu; 96, comparison_operator:mu > 0; 97, attribute; 98, identifier:var; 99, string_content:spatial_dispersions; 100, identifier:dispersions; 101, identifier:copy; 102, subscript; 103, identifier:mean; 104, identifier:dispersions; 105, identifier:ma; 106, parenthesized_expression; 107, identifier:flatten; 108, attribute; 109, identifier:var; 110, string_content:weights; 111, identifier:self; 112, identifier:adata; 113, identifier:axis; 114, integer:0; 115, identifier:var; 116, identifier:size; 117, identifier:mu; 118, integer:0; 119, identifier:mu; 120, integer:0; 121, identifier:self; 122, identifier:adata; 123, call; 124, slice; 125, binary_operator:(dispersions / dispersions.max())**0.5; 126, identifier:self; 127, identifier:adata; 128, attribute; 129, argument_list; 130, unary_operator; 131, parenthesized_expression; 132, float:0.5; 133, identifier:np; 134, identifier:sort; 135, identifier:dispersions; 136, identifier:num_norm_avg; 137, binary_operator:dispersions / dispersions.max(); 138, identifier:dispersions; 139, call; 140, attribute; 141, argument_list; 142, identifier:dispersions; 143, identifier:max
code: def dispersion_ranking_NN(self, nnm, num_norm_avg=50):
          """Computes the spatial dispersion factors for each gene.
  
          Parameters
          ----------
          nnm - scipy.sparse, float
              Square cell-to-cell nearest-neighbor matrix.
  
          num_norm_avg - int, optional, default 50
              The top 'num_norm_avg' dispersions are averaged to determine the
              normalization factor when calculating the weights. This ensures
              that outlier genes do not significantly skew the weight
              distribution.
  
          Returns:
          -------
          indices - ndarray, int
              The indices corresponding to the gene weights sorted in decreasing
              order.
  
          weights - ndarray, float
              The vector of gene weights.
          """
  
          self.knn_avg(nnm)
          D_avg = self.adata.layers['X_knn_avg']
  
          mu, var = sf.mean_variance_axis(D_avg, axis=0)
  
          dispersions = np.zeros(var.size)
          dispersions[mu > 0] = var[mu > 0] / mu[mu > 0]
  
          self.adata.var['spatial_dispersions'] = dispersions.copy()
  
          ma = np.sort(dispersions)[-num_norm_avg:].mean()
          dispersions[dispersions >= ma] = ma
  
          weights = ((dispersions / dispersions.max())**0.5).flatten()
  
          self.adata.var['weights'] = weights
  
          return weights