<commit_before>import numpy as np
from matplotlib import pyplot as pl
from matplotlib import rcParams
from . import utils

# --------------------------------------------------------------------------------
# Plot result of preprocessing functions
# --------------------------------------------------------------------------------


def filter_genes_dispersion(result, log=False, save=None, show=None):
    """Plot dispersions vs. means for genes.

    Produces Supp. Fig. 5c of Zheng et al. (2017) and MeanVarPlot() of Seurat.

    Parameters
    ----------
    result: np.recarray
        Result of sc.pp.filter_genes_dispersion.
    log : bool
        Plot on logarithmic axes.
    """
    gene_subset = result.gene_subset
    means = result.means
    dispersions = result.dispersions
    dispersions_norm = result.dispersions_norm
    for id, d in enumerate([dispersions_norm, dispersions]):
        pl.figure(figsize=rcParams['figure.figsize'])
        for label, color, mask in zip(['highly variable genes', 'other genes'],
                                      ['black', 'grey'],
                                      [gene_subset, ~gene_subset]):
            if False: means_, disps_ = np.log10(means[mask]), np.log10(d[mask])
            else: means_, disps_ = means[mask], d[mask]
            pl.scatter(means_, disps_, label=label, c=color, s=1)
        if log:  # there's a bug in autoscale
            pl.xscale('log')
            pl.yscale('log')
            min_dispersion = np.min(dispersions)
            y_min = 0.95*min_dispersion if min_dispersion > 0 else 1e-1
            pl.xlim(0.95*np.min(means), 1.05*np.max(means))
            pl.ylim(y_min, 1.05*np.max(dispersions))
        pl.legend()
        pl.xlabel(('$log_{10}$ ' if False else '') + 'mean expression of gene')

        if id == 0:
            pl.ylabel(('$log_{10}$ ' if False else '') + 'dispersion of gene'
                  + (' (normalized)'))
            utils.savefig_or_show('filter_genes_dispersion_normalized', show=show, save=save)
        else:
            pl.ylabel(('$log_{10}$ ' if False else '') + 'dispersion of gene'
                  + (' (not normalized)'))
            utils.savefig_or_show('filter_genes_dispersion', show=show, save=save)<commit_msg>Cleanup suggested by @flying-sheep<commit_after>import numpy as np
from matplotlib import pyplot as pl
from matplotlib import rcParams
from . import utils

# --------------------------------------------------------------------------------
# Plot result of preprocessing functions
# --------------------------------------------------------------------------------


def filter_genes_dispersion(result, log=False, save=None, show=None):
    """Plot dispersions vs. means for genes.

    Produces Supp. Fig. 5c of Zheng et al. (2017) and MeanVarPlot() of Seurat.

    Parameters
    ----------
    result: np.recarray
        Result of sc.pp.filter_genes_dispersion.
    log : bool
        Plot on logarithmic axes.
    """
    gene_subset = result.gene_subset
    means = result.means
    dispersions = result.dispersions
    dispersions_norm = result.dispersions_norm
    for id, d in enumerate([dispersions_norm, dispersions]):
        pl.figure(figsize=rcParams['figure.figsize'])
        for label, color, mask in zip(['highly variable genes', 'other genes'],
                                      ['black', 'grey'],
                                      [gene_subset, ~gene_subset]):
            if False: means_, disps_ = np.log10(means[mask]), np.log10(d[mask])
            else: means_, disps_ = means[mask], d[mask]
            pl.scatter(means_, disps_, label=label, c=color, s=1)
        if log:  # there's a bug in autoscale
            pl.xscale('log')
            pl.yscale('log')
            min_dispersion = np.min(dispersions)
            y_min = 0.95*min_dispersion if min_dispersion > 0 else 1e-1
            pl.xlim(0.95*np.min(means), 1.05*np.max(means))
            pl.ylim(y_min, 1.05*np.max(dispersions))
        pl.legend()
        pl.xlabel(('$log_{10}$ ' if False else '') + 'mean expression of gene')

        file_name = 'filter_genes_dispersion{}'.format('_normalized' if id == 0 else '')
        pl.ylabel(('$log_{10}$ ' if False else '') + 'dispersion of gene'
                  + (' (normalized)' if id == 0 else ' (not normalized)'))
        utils.savefig_or_show(file_name, show=show, save=save)
        
{'fraction_non_alphanumeric': 0.1885245901639344, 'fraction_numerical': 0.014344262295081968, 'mean_word_length': 1.8415265200517466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5428069', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1254, 'n_words': 320}