<commit_before># Copyright 2016-2017 <NAME> (http://falexwolf.de).
"""
Plotting
"""   

import numpy as np
from .compat.matplotlib import pyplot as pl
from matplotlib import rcParams
from matplotlib import ticker
from matplotlib.figure import SubplotParams as sppars
from . import settings as sett

#--------------------------------------------------------------------------------
# Scanpy Plotting Functions 
#--------------------------------------------------------------------------------

def savefig(writekey):
    if sett.savefigs:
        pl.savefig(sett.figdir + writekey + '.' + sett.extf)

def plot_tool(dplot, adata,
              smp=None,
              names=None,
              comps='1,2',
              cont=None,
              layout='2d',
              legendloc='lower right',
              cmap=None,
              adjust_right=0.75,
              size=3,
              subtitles=('one title',),
              component_name='comp'):
    """
    Scatter plots.

    Parameters
    ----------
    dplot : dict
        Dict returned by plotting tool.
    adata : AnnData
        Annotated data matrix.
    smp : str, optional (default: first annotation)
        Sample/Cell annotation for coloring in the form "ann1,ann2,...". String
        annotation is plotted assuming categorical annotation, float and integer
        annotation is plotted assuming continuous annoation. Option 'cont'
        allows to switch between these default choices.
    names : str, optional (default: all names in smp)
        Allows to restrict groups in sample annotation (smp) to a few.
    comps : str, optional (default: '1,2')
         String in the form '1,2,3'.
    cont : bool, None (default: None)
        Switch on continuous layout, switch off categorical layout.
    layout : {'2d', '3d', 'unfolded 3d'}, optional (default: '2d')
         Layout of plot.
    legendloc : see matplotlib.legend, optional (default: 'lower right')
         Options for keyword argument 'loc'.
    cmap : str (default: continuous: viridis/ categorical: finite palette)
         String denoting matplotlib color map.
    adjust_right : float (default: 0.75)
         Adjust how far the plotting panel extends to the right.
    size : float (default: 3)
         Point size.
    """
    # compute components
    from numpy import array
    comps = array(comps.split(',')).astype(int) - 1
    smps = [None] if smp is None else smp.split(',') if isinstance(smp, str) else smp
    names = None if names is None else names.split(',') if isinstance(names, str) else names
    # highlights
    highlights = adata['highlights'] if 'highlights' in adata else []
    try:
        Y = dplot['Y'][:, comps]
    except IndexError:
        sett.mi('IndexError: Only computed', dplot['Y'].shape[1], ' components')
        sett.mi('--> recompute using scanpy exkey PLOTTOOL -p nr_comps YOUR_NR')
        from sys import exit
        exit(0)

    colors = []
    categoricals = []
    colorbars = []
    for ismp, smp in enumerate(smps):
        c = 'grey'
        categorical = False
        continuous = False
        if len(adata.smp_keys()) > 0:
            if smp is None:
                smp = adata.smp_keys()[0]
                smps[ismp] = smp
            # test whether we have categorial or continuous annotation
            if smp in adata.smp_keys():
                if adata.smp[smp].dtype.char in ['S', 'U']:
                    categorical = True
                    if cont is True:
                        c = adata.smp[smp]
                else:
                    continuous = True
                    c = adata.smp[smp]
                sett.m(0, '... coloring according to', smp)
            # coloring according to gene expression
            elif smp in adata.var_names:
                c = adata.X[:, np.where(smp==adata.var_names)[0][0]]
                continuous = True
                sett.m(0, '... coloring according to expression of gene', smp)
            else:
                raise ValueError('specify valid sample annotation, one of '
                                 + str(adata.smp_keys()) + ' or a gene name '
                                 + str(adata.var_names))
        if cont is not None:
            categorical = not cont
            continuous = cont
        colors.append(c)
        colorbars.append(True if continuous else False)
        if categorical:
            categoricals.append(ismp)

    axs = scatter(Y,
                  subtitles=smps,
                  component_name=component_name,
                  component_indexnames=comps + 1,
                  layout=layout,
                  c=colors,
                  highlights=highlights,
                  colorbars=colorbars,
                  cmap='viridis' if cmap is None else cmap)

    for ismp in categoricals:
        smp = smps[ismp]
        if (smp != 'groups' and 'groups_names' in adata
            and len(np.setdiff1d(adata['groups_names'], adata[smp + '_names']))
                < len(adata['groups_names'])):
            # if there is a correspondence between smp and the 'groups' defined
            # in adata, that is, if smp has corresponding categories with those 
            # in adata['groups_names']
            adata[smp + '_colors'] = pl.cm.get_cmap('jet' if cmap is None else cmap)(
                                    pl.Normalize()(adata['groups_ids']))
        elif not smp + '_colors' in adata:
            adata[smp + '_colors'] = pl.cm.get_cmap('jet' if cmap is None else cmap)(
                                            pl.Normalize()(adata[smp + '_ids']))
        for iname, name in enumerate(adata[smp + '_names']):
            if (names is None or (names != None and name in names)):
                group(axs[ismp], smp, iname, adata, dplot['Y'][:, comps], layout, size)
        # for the last panel, let's put the legend outside panel
        if legendloc is None or ismp == categoricals[-1]:
            axs[ismp].legend(frameon=False, loc='center left', bbox_to_anchor=(1, 0.5))
        elif legendloc != 'none':
            axs[ismp].legend(frameon=False, loc=legendloc)

    adjust_right *= 1.05**len(smps)
    pl.subplots_adjust(right=adjust_right)

    savefig(dplot['writekey'] + sett.plotsuffix)
    if not sett.savefigs and sett.autoshow:
        pl.show()

