<commit_before>import argparse
import re
from datetime import timedelta
from operator import itemgetter

from mtools.mplotqueries.plottypes.base_type import BasePlotType

try:
    from matplotlib import __version__ as mpl_version
    from matplotlib.dates import date2num
    from matplotlib.patches import Polygon

except ImportError:
    raise ImportError("Can't import matplotlib. See "
                      "github.com/rueckstiess/mtools/blob/master/INSTALL.md "
                      "for instructions how to install matplotlib or try "
                      "mlogvis instead, which is a simplified version of "
                      "mplotqueries that visualizes the logfile in a web "
                      "browser.")


class ScatterPlotType(BasePlotType):

    plot_type_str = 'scatter'
    sort_order = 3
    default_group_by = 'namespace'

    def __init__(self, args=None, unknown_args=None):
        BasePlotType.__init__(self, args, unknown_args)

        self.logscale = args['logscale']

        # parse arguments further to get --yaxis argument
        argparser = argparse.ArgumentParser("mplotqueries --type scatter")
        argparser.add_argument('--yaxis', '-y', action='store',
                               metavar='FIELD', default='duration')
        argparser.add_argument('--yaxis-regex-mode', action='store_true',
                               default=False)
        args = vars(argparser.parse_args(unknown_args))

        self.field = args['yaxis']
        self.regex_mode = args['yaxis_regex_mode']

        if self.regex_mode:
            self.ylabel = 'regex: "%s"' % self.field
        elif args['yaxis'] == 'duration':
            self.ylabel = 'duration in ms'
        else:
            self.ylabel = args['yaxis']

        self.durlines = []

    def accept_line(self, logevent):
        """Return True if the log line has the nominated yaxis field."""
        if self.regex_mode:
            return bool(re.search(self.field, logevent.line_str))
        else:
            return getattr(logevent, self.field) is not None

    def plot_group(self, group, idx, axis):
        # create x-coordinates for all log lines in this group
        x = date2num([logevent.datetime for logevent in self.groups[group]])

        color, marker = self.color_map(group)

        # duration plots require y coordinate and use plot_date
        if self.regex_mode:
            y = [float(re.search(self.field, logevent.line_str)
                       .group(1)) for logevent in self.groups[group]]
        else:
            y = [getattr(logevent, self.field)
                 for logevent in self.groups[group]]

        if self.logscale:
            axis.semilogy()

        artist = axis.plot_date(x, y, color=color, markeredgecolor='k',
                                marker=marker, alpha=0.8,
                                markersize=7, picker=5, label=group)[0]
        # add meta-data for picking
        artist._mt_plot_type = self
        artist._mt_group = group

        return artist

    def clicked(self, event):
        """
        Call if an element of this plottype is clicked.

        Implement in sub class.
        """
        group = event.artist._mt_group
        indices = event.ind

        # double click only supported on 1.2 or later
        major, minor, _ = mpl_version.split('.')
        if (int(major), int(minor)) < (1, 2) or not event.mouseevent.dblclick:
            for i in indices:
                print(self.groups[group][i].line_str)

        else:
            # toggle durline
            first = indices[0]
            logevent = self.groups[group][first]

            try:
                # remove triangle for this event
                idx = map(itemgetter(0), self.durlines).index(logevent)
                _, poly = self.durlines[idx]
                poly.remove()<commit_msg>Escape dollar signs in mplotqueries legend. Fixes #613 (#639)<commit_after>import argparse
import re
from datetime import timedelta
from operator import itemgetter

from mtools.mplotqueries.plottypes.base_type import BasePlotType

try:
    from matplotlib import __version__ as mpl_version
    from matplotlib.dates import date2num
    from matplotlib.patches import Polygon

except ImportError:
    raise ImportError("Can't import matplotlib. See "
                      "github.com/rueckstiess/mtools/blob/master/INSTALL.md "
                      "for instructions how to install matplotlib or try "
                      "mlogvis instead, which is a simplified version of "
                      "mplotqueries that visualizes the logfile in a web "
                      "browser.")


class ScatterPlotType(BasePlotType):

    plot_type_str = 'scatter'
    sort_order = 3
    default_group_by = 'namespace'

    def __init__(self, args=None, unknown_args=None):
        BasePlotType.__init__(self, args, unknown_args)

        self.logscale = args['logscale']

        # parse arguments further to get --yaxis argument
        argparser = argparse.ArgumentParser("mplotqueries --type scatter")
        argparser.add_argument('--yaxis', '-y', action='store',
                               metavar='FIELD', default='duration')
        argparser.add_argument('--yaxis-regex-mode', action='store_true',
                               default=False)
        args = vars(argparser.parse_args(unknown_args))

        self.field = args['yaxis']
        self.regex_mode = args['yaxis_regex_mode']

        if self.regex_mode:
            self.ylabel = 'regex: "%s"' % self.field
        elif args['yaxis'] == 'duration':
            self.ylabel = 'duration in ms'
        else:
            self.ylabel = args['yaxis']

        self.durlines = []

    def accept_line(self, logevent):
        """Return True if the log line has the nominated yaxis field."""
        if self.regex_mode:
            return bool(re.search(self.field, logevent.line_str))
        else:
            return getattr(logevent, self.field) is not None

    def plot_group(self, group, idx, axis):
        # create x-coordinates for all log lines in this group
        x = date2num([logevent.datetime for logevent in self.groups[group]])

        color, marker = self.color_map(group)

        # duration plots require y coordinate and use plot_date
        if self.regex_mode:
            y = [float(re.search(self.field, logevent.line_str)
                       .group(1)) for logevent in self.groups[group]]
        else:
            y = [getattr(logevent, self.field)
                 for logevent in self.groups[group]]

        if self.logscale:
            axis.semilogy()

        group_label = group.replace('$', '\$')
        artist = axis.plot_date(x, y, color=color, markeredgecolor='k',
                                marker=marker, alpha=0.8,
                                markersize=7, picker=5, label=group_label)[0]
        # add meta-data for picking
        artist._mt_plot_type = self
        artist._mt_group = group

        return artist

    def clicked(self, event):
        """
        Call if an element of this plottype is clicked.

        Implement in sub class.
        """
        group = event.artist._mt_group
        indices = event.ind

        # double click only supported on 1.2 or later
        major, minor, _ = mpl_version.split('.')
        if (int(major), int(minor)) < (1, 2) or not event.mouseevent.dblclick:
            for i in indices:
                print(self.groups[group][i].line_str)

        else:
            # toggle durline
            first = indices[0]
            logevent = self.groups[group][first]

            try:
                # remove triangle for this event
                idx = map(itemgetter(0), self.durlines).index(logevent)
                _, poly = self.durlines[idx]
                poly.remove()
{'fraction_non_alphanumeric': 0.09503655252020007, 'fraction_numerical': 0.00461716044632551, 'mean_word_length': 1.9854517611026035, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5517438', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2143, 'n_words': 577}