Source

orange / Orange / OrangeWidgets / Data / OWMultiMergeData.py

Full commit
  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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
"""
<name>Multi-key Merge Data</name>
<description>Merge datasets based on values of selected tuples of attributes.</description>
<icon>icons/MergeData.svg</icon>
<priority>100</priority>
<contact>Peter Husen (phusen@bmb.sdu.dk)</contact>
"""

from OWWidget import *
from OWItemModels import PyListModel, VariableListModel

#import OWGUI
import Orange


def slices(indices):
    """ Group the given integer indices into slices
    """
    indices = list(sorted(indices))
    if indices:
        first = last = indices[0]
        for i in indices[1:]:
            if i == last + 1:
                last = i
            else:
                yield first, last + 1
                first = last = i
        yield first, last + 1


def delslice(model, start, end):
    """ Delete the start, end slice (rows) from the model. 
    """
    if isinstance(model, PyListModel):
        del model[start: end]
    elif isinstance(model, QAbstractItemModel):
        model.removeRows(start, end-start)
    else:
        raise TypeError(type(model))


class VariablesListItemModel(VariableListModel):
    """ An Qt item model for for list of orange.Variable objects.
    Supports drag operations
    """
        
    def flags(self, index):
        flags = VariableListModel.flags(self, index)
        if index.isValid():
            flags |= Qt.ItemIsDragEnabled
        else:
            flags |= Qt.ItemIsDropEnabled
        return flags
    
    ###########
    # Drag/Drop
    ###########
    
    MIME_TYPE = "application/x-Orange-VariableListModelData"
    
    def supportedDropActions(self):
        return Qt.MoveAction
    
    def supportedDragActions(self):
        return Qt.MoveAction
    
    def mimeTypes(self):
        return [self.MIME_TYPE]
    
    def mimeData(self, indexlist):
        descriptors = []
        vars = []
        item_data = []
        for index in indexlist:
            var = self[index.row()]
            descriptors.append((var.name, var.varType))
            vars.append(var)
            item_data.append(self.itemData(index))
        
        mime = QMimeData()
        mime.setData(self.MIME_TYPE, QByteArray(str(descriptors)))
        mime._vars = vars
        mime._item_data = item_data
        return mime
    
    def dropMimeData(self, mime, action, row, column, parent):
        if action == Qt.IgnoreAction:
            return True
    
        vars, item_data = self.items_from_mime_data(mime)
        if vars is None:
            return False
        
        if row == -1:
            row = len(self)
            
        self[row: row] = vars
        
        for i, data in enumerate(item_data):
            self.setItemData(self.index(row + i), data)
            
        return True
    
    def items_from_mime_data(self, mime):
        if not mime.hasFormat(self.MIME_TYPE):
            return None, None
        
        if hasattr(mime, "_vars"):
            vars = mime._vars
            item_data = mime._item_data
            return vars, item_data
        else:
            #TODO: get vars from orange.Variable.getExisting
            return None, None


class VariablesListItemView(QListView):
    """ A Simple QListView subclass initialized for displaying
    variables.
    """
    def __init__(self, parent=None):
        QListView.__init__(self, parent)
        self.setSelectionMode(QListView.ExtendedSelection)
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setDragDropMode(QListView.DragDrop)
        if hasattr(self, "setDefaultDropAction"):
            # For compatibility with Qt version < 4.6
            self.setDefaultDropAction(Qt.MoveAction)
        self.setDragDropOverwriteMode(False)
        self.viewport().setAcceptDrops(True)
    
    def startDrag(self, supported_actions):
        indices = self.selectionModel().selectedIndexes()
        indices = [i for i in indices if i.flags() & Qt.ItemIsDragEnabled]
        if indices:
            data = self.model().mimeData(indices)
            if not data:
                return
            # rect = QRect()
            
            drag = QDrag(self)
            drag.setMimeData(data)
            
            default_action = Qt.IgnoreAction
            if hasattr(self, "defaultDropAction") and \
                    self.defaultDropAction() != Qt.IgnoreAction and \
                    supported_actions & self.defaultDropAction():
                default_action = self.defaultDropAction()
            elif supported_actions & Qt.CopyAction and dragDropMode() != QListView.InternalMove:
                default_action = Qt.CopyAction
            
            res = drag.exec_(supported_actions, default_action)
                
            if res == Qt.MoveAction:
                selected = self.selectionModel().selectedIndexes()
                rows = map(QModelIndex.row, selected)
                
                for s1, s2 in reversed(list(slices(rows))):
                    delslice(self.model(), s1, s2)
    
    def render_to_pixmap(self, indices):
        pass


