<commit_before>import pylab
import PyQt4.QtGui as QG
import PyQt4.QtCore as QC
from matplotlib.backends.backend_qt4agg import (
        FigureCanvasQTAgg as FigureCanvas,
        NavigationToolbar2QTAgg as NavigationToolbar)
from matplotlib.figure import Figure
from matplotlib import patches, lines
from collections import deque


class Candlestick(QG.QWidget):
    def __init__(self, parent, max_candles=36, ylim_extra=0.1, **candle_kwargs):
        QG.QWidget.__init__(self, parent)

        # Candle layout configuration.
        self.config = {'empty': candle_kwargs.get('empty', 'none'),
                       'filled': candle_kwargs.get('filled', 'k'),
                       'edgecolor': candle_kwargs.get('edgecolor', 'k')}

        self.navbar = True
        self.max_candles = max_candles
        self.ylim_extra = ylim_extra
        self.ylim = [float('inf'), float('-inf')]
        self.vol_max = 0
        self.x = 0

        self.candle = deque()

        self.create_plot()
        self.setup_gui()


    def create_plot(self):
        self.fig = Figure(figsize=(8, 3.8))
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self)

        ax_bbox = ([0.1, 0.28, 0.87, 0.68], [0.1, 0.04, 0.87, 0.2])
        self.ax = self.fig.add_axes(ax_bbox[0]) # candlesticks
        self.ax_vol = self.fig.add_axes(ax_bbox[1], sharex=self.ax) # volume

        pylab.setp(self.ax.get_xticklabels(), visible=False)
        pylab.setp(self.ax_vol.get_xticklabels(), visible=False)
        # XXX Make the following settings configurable.
        self.vol = self.ax_vol.bar(range(-1, self.max_candles + 1),
                [0] + ([0] * self.max_candles) + [0], align='center',
                width=0.4, color='orange', alpha=0.5)
        self.ax.set_xlim(-1, self.max_candles + 1)
        self.ax_vol.set_ylim(0, 1)

    def setup_gui(self):
        layout = QG.QVBoxLayout()
        layout.setMargin(0)
        layout.addWidget(self.canvas)
        if self.navbar:
            self.mpl_bar = NavigationToolbar(self.canvas, self)
            layout.addWidget(self.mpl_bar)
        self.setLayout(layout)


    def _make_candle(self, x, o, h, l, c):
        empty = self.config['empty']
        filled = self.config['filled']
        edgecolor = self.config['edgecolor']

        lower_val = min(o, c)
        upper_val = max(o, c)

        body_color = empty if c > o else filled
        body_width = 0.42
        body_height = abs(o - c)
        body_options = {'width': body_width, 'edgecolor': edgecolor,
                        'height': body_height, 'facecolor': body_color,
                        'xy': (x - body_width/2., lower_val)}

        line_options = {'color': body_options['edgecolor'], 'lw': 1,
                        'xdata': (x, x)}

        body = patches.Rectangle(**body_options)
        upper = lines.Line2D(ydata=(upper_val, h), **line_options)
        lower = lines.Line2D(ydata=(l, lower_val), **line_options)

        return upper, body, lower

    def _update_ylim(self, lu, ll, v):<commit_msg>Show OHLCV information when hovering mouse.<commit_after>import pylab
import PyQt4.QtGui as QG
import PyQt4.QtCore as QC
from matplotlib import patches, lines
from matplotlib.figure import Figure
from matplotlib.colors import ColorConverter
from matplotlib.backends.backend_qt4agg import (
        FigureCanvasQTAgg as FigureCanvas,
        NavigationToolbar2QTAgg as NavigationToolbar)
from collections import deque


class Candlestick(QG.QWidget):
    def __init__(self, parent, max_candles=36, ylim_extra=0.1, **candle_kwargs):
        QG.QWidget.__init__(self, parent)

        # Candle layout configuration.
        self.config = {'empty': candle_kwargs.get('empty', 'none'),
                       'filled': candle_kwargs.get('filled', 'k'),
                       'edgecolor': candle_kwargs.get('edgecolor', 'k')}

        self._filled_rgba = ColorConverter().to_rgba(self.config['filled'])

        self.navbar = True
        self.max_candles = max_candles
        self.ylim_extra = ylim_extra
        self.ylim = [float('inf'), float('-inf')]
        self.vol_max = 0
        self.x = 0

        self.candle = deque()

        self.create_plot()
        self.setup_gui()


    def create_plot(self):
        self.fig = Figure(figsize=(8, 3.8))
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self)

        ax_bbox = ([0.1, 0.28, 0.87, 0.68], [0.1, 0.04, 0.87, 0.2])
        self.ax = self.fig.add_axes(ax_bbox[0]) # candlesticks
        self.ax_vol = self.fig.add_axes(ax_bbox[1], sharex=self.ax) # volume

        pylab.setp(self.ax.get_xticklabels(), visible=False)
        pylab.setp(self.ax_vol.get_xticklabels(), visible=False)
        # XXX Make the following settings configurable.
        self.vol = self.ax_vol.bar(range(-1, self.max_candles + 1),
                [0] + ([0] * self.max_candles) + [0], align='center',
                width=0.7, color='orange', alpha=0.5)
        self.ax.set_xlim(-1, self.max_candles + 1)
        self.ax_vol.set_ylim(0, 1)

        self.ax.format_coord = self._format_coord
        self.ax_vol.format_coord = self._format_coord

    def setup_gui(self):
        layout = QG.QVBoxLayout()
        layout.setMargin(0)
        layout.addWidget(self.canvas)
        if self.navbar:
            self.mpl_bar = NavigationToolbar(self.canvas, self)
            layout.addWidget(self.mpl_bar)
        self.setLayout(layout)


    def _format_coord(self, x, y):
        # This function is called by Matplotlib in order to
        # present information regarding the current position of
        # the mouse's cursor.
        x, y = int(round(x)), int(round(y))
        if 0 <= x < self.x:
            # self.candle[i] at arbitrary i is O(n), take care.
            lu, b, ll, volume = self.candle[x]
            low = ll.get_ydata()[0]
            high = lu.get_ydata()[1]
            o, c = ll.get_ydata()[1], lu.get_ydata()[0]
            if b.get_facecolor() == self._filled_rgba:
                o, c = c, o
            return 'O: %.4f  H: %.4f %s-\nL: %.4f  C: %.4f  V: %10.4f' % (
                    o, high, ' ' * 20, low, c, volume)
        else:
            return ''

    def _make_candle(self, x, o, h, l, c):
        empty = self.config['empty']
        filled = self.config['filled']
        edgecolor = self.config['edgecolor']

        lower_val = min(o, c)
        upper_val = max(o, c)

        body_color = empty if c > o else filled
        body_width = 0.72
        body_height = abs(o - c)
        body_options = {'width': body_width, 'edgecolor': edgecolor,
                        'height': body_height, 'facecolor': body_color,
                        'xy': (x - body_width/2., lower_val)}

        line_options = {'color': body_options['edgecolor'], 'lw': 1,
                        'xdata': (x, x)}

        body = patches.Rectangle(**body_options)
        upper = lines.Line2D(ydata=(upper_val, h), **line_options)
        lower = lines.Line2D(ydata=(l, lower_val), **line_options)

        return upper, body, lower

    def _update_ylim(self, lu, ll, v):
{'fraction_non_alphanumeric': 0.13123063925654746, 'fraction_numerical': 0.01802309208673613, 'mean_word_length': 2.2657471264367817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '566621', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2352, 'n_words': 506}