Пример #1
0
    def btnConvert_click(self):
        totalTime = 0

        msgBox = QMessageBox()

        Model = ui.cbMethod.currentText()

        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo   = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return

        if FoldTo < FoldFrom:
            print("Please check fold parameters!")
            return

        for fold_all in range(FoldFrom, FoldTo+1):
            tic = time.time()
            # Regularization
            try:
                NIter = np.int32(ui.txtIter.text())
            except:
                msgBox.setText("Number of iterations is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            try:
                Gamma = np.float(ui.txtGamma.text())
            except:
                msgBox.setText("Gamma is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # AR Rank
            try:
                Rank = np.int32(ui.txtRank.text())
            except:
                msgBox.setText("Rank value is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # AR Rank
            try:
                Rho = np.float(ui.txtRho.text())
            except:
                msgBox.setText("Rho value is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # OutFile
            OutFile = ui.txtOutFile.text()
            OutFile = OutFile.replace("$FOLD$", str(fold_all))
            if not len(OutFile):
                msgBox.setText("Please enter out file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # InFile
            InFile = ui.txtInFile.text()
            InFile = InFile.replace("$FOLD$", str(fold_all))
            if not len(InFile):
                msgBox.setText("Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not os.path.isfile(InFile):
                msgBox.setText("Input file not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            InData = mainIO_load(InFile)
            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

            # Data
            if not len(ui.txtITrData.currentText()):
                msgBox.setText("Please enter Input Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeData.currentText()):
                msgBox.setText("Please enter Input Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrData.text()):
                msgBox.setText("Please enter Output Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeData.text()):
                msgBox.setText("Please enter Output Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            try:
                XTr = InData[ui.txtITrData.currentText()]
                XTe = InData[ui.txtITeData.currentText()]

                if ui.cbScale.isChecked() and not ui.rbScale.isChecked():
                    XTr = preprocessing.scale(XTr)
                    XTe = preprocessing.scale(XTe)
                    print("Whole of data is scaled X~N(0,1).")
            except:
                print("Cannot load data")
                return

            # NComponent
            try:
                NumFea = np.int32(ui.txtNumFea.text())
            except:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea < 0:
                msgBox.setText("Number of features must be greater than zero!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea > np.shape(XTr)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # Label
            if not len(ui.txtITrLabel.currentText()):
                    msgBox.setText("Please enter Train Input Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            if not len(ui.txtITeLabel.currentText()):
                    msgBox.setText("Please enter Test Input Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            if not len(ui.txtOTrLabel.text()):
                    msgBox.setText("Please enter Train Output Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            if not len(ui.txtOTeLabel.text()):
                    msgBox.setText("Please enter Test Output Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            try:
                OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(InData[ui.txtITrLabel.currentText()])
                OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(InData[ui.txtITeLabel.currentText()])
            except:
                print("Cannot load labels!")

            # Subject
            if not len(ui.txtITrSubject.currentText()):
                msgBox.setText("Please enter Train Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeSubject.currentText()):
                msgBox.setText("Please enter Test Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrSubject.text()):
                msgBox.setText("Please enter Train Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeSubject.text()):
                msgBox.setText("Please enter Test Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                TrSubject = InData[ui.txtITrSubject.currentText()]
                OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject)
                TeSubject = InData[ui.txtITeSubject.currentText()]
                OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject)
            except:
                print("Cannot load Subject IDs")
                return

            # Task
            if ui.cbTask.isChecked():
                if not len(ui.txtITrTask.currentText()):
                    msgBox.setText("Please enter Input Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeTask.currentText()):
                    msgBox.setText("Please enter Input Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrTask.text()):
                    msgBox.setText("Please enter Output Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeTask.text()):
                    msgBox.setText("Please enter Output Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrTask = np.asarray(InData[ui.txtITrTask.currentText()])
                    OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask)
                    TeTask = np.asarray(InData[ui.txtITeTask.currentText()])
                    OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask)
                    TrTaskIndex = TrTask.copy()
                    for tasindx, tas in enumerate(np.unique(TrTask)):
                        TrTaskIndex[TrTask == tas] = tasindx + 1
                    TeTaskIndex = TeTask.copy()
                    for tasindx, tas in enumerate(np.unique(TeTask)):
                        TeTaskIndex[TeTask == tas] = tasindx + 1
                except:
                    print("Cannot load Tasks!")
                    return

            # Run
            if ui.cbRun.isChecked():
                if not len(ui.txtITrRun.currentText()):
                    msgBox.setText("Please enter Train Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeRun.currentText()):
                    msgBox.setText("Please enter Test Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrRun.text()):
                    msgBox.setText("Please enter Train Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeRun.text()):
                    msgBox.setText("Please enter Test Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrRun = InData[ui.txtITrRun.currentText()]
                    OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun)
                    TeRun = InData[ui.txtITeRun.currentText()]
                    OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun)
                except:
                    print("Cannot load Runs!")
                    return

            # Counter
            if ui.cbCounter.isChecked():
                if not len(ui.txtITrCounter.currentText()):
                    msgBox.setText("Please enter Train Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeCounter.currentText()):
                    msgBox.setText("Please enter Test Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrCounter.text()):
                    msgBox.setText("Please enter Train Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeCounter.text()):
                    msgBox.setText("Please enter Test Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrCounter = InData[ui.txtITrCounter.currentText()]
                    OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(TrCounter)
                    TeCounter = InData[ui.txtITeCounter.currentText()]
                    OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(TeCounter)
                except:
                    print("Cannot load Counters!")
                    return

            # Matrix Label
            if ui.cbmLabel.isChecked():
                if not len(ui.txtITrmLabel.currentText()):
                    msgBox.setText("Please enter Train Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITemLabel.currentText()):
                    msgBox.setText("Please enter Test Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrmLabel.text()):
                    msgBox.setText("Please enter Train Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTemLabel.text()):
                    msgBox.setText("Please enter Test Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrmLabel.text()] = InData[ui.txtITrmLabel.currentText()]
                    OutData[ui.txtOTemLabel.text()] = InData[ui.txtITemLabel.currentText()]
                except:
                    print("Cannot load matrix lables!")
                    return

            # Design
            if ui.cbDM.isChecked():
                if not len(ui.txtITrDM.currentText()):
                    msgBox.setText("Please enter Train Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeDM.currentText()):
                    msgBox.setText("Please enter Test Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrDM.text()):
                    msgBox.setText("Please enter Train Output Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeDM.text()):
                    msgBox.setText("Please enter Test Output Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrDM.text()] = InData[ui.txtITrDM.currentText()]
                    OutData[ui.txtOTeDM.text()] = InData[ui.txtITeDM.currentText()]
                except:
                    print("Cannot load design matrices!")
                    return

            # Coordinate
            if ui.cbCol.isChecked():
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCol.text()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
                except:
                    print("Cannot load coordinator!")
                    return

            # Condition
            if ui.cbCond.isChecked():
                if not len(ui.txtCond.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCond.text()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
                except:
                    print("Cannot load conditions!")
                    return

            # FoldID
            if ui.cbFoldID.isChecked():
                if not len(ui.txtFoldID.currentText()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldID.text()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldID.text()] = reshape_1Dvector(InData[ui.txtFoldID.currentText()])
                except:
                    print("Cannot load Fold ID!")
                    return

            # FoldInfo
            if ui.cbFoldInfo.isChecked():
                if not len(ui.txtFoldInfo.currentText()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldInfo.text()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldInfo.text()] = InData[ui.txtFoldInfo.currentText()]
                except:
                    print("Cannot load Fold Info!")
                    return
                pass

            # Number of Scan
            if ui.cbNScan.isChecked():
                if not len(ui.txtITrScan.currentText()):
                    msgBox.setText("Please enter Number of Scan variable name for Input Train!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeScan.currentText()):
                    msgBox.setText("Please enter Number of Scan variable name for Input Test!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrScan.text()):
                    msgBox.setText("Please enter Number of Scan variable name for Output Train!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeScan.text()):
                    msgBox.setText("Please enter Number of Scan variable name for Output Test!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrScan.text()] = reshape_1Dvector(InData[ui.txtITrScan.currentText()])
                    OutData[ui.txtOTeScan.text()] = reshape_1Dvector(InData[ui.txtITeScan.currentText()])
                except:
                    print("Cannot load NScan!")
                    return

            # Train Analysis Level
            print("Calculating Analysis Level for Training Set ...")
            TrGroupFold = None
            FoldStr = ""
            if ui.cbFSubject.isChecked():
                if not ui.rbFRun.isChecked():
                    TrGroupFold = TrSubject
                    FoldStr = "Subject"
                else:
                    TrGroupFold = np.concatenate((TrSubject,TrRun))
                    FoldStr = "Subject+Run"

            if ui.cbFTask.isChecked():
                TrGroupFold = np.concatenate((TrGroupFold,TrTaskIndex)) if TrGroupFold is not None else TrTaskIndex
                FoldStr = FoldStr + "+Task"

            if ui.cbFCounter.isChecked():
                TrGroupFold = np.concatenate((TrGroupFold,TrCounter)) if TrGroupFold is not None else TrCounter
                FoldStr = FoldStr + "+Counter"

            TrGroupFold = np.transpose(TrGroupFold)

            TrUniqFold = np.array(list(set(tuple(i) for i in TrGroupFold.tolist())))

            TrFoldIDs = np.arange(len(TrUniqFold)) + 1

            TrListFold = list()
            for gfold in TrGroupFold:
                for ufoldindx, ufold in enumerate(TrUniqFold):
                    if (ufold == gfold).all():
                        currentID = TrFoldIDs[ufoldindx]
                        break
                TrListFold.append(currentID)
            TrListFold = np.int32(TrListFold)
            TrListFoldUniq = np.unique(TrListFold)


            # Test Analysis Level
            print("Calculating Analysis Level for Testing Set ...")
            TeGroupFold = None
            if ui.cbFSubject.isChecked():
                if not ui.rbFRun.isChecked():
                    TeGroupFold = TeSubject
                else:
                    TeGroupFold = np.concatenate((TeSubject,TeRun))

            if ui.cbFTask.isChecked():
                TeGroupFold = np.concatenate((TeGroupFold,TeTaskIndex)) if TeGroupFold is not None else TeTaskIndex

            if ui.cbFCounter.isChecked():
                TeGroupFold = np.concatenate((TeGroupFold,TeCounter)) if TeGroupFold is not None else TeCounter

            TeGroupFold = np.transpose(TeGroupFold)

            TeUniqFold = np.array(list(set(tuple(i) for i in TeGroupFold.tolist())))

            TeFoldIDs = np.arange(len(TeUniqFold)) + 1

            TeListFold = list()
            for gfold in TeGroupFold:
                for ufoldindx, ufold in enumerate(TeUniqFold):
                    if (ufold == gfold).all():
                        currentID = TeFoldIDs[ufoldindx]
                        break
                TeListFold.append(currentID)
            TeListFold = np.int32(TeListFold)
            TeListFoldUniq = np.unique(TeListFold)

            # Train Partition
            print("Partitioning Training Data ...")
            TrX = list()
            TrShape = None
            for foldindx, fold in enumerate(TrListFoldUniq):
                dat = XTr[np.where(TrListFold == fold)]
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    dat = preprocessing.scale(dat)
                    print("Data belong to View " + str(foldindx + 1) + " is scaled X~N(0,1).")

                AR_MAT = AR(np.shape(dat)[0], rho=Rho, rank=Rank)

                TrX.append(np.dot(np.transpose(dat), AR_MAT))
                if TrShape is None:
                    TrShape = np.shape(dat)
                else:
                    if not(TrShape == np.shape(dat)):
                        print("ERROR: Train, Reshape problem for Fold " + str(foldindx + 1) + ", Shape: " + str(np.shape(dat)))
                        return
                print("Train: View " + str(foldindx + 1) + " is extracted. Shape: " + str(np.shape(dat)))

            print("Training Shape (sub x voxel x time): " + str(np.shape(TrX)))

            # Test Partition
            print("Partitioning Testing Data ...")
            TeX = list()
            TeShape = None
            for foldindx, fold in enumerate(TeListFoldUniq):
                dat = XTe[np.where(TeListFold == fold)]
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    dat = preprocessing.scale(dat)
                    print("Data belong to View " + str(foldindx + 1) + " is scaled X~N(0,1).")

                AR_MAT = AR(np.shape(dat)[0], rho=Rho, rank=Rank)
                TeX.append(np.dot(np.transpose(dat), AR_MAT))
                if TeShape is None:
                    TeShape = np.shape(dat)
                else:
                    if not(TeShape == np.shape(dat)):
                        print("Test: Reshape problem for Fold " + str(foldindx + 1))
                        return
                print("Test: View " + str(foldindx + 1) + " is extracted.")

            print("Testing Shape (sub x voxel x time): " + str(np.shape(TeX)))

            if NumFea == 0:
                NumFea = np.min(np.shape(TrX)[1:3])
                print("Number of features are automatically selected as ", NumFea)

            MessageString = ""
            SharedR = None
            try:

                if Model == "Probabilistic SRM":
                    model = SRM(n_iter=NIter,features=NumFea)
                    MessageString = "Probabilistic "
                elif Model == "Deterministic SRM":
                    model = DetSRM(n_iter=NIter,features=NumFea)
                    MessageString = "Deterministic "
                else:
                    model = RSRM(n_iter=NIter, features=NumFea, gamma=Gamma)
                    SharedR = True
                    MessageString = "Robust "


                print("Running Hyperalignment on Training Data ...")
                model.fit(TrX)
                if SharedR == True:
                    S = model.r_
                    Spec_train = list()
                    for spec in model.s_:
                        Spec_train.append(np.transpose(spec))
                else:
                    S = model.s_

                WTr = list()
                for wtri in model.w_ :
                    WTr.append(wtri)
                # Train Dot Product
                print("Producting Training Data ...")
                TrHX = None
                for mapping, viewi in zip(WTr, TrX):
                    TrHX = np.concatenate((TrHX, np.transpose(np.dot(np.transpose(mapping),viewi)))) if TrHX is not None else np.transpose(np.dot(np.transpose(mapping),viewi))
                OutData[ui.txtOTrData.text()] = TrHX


                print("Running Hyperalignment on Testing Data ...")
                TeHX = None
                WTe = list()
                if SharedR:
                    Specific_test = np.zeros(np.shape(TeX))
                    for ijk in range(NIter):
                        # Considering Specific_Test is fixed and Updating Wi
                        WTe = list()
                        for vid, view in enumerate(TeX):
                            product = np.dot(view - Specific_test[vid], np.transpose(S))
                            U, _, V = lg.svd(product, full_matrices=False, check_finite=False)
                            WTe.append(np.dot(U, V))
                        # Considering Wi is fixed and Updating Specific_test
                        Specific_test = list()
                        for vid, (view, Wtest) in enumerate(zip(TeX, WTe)):
                            NewSpec = view - np.dot(Wtest, S)
                            posIndex = NewSpec > Gamma
                            negIndex = NewSpec < -Gamma
                            NewSpec[posIndex] -= Gamma
                            NewSpec[negIndex] += Gamma
                            NewSpec[np.logical_and(~posIndex, ~negIndex)] = .0
                            Specific_test.append(NewSpec)

                    # Generating the concatenate results
                    Spec_test = list()
                    for (Wtest, spec) in zip(WTe, Specific_test):
                        TeHX = np.concatenate((TeHX, np.transpose(np.dot(np.transpose(Wtest), view - spec)))) if TeHX is not None else np.transpose(np.dot(np.transpose(Wtest),view - spec))
                        Spec_test.append(np.transpose(spec))
                else:
                    for vid, view in enumerate(TeX):
                            product = np.dot(view, np.transpose(S))
                            U, _, V = lg.svd(product, full_matrices=False, check_finite=False)
                            Wtest = np.dot(U,V)
                            WTe.append(Wtest)
                            TeHX = np.concatenate((TeHX, np.transpose(np.dot(np.transpose(Wtest),view)))) if TeHX is not None else np.transpose(np.dot(np.transpose(Wtest),view))
                OutData[ui.txtOTeData.text()] = TeHX
            except Exception as e:
                print(e)

            HAParam = dict()
            if ui.cbSaveShare.isChecked():
                HAParam["Share"]    = S
            if SharedR:
                HAParam["Specific_train"] = Spec_train
                HAParam["Specific_test"]  = Spec_test
            if ui.cbSaveMap.isChecked():
                HAParam["WTrain"]   = WTr
                HAParam["WTest"]    = WTe
            HAParam["Model"]    = Model
            OutData["FunctionalAlignment"] = HAParam
            OutData["Runtime"] = time.time() - tic
            totalTime +=  OutData["Runtime"]

            print("Saving ...")
            mainIO_save(OutData, OutFile)
            print("Fold " + str(fold_all) + " is DONE: " + OutFile)

        print("Runtime: ", totalTime)
        print(MessageString + "Autoregressive Shared Response Model is done.")
        msgBox.setText(MessageString + "Autoregressive Shared Response Model is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #2
0
    def btnConvert_click(self):
        totalTime = 0
        msgBox = QMessageBox()

        # Batch
        try:
            Batch = np.int32(ui.txtBatch.text())
        except:
            msgBox.setText("Size of batch is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if Batch == 0:
            Batch = None

        # Kernel
        Kernel = ui.cbKernel.currentText()
        # Method
        Method = ui.cbMethod.currentText()

        # Gamma
        try:
            Gamma = np.float(ui.txtGamma.text())
        except:
            msgBox.setText("Gamma is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Degree
        try:
            Degree = np.int32(ui.txtDegree.text())
        except:
            msgBox.setText("Degree is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Coef0
        try:
            Coef0 = np.float(ui.txtCoef0.text())
        except:
            msgBox.setText("Coef0 is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Alpha
        try:
            Alpha = np.int32(ui.txtAlpha.text())
        except:
            msgBox.setText("Alpha is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Tol
        try:
            Tol = np.float(ui.txtTole.text())
        except:
            msgBox.setText("Tolerance is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # HAIte
        try:
            Iter = np.int32(ui.txtIter.text())
        except:
            msgBox.setText("Number of HA Iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if Iter <= 0:
            msgBox.setText("Number of HA Iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # MaxIte
        try:
            MaxIter = np.int32(ui.txtMaxIter.text())
        except:
            msgBox.setText("Maximum number of iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if MaxIter <= 0:
            MaxIter = None

        # Number of Job
        try:
            NJob = np.int32(ui.txtJobs.text())
        except:
            msgBox.setText("The number of parallel jobs is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if NJob < -1 or NJob == 0:
            msgBox.setText(
                "The number of parallel jobs must be -1 or greater than 0!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        TrFoldErr = list()
        TeFoldErr = list()

        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return

        if FoldTo < FoldFrom:
            print("Please check fold parameters!")
            return

        for fold_all in range(FoldFrom, FoldTo + 1):
            tic = time.time()

            # Regularization
            try:
                Regularization = np.float(ui.txtRegularization.text())
            except:
                msgBox.setText("Regularization value is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if Regularization <= 0 or Regularization >= 1:
                msgBox.setText("HA Regularization must be between 0 and 1")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # OutFile
            OutFile = ui.txtOutFile.text()
            OutFile = OutFile.replace("$FOLD$", str(fold_all))
            if not len(OutFile):
                msgBox.setText("Please enter out file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # InFile
            InFile = ui.txtInFile.text()
            InFile = InFile.replace("$FOLD$", str(fold_all))
            if not len(InFile):
                msgBox.setText("Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not os.path.isfile(InFile):
                msgBox.setText("Input file not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            InData = mainIO_load(InFile)
            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

            # Data
            if not len(ui.txtITrData.currentText()):
                msgBox.setText("Please enter Input Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeData.currentText()):
                msgBox.setText("Please enter Input Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrData.text()):
                msgBox.setText("Please enter Output Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeData.text()):
                msgBox.setText("Please enter Output Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            try:
                XTr = InData[ui.txtITrData.currentText()]
                XTe = InData[ui.txtITeData.currentText()]

                if ui.cbScale.isChecked() and not ui.rbScale.isChecked():
                    XTr = preprocessing.scale(XTr)
                    XTe = preprocessing.scale(XTe)
                    print("Whole of data is scaled X~N(0,1).")
            except:
                print("Cannot load data")
                return

            # NComponent
            try:
                NumFea = np.int32(ui.txtNumFea.text())
            except:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea < 1:
                msgBox.setText("Number of features must be greater than zero!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea > np.shape(XTr)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # Label
            if not len(ui.txtITrLabel.currentText()):
                msgBox.setText("Please enter Train Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeLabel.currentText()):
                msgBox.setText("Please enter Test Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrLabel.text()):
                msgBox.setText(
                    "Please enter Train Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeLabel.text()):
                msgBox.setText("Please enter Test Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(
                    InData[ui.txtITrLabel.currentText()])
                OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(
                    InData[ui.txtITeLabel.currentText()])
            except:
                print("Cannot load labels!")

            # Subject
            if not len(ui.txtITrSubject.currentText()):
                msgBox.setText(
                    "Please enter Train Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeSubject.currentText()):
                msgBox.setText(
                    "Please enter Test Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrSubject.text()):
                msgBox.setText(
                    "Please enter Train Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeSubject.text()):
                msgBox.setText(
                    "Please enter Test Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                TrSubject = InData[ui.txtITrSubject.currentText()]
                OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject)
                TeSubject = InData[ui.txtITeSubject.currentText()]
                OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject)
            except:
                print("Cannot load Subject IDs")
                return

            # Task
            if ui.cbTask.isChecked():
                if not len(ui.txtITrTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrTask.text()):
                    msgBox.setText(
                        "Please enter Output Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeTask.text()):
                    msgBox.setText(
                        "Please enter Output Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrTask = np.asarray(InData[ui.txtITrTask.currentText()])
                    OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask)
                    TeTask = np.asarray(InData[ui.txtITeTask.currentText()])
                    OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask)
                    TrTaskIndex = TrTask.copy()
                    for tasindx, tas in enumerate(np.unique(TrTask)):
                        TrTaskIndex[TrTask == tas] = tasindx + 1
                    TeTaskIndex = TeTask.copy()
                    for tasindx, tas in enumerate(np.unique(TeTask)):
                        TeTaskIndex[TeTask == tas] = tasindx + 1
                except:
                    print("Cannot load Tasks!")
                    return

            # Run
            if ui.cbRun.isChecked():
                if not len(ui.txtITrRun.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeRun.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrRun.text()):
                    msgBox.setText(
                        "Please enter Train Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeRun.text()):
                    msgBox.setText(
                        "Please enter Test Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrRun = InData[ui.txtITrRun.currentText()]
                    OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun)
                    TeRun = InData[ui.txtITeRun.currentText()]
                    OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun)
                except:
                    print("Cannot load Runs!")
                    return

            # Counter
            if ui.cbCounter.isChecked():
                if not len(ui.txtITrCounter.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeCounter.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrCounter.text()):
                    msgBox.setText(
                        "Please enter Train Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeCounter.text()):
                    msgBox.setText(
                        "Please enter Test Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrCounter = InData[ui.txtITrCounter.currentText()]
                    OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(
                        TrCounter)
                    TeCounter = InData[ui.txtITeCounter.currentText()]
                    OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(
                        TeCounter)
                except:
                    print("Cannot load Counters!")
                    return

            # Matrix Label
            if ui.cbmLabel.isChecked():
                if not len(ui.txtITrmLabel.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITemLabel.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrmLabel.text()):
                    msgBox.setText(
                        "Please enter Train Output Matrix Label variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTemLabel.text()):
                    msgBox.setText(
                        "Please enter Test Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrmLabel.text()] = InData[
                        ui.txtITrmLabel.currentText()]
                    OutData[ui.txtOTemLabel.text()] = InData[
                        ui.txtITemLabel.currentText()]
                except:
                    print("Cannot load matrix lables!")
                    return

            # Design
            if ui.cbDM.isChecked():
                if not len(ui.txtITrDM.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeDM.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrDM.text()):
                    msgBox.setText(
                        "Please enter Train Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeDM.text()):
                    msgBox.setText(
                        "Please enter Test Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrDM.text()] = InData[
                        ui.txtITrDM.currentText()]
                    OutData[ui.txtOTeDM.text()] = InData[
                        ui.txtITeDM.currentText()]
                except:
                    print("Cannot load design matrices!")
                    return

            # Coordinate
            if ui.cbCol.isChecked():
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCol.text()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCol.text()] = InData[
                        ui.txtCol.currentText()]
                except:
                    print("Cannot load coordinator!")
                    return

            # Condition
            if ui.cbCond.isChecked():
                if not len(ui.txtCond.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCond.text()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCond.text()] = InData[
                        ui.txtCond.currentText()]
                except:
                    print("Cannot load conditions!")
                    return

            # FoldID
            if ui.cbFoldID.isChecked():
                if not len(ui.txtFoldID.currentText()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldID.text()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldID.text()] = reshape_1Dvector(
                        InData[ui.txtFoldID.currentText()])
                except:
                    print("Cannot load Fold ID!")
                    return

            # FoldInfo
            if ui.cbFoldInfo.isChecked():
                if not len(ui.txtFoldInfo.currentText()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldInfo.text()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldInfo.text()] = InData[
                        ui.txtFoldInfo.currentText()]
                except:
                    print("Cannot load Fold Info!")
                    return
                pass

            # Number of Scan
            if ui.cbNScan.isChecked():
                if not len(ui.txtITrScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrScan.text()] = reshape_1Dvector(
                        InData[ui.txtITrScan.currentText()])
                    OutData[ui.txtOTeScan.text()] = reshape_1Dvector(
                        InData[ui.txtITeScan.currentText()])
                except:
                    print("Cannot load NScan!")
                    return

            # Train Analysis Level
            print("Calculating Analysis Level for Training Set ...")
            TrGroupFold = None
            FoldStr = ""
            if ui.cbFSubject.isChecked():
                if not ui.rbFRun.isChecked():
                    TrGroupFold = TrSubject
                    FoldStr = "Subject"
                else:
                    TrGroupFold = np.concatenate((TrSubject, TrRun))
                    FoldStr = "Subject+Run"

            if ui.cbFTask.isChecked():
                TrGroupFold = np.concatenate(
                    (TrGroupFold,
                     TrTaskIndex)) if TrGroupFold is not None else TrTaskIndex
                FoldStr = FoldStr + "+Task"

            if ui.cbFCounter.isChecked():
                TrGroupFold = np.concatenate(
                    (TrGroupFold,
                     TrCounter)) if TrGroupFold is not None else TrCounter
                FoldStr = FoldStr + "+Counter"

            TrGroupFold = np.transpose(TrGroupFold)

            TrUniqFold = np.array(
                list(set(tuple(i) for i in TrGroupFold.tolist())))

            TrFoldIDs = np.arange(len(TrUniqFold)) + 1

            TrListFold = list()
            for gfold in TrGroupFold:
                for ufoldindx, ufold in enumerate(TrUniqFold):
                    if (ufold == gfold).all():
                        currentID = TrFoldIDs[ufoldindx]
                        break
                TrListFold.append(currentID)
            TrListFold = np.int32(TrListFold)
            TrListFoldUniq = np.unique(TrListFold)

            # Test Analysis Level
            print("Calculating Analysis Level for Testing Set ...")
            TeGroupFold = None
            if ui.cbFSubject.isChecked():
                if not ui.rbFRun.isChecked():
                    TeGroupFold = TeSubject
                else:
                    TeGroupFold = np.concatenate((TeSubject, TeRun))

            if ui.cbFTask.isChecked():
                TeGroupFold = np.concatenate(
                    (TeGroupFold,
                     TeTaskIndex)) if TeGroupFold is not None else TeTaskIndex

            if ui.cbFCounter.isChecked():
                TeGroupFold = np.concatenate(
                    (TeGroupFold,
                     TeCounter)) if TeGroupFold is not None else TeCounter

            TeGroupFold = np.transpose(TeGroupFold)

            TeUniqFold = np.array(
                list(set(tuple(i) for i in TeGroupFold.tolist())))

            TeFoldIDs = np.arange(len(TeUniqFold)) + 1

            TeListFold = list()
            for gfold in TeGroupFold:
                for ufoldindx, ufold in enumerate(TeUniqFold):
                    if (ufold == gfold).all():
                        currentID = TeFoldIDs[ufoldindx]
                        break
                TeListFold.append(currentID)
            TeListFold = np.int32(TeListFold)
            TeListFoldUniq = np.unique(TeListFold)

            # Train Partition
            print("Partitioning Training Data ...")
            TrX = list()
            TrShape = None

            if Method == "PCA":
                svdmodel = PCA(n_components=NumFea, copy=False, tol=Tol)
            elif Method == "Kernel PCA":
                svdmodel = KernelPCA(n_components=NumFea,kernel=Kernel,gamma=Gamma,degree=Degree,\
                              coef0=Coef0, alpha=Alpha, tol=Tol, max_iter=MaxIter, n_jobs=NJob,copy_X=False)
            else:
                svdmodel = IncrementalPCA(n_components=NumFea,
                                          copy=False,
                                          batch_size=Batch)

            for foldindx, fold in enumerate(TrListFoldUniq):
                dat = XTr[np.where(TrListFold == fold)]
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    dat = preprocessing.scale(dat)
                    print("Data belong to View " + str(foldindx + 1) +
                          " is scaled X~N(0,1).")

                dat = svdmodel.fit_transform(dat)
                TrX.append(dat)
                if TrShape is None:
                    TrShape = np.shape(dat)
                else:
                    if not (TrShape == np.shape(dat)):
                        print("ERROR: Train, Reshape problem for Fold " +
                              str(foldindx + 1) + ", Shape: " +
                              str(np.shape(dat)))
                        return
                print("Train: View " + str(foldindx + 1) +
                      " is extracted. Shape: " + str(np.shape(dat)))

            print("Training Shape: " + str(np.shape(TrX)))

            # Test Partition
            print("Partitioning Testing Data ...")
            TeX = list()
            TeShape = None
            for foldindx, fold in enumerate(TeListFoldUniq):
                dat = XTe[np.where(TeListFold == fold)]
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    dat = preprocessing.scale(dat)
                    print("Data belong to View " + str(foldindx + 1) +
                          " is scaled X~N(0,1).")
                dat = svdmodel.fit_transform(dat)
                TeX.append(dat)
                if TeShape is None:
                    TeShape = np.shape(dat)
                else:
                    if not (TeShape == np.shape(dat)):
                        print("Test: Reshape problem for Fold " +
                              str(foldindx + 1))
                        return
                print("Test: View " + str(foldindx + 1) + " is extracted.")

            print("Testing Shape: " + str(np.shape(TeX)))

            #model = RHA(regularization=Regularization,Dim=NumFea)
            model = rHA(alpha=Regularization)

            print("Running Hyperalignment on Training Data ...")
            model.train(TrX, iter=Iter)
            G = model.G
            TrU = model.TrainQt
            TrErr = model.TrainError
            OutData[ui.txtOTrData.text()] = model.TrainXp

            print("Running Hyperalignment on Testing Data ...")
            model.test(TeX)
            TeU = model.TestQt
            TeErr = model.TestError
            OutData[ui.txtOTeData.text()] = model.TestXp

            HAParam = dict()
            HAParam["Method"] = Method
            HAParam["Kernel"] = Kernel
            HAParam["Share"] = G
            HAParam["Train"] = TrU
            HAParam["TrainError"] = TrErr
            HAParam["Test"] = TeU
            HAParam["TestError"] = TeErr
            HAParam["Level"] = FoldStr
            OutData["FunctionalAlignment"] = HAParam
            OutData["Runtime"] = time.time() - tic
            totalTime += OutData["Runtime"]

            print("Saving ...")
            mainIO_save(OutData, OutFile)
            print("Fold " + str(fold_all) + " is DONE: " + OutFile)

        print("Training -> Alignment Error: mean " + str(np.mean(TrFoldErr)) +
              " std " + str(np.std(TrFoldErr)))
        print("Testing  -> Alignment Error: mean " + str(np.mean(TeFoldErr)) +
              " std " + str(np.std(TeFoldErr)))
        print("Runtime: ", totalTime)
        print("Kernel/SVD Hyperalignment is done.")
        msgBox.setText("Kernel/SVD Hyperalignment is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #3
0
    def btnConvert_click(self):

        msgBox = QMessageBox()

        # Subject
        if ui.cbSubject.isChecked():
            if not len(ui.txtSubject.currentText()):
                msgBox.setText("Please enter Train Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTSubject.currentText()):
                msgBox.setText("Please enter Test Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtTask.currentText()):
                msgBox.setText("Please enter Train Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTTask.currentText()):
                msgBox.setText("Please enter Test Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtRun.currentText()):
                msgBox.setText("Please enter Train Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTRun.currentText()):
                msgBox.setText("Please enter Test Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtCounter.currentText()):
                msgBox.setText("Please enter Train Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTCounter.currentText()):
                msgBox.setText("Please enter Test Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Label
        if not len(ui.txtLabel.currentText()):
            msgBox.setText("Please enter Train Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not len(ui.txtTLabel.currentText()):
            msgBox.setText("Please enter Test Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText(
                    "Please enter Train Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTmLabel.currentText()):
                msgBox.setText("Please enter Test Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Data
        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Train Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not len(ui.txtTData.currentText()):
            msgBox.setText("Please enter Test Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText(
                    "Please enter Train Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTDM.currentText()):
                msgBox.setText(
                    "Please enter Test Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Coordinate
        if ui.cbCol.isChecked():
            if not len(ui.txtCol.currentText()):
                msgBox.setText("Please enter Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtNScan.currentText()):
                msgBox.setText(
                    "Please enter Train Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtTNScan.currentText()):
                msgBox.setText(
                    "Please enter Test Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        if ui.cbFoldID.isChecked():
            if not len(ui.txtFoldID.currentText()):
                msgBox.setText("Please enter Fold ID variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        if ui.cbFoldInfo.isChecked():
            if not len(ui.txtFoldInfo.currentText()):
                msgBox.setText("Please enter Fold Info variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            ClassID = np.int32(ui.txtClassID.text())
        except:
            msgBox.setText("Class ID is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        InData = mainIO_load(InFile)
        OutData = dict()
        OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

        if ui.cbFoldID.isChecked():
            OutData[ui.txtFoldID.currentText()] = reshape_1Dvector(
                InData[ui.txtFoldID.currentText()])

        if ui.cbFoldInfo.isChecked():
            OutData[ui.txtFoldInfo.currentText()] = InData[
                ui.txtFoldInfo.currentText()]

        # Condition
        if ui.cbCond.isChecked():
            try:
                Cond = InData[ui.txtCond.currentText()]
                New_Condition = Conditions()
                for condition in Cond:
                    if reshape_condition_cell(
                            condition[0]) != ui.txtClassName.currentText():
                        New_Condition.add_cond(reshape_condition_cell(condition[0]),\
                                               reshape_condition_cell(condition[1]))
                OutData[ui.txtCond.currentText()] = np.array(
                    New_Condition.get_cond(), dtype=object)
            except Exception as e:
                print(e)
                print("Cannot load Condition ID!")
                return

        # Label
        try:
            Y = InData[ui.txtLabel.currentText()]
            YT = InData[ui.txtTLabel.currentText()]
        except:
            print("Cannot load class labels!")
            return

        if not len(np.where(Y == ClassID)[0]):
            msgBox.setText("Train Data: There is no label with this Class ID!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(np.where(YT == ClassID)[0]):
            msgBox.setText("Test Data: There is no label with this Class ID!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        NoneZeroArea = np.where(Y != ClassID)
        New_Y = Y[NoneZeroArea]
        OutData[ui.txtLabel.currentText()] = reshape_1Dvector(New_Y)

        NoneZeroAreaT = np.where(YT != ClassID)
        New_YT = YT[NoneZeroAreaT]
        OutData[ui.txtTLabel.currentText()] = reshape_1Dvector(New_YT)

        # Data
        try:
            X = InData[ui.txtData.currentText()]
            XT = InData[ui.txtTData.currentText()]
        except:
            print("Cannot load data")
            return
        OutData[ui.txtData.currentText()] = X[NoneZeroArea[1]]
        OutData[ui.txtTData.currentText()] = XT[NoneZeroAreaT[1]]

        # Subject
        if ui.cbSubject.isChecked():
            try:
                Subject = InData[ui.txtSubject.currentText()]
                OutData[ui.txtSubject.currentText()] = reshape_1Dvector(
                    Subject[NoneZeroArea])

                SubjectT = InData[ui.txtTSubject.currentText()]
                OutData[ui.txtTSubject.currentText()] = reshape_1Dvector(
                    SubjectT[NoneZeroAreaT])

            except:
                print("Cannot load Subject ID!")
                return

        # Task
        if ui.cbTask.isChecked():
            try:
                Task = InData[ui.txtTask.currentText()]
                OutData[ui.txtTask.currentText()] = reshape_1Dvector(
                    np.array(Task[NoneZeroArea], dtype=object))

                TaskT = InData[ui.txtTTask.currentText()]
                OutData[ui.txtTTask.currentText()] = reshape_1Dvector(
                    np.array(TaskT[NoneZeroAreaT], dtype=object))
            except:
                print("Cannot load Task ID!")
                return

        # Run
        if ui.cbRun.isChecked():
            try:
                Run = InData[ui.txtRun.currentText()]
                OutData[ui.txtRun.currentText()] = reshape_1Dvector(
                    Run[NoneZeroArea])

                RunT = InData[ui.txtTRun.currentText()]
                OutData[ui.txtTRun.currentText()] = reshape_1Dvector(
                    RunT[NoneZeroAreaT])
            except:
                print("Cannot load Run ID!")
                return

        # Counter
        if ui.cbCounter.isChecked():
            try:
                Counter = InData[ui.txtCounter.currentText()]
                OutData[ui.txtCounter.currentText()] = reshape_1Dvector(
                    Counter[NoneZeroArea])

                CounterT = InData[ui.txtTCounter.currentText()]
                OutData[ui.txtTCounter.currentText()] = reshape_1Dvector(
                    CounterT[NoneZeroAreaT])
            except:
                print("Cannot load Counter ID!")
                return

        # Matrix Label
        if ui.cbmLabel.isChecked():
            try:
                OutData[ui.txtmLabel.currentText()] = label_binarize(
                    New_Y, np.unique(New_Y))
                OutData[ui.txtTmLabel.currentText()] = label_binarize(
                    New_YT, np.unique(New_YT))
            except:
                print("Cannot load Matrix Label ID!")
                return

        # Design
        if ui.cbDM.isChecked():
            try:
                DM = InData[ui.txtDM.currentText()]
                OutData[ui.txtDM.currentText()] = DM[NoneZeroArea[1]]

                DMT = InData[ui.txtTDM.currentText()]
                OutData[ui.txtTDM.currentText()] = DMT[NoneZeroAreaT[1]]
            except:
                print("Cannot load Design Matrix ID!")
                return

        # Coordinate
        if ui.cbCol.isChecked():
            try:
                OutData[ui.txtCol.currentText()] = InData[
                    ui.txtCol.currentText()]
            except:
                print("Cannot load Coordinate ID!")
                return

        # NScan
        if ui.cbNScan.isChecked():
            try:
                NScan = InData[ui.txtNScan.currentText()]
                OutData[ui.txtNScan.currentText()] = reshape_1Dvector(
                    NScan[NoneZeroArea])
                NScanT = InData[ui.txtTNScan.currentText()]
                OutData[ui.txtTNScan.currentText()] = reshape_1Dvector(
                    NScanT[NoneZeroAreaT])
            except:
                print("Cannot load NScan ID!")
                return

        print("Saving ...")
        mainIO_save(OutData, ui.txtOutFile.text())
        print("Train: Number of selected instances: ",
              np.shape(NoneZeroArea)[1])
        print("Train: Number of all instances: ", np.shape(Y)[1])
        print("Test: Number of selected instances: ",
              np.shape(NoneZeroAreaT)[1])
        print("Test: Number of all instances: ", np.shape(YT)[1])
        print("DONE.")
        msgBox.setText("Rest scans are removed.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #4
0
    def btnConvert_click(self):
        global data
        data = None
        msgBox = QMessageBox()
        if ui.vwSele.rowCount() == 0:
            print("Please select some voxel first")
            msgBox.setText("Please select some voxel first")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return
        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        print("Loading...")
        InData = mainIO_load(InFile)
        OutData = dict()
        OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
        # Label
        if not len(ui.txtLabel.currentText()):
            msgBox.setText("Please enter Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return
        try:
            OutData[ui.txtOLabel.text()] = reshape_1Dvector(
                InData[ui.txtLabel.currentText()])
        except:
            print("Cannot load Label ID")
            return
        # Subject
        if ui.cbSubject.isChecked():
            if not len(ui.txtSubject.currentText()):
                msgBox.setText("Please enter Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOSubject.text()] = reshape_1Dvector(
                    InData[ui.txtSubject.currentText()])
            except:
                print("Cannot load Subject ID")
                return
        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtTask.currentText()):
                msgBox.setText("Please enter Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOTask.text()] = reshape_1Dvector(
                    InData[ui.txtTask.currentText()])
            except:
                print("Cannot load Task ID")
                return
        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtRun.currentText()):
                msgBox.setText("Please enter Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtORun.text()] = reshape_1Dvector(
                    InData[ui.txtRun.currentText()])
            except:
                print("Cannot load Run ID")
                return
        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtCounter.currentText()):
                msgBox.setText("Please enter Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOCounter.text()] = reshape_1Dvector(
                    InData[ui.txtCounter.currentText()])
            except:
                print("Cannot load Counter ID")
                return
        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText("Please enter Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOmLabel.text()] = InData[
                    ui.txtmLabel.currentText()]
            except:
                print("Cannot load Matrix Label ID")
                return
        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText("Please enter Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]
            except:
                print("Cannot load Design ID")
                return
        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
            except:
                print("Cannot load Condition ID")
                return
        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtScan.currentText()):
                msgBox.setText("Please enter Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOScan.text()] = reshape_1Dvector(
                    InData[ui.txtScan.currentText()])
            except:
                print("Cannot load NumScan ID")
                return
        # Data
        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            X = InData[ui.txtData.currentText()]
        except:
            print("Cannot load data")
            return
        # Coordinate
        if not len(ui.txtCol.currentText()):
            msgBox.setText("Please enter Coordinator variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            Coord = InData[ui.txtCol.currentText()]
        except:
            print("Cannot load data")
            return

        # Make dict of Coord indexes
        dCoord = {}
        for cooInd, coo in enumerate(np.transpose(Coord)):
            dCoord[tuple(coo)] = cooInd
        # Find selected indexes
        VoxelID = list()
        NewCoord = list()
        for rowID in range(ui.vwSele.rowCount()):
            itemString = str(ui.vwSele.item(rowID, 0).text()).split(";")
            item = (int(itemString[0]), int(itemString[1]), int(itemString[2]))
            try:
                VoxelID.append(dCoord[item])
                NewCoord.append(item)
            except:
                print(f"WARNING: Cannot find selected coordinate: {item}")
        OutData[ui.txtOCol.text()] = np.transpose(NewCoord)
        OutData[ui.txtOData.text()] = X[:, VoxelID]
        print("Saving ...")
        mainIO_save(OutData, ui.txtOutFile.text())
        print("DONE.")
        msgBox.setText("Data with new ROI is created.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #5
0
    def btnRedraw_click(self):
        msgBox = QMessageBox()

        ofile = LoadFile("Load result file ...", ['Result files (*.ezx *.mat)'], 'ezx',\
                             os.path.dirname(ui.txtOutFile.text()))

        FontSize = ui.txtFontSize.value()

        if len(ofile):
            try:
                Res     = mainIO_load(ofile)
                LUnique = reshape_1Dvector(Res["Label"])[0]
                LNum    = np.shape(LUnique)[0]
                SubID   = Res["SubjectID"]
                ConID   = Res["CounterID"]
                RunID   = Res["RunID"]
                TaskIDTitle = reshape_condition_cell(Res["Task"])
            except:
                print("Cannot load result file!")
                msgBox.setText("Cannot load result file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False


            HasDefaultCond = False
            # Condition
            if not len(ui.txtCond.currentText()):
                try:
                    Cond = Res["condition"]
                    HasDefaultCond = True
                except:
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False

            if not HasDefaultCond:
                try:
                    Cond = Res[ui.txtCond.currentText()]
                except:
                    msgBox.setText("Condition value is wrong!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False


            labels = list()
            for con in Cond:
                labels.append(reshape_condition_cell(con[1]))


            if ui.cbCorr.isChecked():
                try:
                    Corr = Res["Correlation"]
                except:
                    print("Cannot load Correlation variable!")
                    msgBox.setText("Cannot load Correlation variable!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False


                NumData = np.shape(Corr)[0]
                fig1 = plt.figure(num=None, figsize=(NumData, NumData), dpi=100)
                plt.pcolor(Corr, vmin=np.min(Corr), vmax=np.max(Corr))
                plt.xlim([0, NumData])
                plt.ylim([0, NumData])
                cbar = plt.colorbar()
                cbar.ax.tick_params(labelsize=FontSize)
                ax = plt.gca()
                ax.invert_yaxis()
                ax.set_aspect(1)

                ax.set_yticks(np.arange(NumData) + 0.5, minor=False)
                ax.set_xticks(np.arange(NumData) + 0.5, minor=False)
                ax.set_xticklabels(labels, minor=False, fontsize=FontSize, rotation=45)
                ax.set_yticklabels(labels, minor=False, fontsize=FontSize)
                ax.grid(False)
                ax.set_aspect(1)
                ax.set_frame_on(False)
                for t in ax.xaxis.get_major_ticks():
                    t.tick1On = False
                    t.tick2On = False
                for t in ax.yaxis.get_major_ticks():
                    t.tick1On = False
                    t.tick2On = False

                if len(ui.txtTitleCov.text()):
                    plt.title(ui.txtTitleCov.text())
                else:
                    plt.title('Gradient RSA: Correlation\nTask: %s\nSub: %d, Counter: %d, Run: %d' % (TaskIDTitle, SubID, ConID, RunID))
                plt.show()


            if ui.cbCov.isChecked():
                try:
                    Cov = Res["Covariance"]
                except:
                    print("Cannot load Covariance variable!")
                    msgBox.setText("Cannot load Covariance variable!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False


                NumData = np.shape(Cov)[0]
                fig2 = plt.figure(num=None, figsize=(NumData, NumData), dpi=100)
                plt.pcolor(Cov, vmin=np.min(Cov), vmax=np.max(Cov))
                plt.xlim([0, NumData])
                plt.ylim([0, NumData])
                cbar = plt.colorbar()
                cbar.ax.tick_params(labelsize=FontSize)
                ax = plt.gca()
                ax.invert_yaxis()
                ax.set_aspect(1)

                ax.set_yticks(np.arange(NumData) + 0.5, minor=False)
                ax.set_xticks(np.arange(NumData) + 0.5, minor=False)
                ax.set_xticklabels(labels, minor=False, fontsize=FontSize, rotation=45)
                ax.set_yticklabels(labels, minor=False, fontsize=FontSize)
                ax.grid(False)
                ax.set_aspect(1)
                ax.set_frame_on(False)
                for t in ax.xaxis.get_major_ticks():
                    t.tick1On = False
                    t.tick2On = False
                for t in ax.yaxis.get_major_ticks():
                    t.tick1On = False
                    t.tick2On = False
                if len(ui.txtTitleCorr.text()):
                    plt.title(ui.txtTitleCorr.text())
                else:
                    plt.title('Gradient RSA: Covariance\nTask: %s\nSub: %d, Counter: %d, Run: %d' % (TaskIDTitle, SubID, ConID, RunID))
                plt.show()

            fig3 = plt.figure(figsize=(25, 10), )
            dn = dendrogram(Res["Linkage"], labels=labels, leaf_font_size=FontSize, color_threshold=1)
            if len(ui.txtTitleDen.text()):
                plt.title(ui.txtTitleDen.text())
            else:
                plt.title('Gradient RSA: Similarity Analysis\nTask: %s\nSub: %d, Counter: %d, Run: %d' % (TaskIDTitle, SubID, ConID, RunID))
            plt.show()
Пример #6
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        try:
            Threshold = np.float64(ui.txtThreshold.text())
        except:
            msgBox.setText("Standard deviation threshold value is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            net_model = createModel(ui.tbModel)
        except Exception as e:
            msgBox.setText(str(e))
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if net_model is None:
            msgBox.setText("Please create a network model!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if ui.rbScale.isChecked() == True and ui.rbALScale.isChecked(
        ) == False:
            msgBox.setText(
                "Subject Level Normalization is just available for Subject Level Analysis!"
            )
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        InData = mainIO_load(InFile)
        OutData = dict()
        OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            X = InData[ui.txtData.currentText()]

            if ui.cbScale.isChecked() and (not ui.rbScale.isChecked()):
                X = X - X.mean()
                X = X / X.std()
                print("Whole of data is scaled X~N(0,1).")
        except:
            print("Cannot load data")
            return
        # Subject
        if not len(ui.txtSubject.currentText()):
            msgBox.setText("Please enter Subject variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            Subject = InData[ui.txtSubject.currentText()]
            OutData[ui.txtOSubject.text()] = reshape_1Dvector(Subject)
        except:
            print("Cannot load Subject ID")
            return
        # Label
        if not len(ui.txtLabel.currentText()):
            msgBox.setText("Please enter Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        OutData[ui.txtOLabel.text()] = reshape_1Dvector(
            InData[ui.txtLabel.currentText()])
        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtTask.currentText()):
                msgBox.setText("Please enter Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOTask.text()] = reshape_1Dvector(
                InData[ui.txtTask.currentText()])
        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtRun.currentText()):
                msgBox.setText("Please enter Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtORun.text()] = reshape_1Dvector(
                InData[ui.txtRun.currentText()])
        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtCounter.currentText()):
                msgBox.setText("Please enter Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCounter.text()] = reshape_1Dvector(
                InData[ui.txtCounter.currentText()])
        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText("Please enter Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()]
        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText("Please enter Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]
        # Coordinate
        if ui.cbCol.isChecked():
            if not len(ui.txtCol.currentText()):
                msgBox.setText("Please enter Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtScan.currentText()):
                msgBox.setText("Please enter Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOScan.text()] = reshape_1Dvector(
                InData[ui.txtScan.currentText()])
        if ui.rbALScale.isChecked():
            print("Partition data to subject level ...")
            SubjectUniq = np.unique(Subject)
            X_Sub = list()
            for subj in SubjectUniq:
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    extractXsub = X[np.where(Subject == subj)[1], :]
                    extractXsub = extractXsub - extractXsub.mean()
                    extractXsub = extractXsub / extractXsub.std()
                    X_Sub.append(extractXsub)
                    print("Data in subject level is scaled, X_" + str(subj) +
                          "~N(0,1).")
                else:
                    X_Sub.append(X[np.where(Subject == subj)[1], :])
                print("Subject ", subj, " is extracted from data.")
            print("Running CNN in subject level ...")
            X_Sub_PCA = list()
            lenPCA = len(X_Sub)
            for xsubindx, xsub in enumerate(X_Sub):
                net = CNN(net_model)
                if ui.rbMat.isChecked():
                    xsub_tran = net.tonumpy(
                        net.vectorize(
                            net(torch.Tensor(np.expand_dims(xsub, axis=1)))))
                else:
                    xsub_tran = net.tonumpy(
                        net(torch.Tensor(np.expand_dims(xsub, axis=1))))
                X_Sub_PCA.append(xsub_tran)
                print("CNN: ", xsubindx + 1, " of ", lenPCA,
                      " is done. Shape: " + str(np.shape(xsub_tran)))
            print("Data integration ... ")
            X_new = None
            for xsubindx, xsub in enumerate(X_Sub_PCA):
                X_new = np.concatenate(
                    (X_new, xsub)) if X_new is not None else xsub
                print("Integration: ", xsubindx + 1, " of ", lenPCA,
                      " is done.")
            if not ui.rbMat.isChecked():
                X_new = X_new[:, 0, :, :, :]
            # Apply Thresholding
            print("Data shape before thresholding: ", np.shape(X_new))
            X_new_th = X_new
            if ui.rbMat.isChecked() and ui.cbThreshold.isChecked():
                index = np.where(np.std(X_new, axis=0) >= Threshold)
                X_new_index = X_new[:, index]
                print("Standard deviation thresholding ...")
                X_new_th = None
                for xx_new in X_new_index:
                    X_new_th = xx_new if X_new_th is None else np.concatenate(
                        (X_new_th, xx_new), axis=0)
            # Apply Normalization
            if ui.rbMat.isChecked() and ui.cbOutNormal.isChecked():
                X_new_th = X_new_th - np.mean(X_new_th)
                X_new_th = X_new_th / np.std(X_new_th)
                print("Output data is normalized!")
            OutData[ui.txtOData.text()] = X_new_th
        else:
            print("Running CNN ...")
            net = CNN(net_model)
            if ui.rbMat.isChecked():
                X_new = net.tonumpy(
                    net.vectorize(net(torch.Tensor(np.expand_dims(X,
                                                                  axis=1)))))
            else:
                X_new = net.tonumpy(
                    net(torch.Tensor(np.expand_dims(X, axis=1))))
            print("CNN is done. Shape: " + str(np.shape(X_new)))
            # Apply Thresholding
            print("Data shape before thresholding: ", np.shape(X_new))
            X_new_th = X_new
            if ui.rbMat.isChecked() and ui.cbThreshold.isChecked():
                index = np.where(np.std(X_new, axis=0) >= Threshold)
                X_new_index = X_new[:, index]
                print("Standard deviation thresholding ...")
                X_new_th = None
                for xx_new in X_new_index:
                    X_new_th = xx_new if X_new_th is None else np.concatenate(
                        (X_new_th, xx_new), axis=0)
            # Apply Normalization
            if ui.rbMat.isChecked() and ui.cbOutNormal.isChecked():
                X_new_th = X_new_th - np.mean(X_new_th)
                X_new_th = X_new_th / np.std(X_new_th)
                print("Output data is normalized!")
            OutData[ui.txtOData.text()] = X_new_th
        print("Saving ...")
        print("Final data shape: ", np.shape(OutData[ui.txtOData.text()]))
        mainIO_save(OutData, ui.txtOutFile.text())
        print("DONE.")
        msgBox.setText("CNN is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #7
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        InData = mainIO_load(InFile)
        OutData = dict()
        OutData["dataShape"] = 4
        # Coordinate
        if not len(ui.txtCol.currentText()):
            msgBox.setText("Please enter Coordinate variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            Coor = InData[ui.txtCol.currentText()]
            OutData[ui.txtOCol.text()] = Coor
        except:
            print("Cannot load Coordinate!")
            return
        # imgShape
        if not len(ui.txtImgShape.currentText()):
            msgBox.setText("Please enter imgShape variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            imgShape = InData[ui.txtImgShape.currentText()]
            OutData[ui.txtOImgShape.text()] = reshape_1Dvector(imgShape)
        except:
            print("Cannot load Coordinate!")
            return
        # Data
        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            X = InData[ui.txtData.currentText()]

            if ui.cbNormalize.isChecked():
                X = preprocessing.scale(X)
                print("Data is scaled X~N(0,1).")
        except:
            print("Cannot load data")
            return
        # Subject
        if ui.cbSubject.isChecked():
            if not len(ui.txtSubject.currentText()):
                msgBox.setText("Please enter Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOSubject.text()] = reshape_1Dvector(
                InData[ui.txtSubject.currentText()])
        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtTask.currentText()):
                msgBox.setText("Please enter Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOTask.text()] = reshape_1Dvector(
                InData[ui.txtTask.currentText()])
        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtRun.currentText()):
                msgBox.setText("Please enter Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtORun.text()] = reshape_1Dvector(
                InData[ui.txtRun.currentText()])
        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtCounter.currentText()):
                msgBox.setText("Please enter Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCounter.text()] = reshape_1Dvector(
                InData[ui.txtCounter.currentText()])
        # Label
        if ui.cbLabel.isChecked():
            if not len(ui.txtLabel.currentText()):
                msgBox.setText("Please enter Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOLabel.text()] = reshape_1Dvector(
                InData[ui.txtLabel.currentText()])
        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText("Please enter Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()]
        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText("Please enter Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]
        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtScan.currentText()):
                msgBox.setText("Please enter Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOScan.text()] = reshape_1Dvector(
                InData[ui.txtScan.currentText()])
        # Boxed Data
        if ui.rb4DShape2.isChecked():
            imgShape = np.array(
                [np.max(Coor, axis=1) - np.min(Coor, axis=1) + 1])
            Coor = np.array([
                Coor[0] - np.min(Coor, axis=1)[0],
                Coor[1] - np.min(Coor, axis=1)[1],
                Coor[2] - np.min(Coor, axis=1)[2]
            ])
            OutData[ui.txtOCol.text() + "_box"] = Coor
        imgShape = imgShape[0]
        timepoints = np.shape(X)[0]
        Xnew = list()
        for i, xi in enumerate(X):
            xi_new = np.zeros(imgShape)
            for j, (Xi, Xj, Xk) in enumerate(Coor.T):
                xi_new[Xi, Xj, Xk] = xi[j]
            Xnew.append(xi_new)
            if (i + 1) % 100 == 0:
                print("Time point {:8d} of {:8d} is reshaped!".format(
                    i + 1, timepoints))
        print("Time point {:8d} of {:8d} is reshaped!".format(
            i + 1, timepoints))
        OutData[ui.txtOData.text()] = np.array(Xnew, dtype=np.float32)
        print("Data shape: ", np.shape(Xnew))
        print("Saving ...")
        mainIO_save(OutData, ui.txtOutFile.text())
        print("DONE.")
        msgBox.setText("Data is reshaped.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #8
0
    def btnConvert_click(self):

        msgBox = QMessageBox()

        InFFile = ui.txtInFFile.text()
        if not len(InFFile):
            msgBox.setText("Please select the first input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        elif not os.path.isfile(InFFile):
            msgBox.setText("The first input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        InSFile = ui.txtInSFile.text()
        if not len(InSFile):
            msgBox.setText("Please select the second input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        elif not os.path.isfile(InSFile):
            msgBox.setText("The second input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please select the output file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not ui.cbCondUnion.isChecked():
            if not len(ui.txtFCondPre.text()):
                msgBox.setText("Please enter the condition prefix!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        InFData = mainIO_load(InFFile)
        InSData = mainIO_load(InSFile)
        OutData = dict()

        # Data
        if not len(ui.txtFData.currentText()):
            msgBox.setText("Please enter First Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not len(ui.txtSData.currentText()):
            msgBox.setText("Please enter Second Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not len(ui.txtOData.text()):
            msgBox.setText("Please enter Out Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            InFX = InFData[ui.txtFData.currentText()]
        except:
            print("Cannot load data from the first file!")
            return

        try:
            InSX = InSData[ui.txtSData.currentText()]
        except:
            print("Cannot load data from the second file!")
            return

        if np.shape(InFX)[1] != np.shape(InSX)[1]:
            print("Both data files must have the same size!")
            return

        # fMRI Size
        InterfMRISize = InFData["imgShape"][0] - InSData["imgShape"][0]
        if (np.max(InterfMRISize) != 0) or (np.min(InterfMRISize) != 0):
            print(
                "Both data files must extract from the same size fMRI images!")
            return

        OutData["imgShape"] = reshape_1Dvector(InFData["imgShape"])

        # Coordinate
        if ui.cbCol.isChecked():
            if not len(ui.txtFCol.currentText()):
                msgBox.setText("Please enter First Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSCol.currentText()):
                msgBox.setText(
                    "Please enter Second Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOCoI.text()):
                msgBox.setText("Please enter Out Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                InFCol = np.array(InFData[ui.txtFCol.currentText()])
            except:
                print("Cannot load coordinate in the first file!")
                return
            try:
                InSCol = np.array(InSData[ui.txtSCol.currentText()])
            except:
                print("Cannot load coordinate in the second file!")
                return

            SubCol = InFCol - InSCol
            if (np.max(SubCol) != 0) or (np.min(SubCol) != 0):
                msgBox.setText("Coordinates are not matched!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            OutData[ui.txtOCoI.text()] = InFData[ui.txtFCol.currentText()]

        OutData[ui.txtOData.text()] = np.concatenate((InFX, InSX))

        # Label
        if not len(ui.txtFLabel.currentText()):
            msgBox.setText("Please enter First Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not len(ui.txtSLabel.currentText()):
            msgBox.setText("Please enter Second Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not len(ui.txtOLabel.text()):
            msgBox.setText("Please enter Out Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            InFL = InFData[ui.txtFLabel.currentText()]
            InSL = InSData[ui.txtSLabel.currentText()]

            InFLUniq = np.unique(InFL)
            InSLUniq = np.unique(InSL)
            if not ui.cbRelabeling.isChecked():
                OutData[ui.txtOLabel.text()] = reshape_1Dvector(
                    np.concatenate((InFL, InSL), 1)[0])
                InSLUniqNew = InSLUniq.copy()
            else:

                InSLUniqNew = InSLUniq + np.max(InFLUniq) + 1

                InSLNew = InSL.copy()
                for lblinx, lbl in enumerate(InSLUniq):
                    InSLNew[np.where(InSL == lbl)] = InSLUniqNew[lblinx]

                OutData[ui.txtOLabel.text()] = reshape_1Dvector(
                    np.concatenate((InFL, InSLNew), 1)[0])

        except:
            print("Cannot combine Labels!")
            return

        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtOmLabel.text()):
                msgBox.setText("Please enter Out Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOmLabel.text()] = label_binarize(
                OutData[ui.txtOLabel.text()],
                np.unique(OutData[ui.txtOLabel.text()]))

        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtFCond.currentText()):
                msgBox.setText("Please enter First Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSCond.currentText()):
                msgBox.setText("Please enter Second Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOCond.text()):
                msgBox.setText("Please enter Out Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if ui.cbCondUnion.isChecked():
                OutData[ui.txtOCond.text()] = np.concatenate(
                    (InFData[ui.txtFCond.currentText()],
                     InSData[ui.txtSCond.currentText()]))
            else:
                NewCond = Conditions()
                InFCond = InFData[ui.txtFCond.currentText()]
                for cond in InFCond:
                    NewCond.add_cond(
                        str.replace(cond[0][0], ui.txtFCondPre.text(),
                                    ui.txtOCondPre.text()), cond[1][0])

                InSCond = InSData[ui.txtSCond.currentText()]
                for cond in InSCond:
                    condID = np.int32(
                        str.replace(cond[0][0],
                                    ui.txtSCondPre.text() + "_", ""))
                    condIDNew = InSLUniqNew[np.where(InSLUniq == condID)][0]
                    NewCond.add_cond(
                        ui.txtOCondPre.text() + "_" + str(condIDNew),
                        cond[1][0])

                OutData[ui.txtOCond.text()] = np.array(NewCond.get_cond(),
                                                       dtype=object)

        # Subject
        if ui.cbSubject.isChecked():
            if not len(ui.txtFSubject.currentText()):
                msgBox.setText("Please enter First Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSSubject.currentText()):
                msgBox.setText("Please enter Second Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOSubject.text()):
                msgBox.setText("Please enter Out Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOSubject.text()] = reshape_1Dvector(
                    np.concatenate((InFData[ui.txtFSubject.currentText()],
                                    InSData[ui.txtSSubject.currentText()]),
                                   1)[0])
            except:
                print("Cannot combine Subject IDs!")
                return

        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtFTask.currentText()):
                msgBox.setText("Please enter First Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSTask.currentText()):
                msgBox.setText("Please enter Second Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTask.text()):
                msgBox.setText("Please enter Out Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOTask.text()] = reshape_1Dvector(
                    np.concatenate((InFData[ui.txtFTask.currentText()],
                                    InSData[ui.txtSTask.currentText()]), 1)[0])
            except:
                print("Cannot combine Task IDs!")
                return

        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtFRun.currentText()):
                msgBox.setText("Please enter First Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSRun.currentText()):
                msgBox.setText("Please enter Second Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtORun.text()):
                msgBox.setText("Please enter Out Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtORun.text()] = reshape_1Dvector(
                    np.concatenate((InFData[ui.txtFRun.currentText()],
                                    InSData[ui.txtSRun.currentText()]), 1)[0])
            except:
                print("Cannot combine Run IDs!")
                return

        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtFCounter.currentText()):
                msgBox.setText("Please enter First Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSCounter.currentText()):
                msgBox.setText("Please enter Second Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOCounter.text()):
                msgBox.setText("Please enter Out Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOCounter.text()] = reshape_1Dvector(
                    np.concatenate((InFData[ui.txtFCounter.currentText()],
                                    InSData[ui.txtSCounter.currentText()]),
                                   1)[0])
            except:
                print("Cannot combine Counter IDs!")
                return

        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtFScan.currentText()):
                msgBox.setText("Please enter First NScan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
        if ui.cbNScan.isChecked():
            if not len(ui.txtSScan.currentText()):
                msgBox.setText("Please enter Second NScan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
        if ui.cbNScan.isChecked():
            if not len(ui.txtOScan.text()):
                msgBox.setText("Please enter Out NScan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
        try:
            OutData[ui.txtOScan.text()] = reshape_1Dvector(
                np.concatenate((InFData[ui.txtFScan.currentText()],
                                InSData[ui.txtSScan.currentText()]), 1)[0])
        except:
            print("Cannot combine Counter IDs!")
            return

        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtFDM.currentText()):
                msgBox.setText(
                    "Please enter First Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtSDM.currentText()):
                msgBox.setText(
                    "Please enter Second Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtODM.text()):
                msgBox.setText("Please enter Out Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtODM.text()] = np.concatenate(
                    (InFData[ui.txtFDM.currentText()],
                     InSData[ui.txtSDM.currentText()]))
            except:
                print("Cannot combine Design Matrices!")
                return

        print("Saving ...")
        mainIO_save(OutData, ui.txtOutFile.text())
        print("DONE.")
        msgBox.setText("Datasets are combined.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #9
0
 def btnConvert_click(self):
     msgBox = QMessageBox()
     # OutFile
     OutFile = ui.txtOutFile.text()
     if not len(OutFile):
         msgBox.setText("Please enter out file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # InFile
     InFile = ui.txtInFile.text()
     if not len(InFile):
         msgBox.setText("Please enter input file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if not os.path.isfile(InFile):
         msgBox.setText("Input file not found!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if not len(ui.txtData.currentText()):
         msgBox.setText("Please enter Data variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     try:
         Sigma = np.int32(ui.txtSigma.text())
     except:
         msgBox.setText("Sigma is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if Sigma < 1:
         msgBox.setText("Sigma must be greater than zero!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # Design
     if not len(ui.txtDM.currentText()):
         msgBox.setText("Please enter Design Matrix variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     InData = mainIO_load(InFile)
     OutData = dict()
     OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
     try:
         X = InData[ui.txtData.currentText()]
     except:
         print("Cannot load data")
         return
     try:
         DM = np.transpose(InData[ui.txtDM.currentText()])
     except:
         print("Cannot load design")
         return
     # Gaussian Kernel
     G = np.arange(-2 * Sigma, 2 * Sigma + 1, 1)
     G = np.exp(-1 * G**2 / 2 * Sigma * Sigma)
     G = G / np.sum(G)
     LocalMaximum = list()
     for dmx in DM:
         dmx = dmx - np.min(dmx)
         dmx = dmx / np.max(dmx)
         LocalMaximum.append(
             argrelextrema(np.convolve(dmx, G)[0:len(dmx)], np.greater))
     #Check Conflict
     AllLabels = []
     AllIndex = []
     for lblinx, lbl in enumerate(LocalMaximum):
         lbldata = LocalMaximum[lblinx]
         if lblinx == 0:
             AllIndex = lbldata[0]
             AllLabels = np.ones(np.shape(lbldata)[1])
         else:
             Indexs = list()
             for dat in lbldata:
                 if len(np.where(AllIndex == dat)[0]) == 0:
                     Indexs.append(dat)
             AllIndex = np.concatenate((AllIndex, Indexs[0]))
             AllLabels = np.concatenate(
                 (AllLabels, (lblinx + 1) * np.ones(np.shape(Indexs)[1])))
     # Label
     OutData[ui.txtOLabel.text()] = reshape_1Dvector(np.int32(AllLabels))
     # Data
     OutData[ui.txtOData.text()] = X[AllIndex, :]
     # DM
     OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()][AllIndex, :]
     # Subject
     if ui.cbSubject.isChecked():
         if not len(ui.txtSubject.currentText()):
             msgBox.setText("Please enter Subject variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
     try:
         OutData[ui.txtOSubject.text()] = reshape_1Dvector(
             InData[ui.txtSubject.currentText()][0][AllIndex])
     except:
         print("Cannot load Subject ID")
         return
     # Task
     if ui.cbTask.isChecked():
         if not len(ui.txtTask.currentText()):
             msgBox.setText("Please enter Task variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOTask.text()] = reshape_1Dvector(
             InData[ui.txtTask.currentText()][0][AllIndex])
     # Run
     if ui.cbRun.isChecked():
         if not len(ui.txtRun.currentText()):
             msgBox.setText("Please enter Run variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtORun.text()] = reshape_1Dvector(
             InData[ui.txtRun.currentText()][0][AllIndex])
     # Counter
     if ui.cbCounter.isChecked():
         if not len(ui.txtCounter.currentText()):
             msgBox.setText("Please enter Counter variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCounter.text()] = reshape_1Dvector(
             InData[ui.txtCounter.currentText()][0][AllIndex])
     # Matrix Label
     if ui.cbmLabel.isChecked():
         if not len(ui.txtmLabel.currentText()):
             msgBox.setText("Please enter Matrix Label variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOmLabel.text()] = label_binarize(
             AllLabels, np.unique(AllLabels))
     # Coordinate
     if ui.cbCol.isChecked():
         if not len(ui.txtCol.currentText()):
             msgBox.setText("Please enter Coordinator variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
     # Condition
     if ui.cbCond.isChecked():
         if not len(ui.txtCond.currentText()):
             msgBox.setText("Please enter Condition variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
     # Number of Scan
     if ui.cbNScan.isChecked():
         if not len(ui.txtScan.currentText()):
             msgBox.setText("Please enter Number of Scan variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOScan.text()] = reshape_1Dvector(
             InData[ui.txtScan.currentText()][0][AllIndex])
     Models = dict()
     Models["Name"] = "MRPA"
     Models["Sigma"] = Sigma
     OutData["ModelParameter"] = Models
     print("Saving ...")
     mainIO_save(OutData, ui.txtOutFile.text())
     print("DONE.")
     print("Number of all instances :", np.shape(X)[0])
     print("Number of selected instances: ", len(AllLabels))
     msgBox.setText("MRPA is done.")
     msgBox.setIcon(QMessageBox.Information)
     msgBox.setStandardButtons(QMessageBox.Ok)
     msgBox.exec_()
Пример #10
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return

        if FoldTo < FoldFrom:
            print("Please check fold parameters!")
            return

        for fold in range(FoldFrom, FoldTo + 1):
            # Tol
            try:
                Tol = np.float(ui.txtTole.text())
            except:
                msgBox.setText("Tolerance is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # Solver
            Solver = ui.cbSolver.currentData()
            # OutFile
            OutFile = ui.txtOutFile.text()
            OutFile = OutFile.replace("$FOLD$", str(fold))
            if not len(OutFile):
                msgBox.setText("Please enter out file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # InFile
            InFile = ui.txtInFile.text()
            InFile = InFile.replace("$FOLD$", str(fold))
            if not len(InFile):
                msgBox.setText("Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not os.path.isfile(InFile):
                msgBox.setText("Input file not found!\n" + InFile)
                print(InFile + " - not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            InData = mainIO_load(InFile)
            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
            # Data
            if not len(ui.txtITrData.currentText()):
                msgBox.setText("Please enter Input Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeData.currentText()):
                msgBox.setText("Please enter Input Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrData.text()):
                msgBox.setText("Please enter Output Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeData.text()):
                msgBox.setText("Please enter Output Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                XTr = InData[ui.txtITrData.currentText()]
                XTe = InData[ui.txtITeData.currentText()]
                if ui.cbScale.isChecked():
                    XTr = preprocessing.scale(XTr)
                    XTe = preprocessing.scale(XTe)
                    print("Whole of data is scaled X~N(0,1).")
            except:
                print("Cannot load data")
                return
            # NComponent
            try:
                NumFea = np.int32(ui.txtNumFea.text())
            except:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea < 1:
                msgBox.setText("Number of features must be greater than zero!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea > np.shape(XTr)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # Label
            if not len(ui.txtITrLabel.currentText()):
                msgBox.setText("Please enter Train Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeLabel.currentText()):
                msgBox.setText("Please enter Test Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrLabel.text()):
                msgBox.setText(
                    "Please enter Train Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeLabel.text()):
                msgBox.setText("Please enter Test Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                YTr = InData[ui.txtITrLabel.currentText()][0]
                YTe = InData[ui.txtITeLabel.currentText()][0]
                OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(YTr)
                OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(YTe)
            except:
                print("Cannot load labels!")
            # Subject
            if ui.cbSubject.isChecked():
                if not len(ui.txtITrSubject.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Subject variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeSubject.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Subject variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrSubject.text()):
                    msgBox.setText(
                        "Please enter Train Output Subject variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeSubject.text()):
                    msgBox.setText(
                        "Please enter Test Output Subject variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(
                        InData[ui.txtITrSubject.currentText()])
                    OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(
                        InData[ui.txtITeSubject.currentText()])
                except:
                    print("Cannot load Subject IDs")
                    return
            # Task
            if ui.cbTask.isChecked():
                if not len(ui.txtITrTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrTask.text()):
                    msgBox.setText(
                        "Please enter Output Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeTask.text()):
                    msgBox.setText(
                        "Please enter Output Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrTask.text()] = reshape_1Dvector(
                        InData[ui.txtITrTask.currentText()])
                    OutData[ui.txtOTeTask.text()] = reshape_1Dvector(
                        InData[ui.txtITeTask.currentText()])
                except:
                    print("Cannot load Tasks!")
                    return
            # Run
            if ui.cbRun.isChecked():
                if not len(ui.txtITrRun.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeRun.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrRun.text()):
                    msgBox.setText(
                        "Please enter Train Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeRun.text()):
                    msgBox.setText(
                        "Please enter Test Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrRun.text()] = reshape_1Dvector(
                        InData[ui.txtITrRun.currentText()])
                    OutData[ui.txtOTeRun.text()] = reshape_1Dvector(
                        InData[ui.txtITeRun.currentText()])
                except:
                    print("Cannot load Runs!")
                    return
            # Counter
            if ui.cbCounter.isChecked():
                if not len(ui.txtITrCounter.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeCounter.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrCounter.text()):
                    msgBox.setText(
                        "Please enter Train Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeCounter.text()):
                    msgBox.setText(
                        "Please enter Test Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(
                        InData[ui.txtITrCounter.currentText()])
                    OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(
                        InData[ui.txtITeCounter.currentText()])
                except:
                    print("Cannot load Counters!")
                    return
            # Matrix Label
            if ui.cbmLabel.isChecked():
                if not len(ui.txtITrmLabel.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITemLabel.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrmLabel.text()):
                    msgBox.setText(
                        "Please enter Train Output Matrix Label variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTemLabel.text()):
                    msgBox.setText(
                        "Please enter Test Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrmLabel.text()] = InData[
                        ui.txtITrmLabel.currentText()]
                    OutData[ui.txtOTemLabel.text()] = InData[
                        ui.txtITemLabel.currentText()]
                except:
                    print("Cannot load matrix lables!")
                    return
            # Design
            if ui.cbDM.isChecked():
                if not len(ui.txtITrDM.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeDM.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrDM.text()):
                    msgBox.setText(
                        "Please enter Train Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeDM.text()):
                    msgBox.setText(
                        "Please enter Test Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrDM.text()] = InData[
                        ui.txtITrDM.currentText()]
                    OutData[ui.txtOTeDM.text()] = InData[
                        ui.txtITeDM.currentText()]
                except:
                    print("Cannot load design matrices!")
                    return
            # Coordinate
            if ui.cbCol.isChecked():
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCol.text()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCol.text()] = InData[
                        ui.txtCol.currentText()]
                except:
                    print("Cannot load coordinator!")
                    return
            # Condition
            if ui.cbCond.isChecked():
                if not len(ui.txtCond.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCond.text()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCond.text()] = InData[
                        ui.txtCond.currentText()]
                except:
                    print("Cannot load conditions!")
                    return
            # FoldID
            if ui.cbFoldID.isChecked():
                if not len(ui.txtFoldID.currentText()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldID.text()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldID.text()] = reshape_1Dvector(
                        InData[ui.txtFoldID.currentText()])
                except:
                    print("Cannot load Fold ID!")
                    return
            # FoldInfo
            if ui.cbFoldInfo.isChecked():
                if not len(ui.txtFoldInfo.currentText()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldInfo.text()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldInfo.text()] = InData[
                        ui.txtFoldInfo.currentText()]
                except:
                    print("Cannot load Fold Info!")
                    return
                pass
            # Number of Scan
            if ui.cbNScan.isChecked():
                if not len(ui.txtITrScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrScan.text()] = reshape_1Dvector(
                        InData[ui.txtITrScan.currentText()])
                    OutData[ui.txtOTeScan.text()] = reshape_1Dvector(
                        InData[ui.txtITeScan.currentText()])
                except:
                    print("Cannot load NScan!")
                    return
            print("Running LDA:")
            model = LDA(n_components=NumFea, solver=Solver, tol=Tol)
            print("Training ...")
            XTr_New = model.fit_transform(XTr, YTr)
            OutData[ui.txtOTrData.text()] = XTr_New
            print("Testing ...")
            XTe_New = model.transform(XTe)
            OutData[ui.txtOTeData.text()] = XTe_New
            print("Saving ...")
            mainIO_save(OutData, OutFile)
            print("Fold " + str(fold) + " is DONE: " + OutFile)
        print("LDA is done.")
        msgBox.setText("LDA is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #11
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        tStart = time.time()

        if not ui.cbCov.isChecked() and not ui.cbCorr.isChecked():
            msgBox.setText("At least, you must select one metric!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Method
        gpu = ui.cbGPU.currentData()
        Verbose = ui.cbVerbose.isChecked()

        try:
            gamma = np.float(ui.txtGamma.text())
        except:
            msgBox.setText("Gamma is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            Iter = np.int(ui.txtIter.text())
        except:
            msgBox.setText("Max Iteration is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            NumFea = ui.txtNumFea.value()
        except:
            msgBox.setText("Number of features is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if NumFea <= 0:
            NumFea = None

        # Filter
        try:
            Filter = ui.txtFilter.text()
            if not len(Filter):
                Filter = None
            else:
                Filter = Filter.replace("\'", " ").replace(",", " ").replace(
                    "[", "").replace("]", "").split()
                Filter = np.int32(Filter)
        except:
            print("Filter is wrong!")
            return

        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        OutData = dict()

        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtSharedSpace.text()):
            msgBox.setText("Please enter Shared Space variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtSharedVoxelSpace.text()):
            msgBox.setText("Please enter Shared Voxel Space variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtViewSpaces.text()):
            msgBox.setText("Please enter View Space variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtTransformMats.text()):
            msgBox.setText("Please enter Transform Matrices variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        print("Loading ...")
        InData = mainIO_load(InFile)
        OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

        # Data
        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Input Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Label
        if not len(ui.txtLabel.currentText()):
            msgBox.setText("Please enter Train Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Coordinate
        if not len(ui.txtCoordinate.currentText()):
            msgBox.setText("Please enter Input Coordinate variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            Coordinate = InData[ui.txtCoordinate.currentText()]
            OutData["coordinate"] = Coordinate
        except:
            msgBox.setText("Coordinate value is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Condition
        if not len(ui.txtCond.currentText()):
            msgBox.setText("Please enter Condition variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            Cond = InData[ui.txtCond.currentText()]
            OutData["condition"] = Cond
            labels = list()
            for con in Cond:
                labels.append(reshape_condition_cell(con[1]))
            OutData["labels"] = labels

        except:
            msgBox.setText("Condition value is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        FontSize = ui.txtFontSize.value()

        try:
            X = InData[ui.txtData.currentText()]
            L = InData[ui.txtLabel.currentText()][0]
            if ui.cbScale.isChecked() and not ui.rbScale.isChecked():
                X = preprocessing.scale(X)
                print("Whole of data is scaled X~N(0,1).")
        except:
            print("Cannot load data or label")
            return

        # Task
        if not len(ui.txtTask.currentText()):
            msgBox.setText("Please enter Task variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            TaskTitle = np.array(InData[ui.txtTask.currentText()][0])
        except:
            msgBox.setText("Task variable name is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        TaskTitleUnique = np.unique(TaskTitle)
        Task = np.zeros(np.shape(TaskTitle))

        for ttinx, tt in enumerate(TaskTitle):
            for ttlinx, ttl in enumerate(TaskTitleUnique):
                if tt[0] == ttl:
                    Task[ttinx] = ttlinx + 1
                    break

        # Subject
        if not len(ui.txtSubject.currentText()):
            msgBox.setText("Please enter Subject variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            Sub = InData[ui.txtSubject.currentText()][0]
        except:
            msgBox.setText("Subject variable name is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Run
        if not len(ui.txtRun.currentText()):
            msgBox.setText("Please enter Run variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            Run = InData[ui.txtRun.currentText()][0]
        except:
            msgBox.setText("Run variable name is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Counter
        if not len(ui.txtCounter.currentText()):
            msgBox.setText("Please enter Counter variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        try:
            Con = InData[ui.txtCounter.currentText()][0]
        except:
            msgBox.setText("Counter variable name is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if Filter is not None:
            for fil in Filter:
                # Remove Training Set
                labelIndx = np.where(L == fil)[0]
                X = np.delete(X, labelIndx, axis=0)
                L = np.delete(L, labelIndx, axis=0)
                Task = np.delete(Task, labelIndx, axis=0)
                Sub = np.delete(Sub, labelIndx, axis=0)
                Run = np.delete(Run, labelIndx, axis=0)
                Con = np.delete(Con, labelIndx, axis=0)
                print("Class ID = " + str(fil) + " is removed from data.")

        try:
            Unit = np.int32(ui.txtUnit.text())
        except:
            msgBox.setText("Unit for the test set must be a number!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if Unit < 1:
            msgBox.setText("Unit for the test set must be greater than zero!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        print("Calculating Levels ...")
        GroupFold = None
        FoldStr = ""
        if ui.cbFSubject.isChecked():
            if not ui.rbFRun.isChecked():
                GroupFold = [Sub]
                FoldStr = "Subject"
            else:
                GroupFold = np.concatenate(([Sub], [Run]))
                FoldStr = "Subject+Run"

        if ui.cbFTask.isChecked():
            GroupFold = np.concatenate(
                (GroupFold, [Task])) if GroupFold is not None else [Task]
            FoldStr = FoldStr + "+Task"

        if ui.cbFCounter.isChecked():
            GroupFold = np.concatenate(
                (GroupFold, [Con])) if GroupFold is not None else [Con]
            FoldStr = FoldStr + "+Counter"

        if FoldStr == "":
            FoldStr = "Whole-Data"
            GUFold = [1]
            ListFold = [1]
            UniqFold = [1]
            GroupFold = [1]
            UnitFold = np.ones((1, np.shape(X)[0]))
        else:
            GroupFold = np.transpose(GroupFold)
            UniqFold = np.array(list(set(tuple(i)
                                         for i in GroupFold.tolist())))
            FoldIDs = np.arange(len(UniqFold)) + 1

            if len(UniqFold) <= Unit:
                msgBox.setText(
                    "Unit must be smaller than all possible levels! Number of all levels is: "
                    + str(len(UniqFold)))
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if np.mod(len(UniqFold), Unit):
                msgBox.setText(
                    "Unit must be divorceable to all possible levels! Number of all levels is: "
                    + str(len(UniqFold)))
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            ListFold = list()
            for gfold in GroupFold:
                for ufoldindx, ufold in enumerate(UniqFold):
                    if (ufold == gfold).all():
                        currentID = FoldIDs[ufoldindx]
                        break
                ListFold.append(currentID)

            ListFold = np.int32(ListFold)
            if Unit == 1:
                UnitFold = np.int32(ListFold)
            else:
                UnitFold = np.int32((ListFold - 0.1) / Unit) + 1

            GUFold = np.unique(UnitFold)

        FoldInfo = dict()
        FoldInfo["Unit"] = Unit
        FoldInfo["Group"] = GroupFold
        FoldInfo["Order"] = FoldStr
        FoldInfo["List"] = ListFold
        FoldInfo["Unique"] = UniqFold
        FoldInfo["Folds"] = UnitFold

        OutData["FoldInfo"] = FoldInfo
        OutData["ModelAnalysis"] = "SSA"
        print("Number of all levels is: " + str(len(UniqFold)))

        Xi = list()
        Yi = list()

        for foldID, fold in enumerate(GUFold):
            print("Extracting view " + str(foldID + 1), " of ",
                  str(len(UniqFold)), " ...")
            Index = np.where(UnitFold == fold)
            # Whole-Data
            if FoldStr == "Whole-Data" and np.shape(Index)[0]:
                Index = [Index[1]]

            Xi.append(X[Index])
            yyi = label_binarize(L[Index], np.unique(L))

            # Adopt it for binary labels (just two class SSA comparison)
            if np.shape(yyi)[1] == 1:
                yyi_inv = yyi.copy()
                yyi_inv[np.where(yyi_inv == 0)] = 2
                yyi_inv -= 1
                yyi = np.concatenate((yyi, yyi_inv), axis=1)
            Yi.append(yyi)

        try:
            ssa = SSA(gamma=gamma, gpu=gpu)
            Beta = ssa.run(X=Xi,
                           Y=Yi,
                           Dim=NumFea,
                           verbose=Verbose,
                           Iteration=Iter,
                           ShowError=ui.cbError.isChecked())
            if ui.cbError.isChecked():
                if ssa.LostVec is not None:
                    OutData["LossVec"] = ssa.LostVec
                    OutData["Error"] = ssa.Loss

        except Exception as e:
            msgBox.setText(str(e))
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        OutData["AlgorithmRuntime"] = ssa.Runtime
        OutData[ui.txtSharedSpace.text()] = Beta
        OutData[ui.txtSharedVoxelSpace.text()] = ssa.getSharedVoxelSpace()
        OutData[ui.txtTransformMats.text()] = ssa.getTransformMats()
        if ui.cbViewSpace.isChecked():
            for viewID, view in enumerate(ssa.getSubjectSpace()):
                OutData[ui.txtViewSpaces.text() + "_View" +
                        str(viewID + 1)] = np.transpose(view)
        else:
            OutData[ui.txtViewSpaces.text()] = ssa.getSubjectSpace()

        print("Calculating Distance Matrix ...")
        dis = np.zeros((np.shape(Beta)[0], np.shape(Beta)[0]))
        for i in range(np.shape(Beta)[0]):
            for j in range(i + 1, np.shape(Beta)[0]):
                dis[i, j] = 1 - np.dot(Beta[i, :], Beta[j, :].T)
                dis[j, i] = dis[i, j]
        # dis = dis - np.min(dis)
        # dis = dis / np.max(dis)
        OutData["DistanceMatrix"] = dis
        print("Applying linkage ...")
        Z = linkage(dis,
                    method=ui.cbLMethod.currentData(),
                    metric=ui.cbLMetric.currentData(),
                    optimal_ordering=ui.cbLOrder.isChecked())
        OutData["Linkage"] = Z

        if ui.cbCov.isChecked():
            Cov = np.cov(Beta)
            covClass = SimilarityMatrixBetweenClass(Cov)
            OutData["Covariance"] = Cov
            OutData["Covariance_min"] = covClass.min()
            OutData["Covariance_max"] = covClass.max()
            OutData["Covariance_std"] = covClass.std()
            OutData["Covariance_mean"] = covClass.mean()
        if ui.cbCorr.isChecked():
            Corr = np.corrcoef(Beta)
            corClass = SimilarityMatrixBetweenClass(Corr)
            OutData["Correlation"] = Corr
            OutData["Correlation_min"] = corClass.min()
            OutData["Correlation_max"] = corClass.max()
            OutData["Correlation_std"] = corClass.std()
            OutData["Correlation_mean"] = corClass.mean()

        OutData["Runtime"] = time.time() - tStart
        print("Runtime: ", OutData["Runtime"])
        print("Algorithm Runtime: ", OutData["AlgorithmRuntime"])

        print("Saving results ...")
        mainIO_save(OutData, OutFile)
        print("Output is saved.")

        if ui.cbDiagram.isChecked():
            drawrsa = DrawRSA()

            if ui.cbCorr.isChecked():
                drawrsa.ShowFigure(Corr, labels, ui.txtTitleCorr.text(),
                                   FontSize, ui.txtXRotation.value(),
                                   ui.txtYRotation.value())

            if ui.cbCov.isChecked():
                drawrsa.ShowFigure(Cov, labels, ui.txtTitleCov.text(),
                                   FontSize, ui.txtXRotation.value(),
                                   ui.txtYRotation.value())

            drawrsa.ShowDend(Z, labels, ui.txtTitleDen.text(), FontSize,
                             ui.txtXRotation.value())

        print("DONE.")
        msgBox.setText("Shared Similarity Analysis (SSA) is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #12
0
    def btnConvert_click(self):
        msgBox = QMessageBox()

        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter output file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Data
        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter input Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtOData.text()):
            msgBox.setText("Please enter output Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Label
        if not len(ui.txtLabel.currentText()):
            msgBox.setText("Please enter input Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtOLabel.text()):
            msgBox.setText("Please enter output Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Subject
        if not len(ui.txtSubject.currentText()):
            msgBox.setText("Please enter input Subject variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtOSubject.text()):
            msgBox.setText("Please enter output Subject variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Run
        if not len(ui.txtRun.currentText()):
            msgBox.setText("Please enter input Run variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtORun.text()):
            msgBox.setText("Please enter output Run variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Counter
        if not len(ui.txtCounter.currentText()):
            msgBox.setText("Please enter input Counter variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtOCounter.text()):
            msgBox.setText("Please enter output Counter variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Task
        if not len(ui.txtTask.currentText()):
            msgBox.setText("Please enter input Task variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not len(ui.txtOTask.text()):
            msgBox.setText("Please enter output Task variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # mLabel
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText("Please enter input mLabel variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if not len(ui.txtOmLabel.text()):
                msgBox.setText("Please enter output mLabel variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Coordinate
        if ui.cbCol.isChecked():
            if not len(ui.txtCol.currentText()):
                msgBox.setText("Please enter input Coordinate variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if not len(ui.txtOCol.text()):
                msgBox.setText("Please enter output Coordinate variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText(
                    "Please enter input Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if not len(ui.txtODM.text()):
                msgBox.setText(
                    "Please enter output Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter input Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if not len(ui.txtOCond.text()):
                msgBox.setText("Please enter output Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # NScan
        if ui.cbNScan.isChecked():
            if not len(ui.txtScan.currentText()):
                msgBox.setText(
                    "Please enter input Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if not len(ui.txtOScan.text()):
                msgBox.setText(
                    "Please enter output Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Creating label filters
        try:
            ClassListString = str.split(str.strip(ui.txtFLabels.toPlainText()),
                                        "\n")
            ClassList = list()
            if len(ClassListString):
                for classstr in ClassListString:
                    if len(classstr):
                        ClassList.append(int(classstr))

                print("Label filter is ", ClassList)
        except:
            print("Cannot load label filter")
            msgBox.setText("Cannot load label filter!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Creating Fold filter
        try:
            FoldString = str.split(str.strip(ui.txtFFold.toPlainText()), "\n")
            FoldFilterList = list()
            if len(FoldString):
                for foldstr in FoldString:
                    if len(foldstr):
                        FoldFilterList.append(int(foldstr))

                print("Fold filter is ", FoldFilterList)
        except:
            print("Cannot load fold filter")
            msgBox.setText("Cannot load fold filter!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        InData = mainIO_load(InFile)
        OutData = dict()
        OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

        # Data
        try:
            X = InData[ui.txtData.currentText()]
        except:
            print("Cannot load data")
            return
        # Label
        try:
            Label = InData[ui.txtLabel.currentText()][0]
        except:
            print("Cannot load Labels!")
            return
        # Subject
        try:
            Subject = InData[ui.txtSubject.currentText()]
        except:
            print("Cannot load Subject ID")
            return
        # Run
        try:
            Run = InData[ui.txtRun.currentText()]
        except:
            print("Cannot load Run ID")
            return
        # Counter
        try:
            Counter = InData[ui.txtCounter.currentText()]
        except:
            print("Cannot load Counter ID")
            return
        # Task
        try:
            Task = np.asarray(InData[ui.txtTask.currentText()])
            TaskIndex = Task.copy()
            for tasindx, tas in enumerate(np.unique(Task)):
                TaskIndex[Task == tas] = tasindx + 1
        except:
            print("Cannot load Task ID")
            return
        # mLabel
        mLabel = None
        try:
            if ui.cbmLabel.isChecked():
                mLabel = InData[ui.txtmLabel.currentText()]
        except:
            print("Cannot load mLabel ID")
            return
        # Coordinate
        try:
            if ui.cbCol.isChecked():
                OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
        except:
            print("Cannot load Coordinate ID")
            return
        # Design Matrix
        DM = None
        try:
            if ui.cbDM.isChecked():
                DM = InData[ui.txtDM.currentText()]
        except:
            print("Cannot load Design Matrix ID")
            return
        # Condition
        try:
            if ui.cbCond.isChecked():
                OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
        except:
            print("Cannot load Condition ID")
            return
        # NScan
        Scan = None
        try:
            if ui.cbNScan.isChecked():
                Scan = InData[ui.txtScan.currentText()]
        except:
            print("Cannot load NScan ID")
            return

        # Apply filter
        print("Filtering labels from data ...")

        for lbl in ClassList:
            # Remove Training Set
            labelIndx = np.where(Label == lbl)[0]
            X = np.delete(X, labelIndx, axis=0)
            Label = np.delete(Label, labelIndx)
            Subject = np.delete(Subject, labelIndx, axis=1)
            Run = np.delete(Run, labelIndx, axis=1)
            Task = np.delete(Task, labelIndx, axis=1)
            Counter = np.delete(Counter, labelIndx, axis=1)
            mLabel = np.delete(mLabel, labelIndx,
                               axis=0) if mLabel is not None else None
            DM = np.delete(DM, labelIndx, axis=0) if DM is not None else None
            Scan = np.delete(Scan, labelIndx,
                             axis=1) if Scan is not None else None
            print("Label %d is filtered!" % (lbl))

        Unit = 1
        print("Calculating Alignment Level ...")
        GroupFold = None
        FoldStr = ""
        if ui.cbFSubject.isChecked():
            if not ui.rbFRun.isChecked():
                GroupFold = Subject
                FoldStr = "Subject"
            else:
                GroupFold = np.concatenate((Subject, Run))
                FoldStr = "Subject+Run"

        if ui.cbFTask.isChecked():
            GroupFold = np.concatenate(
                (GroupFold, TaskIndex)) if GroupFold is not None else TaskIndex
            FoldStr = FoldStr + "+Task"

        if ui.cbFCounter.isChecked():
            GroupFold = np.concatenate(
                (GroupFold, Counter)) if GroupFold is not None else Counter
            FoldStr = FoldStr + "+Counter"

        GroupFold = np.transpose(GroupFold)
        UniqFold = np.array(list(set(tuple(i) for i in GroupFold.tolist())))
        FoldIDs = np.arange(len(UniqFold)) + 1

        if len(UniqFold) <= Unit:
            msgBox.setText(
                "Unit for the test set must be smaller than all possible folds! Number of all folds is: "
                + str(len(UniqFold)))
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if np.mod(len(UniqFold), Unit):
            msgBox.setText(
                "Unit for the test set must be divorceable to all possible folds! Number of all folds is: "
                + str(len(UniqFold)))
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        ListFold = list()
        for gfold in GroupFold:
            for ufoldindx, ufold in enumerate(UniqFold):
                if (ufold == gfold).all():
                    currentID = FoldIDs[ufoldindx]
                    break
            ListFold.append(currentID)

        ListFold = np.int32(ListFold)
        if Unit == 1:
            UnitFold = np.int32(ListFold)
        else:
            UnitFold = np.int32((ListFold - 0.1) / Unit) + 1
        GUFold = np.unique(UnitFold)
        print("Number of data in this level is " + str(len(UniqFold)))
        # Filtering folds
        print("Applying fold filter ...")
        for foldfilter in FoldFilterList:
            IndexFold = np.where(UnitFold != foldfilter)
            X = X[IndexFold, :][0]
            Label = Label[IndexFold]
            Subject = Subject[:, IndexFold][0]
            Run = Run[:, IndexFold][0]
            Task = Task[:, IndexFold][0]
            Counter = Counter[:, IndexFold][0]
            mLabel = mLabel[IndexFold, :][0] if mLabel is not None else None
            DM = DM[IndexFold, :][0] if DM is not None else None
            Scan = Scan[:, IndexFold][0] if Scan is not None else None
            UnitFold = UnitFold[IndexFold]
            print("Fold %d is filtered!" % (foldfilter))
        # Reconstruct Labels
        print("Reforming labels based on folds ...")
        LabelCounter = dict()
        for lbl in np.unique(Label):
            LabelCounter[lbl] = None
        for foldID, fold in enumerate(GUFold):
            foldLevelLabel = Label[np.where(UnitFold == fold)]
            foldLevelUnique, foldLevelCounter = np.unique(foldLevelLabel,
                                                          return_counts=True)
            for flu, flc in zip(foldLevelUnique, foldLevelCounter):
                LabelCounter[flu] = np.min(
                    (LabelCounter[flu],
                     flc)) if LabelCounter[flu] is not None else flc
        if ui.cbBalence.isChecked():
            MinAllLabelCounter = None
            for lbl in np.unique(Label):
                MinAllLabelCounter = LabelCounter[
                    lbl] if MinAllLabelCounter is None else np.min(
                        (MinAllLabelCounter, LabelCounter[lbl]))
            for lbl in np.unique(Label):
                LabelCounter[lbl] = MinAllLabelCounter
        # Report Label Threshold
        for lbl in np.unique(Label):
            print("Class ID: {:6d}, \t Selected Timepoints: {:10d}".format(
                lbl, LabelCounter[lbl]))
        # Make free space memory
        del InData

        XOut = None
        LabelOut = None
        SubjectOut = None
        RunOut = None
        TaskOut = None
        CounterOut = None
        mLabelOut = None
        DMOut = None
        ScanOut = None

        for foldID, fold in enumerate(GUFold):
            IndexFold = np.where(UnitFold == fold)
            SX = X[IndexFold, :][0]
            SLabel = Label[IndexFold]
            SSubject = Subject[:, IndexFold][0][0]
            SRun = Run[:, IndexFold][0][0]
            STask = Task[:, IndexFold][0][0]
            SCounter = Counter[:, IndexFold][0][0]
            if ui.cbmLabel.isChecked():
                SmLabel = mLabel[
                    IndexFold, :][0] if mLabel is not None else None
            if ui.cbDM.isChecked():
                SDM = DM[IndexFold, :][0] if DM is not None else None
            if ui.cbNScan.isChecked():
                SScan = Scan[:, IndexFold][0][0] if Scan is not None else None

            for lbl in np.unique(Label):
                IndexLabel = np.where(SLabel == lbl)
                IndexLabel = IndexLabel[0][:LabelCounter[lbl]]
                XOut = SX[IndexLabel, :] if XOut is None else np.concatenate(
                    (XOut, SX[IndexLabel, :]), axis=0)
                LabelOut = SLabel[
                    IndexLabel] if LabelOut is None else np.concatenate(
                        (LabelOut, SLabel[IndexLabel]), axis=0)
                SubjectOut = SSubject[
                    IndexLabel] if SubjectOut is None else np.concatenate(
                        (SubjectOut, SSubject[IndexLabel]), axis=0)
                RunOut = SRun[IndexLabel] if RunOut is None else np.concatenate(
                    (RunOut, SRun[IndexLabel]), axis=0)
                TaskOut = STask[
                    IndexLabel] if TaskOut is None else np.concatenate(
                        (TaskOut, STask[IndexLabel]), axis=0)
                CounterOut = SCounter[
                    IndexLabel] if CounterOut is None else np.concatenate(
                        (CounterOut, SCounter[IndexLabel]), axis=0)
                if ui.cbmLabel.isChecked():
                    mLabelOut = SmLabel[
                        IndexLabel, :] if mLabelOut is None else np.concatenate(
                            (mLabelOut, SmLabel[IndexLabel, :]), axis=0)
                if ui.cbDM.isChecked():
                    DMOut = SDM[
                        IndexLabel, :] if DMOut is None else np.concatenate(
                            (DMOut, SDM[IndexLabel, :]), axis=0)
                if ui.cbNScan.isChecked():
                    ScanOut = SScan[
                        IndexLabel] if ScanOut is None else np.concatenate(
                            (ScanOut, SScan[IndexLabel]), axis=0)

                print("Data belong to fold {:6} and label {:6d} is collected.".
                      format(fold, lbl))

        OutData[ui.txtOData.text()] = XOut
        OutData[ui.txtOTask.text()] = reshape_1Dvector(TaskOut)
        OutData[ui.txtOLabel.text()] = reshape_1Dvector(LabelOut)
        OutData[ui.txtOSubject.text()] = reshape_1Dvector(SubjectOut)
        OutData[ui.txtORun.text()] = reshape_1Dvector(RunOut)
        OutData[ui.txtOCounter.text()] = reshape_1Dvector(CounterOut)
        if ui.cbmLabel.isChecked():
            OutData[ui.txtOmLabel.text()] = mLabelOut
        if ui.cbDM.isChecked():
            OutData[ui.txtODM.text()] = DMOut
        if ui.cbNScan.isChecked():
            OutData[ui.txtOScan.text()] = reshape_1Dvector(ScanOut)
        print("Saving ...")
        mainIO_save(OutData, OutFile)
        print("Temporal Alignment is done.")
        msgBox.setText("Temporal Alignment is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #13
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        totalTime = 0
        # Batch
        try:
            Batch = np.int32(ui.txtBatch.text())
        except:
            msgBox.setText("Size of batch is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if Batch == 0:
            Batch = None

        # Kernel
        Kernel = ui.cbKernel.currentText()
        # Method
        Method = ui.cbMethod.currentText()

        # Gamma
        try:
            Gamma = np.float(ui.txtGamma.text())
        except:
            msgBox.setText("Gamma is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Degree
        try:
            Degree = np.int32(ui.txtDegree.text())
        except:
            msgBox.setText("Degree is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Coef0
        try:
            Coef0 = np.float(ui.txtCoef0.text())
        except:
            msgBox.setText("Coef0 is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Alpha
        try:
            Alpha = np.int32(ui.txtAlpha.text())
        except:
            msgBox.setText("Alpha is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Tol
        try:
            Tol = np.float(ui.txtTole.text())
        except:
            msgBox.setText("Tolerance is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # MaxIte
        try:
            MaxIter = np.int32(ui.txtMaxIter.text())
        except:
            msgBox.setText("Maximum number of iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if MaxIter <= 0:
            MaxIter = None

        # Number of Job
        try:
            NJob = np.int32(ui.txtJobs.text())
        except:
            msgBox.setText("The number of parallel jobs is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if NJob < -1 or NJob == 0:
            msgBox.setText(
                "The number of parallel jobs must be -1 or greater than 0!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return

        if FoldTo < FoldFrom:
            print("Please check fold parameters!")
            return

        for fold_all in range(FoldFrom, FoldTo + 1):
            tic = time.time()
            # OutFile
            OutFile = ui.txtOutFile.text()
            OutFile = OutFile.replace("$FOLD$", str(fold_all))
            if not len(OutFile):
                msgBox.setText("Please enter out file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # InFile
            InFile = ui.txtInFile.text()
            InFile = InFile.replace("$FOLD$", str(fold_all))
            if not len(InFile):
                msgBox.setText("Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not os.path.isfile(InFile):
                msgBox.setText("Input file not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            InData = mainIO_load(InFile)
            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

            # Data
            if not len(ui.txtITrData.currentText()):
                msgBox.setText("Please enter Input Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeData.currentText()):
                msgBox.setText("Please enter Input Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrData.text()):
                msgBox.setText("Please enter Output Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeData.text()):
                msgBox.setText("Please enter Output Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            try:
                XTr = InData[ui.txtITrData.currentText()]
                XTe = InData[ui.txtITeData.currentText()]

                if ui.cbScale.isChecked():
                    XTr = preprocessing.scale(XTr)
                    XTe = preprocessing.scale(XTe)
                    print("Whole of data is scaled X~N(0,1).")
            except:
                print("Cannot load data")
                return

            try:
                NumFea = np.int32(ui.txtNumFea.text())
            except:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea < 0:
                msgBox.setText("Number of features must be greater than zero!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if NumFea > np.shape(XTr)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if NumFea > np.shape(XTe)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # Label
            if not len(ui.txtITrLabel.currentText()):
                msgBox.setText("Please enter Train Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeLabel.currentText()):
                msgBox.setText("Please enter Test Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrLabel.text()):
                msgBox.setText(
                    "Please enter Train Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeLabel.text()):
                msgBox.setText("Please enter Test Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(
                    InData[ui.txtITrLabel.currentText()])
                OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(
                    InData[ui.txtITeLabel.currentText()])
            except:
                print("Cannot load labels!")

            # Subject
            if not len(ui.txtITrSubject.currentText()):
                msgBox.setText(
                    "Please enter Train Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeSubject.currentText()):
                msgBox.setText(
                    "Please enter Test Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrSubject.text()):
                msgBox.setText(
                    "Please enter Train Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeSubject.text()):
                msgBox.setText(
                    "Please enter Test Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                TrSubject = InData[ui.txtITrSubject.currentText()]
                OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject)
                TeSubject = InData[ui.txtITeSubject.currentText()]
                OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject)
            except:
                print("Cannot load Subject IDs")
                return

            # Task
            if ui.cbTask.isChecked():
                if not len(ui.txtITrTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrTask.text()):
                    msgBox.setText(
                        "Please enter Output Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeTask.text()):
                    msgBox.setText(
                        "Please enter Output Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrTask = np.asarray(InData[ui.txtITrTask.currentText()])
                    OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask)
                    TeTask = np.asarray(InData[ui.txtITeTask.currentText()])
                    OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask)
                    TrTaskIndex = TrTask.copy()
                    for tasindx, tas in enumerate(np.unique(TrTask)):
                        TrTaskIndex[TrTask == tas] = tasindx + 1
                    TeTaskIndex = TeTask.copy()
                    for tasindx, tas in enumerate(np.unique(TeTask)):
                        TeTaskIndex[TeTask == tas] = tasindx + 1
                except:
                    print("Cannot load Tasks!")
                    return

            # Run
            if ui.cbRun.isChecked():
                if not len(ui.txtITrRun.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeRun.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrRun.text()):
                    msgBox.setText(
                        "Please enter Train Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeRun.text()):
                    msgBox.setText(
                        "Please enter Test Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrRun = InData[ui.txtITrRun.currentText()]
                    OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun)
                    TeRun = InData[ui.txtITeRun.currentText()]
                    OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun)
                except:
                    print("Cannot load Runs!")
                    return

            # Counter
            if ui.cbCounter.isChecked():
                if not len(ui.txtITrCounter.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeCounter.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrCounter.text()):
                    msgBox.setText(
                        "Please enter Train Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeCounter.text()):
                    msgBox.setText(
                        "Please enter Test Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrCounter = InData[ui.txtITrCounter.currentText()]
                    OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(
                        TrCounter)
                    TeCounter = InData[ui.txtITeCounter.currentText()]
                    OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(
                        TeCounter)
                except:
                    print("Cannot load Counters!")
                    return

            # Matrix Label
            if ui.cbmLabel.isChecked():
                if not len(ui.txtITrmLabel.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITemLabel.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrmLabel.text()):
                    msgBox.setText(
                        "Please enter Train Output Matrix Label variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTemLabel.text()):
                    msgBox.setText(
                        "Please enter Test Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrmLabel.text()] = InData[
                        ui.txtITrmLabel.currentText()]
                    OutData[ui.txtOTemLabel.text()] = InData[
                        ui.txtITemLabel.currentText()]
                except:
                    print("Cannot load matrix lables!")
                    return

            # Design
            if ui.cbDM.isChecked():
                if not len(ui.txtITrDM.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeDM.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrDM.text()):
                    msgBox.setText(
                        "Please enter Train Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeDM.text()):
                    msgBox.setText(
                        "Please enter Test Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrDM.text()] = InData[
                        ui.txtITrDM.currentText()]
                    OutData[ui.txtOTeDM.text()] = InData[
                        ui.txtITeDM.currentText()]
                except:
                    print("Cannot load design matrices!")
                    return

            # Coordinate
            if ui.cbCol.isChecked():
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCol.text()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCol.text()] = InData[
                        ui.txtCol.currentText()]
                except:
                    print("Cannot load coordinator!")
                    return

            # Condition
            if ui.cbCond.isChecked():
                if not len(ui.txtCond.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCond.text()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCond.text()] = InData[
                        ui.txtCond.currentText()]
                except:
                    print("Cannot load conditions!")
                    return

            # FoldID
            if ui.cbFoldID.isChecked():
                if not len(ui.txtFoldID.currentText()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldID.text()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldID.text()] = reshape_1Dvector(
                        InData[ui.txtFoldID.currentText()])
                except:
                    print("Cannot load Fold ID!")
                    return

            # FoldInfo
            if ui.cbFoldInfo.isChecked():
                if not len(ui.txtFoldInfo.currentText()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldInfo.text()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldInfo.text()] = InData[
                        ui.txtFoldInfo.currentText()]
                except:
                    print("Cannot load Fold Info!")
                    return
                pass

            # Number of Scan
            if ui.cbNScan.isChecked():
                if not len(ui.txtITrScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrScan.text()] = reshape_1Dvector(
                        InData[ui.txtITrScan.currentText()])
                    OutData[ui.txtOTeScan.text()] = reshape_1Dvector(
                        InData[ui.txtITeScan.currentText()])
                except:
                    print("Cannot load NScan!")
                    return

            if NumFea == 0:
                NumFea = np.min(np.shape(XTr))
                print("Number of features are automatically selected as ",
                      NumFea)

            try:
                if Method == "PCA":
                    model = PCA(n_components=NumFea, copy=False, tol=Tol)
                elif Method == "Kernel PCA":
                    model = KernelPCA(n_components=NumFea,kernel=Kernel,gamma=Gamma,degree=Degree,\
                                  coef0=Coef0, alpha=Alpha, tol=Tol, max_iter=MaxIter, n_jobs=NJob,copy_X=False)
                else:
                    model = IncrementalPCA(n_components=NumFea,
                                           copy=False,
                                           batch_size=Batch)

                print("Running PCA Functional Alignment on Training Data ...")
                OutData[ui.txtOTrData.text()] = model.fit_transform(XTr)
                print("Running PCA Functional Alignment on Testing Data ...")
                OutData[ui.txtOTeData.text()] = model.fit_transform(XTe)
            except Exception as e:
                print(str(e))

            HAParam = dict()
            HAParam["Method"] = Method
            HAParam["NumFea"] = NumFea
            HAParam["Kernel"] = Kernel
            OutData["FunctionalAlignment"] = HAParam
            OutData["Runtime"] = time.time() - tic
            totalTime += OutData["Runtime"]

            print("Saving ...")
            mainIO_save(OutData, OutFile)
            print("Fold " + str(fold_all) + " is DONE: " + OutFile)
        print("Runtime: ", totalTime)
        print("PCA Functional Alignment is done.")
        msgBox.setText("PCA Functional Alignment is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #14
0
    def btnConvert_click(self):
        msgBox = QMessageBox()

        if not ui.cbFSubject.isChecked():
            if not ui.cbFTask.isChecked():
                if not ui.cbFCounter.isChecked():
                    msgBox.setText("You must at least select one Fold Level!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False

        # Label
        if not len(ui.txtLabel.currentText()):
            msgBox.setText("Please enter Label variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText("Please enter Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText("Please enter Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Coordinate
        if ui.cbCol.isChecked():
            if not len(ui.txtCol.currentText()):
                msgBox.setText("Please enter Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtScan.currentText()):
                msgBox.setText("Please enter Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Subject Condition
        if ui.cbFSubject.isChecked():
            if not ui.cbSubject.isChecked():
                msgBox.setText(
                    "For using task as a fold level, you have to enable task filed in input file!"
                )
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # Run Condition
            if ui.rbFRun.isChecked():
                if not ui.cbRun.isChecked():
                    msgBox.setText(
                        "For using run as a fold level, you have to enable run filed in input file!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False

        # Task Condition
        if not ui.cbTask.isChecked():
            if ui.cbFTask.isChecked():
                msgBox.setText(
                    "For using task as a fold level, you have to enable task filed in input file!"
                )
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # Counter
        if not ui.cbCounter.isChecked():
            if ui.cbFCounter.isChecked():
                msgBox.setText(
                    "For using counter as a fold level, you have to enable counter filed in input file!"
                )
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        # OutDIR
        OutDIR = ui.txtOutDIR.text()
        if not len(OutDIR):
            msgBox.setText("Please enter out directory!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not os.path.isdir(OutDIR):
            msgBox.setText("Output directory not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Out Files
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            InData = mainIO_load(InFile)
        except:
            print("Cannot load data file!")
            return

        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            X = InData[ui.txtData.currentText()]
        except:
            print("Cannot load data")
            return

        # Subject
        if ui.cbSubject.isChecked():
            if not len(ui.txtSubject.currentText()):
                msgBox.setText("Please enter Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        try:
            Subject = InData[ui.txtSubject.currentText()]
        except:
            print("Cannot load Subject ID")
            return

        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtTask.currentText()):
                msgBox.setText("Please enter Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        try:
            Task = np.asarray(InData[ui.txtTask.currentText()])
            TaskIndex = Task.copy()
            for tasindx, tas in enumerate(np.unique(Task)):
                TaskIndex[Task == tas] = tasindx + 1
        except:
            print("Cannot load Task ID")
            return

        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtRun.currentText()):
                msgBox.setText("Please enter Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        try:
            Run = InData[ui.txtRun.currentText()]
        except:
            print("Cannot load Run ID")
            return

        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtCounter.currentText()):
                msgBox.setText("Please enter Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

        try:
            Counter = InData[ui.txtCounter.currentText()]
        except:
            print("Cannot load Counter ID")
            return

        try:
            Unit = np.int32(ui.txtUnit.text())
        except:
            msgBox.setText("Unit for the test set must be a number!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if Unit < 1:
            msgBox.setText("Unit for the test set must be greater than zero!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        print("Calculating Folds ...")
        GroupFold = None
        FoldStr = ""
        if ui.cbFSubject.isChecked():
            if not ui.rbFRun.isChecked():
                GroupFold = Subject
                FoldStr = "Subject"
            else:
                GroupFold = np.concatenate((Subject, Run))
                FoldStr = "Subject+Run"

        if ui.cbFTask.isChecked():
            GroupFold = np.concatenate(
                (GroupFold, TaskIndex)) if GroupFold is not None else TaskIndex
            FoldStr = FoldStr + "+Task"

        if ui.cbFCounter.isChecked():
            GroupFold = np.concatenate(
                (GroupFold, Counter)) if GroupFold is not None else Counter
            FoldStr = FoldStr + "+Counter"

        GroupFold = np.transpose(GroupFold)

        UniqFold = np.array(list(set(tuple(i) for i in GroupFold.tolist())))

        FoldIDs = np.arange(len(UniqFold)) + 1

        if len(UniqFold) <= Unit:
            msgBox.setText(
                "Unit for the test set must be smaller than all possible folds! Number of all folds is: "
                + str(len(UniqFold)))
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if np.mod(len(UniqFold), Unit):
            msgBox.setText(
                "Unit for the test set must be divorceable to all possible folds! Number of all folds is: "
                + str(len(UniqFold)))
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        ListFold = list()
        for gfold in GroupFold:
            for ufoldindx, ufold in enumerate(UniqFold):
                if (ufold == gfold).all():
                    currentID = FoldIDs[ufoldindx]
                    break
            ListFold.append(currentID)

        ListFold = np.int32(ListFold)
        if Unit == 1:
            UnitFold = np.int32(ListFold)
        else:
            UnitFold = np.int32((ListFold - 0.1) / Unit) + 1

        FoldInfo = dict()
        FoldInfo["Unit"] = Unit
        FoldInfo["Group"] = GroupFold
        FoldInfo["Order"] = FoldStr
        FoldInfo["List"] = ListFold
        FoldInfo["Unique"] = UniqFold
        FoldInfo["Folds"] = UnitFold

        GUFold = np.unique(UnitFold)

        print("Number of all folds is: " + str(len(UniqFold)))
        for foldID, fold in enumerate(GUFold):
            print("Saving Fold " + str(foldID + 1), " of ", str(len(UniqFold)),
                  " ...")

            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

            OutData["FoldInfo"] = FoldInfo
            OutData["FoldID"] = [[fold]]

            TestIndex = np.where(UnitFold == fold)
            TrainIndex = np.where(UnitFold != fold)

            OutData[ui.txtTrain.text() + ui.txtOData.text()] = X[TrainIndex]
            OutData[ui.txtTest.text() + ui.txtOData.text()] = X[TestIndex]

            # Subject
            if ui.cbSubject.isChecked():
                OutData[ui.txtTrain.text() +
                        ui.txtOSubject.text()] = reshape_1Dvector(
                            Subject[0, TrainIndex])
                OutData[ui.txtTest.text() +
                        ui.txtOSubject.text()] = reshape_1Dvector(
                            Subject[0, TestIndex])

            # Task
            if ui.cbTask.isChecked():
                OutData[ui.txtTrain.text() +
                        ui.txtOTask.text()] = reshape_1Dvector(
                            Task[0, TrainIndex])
                OutData[ui.txtTest.text() +
                        ui.txtOTask.text()] = reshape_1Dvector(Task[0,
                                                                    TestIndex])

            # Run
            if ui.cbRun.isChecked():
                OutData[ui.txtTrain.text() +
                        ui.txtORun.text()] = reshape_1Dvector(Run[0,
                                                                  TrainIndex])
                OutData[ui.txtTest.text() +
                        ui.txtORun.text()] = reshape_1Dvector(Run[0,
                                                                  TestIndex])

            # Counter
            if ui.cbCounter.isChecked():
                OutData[ui.txtTrain.text() +
                        ui.txtOCounter.text()] = reshape_1Dvector(
                            Counter[0, TrainIndex])
                OutData[ui.txtTest.text() +
                        ui.txtOCounter.text()] = reshape_1Dvector(
                            Counter[0, TestIndex])

            # Label
            Label = InData[ui.txtLabel.currentText()]
            TrainLabel = Label[0, TrainIndex]
            TestLabel = Label[0, TestIndex]
            OutData[ui.txtTrain.text() +
                    ui.txtOLabel.text()] = reshape_1Dvector(TrainLabel)
            OutData[ui.txtTest.text() +
                    ui.txtOLabel.text()] = reshape_1Dvector(TestLabel)

            # m Label
            if ui.cbmLabel.isChecked():
                OutData[ui.txtTrain.text() +
                        ui.txtOmLabel.text()] = label_binarize(
                            TrainLabel[0], np.unique(TrainLabel))
                OutData[ui.txtTest.text() +
                        ui.txtOmLabel.text()] = label_binarize(
                            TestLabel[0], np.unique(TestLabel))

            # DM
            if ui.cbDM.isChecked():
                DM = InData[ui.txtDM.currentText()]
                OutData[ui.txtTrain.text() + ui.txtODM.text()] = DM[TrainIndex]
                OutData[ui.txtTest.text() + ui.txtODM.text()] = DM[TestIndex]

            # NScan
            if ui.cbNScan.isChecked():
                NScan = InData[ui.txtScan.currentText()]
                OutData[ui.txtTrain.text() +
                        ui.txtOScan.text()] = reshape_1Dvector(
                            NScan[0, TrainIndex])
                OutData[ui.txtTest.text() +
                        ui.txtOScan.text()] = reshape_1Dvector(
                            NScan[0, TestIndex])

            # Coordination
            if ui.cbCol.isChecked():
                OutData[ui.txtCol.currentText()] = InData[ui.txtOCol.text()]

            # Condition
            if ui.cbCond.isChecked():
                OutData[ui.txtCond.currentText()] = InData[ui.txtOCond.text()]

            OutFileUpdate = str.replace(OutFile, "$FOLD$", str(foldID + 1))
            mainIO_save(OutData, OutDIR + OutFileUpdate)
            #io.savemat(OutDIR + OutFileUpdate, mdict=OutData)

        print("Cross validation is done.")
        msgBox.setText("Cross validation is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #15
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        totalTime = 0
        Alg = ui.cbAlg.currentText()

        # Tol
        try:
            Tol = np.float(ui.txtTole.text())
        except:
            msgBox.setText("Tolerance is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # MaxIte
        try:
            MaxIter = np.int32(ui.txtMaxIter.text())
        except:
            msgBox.setText("Maximum number of iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if MaxIter < 1:
            msgBox.setText("Maximum number of iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False


        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo   = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return

        if FoldTo < FoldFrom:
            print("Please check fold parameters!")
            return

        for fold_all in range(FoldFrom, FoldTo+1):
            tic = time.time()
            # OutFile
            OutFile = ui.txtOutFile.text()
            OutFile = OutFile.replace("$FOLD$", str(fold_all))
            if not len(OutFile):
                msgBox.setText("Please enter out file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # InFile
            InFile = ui.txtInFile.text()
            InFile = InFile.replace("$FOLD$", str(fold_all))
            if not len(InFile):
                msgBox.setText("Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not os.path.isfile(InFile):
                msgBox.setText("Input file not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            InData = mainIO_load(InFile)
            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

            # Data
            if not len(ui.txtITrData.currentText()):
                msgBox.setText("Please enter Input Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeData.currentText()):
                msgBox.setText("Please enter Input Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrData.text()):
                msgBox.setText("Please enter Output Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeData.text()):
                msgBox.setText("Please enter Output Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            try:
                XTr = InData[ui.txtITrData.currentText()]
                XTe = InData[ui.txtITeData.currentText()]

                if ui.cbScale.isChecked():
                    XTr = preprocessing.scale(XTr)
                    XTe = preprocessing.scale(XTe)
                    print("Whole of data is scaled X~N(0,1).")
            except:
                print("Cannot load data")
                return

            try:
                NumFea = np.int32(ui.txtNumFea.text())
            except:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea < 1:
                msgBox.setText("Number of features must be greater than zero!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if NumFea > np.shape(XTr)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            if NumFea > np.shape(XTe)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False


            # Label
            if not len(ui.txtITrLabel.currentText()):
                    msgBox.setText("Please enter Train Input Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            if not len(ui.txtITeLabel.currentText()):
                    msgBox.setText("Please enter Test Input Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            if not len(ui.txtOTrLabel.text()):
                    msgBox.setText("Please enter Train Output Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            if not len(ui.txtOTeLabel.text()):
                    msgBox.setText("Please enter Test Output Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
            try:
                OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(InData[ui.txtITrLabel.currentText()])
                OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(InData[ui.txtITeLabel.currentText()])
            except:
                print("Cannot load labels!")

            # Subject
            if not len(ui.txtITrSubject.currentText()):
                msgBox.setText("Please enter Train Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeSubject.currentText()):
                msgBox.setText("Please enter Test Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrSubject.text()):
                msgBox.setText("Please enter Train Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeSubject.text()):
                msgBox.setText("Please enter Test Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                TrSubject = InData[ui.txtITrSubject.currentText()]
                OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject)
                TeSubject = InData[ui.txtITeSubject.currentText()]
                OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject)
            except:
                print("Cannot load Subject IDs")
                return

            # Task
            if ui.cbTask.isChecked():
                if not len(ui.txtITrTask.currentText()):
                    msgBox.setText("Please enter Input Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeTask.currentText()):
                    msgBox.setText("Please enter Input Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrTask.text()):
                    msgBox.setText("Please enter Output Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeTask.text()):
                    msgBox.setText("Please enter Output Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrTask = np.asarray(InData[ui.txtITrTask.currentText()])
                    OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask)
                    TeTask = np.asarray(InData[ui.txtITeTask.currentText()])
                    OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask)
                    TrTaskIndex = TrTask.copy()
                    for tasindx, tas in enumerate(np.unique(TrTask)):
                        TrTaskIndex[TrTask == tas] = tasindx + 1
                    TeTaskIndex = TeTask.copy()
                    for tasindx, tas in enumerate(np.unique(TeTask)):
                        TeTaskIndex[TeTask == tas] = tasindx + 1
                except:
                    print("Cannot load Tasks!")
                    return

            # Run
            if ui.cbRun.isChecked():
                if not len(ui.txtITrRun.currentText()):
                    msgBox.setText("Please enter Train Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeRun.currentText()):
                    msgBox.setText("Please enter Test Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrRun.text()):
                    msgBox.setText("Please enter Train Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeRun.text()):
                    msgBox.setText("Please enter Test Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrRun = InData[ui.txtITrRun.currentText()]
                    OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun)
                    TeRun = InData[ui.txtITeRun.currentText()]
                    OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun)
                except:
                    print("Cannot load Runs!")
                    return

            # Counter
            if ui.cbCounter.isChecked():
                if not len(ui.txtITrCounter.currentText()):
                    msgBox.setText("Please enter Train Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeCounter.currentText()):
                    msgBox.setText("Please enter Test Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrCounter.text()):
                    msgBox.setText("Please enter Train Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeCounter.text()):
                    msgBox.setText("Please enter Test Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrCounter = InData[ui.txtITrCounter.currentText()]
                    OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(TrCounter)
                    TeCounter = InData[ui.txtITeCounter.currentText()]
                    OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(TeCounter)
                except:
                    print("Cannot load Counters!")
                    return

            # Matrix Label
            if ui.cbmLabel.isChecked():
                if not len(ui.txtITrmLabel.currentText()):
                    msgBox.setText("Please enter Train Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITemLabel.currentText()):
                    msgBox.setText("Please enter Test Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrmLabel.text()):
                    msgBox.setText("Please enter Train Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTemLabel.text()):
                    msgBox.setText("Please enter Test Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrmLabel.text()] = InData[ui.txtITrmLabel.currentText()]
                    OutData[ui.txtOTemLabel.text()] = InData[ui.txtITemLabel.currentText()]
                except:
                    print("Cannot load matrix lables!")
                    return

            # Design
            if ui.cbDM.isChecked():
                if not len(ui.txtITrDM.currentText()):
                    msgBox.setText("Please enter Train Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeDM.currentText()):
                    msgBox.setText("Please enter Test Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrDM.text()):
                    msgBox.setText("Please enter Train Output Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeDM.text()):
                    msgBox.setText("Please enter Test Output Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrDM.text()] = InData[ui.txtITrDM.currentText()]
                    OutData[ui.txtOTeDM.text()] = InData[ui.txtITeDM.currentText()]
                except:
                    print("Cannot load design matrices!")
                    return

            # Coordinate
            if ui.cbCol.isChecked():
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCol.text()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
                except:
                    print("Cannot load coordinator!")
                    return

            # Condition
            if ui.cbCond.isChecked():
                if not len(ui.txtCond.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCond.text()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
                except:
                    print("Cannot load conditions!")
                    return

            # FoldID
            if ui.cbFoldID.isChecked():
                if not len(ui.txtFoldID.currentText()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldID.text()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldID.text()] = reshape_1Dvector(InData[ui.txtFoldID.currentText()])
                except:
                    print("Cannot load Fold ID!")
                    return

            # FoldInfo
            if ui.cbFoldInfo.isChecked():
                if not len(ui.txtFoldInfo.currentText()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldInfo.text()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldInfo.text()] = InData[ui.txtFoldInfo.currentText()]
                except:
                    print("Cannot load Fold Info!")
                    return
                pass

            # Number of Scan
            if ui.cbNScan.isChecked():
                if not len(ui.txtITrScan.currentText()):
                    msgBox.setText("Please enter Number of Scan variable name for Input Train!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeScan.currentText()):
                    msgBox.setText("Please enter Number of Scan variable name for Input Test!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrScan.text()):
                    msgBox.setText("Please enter Number of Scan variable name for Output Train!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeScan.text()):
                    msgBox.setText("Please enter Number of Scan variable name for Output Test!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrScan.text()] = reshape_1Dvector(InData[ui.txtITrScan.currentText()])
                    OutData[ui.txtOTeScan.text()] = reshape_1Dvector(InData[ui.txtITeScan.currentText()])
                except:
                    print("Cannot load NScan!")
                    return

            try:
                model = FastICA(n_components=NumFea, algorithm=Alg, max_iter=MaxIter, tol=Tol)

                print("Running ICA Functional Alignment on Training Data ...")
                OutData[ui.txtOTrData.text()] = model.fit_transform(XTr)
                print("Running ICA Functional Alignment on Testing Data ...")
                OutData[ui.txtOTeData.text()] = model.fit_transform(XTe)
            except Exception as e:
                print(str(e))

            HAParam = dict()
            HAParam["Method"]   = "FastICA"
            HAParam["NumFea"]   = NumFea
            HAParam["Algorithm"]= Alg
            OutData["FunctionalAlignment"] = HAParam
            OutData["Runtime"] = time.time() - tic
            totalTime += OutData["Runtime"]

            print("Saving ...")
            mainIO_save(OutData, OutFile)
            print("Fold " + str(fold_all) + " is DONE: " + OutFile)

        print("Runtime: ", totalTime)
        print("ICA Functional Alignment is done.")
        msgBox.setText("ICA Functional Alignment is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #16
0
 def btnConvert_click(self):
     msgBox = QMessageBox()
     SVD = ui.cbSVD.currentText()
     # Tol
     try:
         Tol = np.float(ui.txtTole.text())
     except:
         msgBox.setText("Tolerance is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # MaxIte
     try:
         MaxIter = np.int32(ui.txtMaxIter.text())
     except:
         msgBox.setText("Maximum number of iterations is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if MaxIter < 1:
         msgBox.setText("Maximum number of iterations is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # PIter
     try:
         PIter = np.int32(ui.txtPIter.text())
     except:
         msgBox.setText("Power iterations is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if PIter < 1:
         msgBox.setText("Power iterations is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # OutFile
     OutFile = ui.txtOutFile.text()
     if not len(OutFile):
         msgBox.setText("Please enter out file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # InFile
     InFile = ui.txtInFile.text()
     if not len(InFile):
         msgBox.setText("Please enter input file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if not os.path.isfile(InFile):
         msgBox.setText("Input file not found!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if ui.rbScale.isChecked() == True and ui.rbALScale.isChecked(
     ) == False:
         msgBox.setText(
             "Subject Level Normalization is just available for Subject Level Analysis!"
         )
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     InData = mainIO_load(InFile)
     OutData = dict()
     OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
     if not len(ui.txtData.currentText()):
         msgBox.setText("Please enter Data variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     try:
         X = InData[ui.txtData.currentText()]
         if ui.cbScale.isChecked() and (not ui.rbScale.isChecked()):
             X = preprocessing.scale(X)
             print("Whole of data is scaled X~N(0,1).")
     except:
         print("Cannot load data")
         return
     try:
         NumFea = np.int32(ui.txtNumFea.text())
     except:
         msgBox.setText("Number of features is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if NumFea < 1:
         msgBox.setText("Number of features must be greater than zero!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if NumFea > np.shape(X)[1]:
         msgBox.setText("Number of features is wrong!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # Subject
     if not len(ui.txtSubject.currentText()):
         msgBox.setText("Please enter Subject variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     try:
         Subject = InData[ui.txtSubject.currentText()]
         OutData[ui.txtOSubject.text()] = reshape_1Dvector(Subject)
     except:
         print("Cannot load Subject ID")
         return
     # Label
     if not len(ui.txtLabel.currentText()):
         msgBox.setText("Please enter Label variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     OutData[ui.txtOLabel.text()] = reshape_1Dvector(
         InData[ui.txtLabel.currentText()])
     # Task
     if ui.cbTask.isChecked():
         if not len(ui.txtTask.currentText()):
             msgBox.setText("Please enter Task variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOTask.text()] = reshape_1Dvector(
             InData[ui.txtTask.currentText()])
     # Run
     if ui.cbRun.isChecked():
         if not len(ui.txtRun.currentText()):
             msgBox.setText("Please enter Run variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtORun.text()] = reshape_1Dvector(
             InData[ui.txtRun.currentText()])
     # Counter
     if ui.cbCounter.isChecked():
         if not len(ui.txtCounter.currentText()):
             msgBox.setText("Please enter Counter variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCounter.text()] = reshape_1Dvector(
             InData[ui.txtCounter.currentText()])
     # Matrix Label
     if ui.cbmLabel.isChecked():
         if not len(ui.txtmLabel.currentText()):
             msgBox.setText("Please enter Matrix Label variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()]
     # Design
     if ui.cbDM.isChecked():
         if not len(ui.txtDM.currentText()):
             msgBox.setText("Please enter Design Matrix variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]
     # Coordinate
     if ui.cbCol.isChecked():
         if not len(ui.txtCol.currentText()):
             msgBox.setText("Please enter Coordinator variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
     # Condition
     if ui.cbCond.isChecked():
         if not len(ui.txtCond.currentText()):
             msgBox.setText("Please enter Condition variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
     # Number of Scan
     if ui.cbNScan.isChecked():
         if not len(ui.txtScan.currentText()):
             msgBox.setText("Please enter Number of Scan variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOScan.text()] = reshape_1Dvector(
             InData[ui.txtScan.currentText()])
     Models = dict()
     Models["Name"] = "FactorAnalysis"
     if ui.rbALScale.isChecked():
         print("Partition data to subject level ...")
         SubjectUniq = np.unique(Subject)
         X_Sub = list()
         for subj in SubjectUniq:
             if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                 X_Sub.append(
                     preprocessing.scale(
                         X[np.where(Subject == subj)[1], :]))
                 print("Data in subject level is scaled, X_" + str(subj) +
                       "~N(0,1).")
             else:
                 X_Sub.append(X[np.where(Subject == subj)[1], :])
             print("Subject ", subj, " is extracted from data.")
         print("Running Factor Analysis in subject level ...")
         X_Sub_PCA = list()
         lenPCA = len(X_Sub)
         for xsubindx, xsub in enumerate(X_Sub):
             model = FactorAnalysis(n_components=NumFea,
                                    tol=Tol,
                                    max_iter=MaxIter,
                                    svd_method=SVD,
                                    iterated_power=PIter)
             X_Sub_PCA.append(model.fit_transform(xsub))
             Models["Model" + str(xsubindx + 1)] = str(
                 model.get_params(deep=True))
             print("Factor Analysis: ", xsubindx + 1, " of ", lenPCA,
                   " is done.")
         print("Data integration ... ")
         X_new = None
         for xsubindx, xsub in enumerate(X_Sub_PCA):
             X_new = np.concatenate(
                 (X_new, xsub)) if X_new is not None else xsub
             print("Integration: ", xsubindx + 1, " of ", lenPCA,
                   " is done.")
         OutData[ui.txtOData.text()] = X_new
     else:
         print("Running Factor Analysis ...")
         model = FactorAnalysis(n_components=NumFea,
                                tol=Tol,
                                max_iter=MaxIter,
                                svd_method=SVD,
                                iterated_power=PIter)
         OutData[ui.txtOData.text()] = model.fit_transform(X)
         Models["Model"] = str(model.get_params(deep=True))
     OutData["ModelParameter"] = Models
     print("Saving ...")
     mainIO_save(OutData, ui.txtOutFile.text())
     print("DONE.")
     msgBox.setText("Factor Analysis is done.")
     msgBox.setIcon(QMessageBox.Information)
     msgBox.setStandardButtons(QMessageBox.Ok)
     msgBox.exec_()
Пример #17
0
    def btnConvert_click(self):
        totalTime = 0
        msgBox = QMessageBox()

        TrFoldErr = list()
        TeFoldErr = list()

        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return

        if FoldTo < FoldFrom:
            print("Please check fold parameters!")
            return

        for fold_all in range(FoldFrom, FoldTo + 1):
            tic = time.time()
            # Regularization
            try:
                Regularization = np.float(ui.txtRegularization.text())
            except:
                msgBox.setText("Regularization value is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # OutFile
            OutFile = ui.txtOutFile.text()
            OutFile = OutFile.replace("$FOLD$", str(fold_all))
            if not len(OutFile):
                msgBox.setText("Please enter out file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            # InFile
            InFile = ui.txtInFile.text()
            InFile = InFile.replace("$FOLD$", str(fold_all))
            if not len(InFile):
                msgBox.setText("Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not os.path.isfile(InFile):
                msgBox.setText("Input file not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            InData = mainIO_load(InFile)
            OutData = dict()
            OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])

            # Data
            if not len(ui.txtITrData.currentText()):
                msgBox.setText("Please enter Input Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeData.currentText()):
                msgBox.setText("Please enter Input Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrData.text()):
                msgBox.setText("Please enter Output Train Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeData.text()):
                msgBox.setText("Please enter Output Test Data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False

            try:
                XTr = InData[ui.txtITrData.currentText()]
                XTe = InData[ui.txtITeData.currentText()]

                if ui.cbScale.isChecked() and not ui.rbScale.isChecked():
                    XTr = preprocessing.scale(XTr)
                    XTe = preprocessing.scale(XTe)
                    print("Whole of data is scaled X~N(0,1).")
            except:
                print("Cannot load data")
                return

            # NComponent
            try:
                NumFea = np.int32(ui.txtNumFea.text())
            except:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea < 0:
                msgBox.setText("Number of features must be greater than zero!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea > np.shape(XTr)[1]:
                msgBox.setText("Number of features is wrong!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if NumFea == 0:
                NumFea = None

            # Label
            if not len(ui.txtITrLabel.currentText()):
                msgBox.setText("Please enter Train Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeLabel.currentText()):
                msgBox.setText("Please enter Test Input Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrLabel.text()):
                msgBox.setText(
                    "Please enter Train Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeLabel.text()):
                msgBox.setText("Please enter Test Output Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(
                    InData[ui.txtITrLabel.currentText()])
                OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(
                    InData[ui.txtITeLabel.currentText()])
            except:
                print("Cannot load labels!")

            # Subject
            if not len(ui.txtITrSubject.currentText()):
                msgBox.setText(
                    "Please enter Train Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtITeSubject.currentText()):
                msgBox.setText(
                    "Please enter Test Input Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTrSubject.text()):
                msgBox.setText(
                    "Please enter Train Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            if not len(ui.txtOTeSubject.text()):
                msgBox.setText(
                    "Please enter Test Output Subject variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                TrSubject = InData[ui.txtITrSubject.currentText()]
                OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject)
                TeSubject = InData[ui.txtITeSubject.currentText()]
                OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject)
            except:
                print("Cannot load Subject IDs")
                return

            # Task
            if ui.cbTask.isChecked():
                if not len(ui.txtITrTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeTask.currentText()):
                    msgBox.setText(
                        "Please enter Input Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrTask.text()):
                    msgBox.setText(
                        "Please enter Output Train Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeTask.text()):
                    msgBox.setText(
                        "Please enter Output Test Task variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrTask = np.asarray(InData[ui.txtITrTask.currentText()])
                    OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask)
                    TeTask = np.asarray(InData[ui.txtITeTask.currentText()])
                    OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask)
                    TrTaskIndex = TrTask.copy()
                    for tasindx, tas in enumerate(np.unique(TrTask)):
                        TrTaskIndex[TrTask == tas] = tasindx + 1
                    TeTaskIndex = TeTask.copy()
                    for tasindx, tas in enumerate(np.unique(TeTask)):
                        TeTaskIndex[TeTask == tas] = tasindx + 1
                except:
                    print("Cannot load Tasks!")
                    return

            # Run
            if ui.cbRun.isChecked():
                if not len(ui.txtITrRun.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeRun.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrRun.text()):
                    msgBox.setText(
                        "Please enter Train Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeRun.text()):
                    msgBox.setText(
                        "Please enter Test Output Run variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrRun = InData[ui.txtITrRun.currentText()]
                    OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun)
                    TeRun = InData[ui.txtITeRun.currentText()]
                    OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun)
                except:
                    print("Cannot load Runs!")
                    return

            # Counter
            if ui.cbCounter.isChecked():
                if not len(ui.txtITrCounter.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeCounter.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrCounter.text()):
                    msgBox.setText(
                        "Please enter Train Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeCounter.text()):
                    msgBox.setText(
                        "Please enter Test Output Counter variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    TrCounter = InData[ui.txtITrCounter.currentText()]
                    OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(
                        TrCounter)
                    TeCounter = InData[ui.txtITeCounter.currentText()]
                    OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(
                        TeCounter)
                except:
                    print("Cannot load Counters!")
                    return

            # Matrix Label
            if ui.cbmLabel.isChecked():
                if not len(ui.txtITrmLabel.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITemLabel.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrmLabel.text()):
                    msgBox.setText(
                        "Please enter Train Output Matrix Label variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTemLabel.text()):
                    msgBox.setText(
                        "Please enter Test Output Matrix Label variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrmLabel.text()] = InData[
                        ui.txtITrmLabel.currentText()]
                    OutData[ui.txtOTemLabel.text()] = InData[
                        ui.txtITemLabel.currentText()]
                except:
                    print("Cannot load matrix lables!")
                    return

            # Design
            if ui.cbDM.isChecked():
                if not len(ui.txtITrDM.currentText()):
                    msgBox.setText(
                        "Please enter Train Input Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeDM.currentText()):
                    msgBox.setText(
                        "Please enter Test Input Design Matrix variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrDM.text()):
                    msgBox.setText(
                        "Please enter Train Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeDM.text()):
                    msgBox.setText(
                        "Please enter Test Output Design Matrix variable name!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrDM.text()] = InData[
                        ui.txtITrDM.currentText()]
                    OutData[ui.txtOTeDM.text()] = InData[
                        ui.txtITeDM.currentText()]
                except:
                    print("Cannot load design matrices!")
                    return

            # Coordinate
            if ui.cbCol.isChecked():
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCol.text()):
                    msgBox.setText("Please enter Coordinator variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCol.text()] = InData[
                        ui.txtCol.currentText()]
                except:
                    print("Cannot load coordinator!")
                    return

            # Condition
            if ui.cbCond.isChecked():
                if not len(ui.txtCond.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOCond.text()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOCond.text()] = InData[
                        ui.txtCond.currentText()]
                except:
                    print("Cannot load conditions!")
                    return

            # FoldID
            if ui.cbFoldID.isChecked():
                if not len(ui.txtFoldID.currentText()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldID.text()):
                    msgBox.setText("Please enter FoldID variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldID.text()] = reshape_1Dvector(
                        InData[ui.txtFoldID.currentText()])
                except:
                    print("Cannot load Fold ID!")
                    return

            # FoldInfo
            if ui.cbFoldInfo.isChecked():
                if not len(ui.txtFoldInfo.currentText()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOFoldInfo.text()):
                    msgBox.setText("Please enter FoldInfo variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOFoldInfo.text()] = InData[
                        ui.txtFoldInfo.currentText()]
                except:
                    print("Cannot load Fold Info!")
                    return
                pass

            # Number of Scan
            if ui.cbNScan.isChecked():
                if not len(ui.txtITrScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtITeScan.currentText()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Input Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTrScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Train!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if not len(ui.txtOTeScan.text()):
                    msgBox.setText(
                        "Please enter Number of Scan variable name for Output Test!"
                    )
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    OutData[ui.txtOTrScan.text()] = reshape_1Dvector(
                        InData[ui.txtITrScan.currentText()])
                    OutData[ui.txtOTeScan.text()] = reshape_1Dvector(
                        InData[ui.txtITeScan.currentText()])
                except:
                    print("Cannot load NScan!")
                    return

            # Train Analysis Level
            print("Calculating Analysis Level for Training Set ...")
            TrGroupFold = None
            FoldStr = ""
            if ui.cbFSubject.isChecked():
                if not ui.rbFRun.isChecked():
                    TrGroupFold = TrSubject
                    FoldStr = "Subject"
                else:
                    TrGroupFold = np.concatenate((TrSubject, TrRun))
                    FoldStr = "Subject+Run"

            if ui.cbFTask.isChecked():
                TrGroupFold = np.concatenate(
                    (TrGroupFold,
                     TrTaskIndex)) if TrGroupFold is not None else TrTaskIndex
                FoldStr = FoldStr + "+Task"

            if ui.cbFCounter.isChecked():
                TrGroupFold = np.concatenate(
                    (TrGroupFold,
                     TrCounter)) if TrGroupFold is not None else TrCounter
                FoldStr = FoldStr + "+Counter"

            TrGroupFold = np.transpose(TrGroupFold)

            TrUniqFold = np.array(
                list(set(tuple(i) for i in TrGroupFold.tolist())))

            TrFoldIDs = np.arange(len(TrUniqFold)) + 1

            TrListFold = list()
            for gfold in TrGroupFold:
                for ufoldindx, ufold in enumerate(TrUniqFold):
                    if (ufold == gfold).all():
                        currentID = TrFoldIDs[ufoldindx]
                        break
                TrListFold.append(currentID)
            TrListFold = np.int32(TrListFold)
            TrListFoldUniq = np.unique(TrListFold)

            # Test Analysis Level
            print("Calculating Analysis Level for Testing Set ...")
            TeGroupFold = None
            if ui.cbFSubject.isChecked():
                if not ui.rbFRun.isChecked():
                    TeGroupFold = TeSubject
                else:
                    TeGroupFold = np.concatenate((TeSubject, TeRun))

            if ui.cbFTask.isChecked():
                TeGroupFold = np.concatenate(
                    (TeGroupFold,
                     TeTaskIndex)) if TeGroupFold is not None else TeTaskIndex

            if ui.cbFCounter.isChecked():
                TeGroupFold = np.concatenate(
                    (TeGroupFold,
                     TeCounter)) if TeGroupFold is not None else TeCounter

            TeGroupFold = np.transpose(TeGroupFold)

            TeUniqFold = np.array(
                list(set(tuple(i) for i in TeGroupFold.tolist())))

            TeFoldIDs = np.arange(len(TeUniqFold)) + 1

            TeListFold = list()
            for gfold in TeGroupFold:
                for ufoldindx, ufold in enumerate(TeUniqFold):
                    if (ufold == gfold).all():
                        currentID = TeFoldIDs[ufoldindx]
                        break
                TeListFold.append(currentID)
            TeListFold = np.int32(TeListFold)
            TeListFoldUniq = np.unique(TeListFold)

            # Train Partition
            print("Partitioning Training Data ...")
            TrX = list()
            TrShape = None
            for foldindx, fold in enumerate(TrListFoldUniq):
                dat = XTr[np.where(TrListFold == fold)]
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    dat = preprocessing.scale(dat)
                    print("Data belong to View " + str(foldindx + 1) +
                          " is scaled X~N(0,1).")

                TrX.append(dat)
                if TrShape is None:
                    TrShape = np.shape(dat)
                else:
                    if not (TrShape == np.shape(dat)):
                        print("ERROR: Train, Reshape problem for Fold " +
                              str(foldindx + 1) + ", Shape: " +
                              str(np.shape(dat)))
                        return
                print("Train: View " + str(foldindx + 1) +
                      " is extracted. Shape: " + str(np.shape(dat)))

            print("Training Shape: " + str(np.shape(TrX)))

            # Test Partition
            print("Partitioning Testing Data ...")
            TeX = list()
            TeShape = None
            for foldindx, fold in enumerate(TeListFoldUniq):
                dat = XTe[np.where(TeListFold == fold)]
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    dat = preprocessing.scale(dat)
                    print("Data belong to View " + str(foldindx + 1) +
                          " is scaled X~N(0,1).")
                TeX.append(dat)
                if TeShape is None:
                    TeShape = np.shape(dat)
                else:
                    if not (TeShape == np.shape(dat)):
                        print("Test: Reshape problem for Fold " +
                              str(foldindx + 1))
                        return
                print("Test: View " + str(foldindx + 1) + " is extracted.")

            print("Testing Shape: " + str(np.shape(TeX)))

            model = RHA(regularization=Regularization, Dim=NumFea)

            print("Running Hyperalignment on Training Data ...")
            model.fit(TrX)
            G = model.get_G()
            TrU = model.get_U()

            print("Running Hyperalignment on Testing Data ...")
            model.project(TeX)
            TeU = model.get_U_tilde()

            # Train Dot Product
            print("Producting Training Data ...")
            TrHX = None
            TrErr = None
            for foldindx, fold in enumerate(TrListFoldUniq):
                mapping = np.dot(TrX[foldindx], TrU[foldindx])
                TrErr = TrErr + (G -
                                 mapping) if TrErr is not None else G - mapping
                TrHX = np.concatenate(
                    (TrHX, mapping)) if TrHX is not None else mapping
            OutData[ui.txtOTrData.text()] = TrHX
            foldindx = foldindx + 1
            TrErr = TrErr / foldindx
            print("Train: alignment error ", np.linalg.norm(TrErr))
            TrFoldErr.append(np.linalg.norm(TrErr))

            # Train Dot Product
            print("Producting Testing Data ...")
            TeHX = None
            TeErr = None
            for foldindx, fold in enumerate(TeListFoldUniq):
                mapping = np.dot(TeX[foldindx], TeU[foldindx])
                TeErr = TeErr + (G -
                                 mapping) if TeErr is not None else G - mapping
                TeHX = np.concatenate(
                    (TeHX, mapping)) if TeHX is not None else mapping
            OutData[ui.txtOTeData.text()] = TeHX
            foldindx = foldindx + 1
            TeErr = TeErr / foldindx
            print("Test: alignment error ", np.linalg.norm(TeErr))
            TeFoldErr.append(np.linalg.norm(TeErr))

            HAParam = dict()
            HAParam["Share"] = G
            HAParam["Train"] = TrU
            HAParam["Test"] = TeU
            HAParam["Level"] = FoldStr
            OutData["FunctionalAlignment"] = HAParam
            OutData["Runtime"] = time.time() - tic
            totalTime += OutData["Runtime"]

            print("Saving ...")
            mainIO_save(OutData, OutFile)
            print("Fold " + str(fold_all) + " is DONE: " + OutFile)

        print("Training -> Alignment Error: mean " + str(np.mean(TrFoldErr)) +
              " std " + str(np.std(TrFoldErr)))
        print("Testing  -> Alignment Error: mean " + str(np.mean(TeFoldErr)) +
              " std " + str(np.std(TeFoldErr)))
        print("Runtime: ", totalTime)
        print("Regularized Hyperalignment is done.")
        msgBox.setText("Regularized Hyperalignment is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #18
0
    def btnConvert_click(self):
        msgBox = QMessageBox()
        print("Loading...")
        # Job
        NumJob = ui.txtJob.value()
        if NumJob < 1:
            NumJob = getCPUCore()
        try:
            FoldFrom = np.int32(ui.txtFoldFrom.text())
            FoldTo = np.int32(ui.txtFoldTo.text())
        except:
            print("Please check fold parameters!")
            return
        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        ResData = dict()
        Coord = None
        CodeText = ui.txtEvents.toPlainText()
        for fold in range(FoldFrom, FoldTo + 1):
            print(f"Analyzing Fold: {fold}...")
            # InFile
            InFile = ui.txtInFile.text()
            if not len(InFile):
                msgBox.setText(f"FOLD {fold}: Please enter input file!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            InFile = InFile.replace("$FOLD$", str(fold))
            if not os.path.isfile(InFile):
                msgBox.setText(f"FOLD {fold}: Input file not found!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # Load InData
            InData = None
            try:
                InData = mainIO_load(InFile)
            except:
                print(f"FOLD {fold}: Cannot load file: {InFile}")
            # Train data
            if not len(ui.txtData.currentText()):
                msgBox.setText(
                    f"FOLD {fold}: Please enter train data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                Xtr = InData[ui.txtData.currentText()]
                if ui.cbScale.isChecked():
                    Xtr = preprocessing.scale(Xtr)
                    print(
                        f"FOLD {fold}: Whole of train data is scaled X~N(0,1)."
                    )
            except:
                print(f"FOLD {fold}: Cannot load train data")
                msgBox.setText(f"FOLD {fold}: Cannot load train data!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # Test data
            if not len(ui.txtTeData.currentText()):
                msgBox.setText(
                    f"FOLD {fold}: Please enter test data variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                Xte = InData[ui.txtTeData.currentText()]
                if ui.cbScale.isChecked():
                    Xte = preprocessing.scale(Xte)
                    print(
                        f"FOLD {fold}: Whole of test data is scaled X~N(0,1).")
            except:
                print(f"FOLD {fold}: Cannot load test data")
                msgBox.setText(f"FOLD {fold}: Cannot load train data!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            # Train Label
            if not len(ui.txtLabel.currentText()):
                msgBox.setText(
                    f"FOLD {fold}: Please enter Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                Ytr = InData[ui.txtLabel.currentText()][0]
            except:
                print(f"FOLD {fold}: Cannot load label")
                return
            # Test Label
            if not len(ui.txtTeLabel.currentText()):
                msgBox.setText(
                    f"FOLD {fold}: Please enter test Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            try:
                Yte = InData[ui.txtTeLabel.currentText()][0]
            except:
                print(f"FOLD {fold}: Cannot load test label")
                return
            # Coordinate
            if Coord is None:
                if not len(ui.txtCol.currentText()):
                    msgBox.setText("Please enter Condition variable name!")
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                try:
                    Coord = np.transpose(InData[ui.txtCol.currentText()])
                    CoordSize = np.shape(Coord)[0]
                except:
                    print("Cannot load coordinate")
                    return
            print(f"FOLD {fold}: analyzing regions...")
            startIndex = 0
            stepIndex = int(CoordSize / NumJob) + 1
            ThreadList = list()
            ThreadID = 1
            while startIndex < CoordSize:
                try:
                    allvars = dict(locals(), **globals())
                    exec(CodeText, allvars, allvars)
                    model = allvars['model']
                except Exception as e:
                    print(f'Cannot generate model\n{e}')
                    msgBox.setText(f'Cannot generate model\n{e}')
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setStandardButtons(QMessageBox.Ok)
                    msgBox.exec_()
                    return False
                if startIndex + stepIndex < CoordSize:
                    endIndex = startIndex + stepIndex
                else:
                    endIndex = CoordSize
                TrainX = Xtr[:, startIndex:endIndex]
                TestX = Xte[:, startIndex:endIndex]
                T = MultiThreadingVectorClassification(TrX=TrainX,
                                                       Try=Ytr,
                                                       TeX=TestX,
                                                       Tey=Yte,
                                                       startIndex=startIndex,
                                                       model=model,
                                                       processID=ThreadID)
                print(
                    "Thread {:5d} is generated for voxels in range from {:15d} to {:15d}. # voxels: {:15d}. # cores: {:5d}. Step size: {:10d}"
                    .format(ThreadID, startIndex, endIndex, CoordSize, NumJob,
                            stepIndex))
                ThreadList.append(T)
                ThreadID += 1
                startIndex += stepIndex
            print(f"FOLD {fold}: running threads...")
            for tt in ThreadList:
                if not tt.is_alive():
                    tt.start()
            # Waiting the process will be finish
            for tt in ThreadList:
                tt.join()
            print(f"FOLD {fold}: finalizing results...")

            for ttIdx, tt in enumerate(ThreadList):
                for res in tt.Results:
                    try:
                        ResData[res[0]] += res[1]
                    except:
                        ResData[res[0]] = res[1]
                print('FOLD {:5d} and thread {:5d} is done.'.format(
                    fold, ttIdx + 1))

        OutData = dict()
        NumFold = FoldTo - FoldFrom + 1
        FinalResult = list()
        FinalAccuracy = list()
        for key in ResData.keys():
            coo = Coord[key, :]
            acc = ResData[key] / NumFold
            FinalResult.append([[coo], [[acc]]])
            FinalAccuracy.append(acc)
        print("Saving ...")
        mainIO_save({"accuracy": reshape_1Dvector(FinalAccuracy),\
                     "results": reshape_1Dvector(FinalResult),\
                     ui.txtCol.currentText():Coord}, \
                    ui.txtOutFile.text())
        # io.savemat(ui.txtOutFile.text(), mdict={"accuracy": FinalAccuracy,
        #                                         "results": FinalResult,
        #                                         ui.txtCol.currentText():Coord})
        print("DONE.")
        msgBox.setText("Wised voxel analysis is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Пример #19
0
 def btnConvert_click(self):
     msgBox = QMessageBox()
     # OutFile
     OutFile = ui.txtOutFile.text()
     if not len(OutFile):
         msgBox.setText("Please enter out file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # InFile
     InFile = ui.txtInFile.text()
     if not len(InFile):
         msgBox.setText("Please enter input file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if not os.path.isfile(InFile):
         msgBox.setText("Input file not found!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     InData = mainIO_load(InFile)
     OutData = dict()
     OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
     # Subject
     if not len(ui.txtSubject.currentText()):
         msgBox.setText("Please enter Subject variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     try:
         Subject = InData[ui.txtSubject.currentText()]
         OutData[ui.txtOSubject.text()] = reshape_1Dvector(Subject)
     except:
         print("Cannot load Subject ID")
         return
     if not len(ui.txtData.currentText()):
         msgBox.setText("Please enter Data variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     try:
         X = InData[ui.txtData.currentText()]
         if not ui.rbScale.isChecked():
             X_new = preprocessing.scale(X)
             print("Whole of data is scaled X~N(0,1).")
         else:
             print("Partition data to subject level ...")
             SubjectUniq = np.unique(Subject)
             X_Sub = list()
             for subj in SubjectUniq:
                 X_Sub.append(preprocessing.scale(X[np.where(Subject == subj)[1], :]))
                 print("Data in subject level is scaled, X_" + str(subj) + "~N(0,1).")
             lenPCA = len(X_Sub)
             print("Data integration ... ")
             X_new = None
             for xsubindx, xsub in enumerate(X_Sub):
                 X_new = np.concatenate((X_new, xsub)) if X_new is not None else xsub
                 print("Integration: ", xsubindx + 1, " of ", lenPCA, " is done.")
         OutData[ui.txtOData.text()] = X_new
     except:
         print("Cannot load data")
         return
     # Label
     if not len(ui.txtLabel.currentText()):
             msgBox.setText("Please enter Label variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
     OutData[ui.txtOLabel.text()] = reshape_1Dvector(InData[ui.txtLabel.currentText()])
     # Task
     if ui.cbTask.isChecked():
         if not len(ui.txtTask.currentText()):
             msgBox.setText("Please enter Task variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOTask.text()] = reshape_1Dvector(InData[ui.txtTask.currentText()])
     # Run
     if ui.cbRun.isChecked():
         if not len(ui.txtRun.currentText()):
             msgBox.setText("Please enter Run variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtORun.text()] = reshape_1Dvector(InData[ui.txtRun.currentText()])
     # Counter
     if ui.cbCounter.isChecked():
         if not len(ui.txtCounter.currentText()):
             msgBox.setText("Please enter Counter variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCounter.text()] = reshape_1Dvector(InData[ui.txtCounter.currentText()])
     # Matrix Label
     if ui.cbmLabel.isChecked():
         if not len(ui.txtmLabel.currentText()):
             msgBox.setText("Please enter Matrix Label variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()]
     # Design
     if ui.cbDM.isChecked():
         if not len(ui.txtDM.currentText()):
             msgBox.setText("Please enter Design Matrix variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]
     # Coordinate
     if ui.cbCol.isChecked():
         if not len(ui.txtCol.currentText()):
             msgBox.setText("Please enter Coordinator variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]
     # Condition
     if ui.cbCond.isChecked():
         if not len(ui.txtCond.currentText()):
             msgBox.setText("Please enter Condition variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
     # Number of Scan
     if ui.cbNScan.isChecked():
         if not len(ui.txtScan.currentText()):
             msgBox.setText("Please enter Number of Scan variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOScan.text()] = reshape_1Dvector(InData[ui.txtScan.currentText()])
     print("Saving ...")
     mainIO_save(OutData, ui.txtOutFile.text())
     #io.savemat(ui.txtOutFile.text(), mdict=OutData)
     print("DONE.")
     msgBox.setText("Normalization is done.")
     msgBox.setIcon(QMessageBox.Information)
     msgBox.setStandardButtons(QMessageBox.Ok)
     msgBox.exec_()
Пример #20
0
 def btnConvert_click(self):
     msgBox = QMessageBox()
     # OutFile
     OutFile = ui.txtOutFile.text()
     if not len(OutFile):
         msgBox.setText("Please enter out file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     # InFile
     InFile = ui.txtInFile.text()
     if not len(InFile):
         msgBox.setText("Please enter input file!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     if not os.path.isfile(InFile):
         msgBox.setText("Input file not found!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     InData = mainIO_load(InFile)
     OutData = dict()
     OutData["dataShape"] = 2
     OutData["imgShape"] = reshape_1Dvector(InData["imgShape"])
     # Data
     if not len(ui.txtData.currentText()):
         msgBox.setText("Please enter Data variable name!")
         msgBox.setIcon(QMessageBox.Critical)
         msgBox.setStandardButtons(QMessageBox.Ok)
         msgBox.exec_()
         return False
     try:
         X = InData[ui.txtData.currentText()]
     except:
         print("Cannot load data")
         return
     # Coordinate
     if ui.cbCol.isChecked():
         if not len(ui.txtCol.currentText()):
             msgBox.setText("Please enter Coordinate variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         try:
             Coor = InData[ui.txtCol.currentText()]
             OutData[ui.txtOCol.text()] = Coor
         except:
             print("Cannot load Coordinate!")
             return
     if ui.rb4DShape.isChecked():
         if not ui.cbCol.isChecked() or not len(ui.txtCol.currentText()):
             msgBox.setText("For using coordinate vectorization, you must set Coordinate variable!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
     # Coordinate_box
     if ui.cbColBox.isChecked():
         if not len(ui.txtColBox.currentText()):
             msgBox.setText("Please enter Coordinate_box variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         try:
             CoorBox = InData[ui.txtColBox.currentText()]
             OutData[ui.txtOColBox.text()] = CoorBox
         except:
             print("Cannot load Coordinate_box!")
             return
     if ui.rb4DShape2.isChecked():
         if not ui.cbColBox.isChecked() or not len(ui.txtColBox.currentText()):
             msgBox.setText("For using coordinate_box vectorication, you must set Coordinate_box variable!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
     # Subject
     if ui.cbSubject.isChecked():
         if not len(ui.txtSubject.currentText()):
             msgBox.setText("Please enter Subject variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOSubject.text()] = reshape_1Dvector(InData[ui.txtSubject.currentText()])
     # Task
     if ui.cbTask.isChecked():
         if not len(ui.txtTask.currentText()):
             msgBox.setText("Please enter Task variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOTask.text()] = reshape_1Dvector(InData[ui.txtTask.currentText()])
     # Run
     if ui.cbRun.isChecked():
         if not len(ui.txtRun.currentText()):
             msgBox.setText("Please enter Run variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtORun.text()] = reshape_1Dvector(InData[ui.txtRun.currentText()])
     # Counter
     if ui.cbCounter.isChecked():
         if not len(ui.txtCounter.currentText()):
             msgBox.setText("Please enter Counter variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCounter.text()] = reshape_1Dvector(InData[ui.txtCounter.currentText()])
     # Label
     if ui.cbLabel.isChecked():
         if not len(ui.txtLabel.currentText()):
                 msgBox.setText("Please enter Label variable name!")
                 msgBox.setIcon(QMessageBox.Critical)
                 msgBox.setStandardButtons(QMessageBox.Ok)
                 msgBox.exec_()
                 return False
         OutData[ui.txtOLabel.text()] = reshape_1Dvector(InData[ui.txtLabel.currentText()])
     # Matrix Label
     if ui.cbmLabel.isChecked():
         if not len(ui.txtmLabel.currentText()):
             msgBox.setText("Please enter Matrix Label variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()]
     # Design
     if ui.cbDM.isChecked():
         if not len(ui.txtDM.currentText()):
             msgBox.setText("Please enter Design Matrix variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]
     # Condition
     if ui.cbCond.isChecked():
         if not len(ui.txtCond.currentText()):
             msgBox.setText("Please enter Condition variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]
     # Number of Scan
     if ui.cbNScan.isChecked():
         if not len(ui.txtScan.currentText()):
             msgBox.setText("Please enter Number of Scan variable name!")
             msgBox.setIcon(QMessageBox.Critical)
             msgBox.setStandardButtons(QMessageBox.Ok)
             msgBox.exec_()
             return False
         OutData[ui.txtOScan.text()] = reshape_1Dvector(InData[ui.txtScan.currentText()])
     timepoints = np.shape(X)[0]
     Coord = Coor
     if ui.rb4DShape2.isChecked():
         Coord = CoorBox
     Xnew = list()
     for i, xi in enumerate(X):
         xi_new = None
         if ui.rb4DShape.isChecked() or ui.rb4DShape2.isChecked():
             for Di, Dj, Dk in Coord.T:
                 xi_new = [xi[Di, Dj, Dk]] if xi_new is None else np.concatenate((xi_new, [xi[Di, Dj, Dk]]))
         else:
             xi_new = np.reshape(xi, -1)
         Xnew.append(xi_new)
         if (i + 1) % 100 == 0:
             print("Time point {:8d} of {:8d} is reshaped!".format(i + 1, timepoints))
     print("Time point {:8d} of {:8d} is reshaped!".format(i + 1, timepoints))
     if ui.cbNormalize.isChecked():
         Xnew = preprocessing.scale(Xnew)
         print("Data is scaled X~N(0,1).")
     OutData[ui.txtOData.text()] = Xnew
     print("Data shape: ", np.shape(Xnew))
     print("Saving ...")
     mainIO_save(OutData, ui.txtOutFile.text())
     print("DONE.")
     msgBox.setText("Data is reshaped.")
     msgBox.setIcon(QMessageBox.Information)
     msgBox.setStandardButtons(QMessageBox.Ok)
     msgBox.exec_()