-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobject_model_view.py
534 lines (478 loc) · 22.7 KB
/
robject_model_view.py
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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import rpy2.robjects as robjects
import sys, os, time
import rpy2.rlike.container as container
from calculator import CalculatorDialog
def convert(old, value):
if type(old) == int:
temp, ok = value.toInt()
if ok:
return int(temp)
return old
elif type(old) == float:
temp, ok = value.toDouble()
if ok:
return float(temp)
return old
elif type(old) == bool:
return bool(value.toBool())
if value.toString().isEmpty():
return old
return unicode(value.toString())
def convert2(L, typeof):
if typeof == 'Float':
vector = robjects.FloatVector(L)
elif typeof == 'Integer':
vector = robjects.IntVector(L)
elif typeof == 'Factor':
vector = robjects.FactorVector(L)
elif typeof == 'Boolean':
vector = robjects.BoolVector(L)
else:
vector = robjects.StrVector(L)
return vector
class ColumnDialog(QDialog):
def __init__(self, parent=None, title="Insert New Column", name=None,
typeof=None, param1=(None, 1.00), param2=(None, 0.00)):
QDialog.__init__(self, parent)
self.setWindowTitle(title)
nameLabel = QLabel("Name:")
typeLabel = QLabel("Type:")
param1Name = param1[0]
param1Value = param1[1]
param2Name = param2[0]
param2Value = param2[1]
if not param1Name is None:
param1Label = QLabel(param1Name)
if not param2Name is None:
param2Label = QLabel(param2Name)
self.nameEdit = QLineEdit()
self.nameEdit.setText(name)
self.typeCombobox = QComboBox()
types = ['Float', 'Integer', 'String', 'Factor', 'Boolean']
self.typeCombobox.addItems(types)
try:
index = types.index(typeof)
except ValueError:
index = 0
self.typeCombobox.setCurrentIndex(index)
self.param1Spinbox = QDoubleSpinBox()
self.param1Spinbox.setRange(0.00, 9999.00)
self.param1Spinbox.setSingleStep(5.00)
self.param1Spinbox.setValue(param1Value)
self.param2Spinbox = QDoubleSpinBox()
self.param2Spinbox.setRange(0.00, 9999.00)
self.param2Spinbox.setSingleStep(1.00)
self.param2Spinbox.setValue(param2Value)
okButton = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=self)
grid = QGridLayout(self)
if not name is None:
grid.addWidget(nameLabel, 0, 0)
grid.addWidget(self.nameEdit, 0, 1)
if not typeof is None:
grid.addWidget(typeLabel, 1, 0)
grid.addWidget(self.typeCombobox, 1, 1)
if not param1Name is None:
grid.addWidget(param1Label, 2, 0)
grid.addWidget(self.param1Spinbox, 2, 1)
if not param2Name is None:
grid.addWidget(param2Label, 3, 0)
grid.addWidget(self.param2Spinbox, 3, 1)
grid.addWidget(okButton, 4, 0, 1, 2)
self.connect(okButton, SIGNAL("accepted()"), self.accept)
self.connect(okButton, SIGNAL("rejected()"), self.reject)
def name(self):
return self.nameEdit.text()
def typeof(self):
return self.typeCombobox.currentText()
def param1(self):
return self.param1Spinbox.value()
def param2(self):
return self.param2Spinbox.value()
class DataFrameModel(QAbstractTableModel):
def __init__(self, mobject, parent=None):
QAbstractTableModel.__init__(self, parent)
self._mobject = mobject
# caching these is _significantly_ faster
self._rownames = self._mobject.rownames
self._colnames = self._mobject.colnames
self._ncols = self._mobject.ncol
self._nrows = self._mobject.nrow
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
if index.row() >= self._nrows or index.row() < 0:
return QVariant()
if role == Qt.DisplayRole:
return self._mobject[index.column()][index.row()]
elif role == Qt.TextAlignmentRole:
if type(self._mobject[index.column()][index.row()]) in (int, float):
return Qt.AlignRight
return QVariant()
def setData(self, index, data, role=Qt.EditRole):
if index.isValid() and role == Qt.EditRole:
old = self._mobject[index.column()][index.row()]
self._mobject[index.column()][index.row()] = convert(old, data)
self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), index, index)
return True
return False
def headerData(self, section, orientation, role):
if not role == Qt.DisplayRole:
return QVariant()
if orientation == Qt.Horizontal:
return QVariant(self._colnames[section])
elif orientation == Qt.Vertical:
return QVariant(self._rownames[section])
else:
return QVariant()
def rowCount(self, parent=QModelIndex()):
if parent.isValid():
return 0 # no child
return self._nrows
def columnCount(self, parent=QModelIndex()):
if parent.isValid():
return 0 # no child
return self._ncols
def sort(self, col, order=Qt.DescendingOrder):
self.emit(SIGNAL("layoutAboutToBeChanged()"))
indexes = robjects.r.order(self._mobject.rx(col+1),
decreasing = order == Qt.DescendingOrder)
self._mobject = self._mobject.rx(indexes, True)
self._rownames = self._mobject.rownames
self.emit(SIGNAL("layoutChanged()"))
def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
return QAbstractTableModel.flags(self, index) | Qt.ItemIsEditable
def dataFrame(self):
return self._mobject
def removeColumns(self, position, cols, parent=QModelIndex()):
if cols >= self._ncols:
return False
self.beginRemoveColumns(QModelIndex(), position, position+cols-1)
i = robjects.IntVector([-1*(j+1) for j in range(position, position+cols)])
self._mobject = self._mobject.rx(i)
# since we are chaching these values, we need to update manually
self._ncols -= cols
self._colnames = self._mobject.colnames
self.endRemoveColumns()
return True
def insertColumns(self, position, cols, parent=QModelIndex(),
columns=[robjects.IntVector(())], names=["V1"]):
self.beginInsertColumns(QModelIndex(), position, position+cols-1)
ncols = self._ncols+cols
temp = [(str(names[i]), columns[i]) for i in range(cols)]
dframe = robjects.DataFrame(container.OrdDict(temp))
self._mobject = self._mobject.cbind(dframe)
if position < self.columnCount()-1: # if it's not going on to the end
i = robjects.IntVector(range(1,(position+1)+1) +
range((ncols-cols)+1, ncols+1) +
range((position+1)+1,(ncols-cols)+1))
self._mobject = self._mobject.rx(True, i)
self._colnames = self._mobject.colnames
self._ncols = ncols
self.endInsertColumns()
return True
def updateColumn(self, position, col):
if issubclass(type(col), robjects.Vector):
self._mobject[position] = col
top = self.createIndex(0, position)
bottom = self.createIndex(self._nrows-1, position)
self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), top, bottom)
return True
return False
def removeRows(self, position, rows, parent=QModelIndex()):
self.beginRemoveRows(QModelIndex(), position, position+rows-1)
i = robjects.IntVector([-1*(j+1) for j in range(position, position+rows)])
self._mobject = self._mobject.rx(i, True, drop=False)
# since we are caching these values, we need to update manually
self._nrows -= rows
self._rownames = self._mobject.rownames
self.endRemoveRows()
return True
def setHeaderData(self, section, orientation=Qt.Horizontal, value=QVariant(),
role=Qt.EditRole):
if not section < self._ncols and not section >= 0:
return False
if orientation == Qt.Horizontal and role == Qt.EditRole:
self._mobject.colnames[section] = unicode(value)
self.emit(SIGNAL("headerDataChanged(Qt.Orientation, int, int)"),
orientation, section, section)
return True
return False
# def insertRows(position, rows, parent=QModelIndex(), typeof=float):
# self.beginInsertRows(QModelIndex(), position, position+rows-1)
# self.endInsertRows()
# return True
# def insertColumns(position, cols, parent=QModelIndex(), typeof=float):
# self.beginInsertRows(QModelIndex(), position, position+rows-1)
# self.endInsertRows()
# return True
class DataFrameDialog(QDialog):
def __init__(self, mobject, parent=None):
QDialog.__init__(self, parent)
self._dataFrame = DataFrameModel(mobject, self)
self._tableView = QTableView(self)
self.setupModels()
extra = self._tableView.verticalHeader().width()
width = (self._tableView.width()+extra) / self._tableView.horizontalHeader().count()
for i in xrange(self._tableView.horizontalHeader().count()):
self._tableView.setColumnWidth(i, width)
layout = QVBoxLayout(self)
layout.addWidget(self._tableView)
buttons = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=self)
layout.addWidget(buttons)
self.connect(buttons, SIGNAL("accepted()"), self.accept)
self.connect(buttons, SIGNAL("rejected()"), self.reject)
self.initActions()
self.initContextMenus()
def setupModels(self):
self._tableView.setModel(self._dataFrame)
self._tableView.horizontalHeader().setDefaultSectionSize(60)
self._tableView.horizontalHeader().setResizeMode(QHeaderView.Interactive)
self._tableView.verticalHeader().setResizeMode(QHeaderView.Fixed)
self._tableView.setEditTriggers(QAbstractItemView.DoubleClicked)
self._tableView.setSelectionMode(QAbstractItemView.ContiguousSelection)
self._tableView.setSelectionBehavior(QAbstractItemView.SelectItems)
self.connect(self._tableView.selectionModel(),
SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
self, SIGNAL("selectionChanged(QItemSelection)"))
def writeToFile(self, fileName):
self._tableView.model().dataFrame().to_csvfile(fileName, quote=True,
sep=',', eol='\n', na='NA', dec='.', row_names=True,
col_names=True, qmethod='escape', append=False)
def accept(self):
if self._tableView.editTriggers() == QAbstractItemView.NoEditTriggers:
return QDialog.accept(self)
else:
return None
def gotoCell(self):
dialog = QDialog(self)
dialog.setWindowTitle("Goto cell")
rowLabel = QLabel("Row:")
colLabel = QLabel("Col:")
rowSpinbox = QSpinBox(dialog)
rowSpinbox.setRange(1, self._tableView.verticalHeader().count())
colSpinbox = QSpinBox(dialog)
colSpinbox.setRange(1, self._tableView.horizontalHeader().count())
okButton = QDialogButtonBox(QDialogButtonBox.Ok, parent=dialog)
grid = QGridLayout(dialog)
grid.addWidget(rowLabel, 0, 0)
grid.addWidget(rowSpinbox, 0, 1)
grid.addWidget(colLabel, 1, 0)
grid.addWidget(colSpinbox, 1, 1)
grid.addWidget(okButton, 2, 0, 1, 2)
dialog.connect(okButton, SIGNAL("accepted()"), dialog.close)
dialog.exec_()
index = self._tableView.model().index(rowSpinbox.value()-1, colSpinbox.value()-1)
self._tableView.scrollTo(index)
self._tableView.setCurrentIndex(index)
def selectAll(self):
self._tableView.selectAll()
def clearSelection(self):
self._tableView.clearSelection()
def removeColumns(self):
indexes = self._tableView.selectionModel().selectedColumns()
if len(indexes) < 1:
index = self._tableView.horizontalHeader().currentIndex()
if not index.isValid():
return False
indexes = [index]
first = indexes[0]
self._tableView.model().removeColumns(first.column(), len(indexes), QModelIndex())
return True
def removeRows(self):
indexes = self._tableView.selectionModel().selectedRows()
if len(indexes) < 1:
index = self._tableView.verticalHeader().currentIndex()
if not index.isValid():
return False
indexes = [index]
first = indexes[0]
self._tableView.model().removeRows(first.row(), len(indexes), QModelIndex())
return True
def renameColumn(self):
index = self._tableView.horizontalHeader().currentIndex()
if not index.isValid():
return False
name, ok = QInputDialog.getText(self, "Rename column", "Column name:",
text=self._tableView.model()._colnames[index.column()])
if ok and not name.isEmpty():
self._tableView.model().setHeaderData(index.column(), value=unicode(name))
return True
def sortAscending(self):
index = self._tableView.horizontalHeader().currentIndex()
if not index.isValid():
return False
self._tableView.model().sort(index.column(), Qt.AscendingOrder)
def sortDescending(self):
index = self._tableView.horizontalHeader().currentIndex()
if not index.isValid():
return False
self._tableView.model().sort(index.column(), Qt.DescendingOrder)
def insertColumn(self):
index = self._tableView.horizontalHeader().currentIndex()
name = "V%s" % self._tableView.horizontalHeader().count()
dialog = ColumnDialog(self, name=name, typeof="Float", subset=False)
result = dialog.exec_()
if result:
temp = [0]*self._tableView.model().rowCount()
colClass = dialog.typeof()
colName = dialog.name()
vector = convert2(temp, colClass)
self._tableView.model().insertColumns(index.column(), 1, QModelIndex(), [vector], [colName])
return True
return False
def uniqueColumn(self):
index = self._tableView.horizontalHeader().currentIndex()
name = "UID%s" % self._tableView.horizontalHeader().count()
dialog = ColumnDialog(self, title="Insert Unique Id Column", name=name)
result = dialog.exec_()
if result:
colName = dialog.name()
rowCount = self._tableView.model().rowCount()
vector = robjects.IntVector(range(1, rowCount+1))
self._tableView.model().insertColumns(index.column(), 1, QModelIndex(), [vector], [colName])
return True
return False
def randomColumn(self):
index = self._tableView.horizontalHeader().currentIndex()
name = "R%s" % self._tableView.horizontalHeader().count()
dialog = ColumnDialog(self, title="Insert Random Column", name=name,
param1=("Min:", 0.0), param2=("Max:", 1.0))
result = dialog.exec_()
if result:
colName = dialog.name()
minVal = dialog.param1()
maxVal = dialog.param2()
rowCount = self._tableView.model().rowCount()
vector = robjects.FloatVector(robjects.r.runif(rowCount, minVal, maxVal))
self._tableView.model().insertColumns(index.column(), 1,
QModelIndex(), [vector], [colName])
return True
return False
def shuffleColumn(self):
index = self._tableView.horizontalHeader().currentIndex()
vector = robjects.r.sample(self._tableView.model().dataFrame()[index.column()])
self._tableView.model().updateColumn(index.column(), vector)
return True
def fieldCalculator(self):
calculator = CalculatorDialog(self._tableView.model().dataFrame(), self)
if calculator.exec_():
text = calculator.mExpressionTextEdit.toPlainText()
expression = robjects.r.parse(text=unicode(text), n=1, encoding="UTF-8")
vector = robjects.r['with'](self._tableView.model().dataFrame(), expression)
if calculator.mUpdateExistingFieldCheckBox.checkState() == Qt.Checked:
targetField = calculator.mExistingFieldComboBox.currentIndex()
self._tableView.model().updateColumn(targetField, vector)
else: # create new field
targetField = calculator.mOutputFieldNameLineEdit.text()
self._tableView.model().insertColumns(
self._tableView.model().columnCount(), 1, QModelIndex(),
columns=[vector], names=[targetField])
def initActions(self):
self.fieldCalculatorAction = QAction("Calculator", self)
self.addAction(self.fieldCalculatorAction)
self.connect(self.fieldCalculatorAction, SIGNAL("triggered()"), self.fieldCalculator)
self.renameColumnAction = QAction("Rename Column", self)
self.addAction(self.renameColumnAction)
self.connect(self.renameColumnAction, SIGNAL("triggered()"), self.renameColumn)
self.removeColumnAction = QAction("Delete Column(s)", self)
self.addAction(self.removeColumnAction)
self.connect(self.removeColumnAction, SIGNAL("triggered()"), self.removeColumns)
self.insertColumnAction = QAction("Empty Column", self)
self.addAction(self.insertColumnAction)
self.connect(self.insertColumnAction, SIGNAL("triggered()"), self.insertColumn)
self.uniqueColumnAction = QAction("Unique IDs", self)
self.addAction(self.uniqueColumnAction)
self.connect(self.uniqueColumnAction, SIGNAL("triggered()"), self.uniqueColumn)
self.randomColumnAction = QAction("Random Values", self)
self.addAction(self.randomColumnAction)
self.connect(self.randomColumnAction, SIGNAL("triggered()"), self.randomColumn)
self.shuffleColumnAction = QAction("Resample", self)
self.addAction(self.shuffleColumnAction)
self.connect(self.shuffleColumnAction, SIGNAL("triggered()"), self.shuffleColumn)
self.removeRowAction = QAction("Delete Row(s)", self)
self.addAction(self.removeRowAction)
self.connect(self.removeRowAction, SIGNAL("triggered()"), self.removeRows)
self.clearSelectionAction = QAction("Clear Selection", self)
self.addAction(self.clearSelectionAction)
self.connect(self.clearSelectionAction, SIGNAL("triggered()"), self.clearSelection)
self.selectAllAction = QAction("Select All", self)
self.addAction(self.selectAllAction)
self.connect(self.selectAllAction, SIGNAL("triggered()"), self.selectAll)
self.gotoCellAction = QAction("Go to cell", self)
self.addAction(self.gotoCellAction)
self.connect(self.gotoCellAction, SIGNAL("triggered()"), self.gotoCell)
self.sortDescendingAction = QAction("Descending", self)
self.addAction(self.sortDescendingAction)
self.connect(self.sortDescendingAction, SIGNAL("triggered()"), self.sortDescending)
self.sortAscendingAction = QAction("Ascending", self)
self.addAction(self.sortAscendingAction)
self.connect(self.sortAscendingAction, SIGNAL("triggered()"), self.sortAscending)
def initContextMenus(self):
self._tableView.horizontalHeader().setContextMenuPolicy(Qt.CustomContextMenu)
self._tableView.verticalHeader().setContextMenuPolicy(Qt.CustomContextMenu)
self.connect(self._tableView.horizontalHeader(),
SIGNAL("customContextMenuRequested(QPoint)"),
self.horizontalHeaderContext)
self.connect(self._tableView.verticalHeader(),
SIGNAL("customContextMenuRequested(QPoint)"),
self.verticalHeaderContext)
def horizontalHeaderContext(self, point):
col = self._tableView.horizontalHeader().logicalIndexAt(point)
index = self._tableView.model().createIndex(0,col)
self._tableView.horizontalHeader().selectionModel().setCurrentIndex(index,
QItemSelectionModel.NoUpdate)
menu = QMenu("Header Menu", self)
menu.addAction(self.fieldCalculatorAction)
insert = QMenu("Insert...", self)
insert.addAction(self.insertColumnAction)
insert.addAction(self.uniqueColumnAction)
insert.addAction(self.randomColumnAction)
menu.addMenu(insert)
menu.addSeparator()
menu.addAction(self.removeColumnAction)
menu.addAction(self.renameColumnAction)
menu.addSeparator()
menu.addAction(self.clearSelectionAction)
menu.addAction(self.selectAllAction)
menu.addSeparator()
menu.addAction(self.gotoCellAction)
sort = QMenu("Sort...", self)
sort.addAction(self.sortDescendingAction)
sort.addAction(self.sortAscendingAction)
menu.addMenu(sort)
menu.addAction(self.shuffleColumnAction)
menu.exec_(self.mapToGlobal(point))
def verticalHeaderContext(self, point):
row = self._tableView.verticalHeader().logicalIndexAt(point)
index = self._tableView.model().createIndex(row, 0)
self._tableView.verticalHeader().selectionModel().setCurrentIndex(index,
QItemSelectionModel.NoUpdate)
menu = QMenu("Row Menu", self)
menu.addAction(self.removeRowAction)
menu.addSeparator()
menu.addAction(self.clearSelectionAction)
menu.addAction(self.selectAllAction)
menu.addSeparator()
menu.addAction(self.gotoCellAction)
menu.exec_(self.mapToGlobal(point))
def main():
app = QApplication(sys.argv)
if not sys.platform.startswith(("linux", "win")):
app.setCursorFlashTime(0)
app.setApplicationName("test-table")
app.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))
robjects.r.load('test_large.Rdata')
data = robjects.r['white.collar.data']
table = DataFrameDialog(data, None)
table.setWindowTitle("DataFrame Viewer")
# table.connect(dialog, SIGNAL("accepted()"), table.saveDataFrame)
table.show()
app.exec_()
if __name__ == "__main__":
# import profile
# profile.run("main()")
main()