from PyQt5 import QtWidgets
from gui.ui.LocalRMSEPCalc import Ui_Form
from gui.util.Modules import Modules
from libpyhat.regression import local_rmsep
[docs]
class LocalRMSEPCalc(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"))
        self.changeComboListVars(self.chooseUnkpredict, self.get_choices_unk("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())]
        unk_predicts = self.data[self.chooseUnkData.currentText()].df[
            ("predict", self.chooseUnkpredict.currentText())
        ]
        windowsize = self.win_size_spin.value()
        n_neighbors = self.n_neighbors_spin.value()
        sigma = self.sigma_spin.value()
        xmax = self.xmax.value()
        extrap = self.extrapolate_check.isChecked()
        fullfit = self.fitall.isChecked()
        local_rmseps = local_rmsep.local_rmse_calc(
            predictions,
            actuals,
            unk_predicts,
            windowsize=windowsize,
            min_rmsep_num=n_neighbors,
            sigma=sigma,
            extrapolate=extrap,
            full_fit=fullfit,
            xmax=xmax,
        )
        self.data[self.chooseUnkData.currentText()].df[
            ("predict", "Local_RMSEP - " + self.chooseUnkpredict.currentText())
        ] = local_rmseps 
[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 
[docs]
    def get_choices_unk(self, colname):
        try:
            choices = (
                self.data[self.chooseUnkData.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 = LocalRMSEPCalc()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())