source: orange/orange/OrangeWidgets/Unsupervised/OWSOM.py @ 9546:2b6cc6f397fe

Revision 9546:2b6cc6f397fe, 7.7 KB checked in by ales_erjavec <ales.erjavec@…>, 2 years ago (diff)

Renamed widget channel names in line with the new naming rules/convention.
Added backwards compatibility in orngDoc loadDocument to enable loading of schemas saved before the change.

Line 
1"""
2<name>SOM</name>
3<description>Self organizing maps learner.</description>
4<icon>icons/SOM.png</icon>
5<contact>Ales Erjavec (ales.erjevec(@at@)fri.uni.lj.si)</contact> 
6<priority>5010</priority>
7"""
8
9import orange
10import orngSOM
11from OWWidget import *
12import OWGUI
13
14#class orange_input(object):
15#    def __init__(self, input_type, check=None):
16#        pass
17#   
18#    def __get__(self, instance):
19#        return self.__call__       
20
21class OWSOM(OWWidget):
22    settingsList=["xdim", "ydim", "neighborhood", "topology", "alphaType", "iterations1", "iterations2",
23                  "radius1", "radius2", "alpha1", "alpha2", "initialization", "eps"]
24    def __init__(self ,parent=None , signalManager=None, name="SOM"):
25        OWWidget.__init__(self, parent, signalManager, name, wantMainArea=False)
26
27        self.inputs = [("Data", ExampleTable, self.setData)]
28        self.outputs = [("Classifier", orange.Classifier), ("Learner", orange.Learner), ("SOM", orngSOM.SOMMap)]
29
30        self.LearnerName="SOM Map"
31        self.xdim = 5
32        self.ydim = 10
33        self.initialization = orngSOM.InitializeLinear
34        self.neighborhood = 0
35        self.topology = 0
36        self.alphaType = 0
37        self.iterations1 = 100
38        self.iterations2 = 10000
39        self.radius1 = 3
40        self.radius2 = 1
41        self.eps = 1e-5
42        self.alpha1 = 0.05
43        self.alpha2 = 0.01
44        self.loadSettings()
45##        self.TopolMap=[orangeom.SOMLearner.HexagonalTopology, orangeom.SOMLearner.RectangularTopology]
46        self.TopolMap = [orngSOM.HexagonalTopology, orngSOM.RectangularTopology]
47##        self.NeighMap=[orangeom.SOMLearner.BubbleNeighborhood, orangeom.SOMLearner.GaussianNeighborhood]
48        self.NeighMap = [orngSOM.NeighbourhoodGaussian, orngSOM.NeighbourhoodBubble]
49##        self.AlphaMap=[orangeom.SOMLearner.LinearFunction, orangeom.SOMLearner.InverseFunction]
50##        self.AlphaMap=[1, 2]
51        self.learnerName = OWGUI.lineEdit(self.controlArea, self, "LearnerName", box="Learner/Classifier Name", tooltip="Name to be used by other widgets to identify yor Learner/Classifier")
52##        self.learnerName.setText("SOM Classifier")
53##        box = OWGUI.widgetBox(self.controlArea, self, "Dimensions")
54        box = OWGUI.radioButtonsInBox(self.controlArea, self, "topology", ["Hexagonal topology", "Rectangular topology"], box="Topology")
55        OWGUI.spin(box, self, "xdim", 4, 1000, orientation="horizontal", label="Columns")
56        OWGUI.spin(box, self, "ydim", 4, 1000, orientation="horizontal", label="Rows")
57        OWGUI.radioButtonsInBox(self.controlArea, self, "initialization", ["Linear", "Random"], box="Map Initialization")
58        OWGUI.radioButtonsInBox(self.controlArea, self, "neighborhood", ["Gaussian neighborhood", "Bubble neighborhood"], box="Neighborhood")
59##        OWGUI.radioButtonsInBox(self.controlArea, self, "alphaType", ["Linear function", "Inverse function"], box="Alpha Function Type")
60        b = OWGUI.widgetBox(self.controlArea, "Radius")
61        OWGUI.spin(b, self, "radius1", 2,50, orientation="horizontal", label="Initial radius")
62        OWGUI.spin(b, self, "radius2", 1,50, orientation="horizontal", label="Final radius")
63
64        b = OWGUI.widgetBox(self.controlArea , "Stoping Conditions")
65        OWGUI.spin(b, self, "iterations1", 10, 10000, label="Iterations")
66##        OWGUI.doubleSpin(b, self, "eps", 0.0, 1.0, 1e-5, label="Eps")
67##        tabW=OWGUI.tabWidget(self.controlArea)
68##        b1=OWGUI.createTabPage(tabW, "Step 1")
69##        b2=OWGUI.createTabPage(tabW, "Step 2")
70##       
71##        OWGUI.spin(b1, self, "iterations1", 10, 100000, orientation="horizontal", label="Num. iter.")
72##        OWGUI.spin(b2, self, "iterations2", 10, 100000, orientation="horizontal", label="Num. iter.")
73##        OWGUI.spin(b1, self, "radius1", 2,1000, orientation="horizontal", label="Radius")
74##        OWGUI.spin(b2, self, "radius2", 2,1000, orientation="horizontal", label="Radius")
75##        OWGUI.doubleSpin(b1, self, "alpha1", 0.0, 1.0, 0.01, orientation="horizontal", label="Alpha")
76##        OWGUI.doubleSpin(b2, self, "alpha2", 0.0, 1.0, 0.01, orientation="horizontal", label="Alpha")
77##        self.alpha1=self.alpha1
78##        self.alpha2=self.alpha2
79        OWGUI.button(self.controlArea, self,  "&Apply", callback=self.ApplySettings, default=True)
80
81        OWGUI.rubber(self.controlArea)
82       
83        self.data = None       
84       
85        self.resize(100, 100)
86
87    def dataWithDefinedValues(self, data, ):
88        self.warning(1235)
89        self.warning(1236)
90        exclude = []
91        for attr in data.domain.variables:
92            if not any(not ex[attr].isSpecial() for ex in data):
93                exclude.append(attr)
94        if exclude:
95            self.warning(1235, "Excluding attributes with all unknown values: %s." % ", ".join(attr.name for attr in exclude))
96            exclude_class = data.domain.classVar in exclude
97            if exclude_class:
98                self.warning(1236, "Excluding class attribute: %s" % data.domain.classVar.name) 
99            domain = orange.Domain([attr for attr in data.domain.variables if attr not in exclude],
100                                   data.domain.classVar if not exclude_class else False)
101            domain.addmetas(data.domain.getmetas())
102            data = orange.ExampleTable(domain, data)
103           
104        return data
105       
106    def setData(self, data=None):
107        self.data = data
108        if data:
109            self.data = self.dataWithDefinedValues(data)
110            self.ApplySettings()
111        else:
112            self.send("Classifier", None)
113            self.send("SOM", None)
114            self.send("Learner", None)
115       
116
117    def ApplySettings(self):
118        topology = self.TopolMap[self.topology]
119        neigh = self.NeighMap[self.neighborhood]
120##        alphaT=self.AlphaMap[self.alphaType]
121##        params=[{"iterations":self.iterations1, "radius":self.radius1, "alpha":self.alpha1},
122##                {"iterations":self.iterations2, "radius":self.radius2, "alpha":self.alpha2}]
123        self.learner = orngSOM.SOMLearner(name=self.LearnerName, map_shape=(self.xdim, self.ydim), topology=topology, neighborhood=neigh,
124                                        epochs=self.iterations1, eps=self.eps, initialize=self.initialization,
125                                        radius_ini=self.radius1, radius_fin=self.radius2) #alphaType=alphaT, parameters=params)
126
127        self.send("Learner", self.learner)
128        if self.data:
129            self.progressBarInit()
130            self.classifier = self.learner(self.data, progressCallback=self.progressBarSet)
131            self.progressBarFinished()
132            self.classifier.name = self.LearnerName
133            self.classifier.setattr("data", self.data)
134            if self.data.domain.classVar:
135                self.send("Classifier", self.classifier)
136            self.send("SOM", self.classifier)
137       
138
139    def sendReport(self):
140        self.reportSettings("Topology",
141                            [("Shape", ["hexagonal", "rectangular"][self.topology]),
142                             ("Size", "%i columns, %i rows" % (self.xdim, self.ydim))
143                            ])
144        self.reportSettings("Optimization",
145                            [("Initialization", ["linear", "random"][self.initialization]),
146                             ("Neighborhood", ["Gaussian", "bubble"][self.neighborhood]),
147                             ("Radius", "initial: %i, final: %i" % (self.radius1, self.radius2)),
148                             ("Number of iterations", self.iterations1)
149                            ])
150       
151if __name__ == "__main__":
152    app = QApplication(sys.argv)
153    w = OWSOM()
154##    app.setMainWidget(w)
155    w.show()
156    data = orange.ExampleTable("../../doc/datasets/iris.tab")
157   
158    w.setData(data)
159    app.exec_()
Note: See TracBrowser for help on using the repository browser.