Example #1
0
    def set_data(self, data=None):
        self.closeContext()
        self.reset_widget()
        self.corpus = data
        self.search_features = []
        if data is not None:
            if not isinstance(data, Corpus):
                self.corpus = Corpus.from_table(data.domain, data)
            domain = self.corpus.domain
            # Enable/disable tokens checkbox
            if not self.corpus.has_tokens():
                self.show_tokens_checkbox.setCheckState(False)
            self.show_tokens_checkbox.setEnabled(self.corpus.has_tokens())

            self.search_features = list(filter_visible(chain(domain.variables, domain.metas)))
            self.display_features = list(filter_visible(chain(domain.variables, domain.metas)))
            self.search_indices = list(range(len(self.search_features)))
            self.display_indices = list(range(len(self.display_features)))
            self.selection = [0]
            self.openContext(self.corpus)
            self.display_list_indices = self.display_indices
            self.regenerate_docs()
            self.list_docs()
            self.update_info()
            self.set_selection()
            self.show_docs()
        self.commit()
Example #2
0
 def _set_items(self, items, axis=1):
     self.closeContext()
     self.items = items
     model = self.label_cb.model()
     if len(model) == 3:
         self.annotation_if_names = self.annotation
     elif len(model) == 2:
         self.annotation_if_enumerate = self.annotation
     if isinstance(items, Orange.data.Table) and axis:
         model[:] = chain(
             self.basic_annotations, [model.Separator],
             items.domain.class_vars, items.domain.metas,
             [model.Separator] if
             (items.domain.class_vars or items.domain.metas) and next(
                 filter_visible(items.domain.attributes), False) else [],
             filter_visible(items.domain.attributes))
         if items.domain.class_vars:
             self.annotation = items.domain.class_vars[0]
         else:
             self.annotation = "Enumeration"
         self.openContext(items.domain)
     else:
         name_option = bool(
             items is not None
             and (not axis or isinstance(items, list) and all(
                 isinstance(var, Orange.data.Variable) for var in items)))
         model[:] = self.basic_annotations + ["Name"] * name_option
         self.annotation = self.annotation_if_names if name_option \
             else self.annotation_if_enumerate
Example #3
0
    def set_data(self, corpus=None):
        self.closeContext()
        self.reset_widget()
        self.corpus = corpus
        self.search_features = []
        if corpus is not None:
            domain = self.corpus.domain
            # Enable/disable tokens checkbox
            if not self.corpus.has_tokens():
                self.show_tokens_checkbox.setCheckState(False)
            self.show_tokens_checkbox.setEnabled(self.corpus.has_tokens())

            self.search_features = list(filter_visible(chain(domain.variables, domain.metas)))
            self.display_features = list(filter_visible(chain(domain.variables, domain.metas)))
            self.search_indices = list(range(len(self.search_features)))
            self.display_indices = list(range(len(self.display_features)))
            self.selection = [0]
            self.openContext(self.corpus)
            self.display_list_indices = self.display_indices
            self.regenerate_docs()
            self.list_docs()
            self.update_info()
            self.set_selection()
            self.show_docs()
        self.commit()
Example #4
0
    def set_data(self, corpus=None):
        self.closeContext()
        self.reset_widget()
        self.corpus = corpus
        self.search_features = []
        if corpus is not None:
            domain = self.corpus.domain
            # Enable/disable tokens checkbox
            if not self.corpus.has_tokens():
                self.show_tokens_checkbox.setCheckState(False)
            self.show_tokens_checkbox.setEnabled(self.corpus.has_tokens())

            self.search_features = list(
                filter_visible(chain(domain.variables, domain.metas)))
            self.display_features = list(
                filter_visible(chain(domain.variables, domain.metas)))
            self.search_indices = list(range(len(self.search_features)))
            self.display_indices = list(range(len(self.display_features)))
            self.selected_documents = [corpus.titles[0]]
            self.openContext(self.corpus)
            self.display_list_indices = self.display_indices
            self.regenerate_docs()
            self.list_docs()
            self.update_info()
            self.set_selection()
            self.show_docs()
        self.commit()