from functools import partial
class OWMultiMergeData(OWWidget):
    contextHandlers = { "A": DomainContextHandler("A", [ContextField("domainA_role_hints")]),
                        "B": DomainContextHandler("B", [ContextField("domainB_role_hints")]) }

    def __init__(self, parent = None, signalManager = None, name = "Merge data"):
        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0)  #initialize base class

        # set channels
        self.inputs = [("Data A", ExampleTable, self.set_dataA),
                       ("Data B", ExampleTable, self.set_dataB)]
        
        self.outputs = [("Merged Data A+B", ExampleTable),
                        ("Merged Data B+A", ExampleTable)]

        self.domainA_role_hints = {}
        self.domainB_role_hints = {}
        
        # data
        self.dataA = None
        self.dataB = None

        # load settings
        self.loadSettings()

        # ####
        # GUI
        # ####
        
        import sip
        sip.delete(self.controlArea.layout())
        
        layout = QGridLayout()
        layout.setMargin(0)

        # Available A attributes
        box = OWGUI.widgetBox(self.controlArea, "Available A attributes", addToLayout=False)
        self.available_attrsA = VariablesListItemModel()
        self.available_attrsA_view = VariablesListItemView()
        self.available_attrsA_view.setModel(self.available_attrsA)
        
        self.connect(self.available_attrsA_view.selectionModel(),
                     SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
                     partial(self.update_interface_state,
                             self.available_attrsA_view))
        
        box.layout().addWidget(self.available_attrsA_view)
        layout.addWidget(box, 0, 0, 2, 1)


        # Used A Attributes
        box = OWGUI.widgetBox(self.controlArea, "Used A attributes", addToLayout=False)
        self.used_attrsA = VariablesListItemModel()
        self.used_attrsA_view = VariablesListItemView()
        self.used_attrsA_view.setModel(self.used_attrsA)
        self.connect(self.used_attrsA_view.selectionModel(),
                     SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
                     partial(self.update_interface_state,
                             self.used_attrsA_view))
        
        box.layout().addWidget(self.used_attrsA_view)
        layout.addWidget(box, 0, 2, 1, 1)


        # Data A info
        box = OWGUI.widgetBox(self, 'Data A', orientation = "vertical", addToLayout=0)
        self.lblDataAExamples = OWGUI.widgetLabel(box, "num examples")
        self.lblDataAAttributes = OWGUI.widgetLabel(box, "num attributes")
        layout.addWidget(box, 1, 1, 1, 2)


        # Available B attributes
        box = OWGUI.widgetBox(self.controlArea, "Available B attributes", addToLayout=False)
        self.available_attrsB = VariablesListItemModel()
        self.available_attrsB_view = VariablesListItemView()
        self.available_attrsB_view.setModel(self.available_attrsB)
        
        self.connect(self.available_attrsB_view.selectionModel(),
                     SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
                     partial(self.update_interface_state,
                             self.available_attrsB_view))
        
        box.layout().addWidget(self.available_attrsB_view)
        layout.addWidget(box, 2, 0, 2, 1)


        # Used B Attributes
        box = OWGUI.widgetBox(self.controlArea, "Used B attributes", addToLayout=False)
        self.used_attrsB = VariablesListItemModel()
        self.used_attrsB_view = VariablesListItemView()
        self.used_attrsB_view.setModel(self.used_attrsB)
        self.connect(self.used_attrsB_view.selectionModel(),
                     SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
                     partial(self.update_interface_state,
                             self.used_attrsB_view))


        # Data B info
        box.layout().addWidget(self.used_attrsB_view)
        layout.addWidget(box, 2, 2, 1, 1)

        box = OWGUI.widgetBox(self, 'Data B', orientation = "vertical", addToLayout=0)
        self.lblDataBExamples = OWGUI.widgetLabel(box, "num examples")
        self.lblDataBAttributes = OWGUI.widgetLabel(box, "num attributes")
        layout.addWidget(box, 3, 1, 1, 2)


        # A buttons
        bbox = OWGUI.widgetBox(self.controlArea, addToLayout=False, margin=0)
        layout.addWidget(bbox, 0, 1, 1, 1)
        
        self.up_attrA_button = OWGUI.button(bbox, self, "Up", 
                    callback = partial(self.move_up, self.used_attrsA_view))
        self.move_attrA_button = OWGUI.button(bbox, self, ">",
                    callback = partial(self.move_selected, self.used_attrsA_view))
        self.down_attrA_button = OWGUI.button(bbox, self, "Down",
                    callback = partial(self.move_down, self.used_attrsA_view))


        # B buttons
        bbox = OWGUI.widgetBox(self.controlArea, addToLayout=False, margin=0)
        layout.addWidget(bbox, 2, 1, 1, 1)

        self.up_attrB_button = OWGUI.button(bbox, self, "Up",
                    callback = partial(self.move_up, self.used_attrsB_view))
        self.move_attrB_button = OWGUI.button(bbox, self, ">",
                    callback = partial(self.move_selected, self.used_attrsB_view))
        self.down_attrB_button = OWGUI.button(bbox, self, "Down",
                    callback = partial(self.move_down, self.used_attrsB_view))


        # Apply / reset
        bbox = OWGUI.widgetBox(self.controlArea, orientation="horizontal", addToLayout=False, margin=0)
        applyButton = OWGUI.button(bbox, self, "Apply", callback=self.commit)
        resetButton = OWGUI.button(bbox, self, "Reset", callback=self.reset)
        
        layout.addWidget(bbox, 4, 0, 1, 3)
        
        layout.setHorizontalSpacing(0)
        self.controlArea.setLayout(layout)
        
        self.data = None
        self.output_report = None

        self.resize(500, 600)
        
        # For automatic widget testing using
        self._guiElements.extend( \
                  [(QListView, self.available_attrsA_view),
                   (QListView, self.used_attrsA_view),
                   (QListView, self.available_attrsB_view),
                   (QListView, self.used_attrsB_view),
                  ])


    ############################################################################################################################################################
    ## Data input and output management
    ############################################################################################################################################################
    
    
    def set_dataA(self, data=None):
        self.update_domainA_role_hints()
        self.closeContext("A")
        self.dataA = data
        if data is not None:
            self.openContext("A", data)
            all_vars = data.domain.variables + data.domain.getmetas().values()
            
            var_sig = lambda attr: (attr.name, attr.varType)
            
            domain_hints = dict([(var_sig(attr), ("available", i)) \
                            for i, attr in enumerate(data.domain.attributes)])
            
            domain_hints.update(dict([(var_sig(attr), ("available", i)) \
                for i, attr in enumerate(data.domain.getmetas().values())]))
            
            if data.domain.class_var:
                domain_hints[var_sig(data.domain.class_var)] = ("available", 0)
                    
            domain_hints.update(self.domainA_role_hints) # update the hints from context settings
            
            attrs_for_role = lambda role: [(domain_hints[var_sig(attr)][1], attr) \
                    for attr in all_vars if domain_hints[var_sig(attr)][0] == role]
            
            available = [attr for place, attr in sorted(attrs_for_role("available"))]
            used = [attr for place, attr in sorted(attrs_for_role("used"))]
            
            self.available_attrsA[:] = available
            self.used_attrsA[:] = used
        else:
            self.available_attrsA[:] = []
            self.used_attrsA[:] = []
        
        self.updateInfoA()
        self.commit()


    def set_dataB(self, data=None):
        self.update_domainB_role_hints()
        self.closeContext("B")
        self.dataB = data
        if data is not None:
            self.openContext("B", data)
            all_vars = data.domain.variables + data.domain.getmetas().values()
            
            var_sig = lambda attr: (attr.name, attr.varType)
            
            domain_hints = dict([(var_sig(attr), ("available", i)) \
                            for i, attr in enumerate(data.domain.attributes)])
            
            domain_hints.update(dict([(var_sig(attr), ("available", i)) \
                for i, attr in enumerate(data.domain.getmetas().values())]))
            
            if data.domain.class_var:
                domain_hints[var_sig(data.domain.class_var)] = ("available", 0)
                    
            domain_hints.update(self.domainB_role_hints) # update the hints from context settings
            
            attrs_for_role = lambda role: [(domain_hints[var_sig(attr)][1], attr) \
                    for attr in all_vars if domain_hints[var_sig(attr)][0] == role]
            
            available = [attr for place, attr in sorted(attrs_for_role("available"))]
            used = [attr for place, attr in sorted(attrs_for_role("used"))]
            
            self.available_attrsB[:] = available
            self.used_attrsB[:] = used
        else:
            self.available_attrsB[:] = []
            self.used_attrsB[:] = []

        self.updateInfoB()
        self.commit()


    def updateInfoA(self):
        """Updates data A info box.
        """
        if self.dataA:
            self.lblDataAExamples.setText("%s example%s" % self._sp(self.dataA))
            self.lblDataAAttributes.setText("%s attribute%s" % self._sp(self.available_attrsA[:] + self.used_attrsA[:]))
        else:
            self.lblDataAExamples.setText("No data on input A.")
            self.lblDataAAttributes.setText("")


    def updateInfoB(self):
        """Updates data B info box.
        """
        if self.dataB:
            self.lblDataBExamples.setText("%s example%s" % self._sp(self.dataB))
            self.lblDataBAttributes.setText("%s attribute%s" % self._sp(self.available_attrsB[:] + self.used_attrsB[:]))
        else:
            self.lblDataBExamples.setText("No data on input B.")
            self.lblDataBAttributes.setText("")


    def update_domainA_role_hints(self):
        """ Update the domain hints to be stored in the widgets settings.
        """
        hints_from_model = lambda role, model: \
                [((attr.name, attr.varType), (role, i)) \
                 for i, attr in enumerate(model)]
        
        hints = dict(hints_from_model("available", self.available_attrsA))
        hints.update(hints_from_model("used", self.used_attrsA))
        self.domainA_role_hints = hints

    def update_domainB_role_hints(self):
        """ Update the domain hints to be stored in the widgets settings.
        """
        hints_from_model = lambda role, model: \
                [((attr.name, attr.varType), (role, i)) \
                 for i, attr in enumerate(model)]
        
        hints = dict(hints_from_model("available", self.available_attrsB))
        hints.update(hints_from_model("used", self.used_attrsB))
        self.domainB_role_hints = hints

    def move_rows(self, view, rows, offset):
        model = view.model()
        newrows = [min(max(0, row + offset), len(model) - 1) for row in rows]
        
        for row, newrow in sorted(zip(rows, newrows), reverse=offset > 0):
            model[row], model[newrow] = model[newrow], model[row]
            
        selection = QItemSelection()
        for nrow in newrows:
            index = model.index(nrow, 0)
            selection.select(index, index)
        view.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)

    def move_up(self, view):
        selected = self.selected_rows(view)
        self.move_rows(view, selected, -1)
    
    def move_down(self, view):
        selected = self.selected_rows(view)
        self.move_rows(view, selected, 1)
        
    def selected_rows(self, view):
        """ Return the selected rows in the view. 
        """
        rows = view.selectionModel().selectedRows()
        model = view.model()
        return [r.row() for r in rows]

    def move_selected(self, view):
        fromto = {
            self.available_attrsA_view: self.used_attrsA_view,
            self.available_attrsB_view: self.used_attrsB_view,
            self.used_attrsA_view: self.available_attrsA_view,
            self.used_attrsB_view: self.available_attrsB_view
        }
        if self.selected_rows(view):
            self.move_selected_from_to(view, fromto[view])
        else:
            self.move_selected_from_to(fromto[view], view)
    
    def move_selected_from_to(self, src, dst):
        self.move_from_to(src, dst, self.selected_rows(src))
        
    def move_from_to(self, src, dst, rows):
        src_model = src.model()
        attrs = [src_model[r] for r in rows]

        for s1, s2 in reversed(list(slices(rows))):
            del src_model[s1:s2]

        dst_model = dst.model()
        dst_model.extend(attrs)


    def reset(self):
        if self.dataA is not None:
            meta_attrsA = self.dataA.domain.getmetas().values()
            self.available_attrsA[:] = self.dataA.domain.variables + meta_attrsA
            self.used_attrsA[:] = []
            self.update_domainA_role_hints()
        if self.dataB is not None:
            meta_attrsB = self.dataB.domain.getmetas().values()
            self.available_attrsB[:] = self.dataB.domain.variables + meta_attrsB
            self.used_attrsB[:] = []
            self.update_domainB_role_hints()


    def update_interface_state(self, focus=None, selected=None, deselected=None):
        for view in [self.available_attrsA_view, self.used_attrsA_view,
                     self.available_attrsB_view, self.used_attrsB_view]:
            if view is not focus and not view.hasFocus():
                view.selectionModel().clear()
                
        availableA_selected = bool(self.available_attrsA_view.selectionModel().selectedRows())
        availableB_selected = bool(self.available_attrsB_view.selectionModel().selectedRows())
                              #bool(self.selected_rows(self.available_attrs_view))
                                 
        move_attrA_enabled = bool(availableA_selected or \
                                  self.used_attrsA_view.selectionModel().selectedRows())
        move_attrB_enabled = bool(availableB_selected or \
                                  self.used_attrsB_view.selectionModel().selectedRows())

        self.move_attrA_button.setEnabled(move_attrA_enabled)
        self.move_attrB_button.setEnabled(move_attrB_enabled)
        if move_attrA_enabled:
            self.move_attrA_button.setText(">" if availableA_selected else "<")
        if move_attrB_enabled:
            self.move_attrB_button.setText(">" if availableB_selected else "<")

    def commit(self):
        if self.dataA: self.update_domainA_role_hints()
        if self.dataB: self.update_domainB_role_hints()
        self.error(0)
        if self.dataA and self.dataB and list(self.used_attrsA) and list(self.used_attrsB):
            try:
                self.send("Merged Data A+B", self.merge(self.dataA, self.dataB, self.used_attrsA, self.used_attrsB))
                self.send("Merged Data B+A", self.merge(self.dataB, self.dataA, self.used_attrsB, self.used_attrsA))
            except Orange.core.KernelException, ex:
                self.error(0, "Cannot merge the two tables (%r)" % str(ex))
        else:
            self.send("Merged Data A+B", None)
            self.send("Merged Data B+A", None)

    ############################################################################################################################################################
    ## Utility functions
    ############################################################################################################################################################

    def _sp(self, l, capitalize=True):
        """Input: list; returns tuple (str(len(l)), "s"/"")
        """
        n = len(l)
        if n == 0:
            if capitalize:
                return "No", "s"
            else:
                return "no", "s"
        elif n == 1:
            return str(n), ''
        else:
            return str(n), 's'

    def merge(self, dataA, dataB, varsA, varsB):
        """ Merge two tables
        """
        
        vkey = lambda row, vs : tuple( row[v].native() for v in vs )
        
        val2idx = dict([( vkey(e,varsB) , i) for i, e in reversed(list(enumerate(dataB)))])
        
        #for key in ["?", "~", ""]:
        #    if key in val2idx:
        #        val2idx.pop(key)

        metasA = dataA.domain.getmetas().items()
        metasB = dataB.domain.getmetas().items()
        
        includedAttsB = [attrB for attrB in dataB.domain if attrB not in dataA.domain]
        includedMetaB = [(mid, meta) for mid, meta in metasB if (mid, meta) not in metasA]
        includedClassVarB = dataB.domain.classVar and dataB.domain.classVar not in dataA.domain
        
        reducedDomainB = Orange.data.Domain(includedAttsB, includedClassVarB)
        reducedDomainB.addmetas(dict(includedMetaB))
        
        
        mergingB = Orange.data.Table(reducedDomainB)
        
        for ex in dataA:
            ind = val2idx.get( vkey(ex,varsA), None)
            if ind is not None:
                mergingB.append(Orange.data.Instance(reducedDomainB, dataB[ind]))
                
            else:
                mergingB.append(Orange.data.Instance(reducedDomainB, ["?"] * len(reducedDomainB)))
                
        return Orange.data.Table([dataA, mergingB])
    
if __name__=="__main__":
    import sys
    a=QApplication(sys.argv)
    ow=OWMultiMergeData()
    ow.show()
    data = Orange.data.Table("iris.tab")
    data2 = Orange.data.Table("iris.tab")
    ow.set_dataA(data)
    ow.set_dataB(data2)
    a.exec_()