class BackPropagationLearningDialog(QDialog):
    def __init__(self, network, test_data=None):
        QDialog.__init__(self)

        self.ui = Ui_LearningLauncher()
        self.ui.setupUi(self)

        self.network = network
        self._setup_gui()

        self.input_view = DataView(self.ui.learningDataView, header="Input", model=test_data,
            columns=network.inputs_count() + len(network.get_layer(-1).get_neurons()))

    #        self.input_view.randomize_data()

    @pyqtSlot()
    def on_runButton_clicked(self):
        error_metric = ErrorUtil.get_factor(str(self.ui.errorMetricSelector.currentText()))()
        learning_factor = float(self.ui.learningFactorValue.text())
        momentum = float(self.ui.momentumValue.text())
        learning_iterations = int(self.ui.learningIterations.text())
        bias_enabled = self.ui.biasEnabled.isChecked()
        learning = BackpropagationLearning(self.network, error_metric, learning_factor, momentum, bias_enabled)
        learning.learn(DataNormalizer.normalize(self.input_view.get_data()), learning_iterations)
        self.accept()

    @pyqtSlot()
    def on_randomizeButton_clicked(self):
        self.input_view.randomize_data()

    @pyqtSlot()
    def on_readInputButton_clicked(self):
        filePath = QFileDialog().getOpenFileName(None, "Select input data file")
        try:
            with open(filePath, "r") as file:
                file_lines = file.readlines()
                rows_count = len(file_lines)
                self.ui.dataCount.setValue(rows_count)
                for (line, row) in zip(file_lines, range(rows_count)):
                    for (value, column) in zip(line.split(), range(self.input_view.column_count())):
                        self.input_view.set_data(row, column, float(value))
        except Exception as e:
            print e

    @pyqtSlot(int)
    def on_dataCount_valueChanged(self, value):
        old_rows_count = self.input_view.row_count()
        self.input_view.set_rows(value)
        self.input_view.randomize_data(old_rows_count)

    def _setup_gui(self):
        self.ui.errorMetricSelector.addItems(ErrorUtil.registered_factors())
        self.ui.errorMetricSelector.setCurrentIndex(self.ui.errorMetricSelector.findText(ErrorUtil.default_factor()))

    def get_model(self):
        return self.input_view.get_model()
class SimulationLauncherDialog(QDialog):
    def __init__(self, network, test_data):
        QDialog.__init__(self)

        self.ui = Ui_SimulationLauncher()
        self.ui.setupUi(self)

        self.network = network

        inputs_count = len(network.inputs)
        self.input_view = DataView(self.ui.dataView, model=test_data, header="Input", columns=inputs_count)

        outputs_count = len(network.layers[-1].neurons)
        self.output_view = DataView(self.ui.outputView, header="Output", columns=outputs_count)

        self.ui.testsCountSpinBox.setValue(self.input_view.row_count())

    @pyqtSlot()
    def on_runButton_clicked(self):
        if not self.input_view.check_data():
            QMessageBox.warning(None, "Error", "Input data is invalid")
        else:
            inputs = self.input_view.get_data()
            for row_index in range(len(inputs)):
                row = inputs[row_index]
                response = self.network.calculate_network_response(row)
                for i in range(len(response)):
                    self.output_view.set_data(row_index, i, round(response[i], 3))

    @pyqtSlot()
    def on_readFileButton_clicked(self):
        filePath = QFileDialog().getOpenFileName(None, "Select input data file")
        try:
            with open(filePath, "r") as file:
                file_lines = file.readlines()
                rows_count = len(file_lines)
                self.ui.testsCountSpinBox.setValue(rows_count)
                for (line, row) in zip(file_lines, range(rows_count)):
                    for (value, column) in zip(line.split(), range(self.input_view.column_count())):
                        self.input_view.set_data(row, column, float(value))
        except Exception as e:
            print e

    def get_model(self):
        return self.input_view.get_model()

    @pyqtSlot()
    def on_generateRandomButton_clicked(self):
        self.input_view.randomize_data()

    @pyqtSlot(int)
    def on_testsCountSpinBox_valueChanged(self, value):
        self.input_view.set_rows(value)
        self.output_view.set_rows(value)