<commit_before>import sys
import itertools
import enum

from xml.sax.saxutils import escape
from types import SimpleNamespace as namespace

import numpy
import sklearn.metrics

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QEvent, QRectF, QSizeF
from PyQt4.QtCore import pyqtSignal as Signal

import pyqtgraph as pg

import Orange.data
import Orange.distance

from Orange.widgets import widget, gui, settings
from Orange.widgets.utils import itemmodels
from Orange.widgets.unsupervised.owhierarchicalclustering import \
    WrapperLayoutItem


class OWSilhouettePlot(widget.OWWidget):
    name = "Silhouette Plot"
    description = "Silhouette Plot"

    icon = "icons/Silhouette.svg"

    inputs = [("Data", Orange.data.Table, "set_data")]
    outputs = [("Selected Data", Orange.data.Table, widget.Default),
               ("Other Data", Orange.data.Table)]

    replaces = ["orangecontrib.prototypes.widgets.owsilhouetteplot.OWSilhouettePlot"]

    settingsHandler = settings.PerfectDomainContextHandler()

    #: Distance metric index
    distance_idx = settings.Setting(0)
    #: Group/cluster variable index
    cluster_var_idx = settings.ContextSetting(0)
    #: Annotation variable index
    annotation_var_idx = settings.ContextSetting(0)
    #: Group the silhouettes by cluster
    group_by_cluster = settings.Setting(True)
    #: A fixed size for an instance bar
    bar_size = settings.Setting(3)
    #: Add silhouette scores to output data
    add_scores = settings.Setting(False)
    auto_commit = settings.Setting(False)

    Distances = [("Euclidean", Orange.distance.Euclidean),
                 ("Manhattan", Orange.distance.Manhattan)]

    def __init__(self):
        super().__init__()

        self.data = None
        self._effective_data = None
        self._matrix = None
        self._silhouette = None
        self._labels = None
        self._silplot = None

        box = gui.widgetBox(self.controlArea, "Settings",)
        gui.comboBox(box, self, "distance_idx", label="Distance",
                     items=[name for name, _ in OWSilhouettePlot.Distances],
                     callback=self._invalidate_distances)
        self.cluster_var_cb = gui.comboBox(
            box, self, "cluster_var_idx", label="Cluster",
            callback=self._invalidate_scores)
        self.cluster_var_model = itemmodels.VariableListModel(parent=self)
        self.cluster_var_cb.setModel(self.cluster_var_model)

        gui.spin(box, self, "bar_size", minv=1, maxv=10, label="Bar Size",
                 callback=self._update_bar_size)

        gui.checkBox(box, self, "group_by_cluster", "Group by cluster",
                     callback=self._replot)

        self.annotation_cb = gui.comboBox(
            box, self, "annotation_var_idx", label="Annotations",
            callback=self._update_annotations)
        self.annotation_var_model = itemmodels.VariableListModel(parent=self)
        self.annotation_var_model[:] = ["None"]
        self.annotation_cb.setModel(self.annotation_var_model)

        gui.rubber(self.controlArea)

        box = gui.widgetBox(self.controlArea, "Output")
        gui.checkBox(box, self, "add_scores", "Add silhouette scores",)
        gui.auto_commit(box, self, "auto_commit", "Commit", box=False)

        self.scene = QtGui.QGraphicsScene()
        self.view = QtGui.QGraphicsView(self.scene)
        self.view.setRenderHint(QtGui.QPainter.Antialiasing, True)
        self.view.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        self.mainArea.layout().addWidget(self.view)

    def sizeHint(self):
        sh = self.controlArea.sizeHint()
        return sh.expandedTo(QtCore.QSize(600, 720))

    def set_data(self, data):
        """
        Set the input data set.
        """
        self.closeContext()
        self.clear()
        error_msg = ""
        warning_msg = ""<commit_msg>owsilhouetteplot: Add support for (small) SqlTable inputs<commit_after>import sys
import itertools
import enum

from xml.sax.saxutils import escape
from types import SimpleNamespace as namespace

