Ejemplo n.º 1
0
 def test_compare_string(self):
     zoo = Table('zoo')
     zoo1 = zoo[0]['name']  # aardvark
     zoo2 = zoo[1]['name']  # antelope
     self.assertTrue(zoo1 < zoo2)
     self.assertTrue(zoo1 >= "aardvark")
Ejemplo n.º 2
0
    def generateGraph(self):
        self.Error.clear()
        self.Warning.clear()
        matrix = None

        if self.graphMatrix is None:
            if hasattr(self, "infoa"):
                self.infoa.setText("No data loaded.")
            if hasattr(self, "infob"):
                self.infob.setText("")
            if hasattr(self, "infoc"):
                self.infoc.setText("")
            self.pconnected = 0
            self.nedges = 0
            self.graph = None
            return

        nEdges = len(self.graphMatrix) * self.kNN

        if nEdges > 200000:
            self.graph = None
            self.Error.number_of_edges(nEdges)
        else:
            items = None
            matrix = self.graphMatrix
            if matrix is not None and matrix.row_items is not None:
                row_items = self.graphMatrix.row_items
                if isinstance(row_items, Table):
                    if self.graphMatrix.axis == 1:
                        items = row_items
                    else:
                        items = [[v.name] for v in row_items.domain.attributes]
                else:
                    items = [[str(x)] for x in self.graphMatrix.row_items]
            if len(items) != self.graphMatrix.shape[0]:
                self.Warning.invalid_number_of_items()
                items = None
            if items is None:
                items = list(range(self.graphMatrix.shape[0]))
            if not isinstance(items, Table):
                items = Table(
                    Domain([], metas=[StringVariable('label')]),
                    items)

            self.Warning.kNN_too_large.clear()
            if self.kNN >= self.graphMatrix.shape[0]:
                self.Warning.kNN_too_large(self.graphMatrix.shape[0] - 1)

            nb_data = len(matrix)
            row_index = []
            col_index = []
            distances_data = []

            for i in range(nb_data):
                distances = []
                for j in range(nb_data):
                    distances.append((self.graphMatrix[i][j], j))
                distances.sort()
                for k in range(self.kNN):
                    row_index.append(i)
                    col_index.append(distances[k][1])
                    distances_data.append(distances[k][0])

            row = np.array(row_index)
            col = np.array(col_index)
            weights = np.array(distances_data)

            edges = sp.csr_matrix((weights, (row, col)))
            graph = Network(items, edges)

            self.graph = graph

        if self.graph is None:
            self.pconnected = 0
            self.nedges = 0
        else:
            self.pconnected = self.graph.number_of_nodes()
            self.nedges = self.graph.number_of_edges()
        if hasattr(self, "infoa"):
            self.infoa.setText("Data items on input: %d" % self.graphMatrix.shape[0])
        if hasattr(self, "infob"):
            self.infob.setText("Network nodes: %d (%3.1f%%)" % (self.pconnected,
                self.pconnected / float(self.graphMatrix.shape[0]) * 100))
        if hasattr(self, "infoc"):
            self.infoc.setText("Network edges: %d (%.2f edges/node)" % (
                self.nedges, self.nedges / float(self.pconnected)
                if self.pconnected else 0))

        self.Warning.large_number_of_nodes.clear()
        if self.pconnected > 1000 or self.nedges > 2000:
            self.Warning.large_number_of_nodes()
        
        self.send_network()
