<commit_before>"""Interactive operations."""
from __future__ import (absolute_import, division,
                        print_function)

try:
    import matplotlib.pyplot as plt
    from matplotlib import gridspec
    HAS_MPL = True
except ImportError:
    HAS_MPL = False

import numpy as np
from .fit import linear_fit, linear_fun, align
import warnings


__all__ = ["TestWarning", "PlotWarning", "mask", "intervals", "DataSelector",
           "ImageSelector", "select_data"]


class TestWarning(UserWarning):
    pass


class PlotWarning(UserWarning):
    pass


def mask(xs, border_xs, invert=False):
    """Create mask from a list of interval borders.

    Parameters
    ----------
    xs : array
        the array of values to filter
    border_xs : array
        the list of borders. Should be an even number of positions

    Returns
    -------
    mask : array
        Array of boolean values, that work as a mask to xs

    Other Parameters
    ----------------
    invert : bool
        Mask value is False if invert = False, and vice versa.
        E.g. for zapped intervals, invert = False. For baseline fit selections,
        invert = True
    """
    good = np.ones(len(xs), dtype=bool)
    if len(border_xs) >= 2:
        intervals = list(zip(border_xs[:-1:2], border_xs[1::2]))
        for i in intervals:
            good[np.logical_and(xs >= i[0],
                                xs <= i[1])] = False
    if invert:
        good = np.logical_not(good)

    return good


class intervals():
    """A list of xs and ys of the points taken during interactive selection."""

    def __init__(self):
        """Initialize."""
        self.xs = []
        self.ys = []

    def clear(self):
        """Clear."""
        self.xs = []
        self.ys = []

    def add(self, point):
        """Append points."""
        self.xs.append(point[0])
        self.ys.append(point[1])


class DataSelector:
    """Plot and process scans interactively."""

    def __init__(self, xs, ys, ax1, ax2, masks=None, xlabel=None, title=None,
                 test=False):
        """Initialize."""

        self.instructions = """
-------------------------------------------------------------

Interactive plotter.

-------------------------------------------------------------

Choose line to fit: Click on the line

Interval selection: Point mouse + <key>
    z     create zap intervals
    b     suggest intervals to use for baseline fit

Flagging actions:
    x     flag as bad;
    v     Remove flag;

Actions:
    P     print current zap list and fit parameters
    A     align all scans w.r.t. the selected one
    u     update plots with new selections
    B     subtract the baseline;
    r     reset baseline and zapping intervals, and fit parameters;
    q     quit

-------------------------------------------------------------
    """
        if not HAS_MPL:
            raise ImportError("matplotlib not installed")
        self.xs = xs
        self.ys = ys
        self.test = test
        if masks is None:
            masks = dict(list(zip(self.xs.keys(),
                                  [np.ones(len(self.xs[k]), dtype=bool)
                                   for k in self.xs.keys()])))
        self.masks = masks
        self.ax1 = ax1
        self.ax2 = ax2

        self.xlabel = xlabel
        self.title = title
        self.info = {}
        for key in self.xs.keys():
            self.info[key] = {}
            self.info[key]['FLAG'] = False
            self.info[key]['zap'] = intervals()
            self.info[key]['base'] = intervals()
            self.info[key]['fitpars'] = np.array([0, 0])
        self.lines = []
        if not test:
            self.print_instructions()
        self.current = None

        if not test:
            ax1.figure.canvas.mpl_connect('button_press_event', self.on_click)
            ax1.figure.canvas.mpl_connect('key_press_event', self.on_key)
            ax1.figure.canvas.mpl_connect('pick_event', self.on_pick)
            ax2.figure.canvas.mpl_connect('button_press_event', self.on_click)
            ax2.figure.canvas.mpl_connect('key_press_event', self.on_key)
            ax2.figure.canvas.mpl_connect('pick_event', self.on_pick)

        self.plot_all()
        self.zcounter = 0
        self.bcounter = 0
        if not test:
            plt.show()

    def on_click(self, event):
        """Dummy function, in case I want to do something with a click."""
        pass

    def zap(self, event):
        """Create a zap interval."""
        key = self.current
        if key is None:
            return<commit_msg>Small modifications<commit_after>"""Interactive operations."""
from __future__ import (absolute_import, division,
                        print_function)

