from PyQt5 import QtWidgets
import gui.core.caltranMethods as ctm
from gui.ui.CalibrationTransfer import Ui_Form
from gui.util import Qtickle
from gui.util.Modules import Modules
[docs]
class CalibrationTransfer(Ui_Form, Modules):
[docs]
    def setupUi(self, Form):
        super().setupUi(Form)
        Modules.setupUi(self, Form)
        self.caltranMethods() 
[docs]
    def getGuiParams(self):
        """
        Overriding Modules' getGuiParams, because I'll need to do a list of
        lists
        in order to obtain the regressionMethods' parameters
        """
        self.qt = Qtickle.Qtickle(self)
        s = []
        s.append(self.qt.guiSave())
        for items in self.alg:
            s.append(self.alg[items].getGuiParams())
        return s 
[docs]
    def setGuiParams(self, dict):
        """
        Overriding Modules' setGuiParams as we are using a list of lists to
        :param dict:
        :return:
        """
        self.qt = Qtickle.Qtickle(self)
        self.qt.guiRestore(dict[0])
        keys = list(self.alg.keys())
        for i in range(len(dict)):
            self.alg[keys[i - 1]].setGuiParams(dict[i]) 
[docs]
    def selectiveSetGuiParams(self, dict):
        """
        Override Modules' selective Restore function
        Setup Qtickle
        selectively restore the UI, the data to do that will be in the 0th
        element of the dictionary
        We will then iterate through the rest of the dictionary
        Will now restore the parameters for the algorithms in the list,
        Each of the algs have their own selectiveSetGuiParams
        :param dict:
        :return:
        """
        self.qt = Qtickle.Qtickle(self)
        self.qt.selectiveGuiRestore(dict[0])
        keys = list(self.alg.keys())
        for i in range(len(dict)):
            self.alg[keys[i - 1]].selectiveSetGuiParams(dict[i]) 
[docs]
    def hideAll(self):
        for a in self.alg:
            self.alg[a].setHidden(True) 
[docs]
    def caltranMethods(self):
        self.alg = {
            'PDS - Piecewise DS': ctm.caltran_PDS.Ui_Form(),
            'DS - Direct Standardization': ctm.caltran_DS.Ui_Form(),
            'LASSO DS': ctm.caltran_LASSODS.Ui_Form(),
            'Ridge DS': ctm.caltran_RidgeDS.Ui_Form(),
            'CCA - Canonical Correlation Analysis':
                ctm.caltran_CCA.Ui_Form(),
            'New CCA': ctm.caltran_NewCCA.Ui_Form(),
            'Incremental Proximal Descent DS':
                ctm.caltran_IPDDS.Ui_Form(),
            'Forward Backward DS': ctm.caltran_FBDS.Ui_Form(),
            'Sparse Low Rank DS': ctm.caltran_SparseDS.Ui_Form(),
            'Ratio': ctm.caltran_Ratio.Ui_Form(),
            'PDS-PLS - PDS using Partial Least Squares':
                ctm.caltran_cv_PDS_PLS.Ui_Form()
        }
        for item in self.alg:
            self.alg[item].setupUi(self.Form)
            self.methodlayout.addWidget(self.alg[item].get_widget())
            self.alg[item].setHidden(True) 
[docs]
    def change_choices(self, combobox, datacombo):
        combobox.clear()
        try:
            choices = self.data[datacombo.currentText()].df[
                'meta'].columns.values
        except:
            choices = ['No metadata columns!']
        combobox.addItems(choices) 
[docs]
    def run(self):
        datakeyA = self.chooseDataA.currentText()
        datakeyB = self.chooseDataB.currentText()
        datakeyC = self.chooseDatatoTransform.currentText()
        dataAmatchcol = self.chooseDataAMatch.currentText()
        dataBmatchcol = self.chooseDataBMatch.currentText()
        method = self.chooseMethod.currentText()
        params = self.alg[method].run()
        params['method'] = method
        A = self.data[datakeyA].df
        B = self.data[datakeyB].df
        if self.save_inputs_checkbox.isChecked():
            outfileA = datakeyA + '_caltran_averages.csv'
            outfileB = datakeyB + '_caltran_averages.csv'
            A.to_csv(self.outpath + '//' + outfileA)
            B.to_csv(self.outpath + '//' + outfileB)
        print(
            'Deriving transform from ' + datakeyA + ' to ' + datakeyB +
            ' using ' + method
        )
        print('Applying transform to ' + datakeyC)
        self.data[datakeyC].cal_tran(
            A, B, dataAmatchcol, dataBmatchcol,
            params, datakeyA, datakeyB
        )
        if self.save_transform_checkbox.isChecked():
            transform_filename = (datakeyA + '_to_' + datakeyB + '_caltran_'
                                  + method + '.csv')
            self.data[datakeyC].ct_obj.save_transform(
                self.outpath + '//' + transform_filename,
                self.data[datakeyA].df[
                    self.data[datakeyA].spect_label].columns.values
            ) 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = CalibrationTransfer()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())