Example #5
0
    def load_features(self):
        self.search_indices = []
        self.display_indices = []
        if self.corpus is not None:
            domain = self.corpus.domain
            self.search_features = list(filter_visible(chain(domain.variables, domain.metas)))
            self.display_features = list(filter_visible(chain(domain.variables, domain.metas)))
            # FIXME: Select features based on ContextSetting
            self.search_indices = list(range(len(self.search_features)))
            self.display_indices = list(range(len(self.display_features)))

            # Enable/disable tokens checkbox
            if not self.corpus.has_tokens():
                self.show_tokens_checkbox.setCheckState(False)
            self.show_tokens_checkbox.setEnabled(self.corpus.has_tokens())
Example #6
0
 def _visible_variables(cls, domain):
     """Generate variables in order they should be presented in in combos."""
     return chain(
         cls.AllTypes,
         filter_visible(chain(domain.class_vars,
                              domain.metas,
                              domain.attributes)))
Example #7
0
    def add_row(self, attr=None, condition_type=None, condition_value=None):
        model = self.cond_list.model()
        row = model.rowCount()
        model.insertRow(row)

        attr_combo = QComboBox(
            minimumContentsLength=12,
            sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLengthWithIcon)
        attr_combo.row = row
        for var in filter_visible(chain(self.data.domain.variables, self.data.domain.metas)):
            attr_combo.addItem(*gui.attributeItem(var))
        attr_combo.setCurrentIndex(attr or 0)
        self.cond_list.setCellWidget(row, 0, attr_combo)

        index = QPersistentModelIndex(model.index(row, 3))
        temp_button = QPushButton('×', self, flat=True,
                                  styleSheet='* {font-size: 16pt; color: silver}'
                                             '*:hover {color: black}')
        temp_button.clicked.connect(lambda: self.remove_one(index.row()))
        self.cond_list.setCellWidget(row, 3, temp_button)

        self.remove_all_button.setDisabled(False)
        self.set_new_operators(attr_combo, attr is not None,
                               condition_type, condition_value)
        attr_combo.currentIndexChanged.connect(
            lambda _: self.set_new_operators(attr_combo, False))

        self.cond_list.resizeRowToContents(row)
Example #8
0
    def set_data(self, data):
        self.closeContext()
        self.data = data
        self.cb_pa.setEnabled(not isinstance(data, SqlTable))
        self.cb_pc.setEnabled(not isinstance(data, SqlTable))
        self.remove_all_rows()
        self.add_button.setDisabled(data is None)
        self.add_all_button.setDisabled(
            data is None
            or len(data.domain.variables) + len(data.domain.metas) > 100)
        if not data:
            self.data_desc = None
            self.commit()
            return
        self.data_desc = report.describe_data_brief(data)
        self.conditions = []
        try:
            self.openContext(data)
        except Exception:
            pass

        variables = list(
            filter_visible(chain(data.domain.variables, data.domain.metas)))
        varnames = [v.name for v in variables]
        if self.conditions:
            for attr, cond_type, cond_value in self.conditions:
                if attr in varnames:
                    self.add_row(varnames.index(attr), cond_type, cond_value)
        elif variables:
            self.add_row()

        self.update_info(data, self.data_in_variables, "In: ")
        self.unconditional_commit()
Example #9
0
    def update_controls(self):
        if self.data is None and getattr(self.matrix, 'axis', 1) == 0:
            # Column-wise distances
            attr = "Attribute names"
            self.labelvar_model[:] = ["No labels", attr]
            self.shapevar_model[:] = ["Same shape", attr]
            self.colorvar_model[:] = ["Same solor", attr]

            self.color_value = attr
            self.shape_value = attr
        else:
            # initialize the graph state from data
            domain = self.data.domain
            all_vars = list(filter_visible(domain.variables + domain.metas))
            cd_vars = [var for var in all_vars if var.is_primitive()]
            disc_vars = [var for var in all_vars if var.is_discrete]
            cont_vars = [var for var in all_vars if var.is_continuous]
            shape_vars = [var for var in disc_vars
                          if len(var.values) <= len(ScatterPlotItem.Symbols) - 1]
            self.colorvar_model[:] = chain(["Same color"],
                                           [self.colorvar_model.Separator] if cd_vars else [],
                                           cd_vars)
            self.shapevar_model[:] = chain(["Same shape"],
                                           [self.shapevar_model.Separator] if shape_vars else [],
                                           shape_vars)
            self.sizevar_model[:] = chain(["Same size", "Stress"],
                                          [self.sizevar_model.Separator] if cont_vars else [],
                                          cont_vars)
            self.labelvar_model[:] = chain(["No labels"],
                                           [self.labelvar_model.Separator] if all_vars else [],
                                           all_vars)

            if domain.class_var is not None:
                self.color_value = domain.class_var.name
