class Widget(QWidget): def __init__(self, parent=None): super(Widget, self).__init__(parent) label = QLabel() label.setText('The neuron that you will be examining will divide the objects you will show it into the ones\n' + 'that it likes and the ones it dislikes\n' + 'Let\'s assume that be recognized objects are flowers\n') # groupbox1 groupBox1 = QGroupBox() groupBox1.setTitle('Feature weight') groupBox1Label1 = QLabel() groupBox1Label1.setText('Here you enter the neuron\'s weight coefficients for the individual object features. Positve\n' + 'coefficient means approval to the attribute, and negative one will indicate that the given\n' + 'attribute should be disliked by the neuron.') groupBox1Label2 = QLabel() groupBox1Label2.setText('Now, enter if you want the neuron to like the flower that is:') self.spinBoxWeightFragment = QDoubleSpinBox() self.spinBoxWeightFragment.setValue(1.0) self.spinBoxWeightFragment.setRange(-100.0, 100.0) self.spinBoxWeightColorful = QDoubleSpinBox() self.spinBoxWeightColorful.setValue(2.0) self.spinBoxWeightColorful.setRange(-100.0, 100.0) groupBox1HLayout = QHBoxLayout() groupBox1HLayout.addWidget(QLabel('Fragment:')) groupBox1HLayout.addWidget(self.spinBoxWeightFragment) groupBox1HLayout.addWidget(QLabel('Colorful:')) groupBox1HLayout.addWidget(self.spinBoxWeightColorful) groupBox1VLayout = QVBoxLayout() groupBox1VLayout.addWidget(groupBox1Label1) groupBox1VLayout.addWidget(groupBox1Label2) groupBox1VLayout.addLayout(groupBox1HLayout) groupBox1.setLayout(groupBox1VLayout) # groupbox2 groupBox2 = QGroupBox() groupBox2.setTitle('Evaluated object') self.spinBoxObjectFragment = QDoubleSpinBox() self.spinBoxObjectFragment.setRange(-100.0, 100.0) self.spinBoxObjectColorful = QDoubleSpinBox() self.spinBoxObjectColorful.setRange(-100.0, 100.0) groupBox2HLayout = QHBoxLayout() groupBox2HLayout.addWidget(QLabel('Fragment')) groupBox2HLayout.addWidget(self.spinBoxObjectFragment) groupBox2HLayout.addWidget(QLabel('Colorful')) groupBox2HLayout.addWidget(self.spinBoxObjectColorful) groupBox2VLayout = QVBoxLayout() groupBox2VLayout.addWidget(QLabel('After setting the weight, yout can perform experiments. Enter if the flower is:')) groupBox2VLayout.addLayout(groupBox2HLayout) groupBox2.setLayout(groupBox2VLayout) # groupbox3 groupBox3 = QGroupBox() groupBox3.setTitle('Neuron\'s response') pushbuttonGroup3 = QPushButton('Recalculate!') self.output = QLabel() self.attitude = QLabel() groupBox3HLayout1 = QHBoxLayout() groupBox3HLayout1.addWidget(QLabel('The neuron\'s response value is:')) groupBox3HLayout1.addWidget(self.output) groupBox3HLayout2 = QHBoxLayout() groupBox3HLayout2.addWidget(QLabel('It means that the neuron\'s attitude against the flower is:')) groupBox3HLayout2.addWidget(self.attitude) groupBox3HLayout2.addWidget(pushbuttonGroup3) groupBox3VLayout = QVBoxLayout() groupBox3VLayout.addLayout(groupBox3HLayout1) groupBox3VLayout.addLayout(groupBox3HLayout2) groupBox3.setLayout(groupBox3VLayout) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(groupBox1) layout.addWidget(groupBox2) layout.addWidget(groupBox3) self.setLayout(layout) self.setWindowTitle('Single neuron examination (example 01a)') self.connect(self.spinBoxWeightFragment, SIGNAL('valueChanged(double)'), self.evaluateObject) self.connect(self.spinBoxWeightColorful, SIGNAL('valueChanged(double)'), self.evaluateObject) self.connect(self.spinBoxObjectFragment, SIGNAL('valueChanged(double)'), self.evaluateObject) self.connect(self.spinBoxObjectColorful, SIGNAL('valueChanged(double)'), self.evaluateObject) self.connect(pushbuttonGroup3, SIGNAL('clicked()'), self.evaluateObject) self.neuron = Neuron(2) def evaluateObject(self): self.neuron.weights[0] = self.spinBoxWeightFragment.value() self.neuron.weights[1] = self.spinBoxWeightColorful.value() signals = [self.spinBoxObjectFragment.value(), self.spinBoxObjectColorful.value()] response = self.neuron.response(signals) strength = self.neuron.memoryTraceStrength(Neuron.StrenghtNormManhattan) attitude = '' palette = QPalette() if abs(response) < 0.2 * strength: print 'response ', response, '\t0.2 * strength ', 0.2 * strength, '\tstrenght ', strength, '\tindiffrent' attitude = 'indiffrent' palette.setColor(QPalette.WindowText, Qt.darkCyan) elif response < 0: print 'responce ', response, '\tstrength ', strength, '\tnegative' attitude = 'negative' palette.setColor(QPalette.WindowText, Qt.blue) else: print 'responce ', response, '\tstrength ', strength, '\tpositive' attitude = 'positive' palette.setColor(QPalette.WindowText, Qt.red) self.output.setText(str(response)) self.attitude.setText(attitude) self.attitude.setPalette(palette)
class Widget(QWidget): def __init__(self, parent=None): super(Widget, self).__init__(parent) groupBox1 = QGroupBox() groupBox1.setTitle('Feature weights (model object)') self.w1 = QDoubleSpinBox() self.w1.setRange(-10.0, 10.0) self.w1.setValue(5.0) self.w2 = QDoubleSpinBox() self.w2.setRange(-10.0, 10.0) self.w2.setValue(5.0) buttonHelpGroupBox1 = QPushButton('help') buttonHelpGroupBox1.setToolTip('Here you enter the neuron\'s weight coefficients for the individual object features.\n' + 'Positive coefficient means approval to the feature, and negative one will indicate that\n' + 'the given feature should be disliked by the neuron.\n\n' + 'Now the only important thing will be the numeric values of the weights and input signals.\n' + 'However, if you want, you can still imagine that the examined objects are flowers - as\n' + 'in the previous program. The w(1) weight will then indicate the fragrance intensity of\n' + 'the flower, and the w(2) weight - its colour intensity.\n\n' + 'You can also think about this as the location of the "model object".') buttonHelpGroupBox1.setEnabled(False) hBoxLayoutGroupBox1 = QHBoxLayout() hBoxLayoutGroupBox1.addWidget(QLabel('w(1) = ')) hBoxLayoutGroupBox1.addWidget(self.w1) hBoxLayoutGroupBox1.addWidget(QLabel('w(2) = ')) hBoxLayoutGroupBox1.addWidget(self.w2) hBoxLayoutGroupBox1.addWidget(buttonHelpGroupBox1) groupBox1.setLayout(hBoxLayoutGroupBox1) groupBox2 = QGroupBox() groupBox2.setTitle('Evaluated object') self.x1 = QDoubleSpinBox() self.x1.setRange(-10.0, 10.0) self.x1.setValue(-5.0) self.x2 = QDoubleSpinBox() self.x2.setRange(-10.0, 10.0) self.x2.setValue(-5.0) buttonHelpGroupBox2 = QPushButton('help') buttonHelpGroupBox2.setToolTip('Here you can enter the feature values of the evaluated object. As above, you are\n' + 'free to imagine that it is a flower whose fragrance intensity is x(1) and colour intensity\n' + 'is x(2).a') buttonHelpGroupBox2.setEnabled(False) hBoxLayoutGroupBox2 = QHBoxLayout() hBoxLayoutGroupBox2.addWidget(QLabel('x(1) = ')) hBoxLayoutGroupBox2.addWidget(self.x1) hBoxLayoutGroupBox2.addWidget(QLabel('x(2) = ')) hBoxLayoutGroupBox2.addWidget(self.x2) hBoxLayoutGroupBox2.addWidget(buttonHelpGroupBox2) groupBox2.setLayout(hBoxLayoutGroupBox2) groupBox3 = QGroupBox() groupBox3.setTitle('Graph') self.colorResponse = QLabel('Evaluated object') buttonHelpGroupBox3 = QPushButton('help') buttonHelpGroupBox3.setToolTip('On your left, you see a signal graph. The blue point represents the location of the\n' + 'model object. The other point is the location of the evaluated object. Its color, shown\n' + 'on the chart\'s key, corresponds to the neuron\'s attitude towards the evaluated\n' + 'object.\n\n' + 'Click the graph with the right mouse button to set the model object location. The left\n' + 'mouse button sets the evaluated object. You can also perform dragging to move the\n' + 'objects smoothly.') buttonHelpGroupBox3.setEnabled(False) hBoxLayoutGroupBox3 = QHBoxLayout() hBoxLayoutGroupBox3.addWidget(self.colorResponse) hBoxLayoutGroupBox3.addWidget(buttonHelpGroupBox3) vBoxLayoutGroupBox3 = QVBoxLayout() vBoxLayoutGroupBox3.addWidget(QLabel('Key')) vBoxLayoutGroupBox3.addWidget(QLabel('Model object')) vBoxLayoutGroupBox3.addLayout(hBoxLayoutGroupBox3) groupBox3.setLayout(vBoxLayoutGroupBox3) self.response = QLabel('0') hBoxLayoutResponse = QHBoxLayout() hBoxLayoutResponse.addWidget(QLabel('Neuron\'s response: ')) hBoxLayoutResponse.addWidget(self.response) vBoxlayout = QVBoxLayout() vBoxlayout.addWidget(QLabel('The neuron that you will be examining will divide the objects you\n' + 'will show it into the ones that it likes and the ones id dislikes.')) vBoxlayout.addWidget(groupBox1) vBoxlayout.addWidget(groupBox2) vBoxlayout.addLayout(hBoxLayoutResponse) vBoxlayout.addWidget(groupBox3) self.figure = plot.figure() self.canvas = FigureCanvas(self.figure) self.customePlot() hBoxLayout = QHBoxLayout() hBoxLayout.addWidget(self.canvas) hBoxLayout.addLayout(vBoxlayout) self.neuron = Neuron(2) self.evaluatedObject() self.setLayout(hBoxLayout) self.setWindowTitle('Single neuron examination with visualization (example 01b)') self.connect(self.w1, SIGNAL('valueChanged(double)'), self.evaluatedObject) self.connect(self.w2, SIGNAL('valueChanged(double)'), self.evaluatedObject) self.connect(self.x1, SIGNAL('valueChanged(double)'), self.evaluatedObject) self.connect(self.x2, SIGNAL('valueChanged(double)'), self.evaluatedObject) def evaluatedObject(self): self.figure.clear() self.customePlot() ax = self.figure.add_subplot(111) valueW1 = self.w1.value() valueW2 = self.w2.value() self.neuron.weights[0] = valueW1 self.neuron.weights[1] = valueW2 plot.scatter(valueW1, valueW2, color='k') plot.plot([0.0, valueW1],[0.0, valueW2], color='k') plot.plot([0.0, valueW1], [valueW2, valueW2], 'r--', linewidth=2) plot.plot([valueW1, valueW1], [0.0, valueW2], 'r--', linewidth=2) valueX1 = self.x1.value() valueX2 = self.x2.value() inputs = [valueX1, valueX2] response = self.neuron.response(inputs) strength = self.neuron.memoryTraceStrength(Neuron.StrenghtNormEuclidean) palette = QPalette() if abs(response) < 0.2 * strength: palette.setColor(QPalette.WindowText, Qt.darkCyan) elif response < 0: palette.setColor(QPalette.WindowText, Qt.blue) else: palette.setColor(QPalette.WindowText, Qt.red) self.colorResponse.setPalette(palette) self.response.setPalette(palette) self.response.setText(str(response)) plot.scatter(valueX1, valueX2, color='k') plot.plot([0.0, valueX1],[0.0, valueX2], color='k') plot.plot([0.0, valueX1], [valueX2, valueX2], 'b--', linewidth=2) plot.plot([valueX1, valueX1], [0.0, valueX2], 'b--', linewidth=2) self.canvas.draw() def customePlot(self): plot.xlim(-10.0, 10.0) plot.ylim(-10.0, 10.0) ax = plot.subplot() ax.grid(True, which='both') # y axis ax.spines['bottom'].set_position('center') # x axis ax.spines['left'].set_position('center')
class Widget(QWidget): def __init__(self, parent=None): super(Widget, self).__init__(parent) self.widgetInputCount = inputcountselectionpanel.Widget() self.widgetExperiment = experimentpanel.Widget() self.stackedWidget = QStackedWidget() self.stackedWidget.addWidget(self.widgetInputCount) self.stackedWidget.addWidget(self.widgetExperiment) self.buttonBack = QPushButton('Back') self.buttonBack.setEnabled(False) self.buttonNext = QPushButton('Next') hboxLayoutButton = QHBoxLayout() hboxLayoutButton.addStretch(1) hboxLayoutButton.addWidget(self.buttonBack) hboxLayoutButton.addWidget(self.buttonNext) vboxLayout = QVBoxLayout() vboxLayout.addWidget(self.stackedWidget) vboxLayout.addLayout(hboxLayoutButton) self.setWindowTitle('Examination of a single neuron with multiple inputs (example 01c)') self.setLayout(vboxLayout) self.connect(self.buttonNext, SIGNAL('clicked()'), self.nextWidget) self.connect(self.buttonBack, SIGNAL('clicked()'), self.backWidget) self.connect(self.widgetExperiment.buttonRecalculate, SIGNAL('clicked()'), self.updateResult) def nextWidget(self): self.buttonNext.setEnabled(False) self.buttonBack.setEnabled(True) self.stackedWidget.setCurrentIndex(1) self.widgetExperiment.tableWidget.clear() self.widgetExperiment.memoryEdit.clear() self.widgetExperiment.signalEdit.clear() self.widgetExperiment.outputEdit.clear() self.widgetExperiment.tableWidget.setRowCount(self.widgetInputCount.spinInput.value()) self.widgetExperiment.tableWidget.setColumnCount(3) self.widgetExperiment.tableWidget.setHorizontalHeaderLabels(['i', 'w(i)', 'x(i)']) self.neuron = Neuron(self.widgetInputCount.spinInput.value()) for i in xrange(len(self.neuron.weights)): spinBox1 = QDoubleSpinBox() spinBox1.setRange(-100.0, 100.0) spinBox2 = QDoubleSpinBox() spinBox2.setRange(-100.0, 100.0) self.connect(spinBox1, SIGNAL('valueChanged(double)'), self.updateResult) self.connect(spinBox2, SIGNAL('valueChanged(double)'), self.updateResult) self.widgetExperiment.tableWidget.setCellWidget(i, 0, QLabel(str(i + 1))) self.widgetExperiment.tableWidget.setCellWidget(i, 1, spinBox1) self.widgetExperiment.tableWidget.setCellWidget(i, 2, spinBox2) def backWidget(self): self.buttonNext.setEnabled(True) self.buttonBack.setEnabled(False) self.stackedWidget.setCurrentIndex(0) def updateResult(self): for i in xrange(len(self.neuron.weights)): self.neuron.weights[i] = self.widgetExperiment.tableWidget.cellWidget(i, 1).value() inputs = [self.widgetExperiment.tableWidget.cellWidget(i, 2).value() for i in xrange(len(self.neuron.weights))] response = self.neuron.response(inputs) signalStrength = Neuron.strength(inputs, Neuron.StrenghtNormEuclidean) memStrength = self.neuron.memoryTraceStrength(Neuron.StrenghtNormEuclidean) self.widgetExperiment.signalEdit.setText(str(signalStrength)) self.widgetExperiment.memoryEdit.setText(str(memStrength)) self.widgetExperiment.outputEdit.setText(str(response))