forked from biolab/orange3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathowcreateclass.py
More file actions
567 lines (493 loc) · 22.4 KB
/
owcreateclass.py
File metadata and controls
567 lines (493 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
"""Widget for creating classes from non-numeric attribute by substrings"""
import re
from itertools import count
import numpy as np
from AnyQt.QtWidgets import QGridLayout, QLabel, QLineEdit, QSizePolicy
from AnyQt.QtCore import QSize, Qt
from Orange.data import StringVariable, DiscreteVariable, Domain
from Orange.data.table import Table
from Orange.statistics.util import bincount
from Orange.preprocess.transformation import Transformation, Lookup
from Orange.widgets import gui, widget
from Orange.widgets.settings import DomainContextHandler, ContextSetting
from Orange.widgets.utils.itemmodels import DomainModel
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.utils.state_summary import format_summary_details
from Orange.widgets.widget import Msg, Input, Output
def map_by_substring(a, patterns, case_sensitive, match_beginning):
"""
Map values in a using a list of patterns. The patterns are considered in
order of appearance.
Args:
a (np.array): input array of `dtype` `str`
patterns (list of str): list of stirngs
case_sensitive (bool): case sensitive match
match_beginning (bool): match only at the beginning of the string
Returns:
np.array of floats representing indices of matched patterns
"""
res = np.full(len(a), np.nan)
if not case_sensitive:
a = np.char.lower(a)
patterns = (pattern.lower() for pattern in patterns)
for val_idx, pattern in reversed(list(enumerate(patterns))):
indices = np.char.find(a, pattern)
matches = indices == 0 if match_beginning else indices != -1
res[matches] = val_idx
return res
class ValueFromStringSubstring(Transformation):
"""
Transformation that computes a discrete variable from a string variable by
pattern matching.
Given patterns `["abc", "a", "bc", ""]`, string data
`["abcd", "aa", "bcd", "rabc", "x"]` is transformed to values of the new
attribute with indices`[0, 1, 2, 0, 3]`.
Args:
variable (:obj:`~Orange.data.StringVariable`): the original variable
patterns (list of str): list of string patterns
case_sensitive (bool, optional): if set to `True`, the match is case
sensitive
match_beginning (bool, optional): if set to `True`, the pattern must
appear at the beginning of the string
"""
def __init__(self, variable, patterns,
case_sensitive=False, match_beginning=False):
super().__init__(variable)
self.patterns = patterns
self.case_sensitive = case_sensitive
self.match_beginning = match_beginning
def transform(self, c):
"""
Transform the given data.
Args:
c (np.array): an array of type that can be cast to dtype `str`
Returns:
np.array of floats representing indices of matched patterns
"""
nans = np.equal(c, None)
c = c.astype(str)
c[nans] = ""
res = map_by_substring(
c, self.patterns, self.case_sensitive, self.match_beginning)
res[nans] = np.nan
return res
def __eq__(self, other):
return super().__eq__(other) \
and self.patterns == other.patterns \
and self.case_sensitive == other.case_sensitive \
and self.match_beginning == other.match_beginning
def __hash__(self):
return hash((type(self), self.variable,
tuple(self.patterns),
self.case_sensitive, self.match_beginning))
class ValueFromDiscreteSubstring(Lookup):
"""
Transformation that computes a discrete variable from discrete variable by
pattern matching.
Say that the original attribute has values
`["abcd", "aa", "bcd", "rabc", "x"]`. Given patterns
`["abc", "a", "bc", ""]`, the values are mapped to the values of the new
attribute with indices`[0, 1, 2, 0, 3]`.
Args:
variable (:obj:`~Orange.data.DiscreteVariable`): the original variable
patterns (list of str): list of string patterns
case_sensitive (bool, optional): if set to `True`, the match is case
sensitive
match_beginning (bool, optional): if set to `True`, the pattern must
appear at the beginning of the string
"""
def __init__(self, variable, patterns,
case_sensitive=False, match_beginning=False):
super().__init__(variable, [])
self.case_sensitive = case_sensitive
self.match_beginning = match_beginning
self.patterns = patterns # Finally triggers computation of the lookup
def __setattr__(self, key, value):
"""__setattr__ is overloaded to recompute the lookup table when the
patterns, the original attribute or the flags change."""
super().__setattr__(key, value)
if hasattr(self, "patterns") and \
key in ("case_sensitive", "match_beginning", "patterns",
"variable"):
self.lookup_table = map_by_substring(
self.variable.values, self.patterns,
self.case_sensitive, self.match_beginning)
class OWCreateClass(widget.OWWidget):
name = "Create Class"
description = "Create class attribute from a string attribute"
icon = "icons/CreateClass.svg"
category = "Data"
keywords = []
class Inputs:
data = Input("Data", Table)
class Outputs:
data = Output("Data", Table)
want_main_area = False
settingsHandler = DomainContextHandler()
attribute = ContextSetting(None)
class_name = ContextSetting("class")
rules = ContextSetting({})
match_beginning = ContextSetting(False)
case_sensitive = ContextSetting(False)
TRANSFORMERS = {StringVariable: ValueFromStringSubstring,
DiscreteVariable: ValueFromDiscreteSubstring}
# Cached variables are used so that two instances of the widget with the
# same settings will create the same variable. The usual `make` wouldn't
# work here because variables with `compute_value` are not reused.
cached_variables = {}
class Warning(widget.OWWidget.Warning):
no_nonnumeric_vars = Msg("Data contains only numeric variables.")
class Error(widget.OWWidget.Error):
class_name_duplicated = Msg("Class name duplicated.")
class_name_empty = Msg("Class name should not be empty.")
def __init__(self):
super().__init__()
self.data = None
# The following lists are of the same length as self.active_rules
#: list of pairs with counts of matches for each patter when the
# patterns are applied in order and when applied on the entire set,
# disregarding the preceding patterns
self.match_counts = []
#: list of list of QLineEdit: line edit pairs for each pattern
self.line_edits = []
#: list of QPushButton: list of remove buttons
self.remove_buttons = []
#: list of list of QLabel: pairs of labels with counts
self.counts = []
combo = gui.comboBox(
self.controlArea, self, "attribute", label="From column: ",
box=True, orientation=Qt.Horizontal, searchable=True,
callback=self.update_rules,
model=DomainModel(valid_types=(StringVariable, DiscreteVariable)))
# Don't use setSizePolicy keyword argument here: it applies to box,
# not the combo
combo.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
patternbox = gui.vBox(self.controlArea, box=True)
#: QWidget: the box that contains the remove buttons, line edits and
# count labels. The lines are added and removed dynamically.
self.rules_box = rules_box = QGridLayout()
patternbox.layout().addLayout(self.rules_box)
box = gui.hBox(patternbox)
gui.button(
box, self, "+", callback=self.add_row, autoDefault=False, flat=True,
minimumSize=(QSize(20, 20)))
gui.rubber(box)
self.rules_box.setColumnMinimumWidth(1, 70)
self.rules_box.setColumnMinimumWidth(0, 10)
self.rules_box.setColumnStretch(0, 1)
self.rules_box.setColumnStretch(1, 1)
self.rules_box.setColumnStretch(2, 100)
rules_box.addWidget(QLabel("Name"), 0, 1)
rules_box.addWidget(QLabel("Substring"), 0, 2)
rules_box.addWidget(QLabel("#Instances"), 0, 3, 1, 2)
self.update_rules()
gui.lineEdit(
self.controlArea, self, "class_name",
label="Name for the new class:",
box=True, orientation=Qt.Horizontal)
optionsbox = gui.vBox(self.controlArea, box=True)
gui.checkBox(
optionsbox, self, "match_beginning", "Match only at the beginning",
callback=self.options_changed)
gui.checkBox(
optionsbox, self, "case_sensitive", "Case sensitive",
callback=self.options_changed)
box = gui.hBox(self.controlArea)
gui.rubber(box)
gui.button(box, self, "Apply", autoDefault=False, width=180,
callback=self.apply)
self.info.set_input_summary(self.info.NoInput)
self.info.set_output_summary(self.info.NoOutput)
# TODO: Resizing upon changing the number of rules does not work
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
@property
def active_rules(self):
"""
Returns the class names and patterns corresponding to the currently
selected attribute. If the attribute is not yet in the dictionary,
set the default.
"""
return self.rules.setdefault(self.attribute and self.attribute.name,
[["", ""], ["", ""]])
def rules_to_edits(self):
"""Fill the line edites with the rules from the current settings."""
for editr, textr in zip(self.line_edits, self.active_rules):
for edit, text in zip(editr, textr):
edit.setText(text)
@Inputs.data
def set_data(self, data):
"""Input data signal handler."""
self.closeContext()
self.rules = {}
self.data = data
summary = len(data) if data else self.info.NoInput
details = format_summary_details(data) if data else ""
self.info.set_input_summary(summary, details)
model = self.controls.attribute.model()
model.set_domain(data and data.domain)
self.Warning.no_nonnumeric_vars(shown=data is not None and not model)
if not model:
self.attribute = None
self.Outputs.data.send(None)
self.info.set_output_summary(self.info.NoOutput)
return
self.attribute = model[0]
self.openContext(data)
self.update_rules()
self.apply()
def update_rules(self):
"""Called when the rules are changed: adjust the number of lines in
the form and fill them, update the counts. The widget does not have
auto-apply."""
self.adjust_n_rule_rows()
self.rules_to_edits()
self.update_counts()
# TODO: Indicator that changes need to be applied
def options_changed(self):
self.update_counts()
def adjust_n_rule_rows(self):
"""Add or remove lines if needed and fix the tab order."""
def _add_line():
self.line_edits.append([])
n_lines = len(self.line_edits)
for coli in range(1, 3):
edit = QLineEdit()
self.line_edits[-1].append(edit)
self.rules_box.addWidget(edit, n_lines, coli)
edit.textChanged.connect(self.sync_edit)
button = gui.button(
None, self, label='×', flat=True, height=20,
styleSheet='* {font-size: 16pt; color: silver}'
'*:hover {color: black}',
autoDefault=False, callback=self.remove_row)
button.setMinimumSize(QSize(12, 20))
self.remove_buttons.append(button)
self.rules_box.addWidget(button, n_lines, 0)
self.counts.append([])
for coli, kwargs in enumerate(
(dict(alignment=Qt.AlignRight),
dict(alignment=Qt.AlignLeft, styleSheet="color: gray"))):
label = QLabel(**kwargs)
self.counts[-1].append(label)
self.rules_box.addWidget(label, n_lines, 3 + coli)
def _remove_line():
for edit in self.line_edits.pop():
edit.deleteLater()
self.remove_buttons.pop().deleteLater()
for label in self.counts.pop():
label.deleteLater()
def _fix_tab_order():
prev = None
for row, rule in zip(self.line_edits, self.active_rules):
for col_idx, edit in enumerate(row):
edit.row, edit.col_idx = rule, col_idx
if prev is not None:
self.setTabOrder(prev, edit)
prev = edit
n = len(self.active_rules)
while n > len(self.line_edits):
_add_line()
while len(self.line_edits) > n:
_remove_line()
_fix_tab_order()
def add_row(self):
"""Append a new row at the end."""
self.active_rules.append(["", ""])
self.adjust_n_rule_rows()
self.update_counts()
def remove_row(self):
"""Remove a row."""
remove_idx = self.remove_buttons.index(self.sender())
del self.active_rules[remove_idx]
self.update_rules()
self.update_counts()
def sync_edit(self, text):
"""Handle changes in line edits: update the active rules and counts"""
edit = self.sender()
edit.row[edit.col_idx] = text
self.update_counts()
def class_labels(self):
"""Construct a list of class labels. Empty labels are replaced with
C1, C2, C3. If C<n> already appears in the list of values given by
the user, the labels start at C<n+1> instead.
"""
largest_c = max((int(label[1:]) for label, _ in self.active_rules
if re.match("^C\\d+", label)),
default=0)
class_count = count(largest_c + 1)
return [label_edit.text() or "C{}".format(next(class_count))
for label_edit, _ in self.line_edits]
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 apply(self):
"""Output the transformed data."""
self.Error.clear()
self.class_name = self.class_name.strip()
if not self.attribute:
self.Outputs.data.send(None)
self.info.set_output_summary(self.info.NoOutput)
return
domain = self.data.domain
if not self.class_name:
self.Error.class_name_empty()
if self.class_name in domain:
self.Error.class_name_duplicated()
if not self.class_name or self.class_name in domain:
self.Outputs.data.send(None)
self.info.set_output_summary(self.info.NoOutput)
return
new_class = self._create_variable()
new_domain = Domain(
domain.attributes, new_class, domain.metas + domain.class_vars)
new_data = self.data.transform(new_domain)
summary = len(new_data) if new_data is not None else self.info.NoOutput
details = format_summary_details(new_data) if new_data is not None else ""
self.info.set_output_summary(summary, details)
self.Outputs.data.send(new_data)
def _create_variable(self):
rules = self.active_rules
# Transposition + stripping
valid_rules = [label or pattern or n_matches
for (label, pattern), n_matches in
zip(rules, self.match_counts)]
patterns = tuple(
pattern for (_, pattern), valid in zip(rules, valid_rules) if valid)
names = tuple(
name for name, valid in zip(self.class_labels(), valid_rules)
if valid)
transformer = self.TRANSFORMERS[type(self.attribute)]
var_key = (self.attribute, self.class_name, names,
patterns, self.case_sensitive, self.match_beginning)
if var_key in self.cached_variables:
return self.cached_variables[var_key]
compute_value = transformer(
self.attribute, patterns, self.case_sensitive, self.match_beginning)
new_var = DiscreteVariable(
self.class_name, names, compute_value=compute_value)
self.cached_variables[var_key] = new_var
return new_var
def send_report(self):
# Pylint gives false positives: these functions are always called from
# within the loop
# pylint: disable=undefined-loop-variable
def _cond_part():
rule = "<b>{}</b> ".format(class_name)
if patt:
rule += "if <b>{}</b> contains <b>{}</b>".format(
self.attribute.name, patt)
else:
rule += "otherwise"
return rule
def _count_part():
if not n_matched:
return "all {} matching instances are already covered " \
"above".format(n_total)
elif n_matched < n_total and patt:
return "{} matching instances (+ {} that are already " \
"covered above".format(n_matched, n_total - n_matched)
else:
return "{} matching instances".format(n_matched)
if not self.attribute:
return
self.report_items("Input", [("Source attribute", self.attribute.name)])
output = ""
names = self.class_labels()
for (n_matched, n_total), class_name, (lab, patt) in \
zip(self.match_counts, names, self.active_rules):
if lab or patt or n_total:
output += "<li>{}; {}</li>".format(_cond_part(), _count_part())
if output:
self.report_items("Output", [("Class name", self.class_name)])
self.report_raw("<ol>{}</ol>".format(output))
if __name__ == "__main__": # pragma: no cover
WidgetPreview(OWCreateClass).run(Table("zoo"))