(self)
Recompute and update the counts of matches.
Recompute and update the counts of matches.
Recompute and update the counts of matches .
def update_counts(self):
        """Recompute and update the counts of matches."""
        def _matcher(strings, pattern):
            """Return indices of strings into patterns; consider case
            sensitivity and matching at the beginning. The given strings are
            assumed to be in lower case if match is case insensitive. Patterns
            are fixed on the fly."""
            if not self.case_sensitive:
                pattern = pattern.lower()
            indices = np.char.find(strings, pattern.strip())
            return indices == 0 if self.match_beginning else indices != -1

        def _lower_if_needed(strings):
            return strings if self.case_sensitive else np.char.lower(strings)

        def _string_counts():
            """
            Generate pairs of arrays for each rule until running out of data
            instances. np.sum over the two arrays in each pair gives the
            number of matches of the remaining instances (considering the
            order of patterns) and of the original data.

            For _string_counts, the arrays contain bool masks referring to the
            original data
            """
            nonlocal data
            data = data.astype(str)
            data = data[~np.char.equal(data, "")]
            data = _lower_if_needed(data)
            remaining = np.array(data)
            for _, pattern in self.active_rules:
                matching = _matcher(remaining, pattern)
                total_matching = _matcher(data, pattern)
                yield matching, total_matching
                remaining = remaining[~matching]
                if not remaining.size:
                    break

        def _discrete_counts():
            """
            Generate pairs similar to _string_counts, except that the arrays
            contain bin counts for the attribute's values matching the pattern.
            """
            attr_vals = np.array(attr.values)
            attr_vals = _lower_if_needed(attr_vals)
            bins = bincount(data, max_val=len(attr.values) - 1)[0]
            remaining = np.array(bins)
            for _, pattern in self.active_rules:
                matching = _matcher(attr_vals, pattern)
                yield remaining[matching], bins[matching]
                remaining[matching] = 0
                if not np.any(remaining):
                    break

        def _clear_labels():
            """Clear all labels"""
            for lab_matched, lab_total in self.counts:
                lab_matched.setText("")
                lab_total.setText("")

        def _set_labels():
            """Set the labels to show the counts"""
            for (n_matched, n_total), (lab_matched, lab_total), (lab, patt) in \
                    zip(self.match_counts, self.counts, self.active_rules):
                n_before = n_total - n_matched
                lab_matched.setText("{}".format(n_matched))
                if n_before and (lab or patt):
                    lab_total.setText("+ {}".format(n_before))
                    if n_matched:
                        tip = "{} of the {} matching instances are already " \
                              "covered above".format(n_before, n_total)
                    else:
                        tip = "All matching instances are already covered above"
                    lab_total.setToolTip(tip)
                    lab_matched.setToolTip(tip)

        def _set_placeholders():
            """Set placeholders for empty edit lines"""
            matches = [n for n, _ in self.match_counts] + \
                      [0] * len(self.line_edits)
            for n_matched, (_, patt) in zip(matches, self.line_edits):
                if not patt.text():
                    patt.setPlaceholderText(
                        "(remaining instances)" if n_matched else "(unused)")

            labels = self.class_labels()
            for label, (lab_edit, _) in zip(labels, self.line_edits):
                if not lab_edit.text():
                    lab_edit.setPlaceholderText(label)

        _clear_labels()
        attr = self.attribute
        if attr is None:
            return
        counters = {StringVariable: _string_counts,
                    DiscreteVariable: _discrete_counts}
        data = self.data.get_column_view(attr)[0]
        self.match_counts = [[int(np.sum(x)) for x in matches]
                             for matches in counters[type(attr)]()]
        _set_labels()
        _set_placeholders()
def update_counts ( self ) : def _matcher ( strings , pattern ) : """Return indices of strings into patterns; consider case
            sensitivity and matching at the beginning. The given strings are
            assumed to be in lower case if match is case insensitive. Patterns
            are fixed on the fly.""" if not self . case_sensitive : pattern = pattern . lower ( ) indices = np . char . find ( strings , pattern . strip ( ) ) return indices == 0 if self . match_beginning else indices != - 1 def _lower_if_needed ( strings ) : return strings if self . case_sensitive else np . char . lower ( strings ) def _string_counts ( ) : """
            Generate pairs of arrays for each rule until running out of data
            instances. np.sum over the two arrays in each pair gives the
            number of matches of the remaining instances (considering the
            order of patterns) and of the original data.

            For _string_counts, the arrays contain bool masks referring to the
            original data
            """ nonlocal data data = data . astype ( str ) data = data [ ~ np . char . equal ( data , "" ) ] data = _lower_if_needed ( data ) remaining = np . array ( data ) for _ , pattern in self . active_rules : matching = _matcher ( remaining , pattern ) total_matching = _matcher ( data , pattern ) yield matching , total_matching remaining = remaining [ ~ matching ] if not remaining . size : break def _discrete_counts ( ) : """
            Generate pairs similar to _string_counts, except that the arrays
            contain bin counts for the attribute's values matching the pattern.
            """ attr_vals = np . array ( attr . values ) attr_vals = _lower_if_needed ( attr_vals ) bins = bincount ( data , max_val = len ( attr . values ) - 1 ) [ 0 ] remaining = np . array ( bins ) for _ , pattern in self . active_rules : matching = _matcher ( attr_vals , pattern ) yield remaining [ matching ] , bins [ matching ] remaining [ matching ] = 0 if not np . any ( remaining ) : break def _clear_labels ( ) : """Clear all labels""" for lab_matched , lab_total in self . counts : lab_matched . setText ( "" ) lab_total . setText ( "" ) def _set_labels ( ) : """Set the labels to show the counts""" for ( n_matched , n_total ) , ( lab_matched , lab_total ) , ( lab , patt ) in zip ( self . match_counts , self . counts , self . active_rules ) : n_before = n_total - n_matched lab_matched . setText ( "{}" . format ( n_matched ) ) if n_before and ( lab or patt ) : lab_total . setText ( "+ {}" . format ( n_before ) ) if n_matched : tip = "{} of the {} matching instances are already " "covered above" . format ( n_before , n_total ) else : tip = "All matching instances are already covered above" lab_total . setToolTip ( tip ) lab_matched . setToolTip ( tip ) def _set_placeholders ( ) : """Set placeholders for empty edit lines""" matches = [ n for n , _ in self . match_counts ] + [ 0 ] * len ( self . line_edits ) for n_matched , ( _ , patt ) in zip ( matches , self . line_edits ) : if not patt . text ( ) : patt . setPlaceholderText ( "(remaining instances)" if n_matched else "(unused)" ) labels = self . class_labels ( ) for label , ( lab_edit , _ ) in zip ( labels , self . line_edits ) : if not lab_edit . text ( ) : lab_edit . setPlaceholderText ( label ) _clear_labels ( ) attr = self . attribute if attr is None : return counters = { StringVariable : _string_counts , DiscreteVariable : _discrete_counts } data = self . data . get_column_view ( attr ) [ 0 ] self . match_counts = [ [ int ( np . sum ( x ) ) for x in matches ] for matches in counters [ type ( attr ) ] ( ) ] _set_labels ( ) _set_placeholders ( )