Example #10
0
    def add_row(self, attr=None, condition_type=None, condition_value=None):
        model = self.cond_list.model()
        row = model.rowCount()
        model.insertRow(row)

        attr_combo = QComboBox(
            minimumContentsLength=12,
            sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLengthWithIcon)
        attr_combo.row = row
        for var in filter_visible(
                chain(self.data.domain.variables, self.data.domain.metas)):
            attr_combo.addItem(*gui.attributeItem(var))
        attr_combo.setCurrentIndex(attr or 0)
        self.cond_list.setCellWidget(row, 0, attr_combo)

        index = QPersistentModelIndex(model.index(row, 3))
        temp_button = QPushButton(
            '×',
            self,
            flat=True,
            styleSheet='* {font-size: 16pt; color: silver}'
            '*:hover {color: black}')
        temp_button.clicked.connect(lambda: self.remove_one(index.row()))
        self.cond_list.setCellWidget(row, 3, temp_button)

        self.remove_all_button.setDisabled(False)
        self.set_new_operators(attr_combo, attr is not None, condition_type,
                               condition_value)
        attr_combo.currentIndexChanged.connect(
            lambda _: self.set_new_operators(attr_combo, False))

        self.cond_list.resizeRowToContents(row)
Example #11
0
    def test_filter_visible(self):
        n_feats = len(self.iris.domain.attributes)

        self.iris.domain.attributes[0].attributes.update({'hidden': True})
        filtered = list(filter_visible(self.iris.domain.attributes))
        self.assertNotIn(self.iris.domain.attributes[0], filtered)
        self.assertEqual(len(filtered), n_feats - 1)
Example #12
0
    def set_data(self, data):
        self.closeContext()
        self.data = data
        self.cb_pa.setEnabled(not isinstance(data, SqlTable))
        self.cb_pc.setEnabled(not isinstance(data, SqlTable))
        self.remove_all_rows()
        self.add_button.setDisabled(data is None)
        self.add_all_button.setDisabled(
            data is None or
            len(data.domain.variables) + len(data.domain.metas) > 100)
        if not data:
            self.data_desc = None
            self.commit()
            return
        self.data_desc = report.describe_data_brief(data)
        self.conditions = []
        try:
            self.openContext(data)
        except Exception:
            pass

        if not self.conditions and len(data.domain.variables):
            self.add_row()
        self.update_info(data, self.data_in_variables, "In: ")
        for attr, cond_type, cond_value in self.conditions:
            attrs = [a.name for a in
                     filter_visible(chain(data.domain.variables, data.domain.metas))]
            if attr in attrs:
                self.add_row(attrs.index(attr), cond_type, cond_value)
        self.unconditional_commit()
    def test_filter_visible(self):
        n_feats = len(self.iris.domain.attributes)

        self.iris.domain.attributes[0].attributes.update({"hidden": True})
        filtered = list(filter_visible(self.iris.domain.attributes))
        self.assertNotIn(self.iris.domain.attributes[0], filtered)
        self.assertEqual(len(filtered), n_feats - 1)
Example #14
0
    def load_features(self):
        self.search_indices = []
        self.display_indices = []
        if self.corpus is not None:
            domain = self.corpus.domain
            self.search_features = list(
                filter_visible(chain(domain.variables, domain.metas)))
            self.display_features = list(
                filter_visible(chain(domain.variables, domain.metas)))
            # FIXME: Select features based on ContextSetting
            self.search_indices = list(range(len(self.search_features)))
            self.display_indices = list(range(len(self.display_features)))

            # Enable/disable tokens checkbox
            if not self.corpus.has_tokens():
                self.show_tokens_checkbox.setCheckState(False)
            self.show_tokens_checkbox.setEnabled(self.corpus.has_tokens())