import numpy
import sklearn.metrics

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QEvent, QRectF, QSizeF
from PyQt4.QtCore import pyqtSignal as Signal

import pyqtgraph as pg

import Orange.data
import Orange.distance

from Orange.widgets import widget, gui, settings
from Orange.widgets.utils import itemmodels
from Orange.widgets.utils.sql import check_sql_input
from Orange.widgets.unsupervised.owhierarchicalclustering import \
    WrapperLayoutItem


class OWSilhouettePlot(widget.OWWidget):
    name = "Silhouette Plot"
    description = "Silhouette Plot"

    icon = "icons/Silhouette.svg"

    inputs = [("Data", Orange.data.Table, "set_data")]
    outputs = [("Selected Data", Orange.data.Table, widget.Default),
               ("Other Data", Orange.data.Table)]

    replaces = ["orangecontrib.prototypes.widgets.owsilhouetteplot.OWSilhouettePlot"]

    settingsHandler = settings.PerfectDomainContextHandler()

    #: Distance metric index
    distance_idx = settings.Setting(0)
    #: Group/cluster variable index
    cluster_var_idx = settings.ContextSetting(0)
    #: Annotation variable index
    annotation_var_idx = settings.ContextSetting(0)
    #: Group the silhouettes by cluster
    group_by_cluster = settings.Setting(True)
    #: A fixed size for an instance bar
    bar_size = settings.Setting(3)
    #: Add silhouette scores to output data
    add_scores = settings.Setting(False)
    auto_commit = settings.Setting(False)

    Distances = [("Euclidean", Orange.distance.Euclidean),
                 ("Manhattan", Orange.distance.Manhattan)]

    def __init__(self):
        super().__init__()

        self.data = None
        self._effective_data = None
        self._matrix = None
        self._silhouette = None
        self._labels = None
        self._silplot = None

        box = gui.widgetBox(self.controlArea, "Settings",)
        gui.comboBox(box, self, "distance_idx", label="Distance",
                     items=[name for name, _ in OWSilhouettePlot.Distances],
                     callback=self._invalidate_distances)
        self.cluster_var_cb = gui.comboBox(
            box, self, "cluster_var_idx", label="Cluster",
            callback=self._invalidate_scores)
        self.cluster_var_model = itemmodels.VariableListModel(parent=self)
        self.cluster_var_cb.setModel(self.cluster_var_model)

        gui.spin(box, self, "bar_size", minv=1, maxv=10, label="Bar Size",
                 callback=self._update_bar_size)

        gui.checkBox(box, self, "group_by_cluster", "Group by cluster",
                     callback=self._replot)

        self.annotation_cb = gui.comboBox(
            box, self, "annotation_var_idx", label="Annotations",
            callback=self._update_annotations)
        self.annotation_var_model = itemmodels.VariableListModel(parent=self)
        self.annotation_var_model[:] = ["None"]
        self.annotation_cb.setModel(self.annotation_var_model)

        gui.rubber(self.controlArea)

        box = gui.widgetBox(self.controlArea, "Output")
        gui.checkBox(box, self, "add_scores", "Add silhouette scores",)
        gui.auto_commit(box, self, "auto_commit", "Commit", box=False)

        self.scene = QtGui.QGraphicsScene()
        self.view = QtGui.QGraphicsView(self.scene)
        self.view.setRenderHint(QtGui.QPainter.Antialiasing, True)
        self.view.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        self.mainArea.layout().addWidget(self.view)

    def sizeHint(self):
        sh = self.controlArea.sizeHint()
        return sh.expandedTo(QtCore.QSize(600, 720))

    @check_sql_input
    def set_data(self, data):
        """
        Set the input data set.
        """
        self.closeContext()
        self.clear()
        error_msg = ""
        warning_msg = ""
{'fraction_non_alphanumeric': 0.10086712573323132, 'fraction_numerical': 0.00408059168579444, 'mean_word_length': 3.0365414307771488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2004442', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2273, 'n_words': 491}