try:
    import matplotlib.pyplot as plt
    from matplotlib import gridspec
    HAS_MPL = True
except ImportError:
    HAS_MPL = False

import numpy as np
from .fit import linear_fit, linear_fun, align
import warnings


__all__ = ["TestWarning", "PlotWarning", "mask", "intervals", "DataSelector",
           "ImageSelector", "create_empty_info", "select_data"]


class TestWarning(UserWarning):
    pass


class PlotWarning(UserWarning):
    pass


def create_empty_info(keys):
    info = {}
    for key in keys:
        info[key] = {}
        info[key]['FLAG'] = False
        info[key]['zap'] = intervals()
        info[key]['base'] = intervals()
        info[key]['fitpars'] = np.array([0, 0])

    return info


def mask(xs, border_xs, invert=False):
    """Create mask from a list of interval borders.

    Parameters
    ----------
    xs : array
        the array of values to filter
    border_xs : array
        the list of borders. Should be an even number of positions

    Returns
    -------
    mask : array
        Array of boolean values, that work as a mask to xs

    Other Parameters
    ----------------
    invert : bool
        Mask value is False if invert = False, and vice versa.
        E.g. for zapped intervals, invert = False. For baseline fit selections,
        invert = True
    """
    good = np.ones(len(xs), dtype=bool)
    if len(border_xs) >= 2:
        intervals = list(zip(border_xs[:-1:2], border_xs[1::2]))
        for i in intervals:
            good[np.logical_and(xs >= i[0],
                                xs <= i[1])] = False
    if invert:
        good = np.logical_not(good)

    return good


class intervals():
    """A list of xs and ys of the points taken during interactive selection."""

    def __init__(self):
        """Initialize."""
        self.xs = []
        self.ys = []

    def clear(self):
        """Clear."""
        self.xs = []
        self.ys = []

    def add(self, point):
        """Append points."""
        self.xs.append(point[0])
        self.ys.append(point[1])


class DataSelector:
    """Plot and process scans interactively."""

    def __init__(self, xs, ys, ax1, ax2, masks=None, xlabel=None, title=None,
                 test=False):
        """Initialize."""

        self.instructions = """
-------------------------------------------------------------

Interactive plotter.

-------------------------------------------------------------

Choose line to fit: Click on the line

Interval selection: Point mouse + <key>
    z     create zap intervals
    b     suggest intervals to use for baseline fit

Flagging actions:
    x     flag as bad;
    v     Remove flag;

Actions:
    P     print current zap list and fit parameters
    A     align all scans w.r.t. the selected one
    u     update plots with new selections
    B     subtract the baseline;
    r     reset baseline and zapping intervals, and fit parameters;
    q     quit

-------------------------------------------------------------
    """
        if not HAS_MPL:
            raise ImportError("matplotlib not installed")
        self.xs = xs
        self.ys = ys
        self.test = test
        if masks is None:
            masks = dict(list(zip(self.xs.keys(),
                                  [np.ones(len(self.xs[k]), dtype=bool)
                                   for k in self.xs.keys()])))
        self.masks = masks
        self.ax1 = ax1
        self.ax2 = ax2

        self.xlabel = xlabel
        self.title = title
        self.info = create_empty_info(self.xs.keys())
        self.lines = []
        if not test:
            self.print_instructions()
        self.current = None

        if not test:
            ax1.figure.canvas.mpl_connect('button_press_event', self.on_click)
            ax1.figure.canvas.mpl_connect('key_press_event', self.on_key)
            ax1.figure.canvas.mpl_connect('pick_event', self.on_pick)
            ax2.figure.canvas.mpl_connect('button_press_event', self.on_click)
            ax2.figure.canvas.mpl_connect('key_press_event', self.on_key)
            ax2.figure.canvas.mpl_connect('pick_event', self.on_pick)

        self.plot_all()
        self.zcounter = 0
        self.bcounter = 0
        if not test:
            plt.show()

    def on_click(self, event):
        """Dummy function, in case I want to do something with a click."""
        pass

    def zap(self, event):
        """Create a zap interval."""
        key = self.current
        if key is None:
            return
{'fraction_non_alphanumeric': 0.14695379287955848, 'fraction_numerical': 0.005410669840926307, 'mean_word_length': 1.721436984687868, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4488202', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2561, 'n_words': 773}