Ejemplo n.º 3
0
    def __call__(self, data, ret=Value):
        if not 0 <= ret <= 2:
            raise ValueError("invalid value of argument 'ret'")
        if (ret > 0 and any(v.is_continuous for v in self.domain.class_vars)):
            raise ValueError("cannot predict continuous distributions")

        # Call the predictor
        if isinstance(data, np.ndarray):
            prediction = self.predict(np.atleast_2d(data))
        elif isinstance(data, scipy.sparse.csr.csr_matrix):
            prediction = self.predict(data)
        elif isinstance(data, Instance):
            if data.domain != self.domain:
                data = Instance(self.domain, data)
            data = Table(data.domain, [data])
            prediction = self.predict_storage(data)
        elif isinstance(data, Table):
            if data.domain != self.domain:
                data = data.from_table(self.domain, data)
            prediction = self.predict_storage(data)
        elif isinstance(data, (list, tuple)):
            if not isinstance(data[0], (list, tuple)):
                data = [data]
            data = Table(self.original_domain, data)
            data = Table(self.domain, data)
            prediction = self.predict_storage(data)
        else:
            raise TypeError("Unrecognized argument (instance of '{}')".format(
                type(data).__name__))

        # Parse the result into value and probs
        multitarget = len(self.domain.class_vars) > 1
        if isinstance(prediction, tuple):
            value, probs = prediction
        elif prediction.ndim == 1 + multitarget:
            value, probs = prediction, None
        elif prediction.ndim == 2 + multitarget:
            value, probs = None, prediction
        else:
            raise TypeError("model returned a %i-dimensional array",
                            prediction.ndim)

        # Ensure that we have what we need to return
        if ret != Model.Probs and value is None:
            value = np.argmax(probs, axis=-1)
        if ret != Model.Value and probs is None:
            if multitarget:
                max_card = max(len(c.values) for c in self.domain.class_vars)
                probs = np.zeros(value.shape + (max_card, ), float)
                for i, cvar in enumerate(self.domain.class_vars):
                    probs[:, i, :], _ = bn.bincount(np.atleast_2d(value[:, i]),
                                                    max_card - 1)
            else:
                probs, _ = bn.bincount(np.atleast_2d(value),
                                       len(self.domain.class_var.values) - 1)
            if ret == Model.ValueProbs:
                return value, probs
            else:
                return probs

        # Return what we need to
        if ret == Model.Probs:
            return probs
        if isinstance(data, Instance) and not multitarget:
            value = Value(self.domain.class_var, value[0])
        if ret == Model.Value:
            return value
        else:  # ret == Model.ValueProbs
            return value, probs
Ejemplo n.º 4
0
    def moved(self):
        point = self.widget.rect().bottomRight()
        global_point = self.widget.mapToGlobal(point)
        self.move(global_point - QPoint(self.width(), 0))


class DropDownToolButton(QToolButton):
    def __init__(self, parent, var, lc):
        QToolButton.__init__(self, parent)
        self.desc_text = ''
        self.popup = CheckBoxPopup(var, lc, parent, self)
        self.setMenu(QMenu())  # to show arrow
        self.clicked.connect(self.open_popup)

    def open_popup(self):
        self.popup.moved()
        self.popup.show()

    def set_text(self):
        metrics = QFontMetrics(self.font())
        self.setText(
            metrics.elidedText(self.desc_text, Qt.ElideRight,
                               self.width() - 15))

    def resizeEvent(self, QResizeEvent):
        self.set_text()


if __name__ == "__main__":  # pragma: no cover
    WidgetPreview(OWSelectRows).run(Table("zoo"))
Ejemplo n.º 5
0
 def test_warnings(self):
     domain = Domain([ContinuousVariable("x")])
     self.assertWarns(OrangeDeprecationWarning, Table, domain)
     self.assertWarns(OrangeDeprecationWarning, Table, domain, Table())
     self.assertWarns(OrangeDeprecationWarning, Table, domain, [[12]])
     self.assertWarns(OrangeDeprecationWarning, Table, np.zeros((5, 5)))
Ejemplo n.º 6
0
 def setUpClass(cls):
     cls.data = Table("iris")
     cls.distances = Euclidean(cls.data)
     cls.init = torgerson(cls.distances)
     cls.args = (cls.distances, 300, 25, 0, cls.init)
Ejemplo n.º 7
0
 def test_string_variables(self):
     self.send_signal(self.widget.Inputs.data, Table("zoo"))
Ejemplo n.º 8
0
        items["Iteration limt"] = self.max_iter if self.limit_iter else "unlimited"
        return items

    def _report_kernel_parameters(self, items):
        gamma = self.gamma or self._default_gamma
        if self.kernel_type == 0:
            items["Kernel"] = "Linear"
        elif self.kernel_type == 1:
            items["Kernel"] = \
                "Polynomial, ({g:.4} x⋅y + {c:.4})<sup>{d}</sup>".format(
                    g=gamma, c=self.coef0, d=self.degree)
        elif self.kernel_type == 2:
            items["Kernel"] = "RBF, exp(-{:.4}|x-y|²)".format(gamma)
        else:
            items["Kernel"] = "Sigmoid, tanh({g:.4} x⋅y + {c:.4})".format(
                g=gamma, c=self.coef0)


if __name__ == "__main__":
    import sys
    from AnyQt.QtWidgets import QApplication

    a = QApplication(sys.argv)
    ow = OWSVM()
    ow.resetSettings()
    d = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')
    ow.set_data(d)
    ow.show()
    a.exec_()
    ow.saveSettings()
