<commit_before>"""ASCII plots (experimental).

The plots are printed directly to standard output.

"""
try:
    import asciiplotlib
    ENABLE_ASCIIPLOTLIB = True
except ImportError:
    asciiplotlib = None
    ENABLE_ASCIIPLOTLIB = False

types = ("hbar",)

dims = {
    "hbar" : [1],
}

def hbar(h1, width=80, show_values=False):
    if ENABLE_ASCIIPLOTLIB:
        data = h1.frequencies
        edges = h1.numpy_bins
        fig = asciiplotlib.figure()
        fig.hist(data, edges, orientation="horizontal")
        fig.show()
    else:
        data = (h1.normalize().frequencies * width).round().astype(int)
        for i in range(h1.bin_count):
            if show_values:
                print("#" * data[i], h1.frequencies[i])
            else:
                print("#" * data[i])

try:
    import xtermcolor

    SUPPORTED_CMAPS = ("Greys", "Greys_r")
    DEFAULT_CMAP = SUPPORTED_CMAPS[0]

    def map(h2: 'Histogram2D', **kwargs):
        """Heat map

        Available only if xtermcolor present

        Parameters
        ----------
        h2 : physt.histogram_nd.Histogram2D
        """

        # Value format
        val_format = kwargs.pop("value_format", ".2f")         
        if isinstance(val_format, str):
            value_format = lambda val: (("{0:" + val_format + "}").format(val))
        
        data = (h2.frequencies / h2.frequencies.max() * 255).astype(int)
        
        # Colour map
        cmap = kwargs.pop("cmap", DEFAULT_CMAP)
        if cmap == "Greys_r":
            data = 255 - data
            colorbar_range = range(h2.shape[1] + 1, -1, -1)
        elif cmap == "Grey":
            colorbar_range = range(h2.shape[1] + 2)<commit_msg>Fix: ascii colormaps<commit_after>"""ASCII plots (experimental).

The plots are printed directly to standard output.

"""
try:
    import asciiplotlib
    ENABLE_ASCIIPLOTLIB = True
except ImportError:
    asciiplotlib = None
    ENABLE_ASCIIPLOTLIB = False

types = ("hbar",)

dims = {
    "hbar" : [1],
}

def hbar(h1, width=80, show_values=False):
    if ENABLE_ASCIIPLOTLIB:
        data = h1.frequencies
        edges = h1.numpy_bins
        fig = asciiplotlib.figure()
        fig.hist(data, edges, orientation="horizontal")
        fig.show()
    else:
        data = (h1.normalize().frequencies * width).round().astype(int)
        for i in range(h1.bin_count):
            if show_values:
                print("#" * data[i], h1.frequencies[i])
            else:
                print("#" * data[i])

try:
    import xtermcolor

    SUPPORTED_CMAPS = ("Greys", "Greys_r")
    DEFAULT_CMAP = SUPPORTED_CMAPS[1]

    def map(h2: 'Histogram2D', **kwargs):
        """Heat map

        Available only if xtermcolor present

        Parameters
        ----------
        h2 : physt.histogram_nd.Histogram2D
        """

        # Value format
        val_format = kwargs.pop("value_format", ".2f")         
        if isinstance(val_format, str):
            value_format = lambda val: (("{0:" + val_format + "}").format(val))
        
        data = (h2.frequencies / h2.frequencies.max() * 255).astype(int)
        
        # Colour map
        cmap = kwargs.pop("cmap", DEFAULT_CMAP)
        if cmap == "Greys":
            data = 255 - data
            colorbar_range = range(h2.shape[1] + 1, -1, -1)
        elif cmap == "Greys_r":
            colorbar_range = range(h2.shape[1] + 2)
{'fraction_non_alphanumeric': 0.13359004739336494, 'fraction_numerical': 0.018957345971563982, 'mean_word_length': 2.050587172538392, 'pattern_counts': {'":': 4, '<': 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': '1967654', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1119, 'n_words': 243}