import numpy as np
import pandas as pd
from PyQt5 import QtWidgets
import gui.core.dimensionalityReductionMethods as drm
from gui.ui.DimensionalityReduction import Ui_Form
from gui.util import Qtickle
from gui.util.Modules import Modules
from libpyhat.spectral_data import SpectralData
[docs]
class DimensionalityReduction(Ui_Form, Modules):
[docs]
    def setupUi(self, Form):
        self.Form = Form
        super().setupUi(Form)
        Modules.setupUi(self, Form)
        self.dimensionalityReductionMethods() 
[docs]
    def update_LDA(self):
        if (self.chooseMethodComboBox.currentText() == 'LDA' or
                self.chooseMethodComboBox.currentText() == 'LFDA'):
            new_y_choices = self.yvar_choices()
            index = self.chooseMethodComboBox.currentIndex()
            self.alg[index - 1].update(new_y_choices)
        else:
            pass 
[docs]
    def yvar_choices(self):
        try:
            yvarchoices = self.data[
                self.chooseDataComboBox.currentText()].df.columns.values
            yvarchoices = [i for i in yvarchoices if
                'Unnamed' not in i]  # remove unnamed columns
            # from choices
        except:
            yvarchoices = ['No valid columns!']
        return yvarchoices 
[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(items.getGuiParams())
        return s 
[docs]
    def setGuiParams(self, dict):
        """
        Overriding Modules' setGuiParams, because we are accessing a list of
        lists
        And each submodule contains it's own `setGuiParams`
        """
        self.qt = Qtickle.Qtickle(self)
        self.qt.guiRestore(dict[0])
        for i in range(len(dict)):
            self.alg[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])
        for i in range(len(dict)):
            self.alg[i - 1].selectiveSetGuiParams(dict[i]) 
[docs]
    def run(self):
        load_fit = False
        method = self.chooseMethodComboBox.currentText()
        datakey = self.chooseDataComboBox.currentText()
        col = self.data[datakey].spect_label
        params, modelkey, ycol = self.getMethodParams(
            self.chooseMethodComboBox.currentIndex()
        )
        self.data[datakey].dim_red(
            col, method, [], params, load_fit,
            ycol=ycol
        )
        dimredkey = datakey + '-' + method
        self.dimredkeys.append(dimredkey)
        self.dimred[dimredkey] = self.data[datakey].do_dim_red
        # store the loadings in a data set that is accessible from the GUI
        if hasattr(self.data[datakey].do_dim_red, 'components_'):
            loadings = np.squeeze(
                np.array(self.data[datakey].do_dim_red.components_)
            )
            loadings = pd.DataFrame(
                loadings,
                index=list(
                    range(
                        1, self.data[
                               datakey].do_dim_red.components_.shape[
                               0] + 1
                    )
                )
            )
            loadings.columns = pd.MultiIndex.from_tuples(
                [(col, i) for i in self.data[datakey].df[col].columns.values]
            )
            loadings_name = method + ' Loadings'
            if loadings_name in self.datakeys:
                pass
            else:
                Modules.data_count += 1
                self.list_amend(
                    self.datakeys, Modules.data_count,
                    loadings_name
                )
            self.data[loadings_name] = SpectralData(
                loadings,
                name=loadings_name,
                spect_label=col
            ) 
[docs]
    def hideAll(self):
        for a in self.alg:
            a.setHidden(True) 
[docs]
    def dimensionalityReductionMethods(self):
        self.alg = []
        list_forms = [drm.dimred_PCA,
            drm.dimred_FastICA,
            drm.dimred_JADE,
            drm.dimred_tSNE,
            drm.dimred_LLE,
            drm.dimred_NNMF,
            drm.dimred_LDA,
            drm.dimred_MNF,
            drm.dimred_LFDA]
        for items in list_forms:
            self.alg.append(items.Ui_Form())
            self.alg[-1].setupUi(self.Form)
            self.dim_reduction_vlayout.addWidget(self.alg[-1].get_widget())
            self.alg[-1].setHidden(True) 
[docs]
    def getMethodParams(self, index):
        return self.alg[index - 1].run() 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = DimensionalityReduction()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())