Ejemplo n.º 9
0
        else:
            self.invalidate()

    def send_report(self):
        # False positives (Setting is not recognized as int)
        # pylint: disable=invalid-sequence-index
        if self.optimize_k and self.selected_row() is not None:
            k_clusters = self.k_from + self.selected_row()
        else:
            k_clusters = self.k
        init_method = self.INIT_METHODS[self.smart_init][0]
        init_method = init_method[0].lower() + init_method[1:]
        self.report_items((
            ("Number of clusters", k_clusters),
            ("Optimization", "{}, {} re-runs limited to {} steps".format(
                init_method, self.n_init, self.max_iterations))))
        if self.data is not None:
            self.report_data("Data", self.data)
            if self.optimize_k:
                self.report_table(
                    "Silhouette scores for different numbers of clusters",
                    self.table_view)

    def onDeleteWidget(self):
        self.cancel()
        super().onDeleteWidget()


if __name__ == "__main__":  # pragma: no cover
    WidgetPreview(OWKMeans).run(Table("iris.tab"))
Ejemplo n.º 10
0
 def test_empty(self):
     autompg = Table('auto-mpg')
     learn = MeanLearner()
     clf = learn(autompg[:0])
     y = clf(autompg[0])
     self.assertTrue(y == 0)
Ejemplo n.º 11
0
 def test_discrete(self):
     iris = Table('iris')
     learn = MeanLearner()
     self.assertRaises(ValueError, learn, iris)
 def setUp(self):
     self.zoo = Table("zoo")
Ejemplo n.º 13
0
            params['Random seed for shuffling'] = self.random_state

        return list(params.items())

    def update_model(self):
        super().update_model()
        coeffs = None
        if self.model is not None:
            if self.model.domain.class_var.is_discrete:
                coeffs = create_coef_table(self.model)
            else:
                attrs = [ContinuousVariable("coef")]
                domain = Domain(attrs, metas=[StringVariable("name")])
                cfs = list(self.model.intercept) + list(
                    self.model.coefficients)
                names = ["intercept"] + \
                        [attr.name for attr in self.model.domain.attributes]
                coeffs = Table(domain, list(zip(cfs, names)))
                coeffs.name = "coefficients"
        self.Outputs.coefficients.send(coeffs)

    @classmethod
    def migrate_settings(cls, settings, version):
        if version < 2:
            settings["max_iter"] = settings.pop("n_iter", 5)
            settings["tol_enabled"] = False


if __name__ == "__main__":  # pragma: no cover
    WidgetPreview(OWSGD).run(Table("iris"))
Ejemplo n.º 14
0
 def setUpClass(cls):
     cls.ionosphere = Table('ionosphere')
Ejemplo n.º 15
0
        self.Outputs.preprocessor.send(prep)

    def clear(self):
        super().clear()
        self.projector = self.projection = None


if __name__ == "__main__":
    class OWProjectionWidgetWithName(OWDataProjectionWidget):
        name = "projection"

        def get_embedding(self):
            if self.data is None:
                return None
            self.valid_data = np.any(np.isfinite(self.data.X), 1)
            x_data = self.data.X
            x_data[x_data == np.inf] = np.nan
            x_data = np.nanmean(x_data[self.valid_data], 1)
            y_data = np.ones(len(x_data))
            return np.vstack((x_data, y_data)).T

    app = QApplication([])
    ow = OWProjectionWidgetWithName()
    table = Table("iris")
    ow.set_data(table)
    ow.set_subset_data(table[::10])
    ow.handleNewSignals()
    ow.show()
    app.exec_()
    ow.saveSettings()
Ejemplo n.º 16
0
    def send_data(self):
        if self.optimize_k:
            row = self.selected_row()
            k = self.k_from + row if row is not None else None
        else:
            k = self.k

        km = self.clusterings.get(k)
        if self.data is None or km is None or isinstance(km, str):
            self.Outputs.annotated_data.send(None)
            self.Outputs.centroids.send(None)
            return

        domain = self.data.domain
        cluster_var = DiscreteVariable(
            get_unique_names(domain, "Cluster"),
            values=["C%d" % (x + 1) for x in range(km.k)]
        )
        clust_ids = km(self.data)
        clust_col = clust_ids.X.ravel()
        silhouette_var = ContinuousVariable(
            get_unique_names(domain, "Silhouette"))
        if km.silhouette_samples is not None:
            self.Warning.no_silhouettes.clear()
            scores = np.arctan(km.silhouette_samples) / np.pi + 0.5
            clust_scores = []
            for i in range(km.k):
                in_clust = clust_col == i
                if in_clust.any():
                    clust_scores.append(np.mean(scores[in_clust]))
                else:
                    clust_scores.append(0.)
            clust_scores = np.atleast_2d(clust_scores).T
        else:
            self.Warning.no_silhouettes()
            scores = np.nan
            clust_scores = np.full((km.k, 1), np.nan)

        new_domain = add_columns(domain, metas=[cluster_var, silhouette_var])
        new_table = self.data.transform(new_domain)
        new_table.get_column_view(cluster_var)[0][:] = clust_col
        new_table.get_column_view(silhouette_var)[0][:] = scores

        centroid_attributes = [
            attr.compute_value.variable
            if isinstance(attr.compute_value, ReplaceUnknowns)
            and attr.compute_value.variable in domain.attributes
            else attr
            for attr in km.pre_domain.attributes]
        centroid_domain = add_columns(
            Domain(centroid_attributes, [], domain.metas),
            metas=[cluster_var, silhouette_var])
        centroids = Table(
            centroid_domain, km.centroids, None,
            np.hstack((np.full((km.k, len(domain.metas)), np.nan),
                       np.arange(km.k).reshape(km.k, 1),
                       clust_scores))
        )
        if self.data.name == Table.name:
            centroids.name = "centroids"
        else:
            centroids.name = f"{self.data.name} centroids"

        self.Outputs.annotated_data.send(new_table)
        self.Outputs.centroids.send(centroids)
