import numpy as np
from PyQt5 import QtWidgets
from gui.ui.LocalRMSEPExplore import Ui_Form
from gui.util.Modules import Modules
from libpyhat.regression import local_rmsep
[docs]
class LocalRMSEPExplore(Ui_Form, Modules):
[docs]
    def setupUi(self, Form):
        super().setupUi(Form)
        Modules.setupUi(self, Form) 
[docs]
    def hidefit(self):
        if self.extrapolate_check.isChecked():
            hide = False
        else:
            hide = True
        self.fitall.setHidden(hide)
        self.fit_local_min.setHidden(hide)
        self.xmax.setHidden(hide)
        self.xmax_label.setHidden(hide) 
[docs]
    def refresh(self):
        self.changeComboListVars(
            self.choosecomp,
            self.get_choices(self.comp_label)
        )
        self.changeComboListVars(
            self.choosepredict,
            self.get_choices('predict')
        ) 
[docs]
    def run(self):
        data = self.data[self.chooseDataComboBox.currentText()]
        self.comp_label = data.comp_label
        predictions = data.df[('predict', self.choosepredict.currentText())]
        actuals = data.df[(self.comp_label, self.choosecomp.currentText())]
        windowsize = [float(i) for i in self.window_size.text().split(',')]
        n_neighbors = np.array(
            [int(i) for i in self.n_neighbors.text().split(',')]
        )
        n_neighbors = n_neighbors[n_neighbors >= 1]
        sigma = [int(i) for i in self.sigma.text().split(',')]
        xmax = self.xmax.value()
        if self.plot_file.text() == '':
            plot_file = None
        else:
            plot_file = self.plot_file.text()
        extrap = self.extrapolate_check.isChecked()
        fullfit = self.fitall.isChecked()
        local_rmsep.local_rmse_explore(
            predictions, actuals,
            windowsize=windowsize,
            min_rmsep_num=n_neighbors,
            sigma=sigma,
            plot_file=plot_file, xmax=xmax,
            outpath=self.outpath,
            element=self.choosecomp.currentText(),
            full_fit=fullfit, extrapolate=extrap
        ) 
[docs]
    def get_choices(self, colname):
        try:
            choices = self.data[self.chooseDataComboBox.currentText()].df[
                colname].columns.values
            choices = [i for i in choices if
                'Unnamed' not in i]  # remove unnamed columns from
            # choices
        except:
            choices = ['No matching columns!']
        return choices 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = LocalRMSEPExplore()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())