<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 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