def group(ax, name, imask, adata, Y, layout='2d', size=3):
    """
    Plot group using representation of data Y.
    """
    mask = adata[name + '_masks'][imask]
    color = adata[name + '_colors'][imask]
    if not isinstance(color[0], str):
        from matplotlib.colors import rgb2hex
        color = rgb2hex(adata[name + '_colors'][imask])
    data = [Y[mask, 0], Y[mask, 1]]
    if layout == '3d':
        data.append(Y[mask, 2])
    ax.scatter(*data,
               c=color,
               edgecolors='face',
               s=size,
               alpha=1,
               label=adata[name + '_names'][imask])

def timeseries(*args,**kwargs):<commit_msg>small change in legend handling<commit_after># Copyright 2016-2017 <NAME> (http://falexwolf.de).
"""
Plotting
"""   

import numpy as np
from .compat.matplotlib import pyplot as pl
from matplotlib import rcParams
from matplotlib import ticker
from matplotlib.figure import SubplotParams as sppars
from . import settings as sett

#--------------------------------------------------------------------------------
# Scanpy Plotting Functions 
#--------------------------------------------------------------------------------

def savefig(writekey):
    if sett.savefigs:
        pl.savefig(sett.figdir + writekey + '.' + sett.extf)

def plot_tool(dplot, adata,
              smp=None,
              names=None,
              comps='1,2',
              cont=None,
              layout='2d',
              legendloc='lower right',
              cmap=None,
              adjust_right=0.75,
              size=3,
              subtitles=('one title',),
              component_name='comp'):
    """
    Scatter plots.

    Parameters
    ----------
    dplot : dict
        Dict returned by plotting tool.
    adata : AnnData
        Annotated data matrix.
    smp : str, optional (default: first annotation)
        Sample/Cell annotation for coloring in the form "ann1,ann2,...". String
        annotation is plotted assuming categorical annotation, float and integer
        annotation is plotted assuming continuous annoation. Option 'cont'
        allows to switch between these default choices.
    names : str, optional (default: all names in smp)
        Allows to restrict groups in sample annotation (smp) to a few.
    comps : str, optional (default: '1,2')
         String in the form '1,2,3'.
    cont : bool, None (default: None)
        Switch on continuous layout, switch off categorical layout.
    layout : {'2d', '3d', 'unfolded 3d'}, optional (default: '2d')
         Layout of plot.
    legendloc : see matplotlib.legend, optional (default: 'lower right')
         Options for keyword argument 'loc'.
    cmap : str (default: continuous: viridis/ categorical: finite palette)
         String denoting matplotlib color map.
    adjust_right : float (default: 0.75)
         Adjust how far the plotting panel extends to the right.
    size : float (default: 3)
         Point size.
    """
    # compute components
    from numpy import array
    comps = array(comps.split(',')).astype(int) - 1
    smps = [None] if smp is None else smp.split(',') if isinstance(smp, str) else smp
    names = None if names is None else names.split(',') if isinstance(names, str) else names
    # highlights
    highlights = adata['highlights'] if 'highlights' in adata else []
    try:
        Y = dplot['Y'][:, comps]
    except IndexError:
        sett.mi('IndexError: Only computed', dplot['Y'].shape[1], ' components')
        sett.mi('--> recompute using scanpy exkey PLOTTOOL -p nr_comps YOUR_NR')
        from sys import exit
        exit(0)

    colors = []
    categoricals = []
    colorbars = []
    for ismp, smp in enumerate(smps):
        c = 'grey'
        categorical = False
        continuous = False
        if len(adata.smp_keys()) > 0:
            if smp is None:
                smp = adata.smp_keys()[0]
                smps[ismp] = smp
            # test whether we have categorial or continuous annotation
            if smp in adata.smp_keys():
                if adata.smp[smp].dtype.char in ['S', 'U']:
                    categorical = True
                    if cont is True:
                        c = adata.smp[smp]
                else:
                    continuous = True
                    c = adata.smp[smp]
                sett.m(0, '... coloring according to', smp)
            # coloring according to gene expression
            elif smp in adata.var_names:
                c = adata.X[:, np.where(smp==adata.var_names)[0][0]]
                continuous = True
                sett.m(0, '... coloring according to expression of gene', smp)
            else:
                raise ValueError('specify valid sample annotation, one of '
                                 + str(adata.smp_keys()) + ' or a gene name '
                                 + str(adata.var_names))
        if cont is not None:
            categorical = not cont
            continuous = cont
        colors.append(c)
        colorbars.append(True if continuous else False)
        if categorical:
            categoricals.append(ismp)

    axs = scatter(Y,
                  subtitles=smps,
                  component_name=component_name,
                  component_indexnames=comps + 1,
                  layout=layout,
                  c=colors,
                  highlights=highlights,
                  colorbars=colorbars,
                  cmap='viridis' if cmap is None else cmap)

    for ismp in categoricals:
        smp = smps[ismp]
        if (smp != 'groups' and 'groups_names' in adata
            and len(np.setdiff1d(adata['groups_names'], adata[smp + '_names']))
                < len(adata['groups_names'])):
            # if there is a correspondence between smp and the 'groups' defined
            # in adata, that is, if smp has corresponding categories with those 
            # in adata['groups_names']
            adata[smp + '_colors'] = pl.cm.get_cmap('jet' if cmap is None else cmap)(
                                    pl.Normalize()(adata['groups_ids']))
        elif not smp + '_colors' in adata:
            adata[smp + '_colors'] = pl.cm.get_cmap('jet' if cmap is None else cmap)(
                                            pl.Normalize()(adata[smp + '_ids']))
        for iname, name in enumerate(adata[smp + '_names']):
            if (names is None or (names != None and name in names)):
                group(axs[ismp], smp, iname, adata, dplot['Y'][:, comps], layout, size)
        # for the last panel, let's put the legend outside panel
        if legendloc is None or ismp == len(axs)-1:
            axs[ismp].legend(frameon=False, loc='center left', bbox_to_anchor=(1, 0.5))
        elif legendloc != 'none':
            axs[ismp].legend(frameon=False, loc=legendloc)

    adjust_right *= 1.05**len(smps)
    pl.subplots_adjust(right=adjust_right)

    savefig(dplot['writekey'] + sett.plotsuffix)
    if not sett.savefigs and sett.autoshow:
        pl.show()

def group(ax, name, imask, adata, Y, layout='2d', size=3):
    """
    Plot group using representation of data Y.
    """
    mask = adata[name + '_masks'][imask]
    color = adata[name + '_colors'][imask]
    if not isinstance(color[0], str):
        from matplotlib.colors import rgb2hex
        color = rgb2hex(adata[name + '_colors'][imask])
    data = [Y[mask, 0], Y[mask, 1]]
    if layout == '3d':
        data.append(Y[mask, 2])
    ax.scatter(*data,
               c=color,
               edgecolors='face',
               s=size,
               alpha=1,
               label=adata[name + '_names'][imask])

def timeseries(*args,**kwargs):
{'fraction_non_alphanumeric': 0.1295790834650194, 'fraction_numerical': 0.008332136187329406, 'mean_word_length': 1.7985929648241206, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3054097', 'n_tokens_mistral': 4065, 'n_tokens_neox': 3896, 'n_words': 1195}