Ejemplo n.º 17
0
 def setUp(self):
     self.widget = self.create_widget(OWDistanceMatrix)
     self.iris = Table("iris")[:5]
     self.distances = Euclidean(self.iris)
Ejemplo n.º 18
0
 def setUpClass(cls):
     cls.data = Table(test_filename("datasets/test5.tab"))
Ejemplo n.º 19
0
 def setUp(self):
     self.widget = self.create_widget(owcolor.OWColor)
     self.iris = Table("iris")
Ejemplo n.º 20
0
 def setUpClass(cls):
     cls.zoo = Table("zoo")  # disc. features, disc. class
     cls.housing = Table("housing")  # cont. features, cont. class
     cls.monk = Table("monks-1")
     cls.adult = Table("adult_sample")
Ejemplo n.º 21
0
 def setUp(self):
     self.widget = self.create_widget(
         OWLouvainClustering, stored_settings={'auto_commit': False})
     self.iris = Table('iris')[::5]
Ejemplo n.º 22
0
 def setUpClass(cls):
     cls.ionosphere = Table('ionosphere')
     cls.iris = Table('iris')
     cls.zoo = Table('zoo')
Ejemplo n.º 23
0
 def test_report(self):
     widget = self.widget
     data = Table("iris")[::5]
     self.send_signal(widget.Inputs.data, data)
     widget.send_report()
Ejemplo n.º 24
0
 def setUp(self):
     self.iris = Table('iris')
Ejemplo n.º 25
0
 def test_invalid_call_with_kwargs(self):
     self.assertRaises(TypeError, Table, Y=[])
     self.assertRaises(TypeError, Table, "iris", 42)
     self.assertRaises(TypeError, Table, Table(), 42)
Ejemplo n.º 26
0
 def setUp(self):
     self.widget = self.create_widget(OWConcatenate)
     self.iris = Table("iris")
     self.titanic = Table("titanic")
Ejemplo n.º 27
0
            settings.migrate_str_to_variable(context, names="lat_attr",
                                             none_placeholder="")
            settings.migrate_str_to_variable(context, names="lon_attr",
                                             none_placeholder="")
            settings.migrate_str_to_variable(context, names="class_attr",
                                             none_placeholder="(None)")

            # those settings can have two none placeholder
            attr_placeholders = [("color_attr", "(Same color)"),
                                 ("label_attr", "(No labels)"),
                                 ("shape_attr", "(Same shape)"),
                                 ("size_attr", "(Same size)")]
            for attr, place in attr_placeholders:
                if context.values[attr][0] == place:
                    context.values[attr] = ("", context.values[attr][1])

                settings.migrate_str_to_variable(context, names=attr,
                                                 none_placeholder="")
        if version < 3:
            settings.rename_setting(context, "lat_attr", "attr_lat")
            settings.rename_setting(context, "lon_attr", "attr_lon")
            settings.rename_setting(context, "color_attr", "attr_color")
            settings.rename_setting(context, "label_attr", "attr_label")
            settings.rename_setting(context, "shape_attr", "attr_shape")
            settings.rename_setting(context, "size_attr", "attr_size")


if __name__ == "__main__":
    data = Table("India_census_district_population")
    WidgetPreview(OWMap).run(data)
Ejemplo n.º 28
0
 def setUp(self):
     super().setUp()
     self.widget = self.create_widget(OWOutliers)
     self.iris = Table("iris")
     self.heart_disease = Table("heart_disease")
Ejemplo n.º 29
0
 def setUpClass(cls):
     cls.iris = Table("iris")
Ejemplo n.º 30
0
 def test_data_name(self):
     table1 = Table('iris')
     table2 = TabReader(table1.__file__).read()
     self.assertEqual(table1.name, 'iris')
     self.assertEqual(table2.name, 'iris')