Example #15
0
 def __group_aggregations(self, agg_funs):
     auto_funcs = self.AutonomousFunctions
     var_indep_funs = [fun for fun in agg_funs if fun in auto_funcs]
     var_dep_funs = []
     attrs = self._table.domain.variables + self._table.domain.metas
     prod = product(filter_visible(attrs),
                    [fun for fun in agg_funs if fun not in auto_funcs])
     for var, fun in prod:
         if self.__include_aggregation(fun, var):
             var_dep_funs.append((var, fun))
     return var_indep_funs, var_dep_funs
Example #16
0
 def set_items(self, items, axis=1):
     self.items = items
     model = self.annot_combo.model()
     if items is None:
         model[:] = ["None", "Enumeration"]
     elif not axis:
         model[:] = ["None", "Enumeration", "Attribute names"]
     elif isinstance(items, Orange.data.Table):
         annot_vars = list(filter_visible(items.domain.variables)) + list(items.domain.metas)
         model[:] = ["None", "Enumeration"] + annot_vars
         self.annotation_idx = 0
         self.openContext(items.domain)
     elif isinstance(items, list) and \
             all(isinstance(item, Orange.data.Variable) for item in items):
         model[:] = ["None", "Enumeration", "Name"]
     else:
         model[:] = ["None", "Enumeration"]
     self.annotation_idx = min(self.annotation_idx, len(model) - 1)
Example #17
0
 def set_items(self, items, axis=1):
     self.items = items
     model = self.annot_combo.model()
     if items is None:
         model[:] = ["None", "Enumeration"]
     elif not axis:
         model[:] = ["None", "Enumeration", "Attribute names"]
     elif isinstance(items, Orange.data.Table):
         annot_vars = list(filter_visible(items.domain.variables)) + list(items.domain.metas)
         model[:] = ["None", "Enumeration"] + annot_vars
         self.annotation_idx = 0
         self.openContext(items.domain)
     elif isinstance(items, list) and \
             all(isinstance(item, Orange.data.Variable) for item in items):
         model[:] = ["None", "Enumeration", "Name"]
     else:
         model[:] = ["None", "Enumeration"]
     self.annotation_idx = min(self.annotation_idx, len(model) - 1)
Example #18
0
 def set_domain(self, domain):
     self.beginResetModel()
     content = []
     # The logic related to separators is a bit complicated: it ensures that
     # even when a section is empty we don't have two separators in a row
     # or a separator at the end
     add_separator = False
     for section in self.order:
         if section is self.Separator:
             add_separator = True
             continue
         if isinstance(section, int):
             if domain is None:
                 continue
             to_add = list(
                 chain(*(vars for i, vars in enumerate((domain.attributes,
                                                        domain.class_vars,
                                                        domain.metas))
                         if (1 << i) & section)))
             if self.skip_hidden_vars:
                 to_add = list(filter_visible(to_add))
             if self.valid_types is not None:
                 to_add = [
                     var for var in to_add
                     if isinstance(var, self.valid_types)
                 ]
             if self.alphabetical:
                 to_add = sorted(to_add, key=lambda x: x.name)
         elif isinstance(section, list):
             to_add = section
         else:
             to_add = [section]
         if to_add:
             if add_separator and content:
                 content.append(self.Separator)
                 add_separator = False
             content += to_add
     try:
         self._within_set_domain = True
         self[:] = content
     finally:
         self._within_set_domain = False
     self.endResetModel()
