<commit_before>"""
A collection of helper functions to aid the construction of custom
figures using Matplotlib and Basemap.

"""
from matplotlib.colors import ListedColormap

from _color_brewer import cdict

def _search_key(cmap_name):
    cat_range = range(3, 13)
    cat_range.reverse()

    for cat in cat_range:
        pal_name = '_%s_cat%s_data' % (cmap_name, cat)

        if pal_name in cdict:
            break

    return pal_name
    
def categorical_cmap(cmap_name, num_cat=None, reverse=False):
    """
    Construct a Matplotlib ListedColormap using ColorBrewer palettes.

    This function returns a Matplotlib ListedColormap using the color
    specifications from the ColorBrewer (http://colorbrewer.org)
    palettes.

    Parameters
    ----------
    cmap_name : string
        The name of the ColorBrewer palette to use for constructing
        the ListedColormap e.g. 'Purples', 'RdBu' etc.
    num_cat : int, optional
        The number of distinct colors (categories) to use in creating
        the colourmap. If `num_cat` is unspecified or larger than the
        maximum size of the colourmap, then a colormap with the
        maximum number of categories is returned. The number of
        catergories in the returned `cmap` can be determined using
        `cmap.N`.
    reverse : bool, optional
        Whether to reverse the entries in the colormap. Default is
        False.

    Returns
    -------
    cmap : matplotlib.colors.ListedColormap
        A ListedColormap constructed using the given parameters is
        returned.

    """
    if num_cat is not None:
        pal_name = '_%s_cat%s_data' % (cmap_name, int(num_cat))

        if pal_name not in cdict:
            pal_name = _search_key(cmap_name)

    else:
        pal_name = _search_key(cmap_name)

    clist = cdict[pal_name]
    if reverse is True:
        clist.reverse()

    cmap = ListedColormap(clist)

    return cmap
<commit_msg>BUG: Force a list copy - reverse method changes list *in place*<commit_after>"""
A collection of helper functions to aid the construction of custom
figures using Matplotlib and Basemap.

"""
from matplotlib.colors import ListedColormap

from _color_brewer import cdict

def _search_key(cmap_name):
    cat_range = range(3, 13)
    cat_range.reverse()

    for cat in cat_range:
        pal_name = '_%s_cat%s_data' % (cmap_name, cat)

        if pal_name in cdict:
            break

    return pal_name
    
def categorical_cmap(cmap_name, num_cat=None, reverse=False):
    """
    Construct a Matplotlib ListedColormap using ColorBrewer palettes.

    This function returns a Matplotlib ListedColormap using the color
    specifications from the ColorBrewer (http://colorbrewer.org)
    palettes.

    Parameters
    ----------
    cmap_name : string
        The name of the ColorBrewer palette to use for constructing
        the ListedColormap e.g. 'Purples', 'RdBu' etc.
    num_cat : int, optional
        The number of distinct colors (categories) to use in creating
        the colourmap. If `num_cat` is unspecified or larger than the
        maximum size of the colourmap, then a colormap with the
        maximum number of categories is returned. The number of
        catergories in the returned `cmap` can be determined using
        `cmap.N`.
    reverse : bool, optional
        Whether to reverse the entries in the colormap. Default is
        False.

    Returns
    -------
    cmap : matplotlib.colors.ListedColormap
        A ListedColormap constructed using the given parameters is
        returned.

    """
    if num_cat is not None:
        pal_name = '_%s_cat%s_data' % (cmap_name, int(num_cat))

        if pal_name not in cdict:
            pal_name = _search_key(cmap_name)

    else:
        pal_name = _search_key(cmap_name)

    clist = cdict[pal_name]
    if reverse is True:
        clist = cdict[pal_name][::-1]

    cmap = ListedColormap(clist)

    return cmap

{'fraction_non_alphanumeric': 0.06943031536113937, 'fraction_numerical': 0.001780264496439471, 'mean_word_length': 2.598353156450137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3186233', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1185, 'n_words': 409}