| 1 | """ |
|---|
| 2 | <name>Model File</name> |
|---|
| 3 | <description>Load a Model Map</description> |
|---|
| 4 | <contact>Miha Stajdohar (miha.stajdohar(@at@)gmail.com)</contact> |
|---|
| 5 | <icon>icons/DistanceFile.png</icon> |
|---|
| 6 | <priority>6510</priority> |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | from OWWidget import * |
|---|
| 10 | |
|---|
| 11 | import OWGUI |
|---|
| 12 | import Orange |
|---|
| 13 | import orngMisc |
|---|
| 14 | import exceptions |
|---|
| 15 | import os.path |
|---|
| 16 | import cPickle as pickle |
|---|
| 17 | import bz2 |
|---|
| 18 | |
|---|
| 19 | class OWModelFile(OWWidget): |
|---|
| 20 | settingsList = ["files", "file_index"] |
|---|
| 21 | |
|---|
| 22 | def __init__(self, parent=None, signalManager=None): |
|---|
| 23 | OWWidget.__init__(self, parent, signalManager, name='Model File', wantMainArea=0, resizingEnabled=1) |
|---|
| 24 | |
|---|
| 25 | self.outputs = [("Distances", Orange.misc.SymMatrix), |
|---|
| 26 | ("Model Meta-data", Orange.data.Table), |
|---|
| 27 | ("Original Data", Orange.data.Table)] |
|---|
| 28 | |
|---|
| 29 | #self.dataFileBox.setTitle("Model File") |
|---|
| 30 | self.files = [] |
|---|
| 31 | self.file_index = 0 |
|---|
| 32 | |
|---|
| 33 | self.matrix = None |
|---|
| 34 | self.model_data = None |
|---|
| 35 | self.original_data = None |
|---|
| 36 | |
|---|
| 37 | self.loadSettings() |
|---|
| 38 | |
|---|
| 39 | self.fileBox = OWGUI.widgetBox(self.controlArea, "Model File", addSpace=True) |
|---|
| 40 | hbox = OWGUI.widgetBox(self.fileBox, orientation=0) |
|---|
| 41 | self.filecombo = OWGUI.comboBox(hbox, self, "file_index", callback=self.loadFile) |
|---|
| 42 | self.filecombo.setMinimumWidth(250) |
|---|
| 43 | button = OWGUI.button(hbox, self, '...', callback=self.browseFile) |
|---|
| 44 | button.setIcon(self.style().standardIcon(QStyle.SP_DirOpenIcon)) |
|---|
| 45 | button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed) |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | # Moved to SymMatrixTransform widget |
|---|
| 49 | # |
|---|
| 50 | # ribg = OWGUI.radioButtonsInBox(self.controlArea, self, "normalizeMethod", [], "Normalize method", callback = self.setNormalizeMode) |
|---|
| 51 | # OWGUI.appendRadioButton(ribg, self, "normalizeMethod", "None", callback = self.setNormalizeMode) |
|---|
| 52 | # OWGUI.appendRadioButton(ribg, self, "normalizeMethod", "To interval [0,1]", callback = self.setNormalizeMode) |
|---|
| 53 | # OWGUI.appendRadioButton(ribg, self, "normalizeMethod", "Sigmoid function: 1 / (1 + e^x)", callback = self.setNormalizeMode) |
|---|
| 54 | # |
|---|
| 55 | # ribg = OWGUI.radioButtonsInBox(self.controlArea, self, "invertMethod", [], "Invert method", callback = self.setInvertMode) |
|---|
| 56 | # OWGUI.appendRadioButton(ribg, self, "invertMethod", "None", callback = self.setInvertMode) |
|---|
| 57 | # OWGUI.appendRadioButton(ribg, self, "invertMethod", "-X", callback = self.setInvertMode) |
|---|
| 58 | # OWGUI.appendRadioButton(ribg, self, "invertMethod", "1 - X", callback = self.setInvertMode) |
|---|
| 59 | # OWGUI.appendRadioButton(ribg, self, "invertMethod", "Max - X", callback = self.setInvertMode) |
|---|
| 60 | # OWGUI.appendRadioButton(ribg, self, "invertMethod", "1 / X", callback = self.setInvertMode) |
|---|
| 61 | |
|---|
| 62 | OWGUI.rubber(self.controlArea) |
|---|
| 63 | |
|---|
| 64 | self.adjustSize() |
|---|
| 65 | |
|---|
| 66 | for i in range(len(self.files) - 1, -1, -1): |
|---|
| 67 | if not (os.path.exists(self.files[i]) and os.path.isfile(self.files[i])): |
|---|
| 68 | del self.files[i] |
|---|
| 69 | |
|---|
| 70 | if self.files: |
|---|
| 71 | self.loadFile() |
|---|
| 72 | |
|---|
| 73 | def browseFile(self): |
|---|
| 74 | if self.files: |
|---|
| 75 | lastPath = os.path.split(self.files[0])[0] |
|---|
| 76 | else: |
|---|
| 77 | lastPath = "." |
|---|
| 78 | fn = unicode(QFileDialog.getOpenFileName(self, "Open Model Map File", |
|---|
| 79 | lastPath, "Model Map (*.bz2)")) |
|---|
| 80 | fn = os.path.abspath(fn) |
|---|
| 81 | if fn in self.files: # if already in list, remove it |
|---|
| 82 | self.files.remove(fn) |
|---|
| 83 | self.files.insert(0, fn) |
|---|
| 84 | self.file_index = 0 |
|---|
| 85 | self.loadFile() |
|---|
| 86 | |
|---|
| 87 | def loadFile(self): |
|---|
| 88 | if self.file_index: |
|---|
| 89 | fn = self.files[self.file_index] |
|---|
| 90 | self.files.remove(fn) |
|---|
| 91 | self.files.insert(0, fn) |
|---|
| 92 | self.file_index = 0 |
|---|
| 93 | else: |
|---|
| 94 | fn = self.files[0] |
|---|
| 95 | |
|---|
| 96 | self.filecombo.clear() |
|---|
| 97 | for file in self.files: |
|---|
| 98 | self.filecombo.addItem(os.path.split(file)[1]) |
|---|
| 99 | #self.filecombo.updateGeometry() |
|---|
| 100 | |
|---|
| 101 | self.matrix = None |
|---|
| 102 | self.model_data = None |
|---|
| 103 | self.original_data = None |
|---|
| 104 | pb = OWGUI.ProgressBar(self, 100) |
|---|
| 105 | |
|---|
| 106 | self.error() |
|---|
| 107 | try: |
|---|
| 108 | matrix, self.model_data, self.original_data = pickle.load(bz2.BZ2File('%s' % fn, "r")) |
|---|
| 109 | |
|---|
| 110 | self.matrix = Orange.misc.SymMatrix(len(matrix)) |
|---|
| 111 | milestones = orngMisc.progressBarMilestones(self.matrix.dim, 100) |
|---|
| 112 | for i in range(self.matrix.dim): |
|---|
| 113 | for j in range(i + 1): |
|---|
| 114 | self.matrix[j, i] = matrix[i, j] |
|---|
| 115 | |
|---|
| 116 | if i in milestones: |
|---|
| 117 | pb.advance() |
|---|
| 118 | pb.finish() |
|---|
| 119 | |
|---|
| 120 | except Exception, ex: |
|---|
| 121 | self.error("Error while reading the file: '%s'" % str(ex)) |
|---|
| 122 | return |
|---|
| 123 | self.relabel() |
|---|
| 124 | |
|---|
| 125 | def relabel(self): |
|---|
| 126 | self.error() |
|---|
| 127 | if self.matrix is not None: |
|---|
| 128 | if self.model_data is not None and self.matrix.dim == len(self.model_data): |
|---|
| 129 | self.matrix.setattr("items", self.model_data) |
|---|
| 130 | else: |
|---|
| 131 | self.error("The number of model doesn't match the matrix dimension. Invalid Model Map file.") |
|---|
| 132 | |
|---|
| 133 | if self.original_data is not None: |
|---|
| 134 | self.matrix.setattr("original_data", self.original_data) |
|---|
| 135 | |
|---|
| 136 | self.send("Distances", self.matrix) |
|---|
| 137 | |
|---|
| 138 | if self.model_data is not None: |
|---|
| 139 | self.send("Model Meta-data", self.model_data) |
|---|
| 140 | |
|---|
| 141 | if self.original_data is not None: |
|---|
| 142 | self.send("Original Data", self.original_data) |
|---|
| 143 | |
|---|
| 144 | if __name__ == "__main__": |
|---|
| 145 | a = QApplication(sys.argv) |
|---|
| 146 | ow = OWModelFile() |
|---|
| 147 | ow.show() |
|---|
| 148 | a.exec_() |
|---|
| 149 | ow.saveSettings() |
|---|