Example #19
0
def find_lat_lon(data, filter_hidden=False, fallback=True):
    """Return inferred latitude and longitude attributes as found in the data domain"""
    assert isinstance(data, Table)

    all_vars = list(chain(data.domain.variables, data.domain.metas))
    if filter_hidden:
        all_vars = list(filter_visible(all_vars))

    lat_attr = next(
        (attr for attr in all_vars
         if attr.is_continuous and attr.name.lower().startswith(('latitude',
                                                                 'lat'))),
        None)
    lon_attr = next((attr for attr in all_vars
                     if attr.is_continuous and attr.name.lower().startswith((
                         'longitude', 'lng', 'long', 'lon'))), None)

    if not fallback:
        return lat_attr, lon_attr

    def _all_between(vals, min, max):
        return np.all((min <= vals) & (vals <= max))

    if not lat_attr:
        for attr in all_vars:
            if attr.is_continuous:
                values = np.nan_to_num(
                    data.get_column_view(attr)[0].astype(float))
                if _all_between(values, -90, 90):
                    lat_attr = attr
                    break
    if not lon_attr:
        for attr in all_vars:
            if attr.is_continuous and attr is not lat_attr:
                values = np.nan_to_num(
                    data.get_column_view(attr)[0].astype(float))
                if _all_between(values, -180, 180):
                    lon_attr = attr
                    break

    return lat_attr, lon_attr
Example #20
0
    def add_row(self, attr=None, condition_type=None, condition_value=None):
        model = self.cond_list.model()
        row = model.rowCount()
        model.insertRow(row)

        attr_combo = QtGui.QComboBox(
            minimumContentsLength=12,
            sizeAdjustPolicy=QtGui.QComboBox.AdjustToMinimumContentsLengthWithIcon)
        attr_combo.row = row
        for var in filter_visible(chain(self.data.domain.variables, self.data.domain.metas)):
            attr_combo.addItem(*gui.attributeItem(var))
        attr_combo.setCurrentIndex(attr or 0)
        self.cond_list.setCellWidget(row, 0, attr_combo)

        self.remove_all_button.setDisabled(False)
        self.set_new_operators(attr_combo, attr is not None,
                               condition_type, condition_value)
        attr_combo.currentIndexChanged.connect(
            lambda _: self.set_new_operators(attr_combo, False))

        self.cond_list.resizeRowToContents(row)
Example #21
0
 def set_domain(self, domain):
     self.beginResetModel()
     content = []
     # The logic related to separators is a bit complicated: it ensures that
     # even when a section is empty we don't have two separators in a row
     # or a separator at the end
     add_separator = False
     for section in self.order:
         if section is self.Separator:
             add_separator = True
             continue
         if isinstance(section, int):
             if domain is None:
                 continue
             to_add = list(chain(
                 *(vars for i, vars in enumerate(
                     (domain.attributes, domain.class_vars, domain.metas))
                   if (1 << i) & section)))
             if self.skip_hidden_vars:
                 to_add = list(filter_visible(to_add))
             if self.valid_types is not None:
                 to_add = [var for var in to_add
                           if isinstance(var, self.valid_types)]
             if self.alphabetical:
                 to_add = sorted(to_add, key=lambda x: x.name)
         elif isinstance(section, list):
             to_add = section
         else:
             to_add = [section]
         if to_add:
             if add_separator and content:
                 content.append(self.Separator)
                 add_separator = False
             content += to_add
     try:
         self._within_set_domain = True
         self[:] = content
     finally:
         self._within_set_domain = False
     self.endResetModel()
Example #22
0
    def add_row(self, attr=None, condition_type=None, condition_value=None):
        model = self.cond_list.model()
        row = model.rowCount()
        model.insertRow(row)

        attr_combo = QComboBox(
            minimumContentsLength=12,
            sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLengthWithIcon)
        attr_combo.row = row
        for var in filter_visible(
                chain(self.data.domain.variables, self.data.domain.metas)):
            attr_combo.addItem(*gui.attributeItem(var))
        attr_combo.setCurrentIndex(attr or 0)
        self.cond_list.setCellWidget(row, 0, attr_combo)

        self.remove_all_button.setDisabled(False)
        self.set_new_operators(attr_combo, attr is not None, condition_type,
                               condition_value)
        attr_combo.currentIndexChanged.connect(
            lambda _: self.set_new_operators(attr_combo, False))

        self.cond_list.resizeRowToContents(row)
Example #23
0
 def _visible_variables(domain):
     """Generate variables in order they should be presented in in combos."""
     return filter_visible(chain(domain.class_vars,
                                 domain.metas,
                                 domain.attributes))