def btnSave_click(self): global data msgBox = QMessageBox() if len(ui.txtInFile.text()): do_compress = False if can_do_compression(ui.txtInFile.text()): reply = QMessageBox.question(None, 'Data Compress', "Do you like to compress data?", QMessageBox.Yes, QMessageBox.No) do_compress = True if reply == QMessageBox.Yes else False mainIO_save(data, ui.txtInFile.text(), do_compression=do_compress) ui.btnSave.setEnabled(False) print("Data is saved in: ", ui.txtInFile.text()) msgBox.setText("Data is saved") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnSaveAs_click(self): global data msgBox = QMessageBox() if not len(ui.txtInFile.text()): return False ofile = SaveFile("Save MatLab data file ...",['easyX files (*.ezx)', 'MatLab files (*.mat)'],'ezx',\ os.path.dirname(ui.txtInFile.text())) if len(ofile): if len(ui.txtInFile.text()): do_compress = False if can_do_compression(ofile): reply = QMessageBox.question( None, 'Data Compress', "Do you like to compress data?", QMessageBox.Yes, QMessageBox.No) do_compress = True if reply == QMessageBox.Yes else False mainIO_save(data, ofile, do_compression=do_compress) ui.btnSave.setEnabled(False) ui.txtInFile.setText(ofile) print("Data is saved in: ", ui.txtInFile.text()) msgBox.setText("Data is saved") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() tStart = time.time() Method = ui.cbMethod.currentData() LossType = ui.cbLossType.currentData() Optim = ui.cbOptim.currentData() try: Epoch = np.int32(ui.txtIter.text()) except: msgBox.setText("Number of iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: BatchSize = np.int32(ui.txtBatch.text()) except: msgBox.setText("Number of batch is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ReportStep = np.int32(ui.txtReportStep.text()) except: msgBox.setText("Number of Report Step is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: LearningRate = np.float32(ui.txtRate.text()) except: msgBox.setText("Learning rate is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: LassoAlpha = np.float32(ui.txtLParam.text()) except: msgBox.setText("Number of Lasso Parameter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ElasticLambda1 = np.float32(ui.txtEL1.text()) except: msgBox.setText("Number of Elastic Lambda 1 is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ElasticAlpha = np.float32(ui.txtEL2.text()) except: msgBox.setText("Number of Elastic Lambda 2 is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: RidgeReg = np.float32(ui.txtRRP.text()) except: msgBox.setText("Number of Ridge Regression Parameter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False 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 # 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 print("Loading ...") InData = mainIO_load(InFile) # 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 # Design if not len(ui.txtDesign.currentText()): msgBox.setText("Please enter Input Design variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Design = InData[ui.txtDesign.currentText()] except: msgBox.setText("Design 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[ui.txtCond.currentText()] = Cond labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) 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] Design = np.delete(Design, labelIndx, axis=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"] = "PyTorch.Group.Gradient.RSA." + ui.cbMethod.currentText( ) print("Number of all levels is: " + str(len(UniqFold))) Cov = None Corr = None AMSE = list() APer = list() # RSA Method OutData['Method'] = dict() OutData['Method']['Method'] = Method OutData['Method']['LossType'] = LossType OutData['Method']['Optimization'] = Optim OutData['Method']['LearningRate'] = LearningRate OutData['Method']['Epoch'] = Epoch OutData['Method']['BatchSize'] = BatchSize OutData['Method']['ReportStep'] = ReportStep OutData['Method']['RidgeAlpha'] = RidgeReg OutData['Method']['ElaticLambda1'] = ElasticLambda1 OutData['Method']['ElaticAlpha'] = ElasticAlpha OutData['Method']['LassoAlpha'] = LassoAlpha OutData['Method']['Verbose'] = ui.cbVerbose.isChecked() Beta = None for foldID, fold in enumerate(GUFold): print("Analyzing level " + 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]] XLi = X[Index] RegLi = Design[Index] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): XLi = preprocessing.scale(XLi) print("Whole of data is scaled X%d~N(0,1)." % (foldID + 1)) print("Running Gradient RSA ...") rsa = GrRSA(method=Method, loss_type=LossType, optim=Optim, learning_rate=LearningRate, epoch=Epoch, \ batch_size=BatchSize, report_step=ReportStep, ridge_param=RidgeReg, elstnet_l1_ratio=ElasticLambda1, \ elstnet_alpha=ElasticAlpha, lasso_alpha=LassoAlpha, verbose=ui.cbVerbose.isChecked(), \ gpu_enable=ui.cbDevice.currentData(), normalization=False) BetaLi, EpsLi, loss_vec, MSE, Performance, _ = rsa.fit( data_vals=XLi, design_vals=RegLi) OutData["LossVec"] = loss_vec print("Calculating MSE for level %d ..." % (foldID + 1)) print("MSE%d: %f" % (foldID + 1, MSE)) print("Perfromance%d: %f" % (foldID + 1, Performance)) OutData["MSE" + str(foldID)] = MSE OutData["Performance" + str(foldID)] = MSE AMSE.append(MSE) APer.append(Performance) Beta = BetaLi if Beta is None else Beta + BetaLi if ui.cbBeta.isChecked(): OutData["BetaL" + str(foldID + 1)] = BetaLi OutData["EpsL" + str(foldID + 1)] = EpsLi # Calculate Results if ui.cbCorr.isChecked(): print("Calculating Correlation for level %d ..." % (foldID + 1)) CorrLi = np.corrcoef(BetaLi) OutData["Corr" + str(foldID + 1)] = CorrLi if Corr is None: Corr = CorrLi.copy() else: if ui.rbAvg.isChecked(): Corr = np.add(Corr, CorrLi) elif ui.rbMin.isChecked(): Corr = np.minimum(Corr, CorrLi) else: Corr = np.maximum(Corr, CorrLi) if ui.cbCov.isChecked(): print("Calculating Covariance for level %d ..." % (foldID + 1)) CovLi = np.cov(BetaLi) OutData["Cov" + str(foldID + 1)] = CovLi if Cov is None: Cov = CovLi.copy() else: if ui.rbAvg.isChecked(): Cov = np.add(Cov, CovLi) elif ui.rbMin.isChecked(): Cov = np.minimum(Cov, CovLi) else: Cov = np.maximum(Cov, CovLi) if ui.cbCov.isChecked(): if ui.rbAvg.isChecked(): Cov = Cov / len(UniqFold) 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(): if ui.rbAvg.isChecked(): Corr = Corr / len(UniqFold) 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["MSE"] = np.mean(AMSE) OutData["MSE_std"] = np.std(AMSE) OutData["Performance"] = np.mean(APer) OutData["Performance_std"] = np.std(APer) # 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] OutData["DistanceMatrix"] = dis Z = linkage(dis) OutData["Linkage"] = Z print("Average MSE: %f" % (OutData["MSE"])) OutData["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (OutData["RunTime"])) print("Saving results ...") mainIO_save(OutData, OutFile) print("Output is saved.") if ui.cbDiagram.isChecked(): if ui.cbCorr.isChecked(): 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.txtTitleCorr.text()): plt.title(ui.txtTitleCorr.text()) else: plt.title('Group MP Gradient RSA: Correlation\nLevel: ' + FoldStr) plt.show() if ui.cbCov.isChecked(): 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.txtTitleCov.text()): plt.title(ui.txtTitleCov.text()) else: plt.title('Group MP Gradient RSA: Covariance\nLevel: ' + FoldStr) plt.show() fig3 = plt.figure(figsize=(25, 10), ) if len(ui.txtTitleDen.text()): plt.title(ui.txtTitleDen.text()) else: plt.title( 'Group MP Gradient RSA: Similarity Analysis\nLevel: ' + FoldStr) dn = dendrogram(Z, labels=labels, leaf_font_size=FontSize, color_threshold=1) plt.show() print("DONE.") msgBox.setText( "Group Representational Similarity Analysis (RSA) is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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_()
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 # 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("Class filter is wrong!") msgBox.setText("Class filter is wrong!") 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 Fold = list() accuracy = list() precision = list() average_precision = list() f1score = list() recall = list() accuracyTr = list() precisionTr = list() average_precisionTr = list() f1scoreTr = list() recallTr = list() InFileList = list() OutData = dict() OutData["ModelAnalysis"] = "Gaussian Naive Bayes" for fold in range(FoldFrom, FoldTo + 1): # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None else: OutModel = OutModel.replace("$FOLD$", str(fold)) # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold)) InFileList.append(InFile) 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) # 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 # 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 TrX = InData[ui.txtITrData.currentText()] TeX = InData[ui.txtITeData.currentText()] TrL = InData[ui.txtITrLabel.currentText()][0] TeL = InData[ui.txtITeLabel.currentText()][0] # Ref Filter Train if ui.cbFilterTrID.isChecked(): try: # Create content structure for training TrFilterContent = ui.txtFilterTrContent.text() TrFilterContent = TrFilterContent.replace(" ", "") if not len(TrFilterContent): print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TrFilterContent = TrFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TrFilterContent = np.int32(TrFilterContent) # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] if np.shape(TrX)[0] != np.shape(TrF)[0] or np.shape( TrL)[0] != np.shape(TrF)[0]: print( "Shape of reference for training must be the same as data and label" ) msgBox.setText( "Shape of reference for training must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for training is wrong!") msgBox.setText("Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove training set try: print( "Removing training set based on reference. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) for content in TrFilterContent: contIndex = np.where(TrF == content)[0] TrL = np.delete(TrL, contIndex, axis=0) TrX = np.delete(TrX, contIndex, axis=0) TrF = np.delete(TrF, contIndex, axis=0) print(" Content", content, "is removed from training set. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) except Exception as e: print("Cannot filter the training set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the training set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Create content structure for testing TeFilterContent = ui.txtFilterTeContent.text() TeFilterContent = TeFilterContent.replace(" ", "") if not len(TeFilterContent): print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TeFilterContent = TeFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TeFilterContent = np.int32(TeFilterContent) # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] if np.shape(TeX)[0] != np.shape(TeF)[0] or np.shape( TeL)[0] != np.shape(TeF)[0]: print( "Shape of reference for testing must be the same as data and label" ) msgBox.setText( "Shape of reference for testing must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for testing is wrong!") msgBox.setText("Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove testing set try: print( "Removing testing set based on reference. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) for content in TeFilterContent: contIndex = np.where(TeF == content)[0] TeL = np.delete(TeL, contIndex, axis=0) TeX = np.delete(TeX, contIndex, axis=0) TeF = np.delete(TeF, contIndex, axis=0) print(" Content", content, "is removed from testing set. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) except Exception as e: print("Cannot filter the testing set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the testing set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(TrL == fil)[0] TrL = np.delete(TrL, labelIndx, axis=0) TrX = np.delete(TrX, labelIndx, axis=0) # Remove Testing Set labelIndx = np.where(TeL == fil)[0] TeL = np.delete(TeL, labelIndx, axis=0) TeX = np.delete(TeX, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): TrX = preprocessing.scale(TrX) TeX = preprocessing.scale(TeX) print( "Whole of data is scaled Train~N(0,1) and Test~N(0,1)." ) except: print("Cannot load data or label") return # FoldID 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 try: currFID = InData[ui.txtFoldID.currentText()][0][0] Fold.append(currFID) except: print("Cannot load Fold ID!") return try: clf = GaussianNB() print("FoldID = " + str(currFID) + " is training ...") clf.fit(TrX, TrL) if OutModel is not None: joblib.dump(clf, OutModel) print("FoldID = " + str(currFID) + " Model is saved: " + OutModel) print("FoldID = " + str(currFID) + " is testing ...") PeL = clf.predict(TeX) PrL = clf.predict(TrX) OutData["fold" + str(currFID) + "_confusion_matrix"] = confusion_matrix( TeL, PeL, np.unique(TeL)) OutData["fold" + str(currFID) + "_classification_report"] = classification_report( TeL, PeL) print(OutData["fold" + str(currFID) + "_classification_report"]) except Exception as e: print(e) msgBox = QMessageBox() msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if ui.cbAverage.isChecked(): acc = accuracy_score(TeL, PeL) accTr = accuracy_score(TrL, PrL) accuracy.append(acc) accuracyTr.append(accTr) print( "FoldID = {:d}, Average Train {:5.2f} Test {:5.2f}" .format(currFID, accTr * 100, acc * 100)) if ui.cbPrecision.isChecked(): pre = precision_score(TeL, PeL, average=ui.cbPrecisionAvg.currentData()) preTr = precision_score( TrL, PrL, average=ui.cbPrecisionAvg.currentData()) precision.append(pre) precisionTr.append(preTr) print( "FoldID = {:d}, Precision Train {:5.2f} Test {:5.2f}" .format(currFID, preTr * 100, pre * 100)) if ui.cbAPrecision.isChecked(): prA = average_precision_score( TeL, PeL, average=ui.cbAPrecisionAvg.currentData()) prATr = average_precision_score( TrL, PrL, average=ui.cbAPrecisionAvg.currentData()) average_precision.append(prA) average_precisionTr.append(prATr) print( "FoldID = {:d}, Average Precision: Train {:5.2f} Test {:5.2f}" .format(currFID, prATr * 100, prA * 100)) if ui.cbRecall.isChecked(): rec = recall_score(TeL, PeL, average=ui.cbRecallAvg.currentData()) recTr = recall_score(TrL, PrL, average=ui.cbRecallAvg.currentData()) recall.append(rec) recallTr.append(recTr) print( "FoldID = {:d}, Recall: Train {:5.2f} Test {:5.2f}" .format(currFID, recTr * 100, rec * 100)) if ui.cbF1.isChecked(): f1 = f1_score(TeL, PeL, average=ui.cbF1Avg.currentData()) f1Tr = f1_score(TrL, PrL, average=ui.cbF1Avg.currentData()) f1score.append(f1) f1scoreTr.append(f1Tr) print( "FoldID = {:d}, F1: Train {:5.2f} Test {:5.2f}" .format(currFID, f1Tr * 100, f1 * 100)) print("FoldID = " + str(currFID) + " is analyzed!") if ui.cbAverage.isChecked(): OutData["FoldAccuracy"] = accuracy MeanAcc = np.mean(accuracy) OutData["MeanTestAccuracy"] = MeanAcc STDAcc = np.std(accuracy) OutData["StdTestAccuracy"] = STDAcc MeanAccTr = np.mean(accuracyTr) OutData["MeanTrainAccuracy"] = MeanAccTr STDAccTr = np.std(accuracyTr) OutData["StdTrainAccuracy"] = STDAccTr print( "Accuracy: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAccTr * 100, STDAccTr, MeanAcc * 100, STDAcc)) if ui.cbPrecision.isChecked(): OutData["ModePrecision"] = ui.cbPrecisionAvg.currentText() OutData["FoldPrecision"] = precision MeanPre = np.mean(precision) OutData["MeanTrainPrecision"] = MeanPre STDPre = np.std(precision) OutData["StdTrainPrecision"] = STDPre MeanPreTr = np.mean(precisionTr) OutData["MeanTestPrecision"] = MeanPreTr STDPreTr = np.std(precisionTr) OutData["StdTestPrecision"] = STDPreTr print( "Precision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanPreTr * 100, STDPreTr, MeanPre * 100, STDPre)) if ui.cbAPrecision.isChecked(): OutData["ModeAveragePrecision"] = ui.cbAPrecisionAvg.currentText() OutData["FoldAveragePrecision"] = average_precision MeanAPre = np.mean(average_precision) OutData["MeanTrainAveragePrecision"] = MeanAPre STDAPre = np.std(average_precision) OutData["StdTestAveragePrecision"] = STDAPre MeanAPreTr = np.mean(average_precisionTr) OutData["MeanTrainAveragePrecision"] = MeanAPreTr STDAPreTr = np.std(average_precisionTr) OutData["StdTrainAveragePrecision"] = STDAPreTr print( "AveragePrecision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAPreTr * 100, STDAPreTr, MeanAPre * 100, STDAPre)) if ui.cbRecall.isChecked(): OutData["ModeRecall"] = ui.cbRecallAvg.currentText() OutData["FoldRecall"] = recall MeanRec = np.mean(recall) OutData["MeanTestRecall"] = MeanRec STDRec = np.std(recall) OutData["StdTestRecall"] = STDRec MeanRecTr = np.mean(recallTr) OutData["MeanTrainRecall"] = MeanRecTr STDRecTr = np.std(recallTr) OutData["StdTrainRecall"] = STDRecTr print( "Recall: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanRecTr * 100, STDRecTr, MeanRec * 100, STDRec)) if ui.cbF1.isChecked(): OutData["ModeF1"] = ui.cbF1Avg.currentText() OutData["FoldF1"] = f1score MeanF1 = np.mean(f1score) OutData["MeanTestF1"] = MeanF1 STDF1 = np.std(f1score) OutData["StdTestF1"] = STDF1 MeanF1Tr = np.mean(f1scoreTr) OutData["MeanTrainF1"] = MeanF1Tr STDF1Tr = np.std(f1scoreTr) OutData["StdTrainF1"] = STDF1Tr print( "F1: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanF1Tr * 100, STDF1Tr, MeanF1 * 100, STDF1)) OutData["InputFiles"] = InFileList print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Gaussian Naive Bayes is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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_()
def btnConvert_click(self): msgBox = QMessageBox() # CType CType = ui.cbCType.currentData() # IParams IParams = ui.cbIParams.currentData() # Warm Start WStart = ui.cbWarmStart.isChecked() # NCluster try: NComponent = np.int32(ui.txtNComponent.text()) except: msgBox.setText("Number of Component is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # NInit try: NInit = np.int32(ui.txtNInit.text()) except: msgBox.setText("Number of init 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 # MaxIter try: MaxIter = np.int32(ui.txtMaxIter.text()) except: msgBox.setText("Max Iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Regularization try: Regularization = np.float(ui.txtRegularization.text()) except: msgBox.setText("Regularization is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Verbose try: Verbose = np.int(ui.txtVerbose.text()) except: msgBox.setText("Verbose is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # IVerbose try: IVerbose = np.int(ui.txtIVerbose.text()) except: msgBox.setText("Verbose Interval is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # 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() OutData["ModelAnalysis"] = "Gaussian Mixture" # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None # 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) # Data if not len(ui.txtData.currentText()): msgBox.setText("Please enter Input Train 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 Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False X = InData[ui.txtData.currentText()] L = InData[ui.txtLabel.currentText()][0] try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(L == fil)[0] L = np.delete(L, labelIndx, axis=0) X = np.delete(X, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): X = preprocessing.scale(X) print("Whole of data is scaled to N(0,1).") except: print("Cannot load data or label") return try: cls = GaussianMixture(n_components=NComponent,covariance_type=CType,tol=Tol,reg_covar=Regularization,\ max_iter=MaxIter,n_init=NInit,init_params=IParams,\ warm_start=WStart,verbose=Verbose,verbose_interval=IVerbose) print("Run Clustering ...") cls.fit(X) PeL = cls.predict(X) except Exception as e: print(e) msgBox = QMessageBox() msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return OutData["predict"] = PeL if OutModel is not None: joblib.dump(cls, OutModel) print("Model is saved: " + OutModel) if ui.cbAverage.isChecked(): acc = accuracy_score(L, PeL) OutData["Accuracy"] = acc print("Average {:5.2f}".format(acc * 100)) if ui.cbNMI.isChecked(): NMI = normalized_mutual_info_score(L, PeL) OutData["NMI"] = NMI print("Normalized Mutual Information (NMI) {:7.6f}".format(NMI)) if ui.cbRIA.isChecked(): RIA = adjusted_rand_score(L, PeL) OutData["RIA"] = RIA print("Rand Index Adjusted (RIA) {:7.6f}".format(RIA)) if ui.cbAMI.isChecked(): AMI = adjusted_mutual_info_score(L, PeL) OutData["AMI"] = AMI print("Adjusted Mutual Information (AMI) {:7.6f}".format(AMI)) print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Gaussian Mixture Clustering is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() tStart = time.time() try: Threshold = np.float(ui.txtThre.text()) assert Threshold <= 1 assert Threshold >= 0 except: msgBox.setText("Threshold is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: EdgeThreshold = np.float(ui.txtEdgeThre.text()) assert EdgeThreshold <= 1 assert EdgeThreshold >= 0 except: msgBox.setText("Edge threshold is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: MetricCode = ui.txtMetric.toPlainText() allvars = dict(locals(), **globals()) exec(MetricCode, allvars, allvars) Metric = allvars['metric'] except Exception as e: msgBox.setText("Metric is wrong!\n" + str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: IntegrationCode = ui.txtIntegration.toPlainText() allvars = dict(locals(), **globals()) exec(IntegrationCode, allvars, allvars) Integration = allvars['integration'] except Exception as e: msgBox.setText("Integration is wrong!\n" + str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False AtlasPath = ui.txtAtlasPath.text() # 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 # Region Filter try: RegionFilter = ui.txtRegions.text() if not len(RegionFilter): RegionFilter = [] else: RegionFilter = RegionFilter.replace("\'", " ").replace( ",", " ").replace("[", "").replace("]", "").split() RegionFilter = np.int32(RegionFilter) except: print("Region 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() # AtlasFile AtlasFile = ui.txtAtlasFile.text() if not len(AtlasFile): msgBox.setText("Please enter atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(AtlasFile): msgBox.setText("Atlas file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: AtlasHDR = nb.load(AtlasFile) AtlasImg = np.asanyarray(AtlasHDR.dataobj) AtlasReg = np.unique(AtlasImg) if not 0 in RegionFilter: AtlasReg = AtlasReg[np.where(AtlasReg != 0)[0]] print("Region 0 is considered as rest mode!") AtlasShape = np.shape(AtlasImg) except: msgBox.setText("Cannot load atlas 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: print("Loading ...") InData = mainIO_load(InFile) imgShape = InData["imgShape"][0] except: print("Cannot load data!") return try: assert imgShape[0] == AtlasShape[0] assert imgShape[1] == AtlasShape[1] assert imgShape[2] == AtlasShape[2] except: msgBox.setText("Input file (" + str(imgShape).replace("]", "").replace("[", "") + ") and Atlas file " + str(AtlasShape).replace(",", "") + " must have the same shape!") 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 # 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 # Condition try: if not len(ui.txtCond.currentText()): Cond = 0 else: Cond = InData[ui.txtCond.currentText()] OutData[ui.txtCond.currentText()] = Cond labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) labels = np.array(labels) except: msgBox.setText("Condition value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: X = InData[ui.txtData.currentText()] L = InData[ui.txtLabel.currentText()][0] if ui.cbScale.isChecked(): X = preprocessing.scale(X) print("Whole of data is scaled X~N(0,1).") except: print("Cannot load data or label") return try: if len(ui.txtCoord.currentText()): Coord = np.transpose(InData[ui.txtCoord.currentText()]) else: Coord = None except: msgBox.setText("Coordinate variable 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) print("Class ID = " + str(fil) + " is removed from data.") Net, ThrNet, ActiveRegions, A, ACoord, listL = ClassicNetworkAnalysis( X=X, L=L, Coord=Coord, Integration=Integration, Metric=Metric, AtlasImg=AtlasImg, affine=AtlasHDR.affine, KeepRegions=RegionFilter, AtlasPath=AtlasPath, Threshold=Threshold) Out = {} Out["Networks"] = Net Out["ThresholdNetworks"] = ThrNet Out["ActiveRegions"] = ActiveRegions Out["Atlas"] = A Out["Atlas_parcellation"] = ACoord Out["Atlas_affine"] = AtlasHDR.affine Out["condition"] = Cond Out["labels"] = listL Out["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (Out["RunTime"])) print("Saving results ...") mainIO_save(Out, OutFile) print("Output is saved.") if ui.cbDiagram.isChecked(): for nnIndex, (nn, tnn) in enumerate(zip(Net, ThrNet)): try: Title = f"Label: {reshape_condition_cell(Cond[nnIndex][1])}" except: Title = f"Label: {nnIndex}" PlotConnectome(tnn, ACoord, Title, EdgeThreshold) fig = plt.figure() plt.imshow(np.transpose(nn)) plt.title(Title) plt.show() print("DONE.") msgBox.setText("Network analysis is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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 = ui.cbMethod.currentData() NuregMethod = ui.cbNuregMethod.currentData() Tau2Prior = ui.cbTau2Prior.currentData() SNRPrior = ui.cbSNRPrior.currentData() GPS = ui.cbGBS.isChecked() GPI = ui.cbGPI.isChecked() BaselineSingle = ui.cbBaselineSingle.isChecked() AutoNuisance = ui.cbAutoNuisance.isChecked() NuregZscore = ui.cbNuregZscore.isChecked() try: miter = np.int(ui.txtMaxIter.text()) assert miter >= 1, None except: msgBox.setText("Max Iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: iiter = np.int(ui.txtInitIter.text()) assert iiter >= 1, None except: msgBox.setText("Init Iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Speed = np.int(ui.txtAnnealSpeed.text()) assert Speed >= 1, None except: msgBox.setText("Anneal speed is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: rank = np.int(ui.txtRank.text()) assert rank >= 0, None if rank == 0: rank = None except: msgBox.setText("Rank is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: NumReg = np.int(ui.txtNumReg.text()) assert NumReg >= 0, None if NumReg == 0: NumReg = None except: msgBox.setText("Number of Reg is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: SNRBin = np.int(ui.txtSNRBins.text()) assert SNRBin > 0, None except: msgBox.setText("SNR bin is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: rhoBin = np.int(ui.txtRhoBins.text()) assert rhoBin > 0, None except: msgBox.setText("Rho bin is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False 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 try: eta = np.float(ui.txtEta.text()) except: msgBox.setText("Eta is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: LogSRange = np.float(ui.txtLogSRange.text()) except: msgBox.setText("LogS range is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: SpaceSmoothRange = np.float(ui.txtSpaceSmoothRange.text()) if SpaceSmoothRange == 0: SpaceSmoothRange = None except: msgBox.setText("Space Smooth Range is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: IntenSmoothRange = np.float(ui.txtIntenSmoothRange.text()) if IntenSmoothRange == 0: IntenSmoothRange = None except: msgBox.setText("Inten Smooth Range is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Tau = np.float(ui.txtTauRange.text()) except: msgBox.setText("Eta is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: CodeText = ui.txtEvents.toPlainText() allvars = dict(locals(), **globals()) exec(CodeText, allvars, allvars) Optimizer = allvars['optimizer'] MinimizeOptions = allvars['minimize_options'] except Exception as e: msgBox.setText("Optimizer is wrong!\n" + str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # 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 print("Loading ...") InData = mainIO_load(InFile) # 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 # Design if not len(ui.txtDesign.currentText()): msgBox.setText("Please enter Input Design variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Design = InData[ui.txtDesign.currentText()] except: msgBox.setText("Design 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[ui.txtCond.currentText()] = Cond labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) 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()] Intensity = np.mean(X, axis=0) 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 try: if len(ui.txtCoord.currentText()): Coord = np.transpose(InData[ui.txtCoord.currentText()]) else: Coord = None except: msgBox.setText("Coordinate variable 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] Design = np.delete(Design, labelIndx, axis=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"] = "SK.Group.RSA." + ui.cbMethod.currentText() print("Number of all levels is: " + str(len(UniqFold))) Cov = None Corr = None AMSE = list() Beta = None for foldID, fold in enumerate(GUFold): print("Analyzing level " + 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]] XLi = X[Index] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): XLi = preprocessing.scale(XLi) print("Whole of data is scaled X%d~N(0,1)." % (foldID + 1)) #RegLi = np.insert(Design[Index], 0, 1, axis=1) RegLi = Design[Index] try: if Method == "brsa": model = BRSA(n_iter=miter, rank=rank, auto_nuisance=AutoNuisance, n_nureg=NumReg, nureg_zscore=NuregZscore, nureg_method=NuregMethod, baseline_single=BaselineSingle, GP_space=GPS, GP_inten=GPI, space_smooth_range=SpaceSmoothRange, inten_smooth_range=IntenSmoothRange, tau_range=Tau, tau2_prior=Tau2Prior, eta=eta, init_iter=iiter, anneal_speed=Speed, tol=tol, optimizer=Optimizer, minimize_options=MinimizeOptions) model.fit(XLi, RegLi, coords=Coord, inten=Intensity) else: model = GBRSA(n_iter=miter, rank=rank, auto_nuisance=AutoNuisance, n_nureg=NumReg, nureg_zscore=NuregZscore, nureg_method=NuregMethod, baseline_single=BaselineSingle, tol=tol, anneal_speed=Speed, SNR_prior=SNRPrior, logS_range=LogSRange, SNR_bins=SNRBin, rho_bins=rhoBin, optimizer=Optimizer, minimize_options=MinimizeOptions) model.fit(XLi, RegLi) except Exception as e: msgBox.setText(str(e)) print(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False BetaLi = model.beta_ Beta = BetaLi if Beta is None else Beta + BetaLi print("Calculating MSE for level %d ..." % (foldID + 1)) MSE = mean_squared_error(XLi, np.matmul(RegLi, BetaLi)) print("MSE%d: %f" % (foldID + 1, MSE)) OutData["MSE" + str(foldID)] = MSE AMSE.append(MSE) if ui.cbBeta.isChecked(): OutData["BetaL" + str(foldID + 1)] = BetaLi # Calculate Results if ui.cbCorr.isChecked(): print("Calculating Correlation for level %d ..." % (foldID + 1)) CorrLi = np.corrcoef(BetaLi) OutData["Corr" + str(foldID + 1)] = CorrLi if Corr is None: Corr = CorrLi.copy() else: if ui.rbAvg.isChecked(): Corr = np.add(Corr, CorrLi) elif ui.rbMin.isChecked(): Corr = np.minimum(Corr, CorrLi) else: Corr = np.maximum(Corr, CorrLi) if ui.cbCov.isChecked(): print("Calculating Covariance for level %d ..." % (foldID + 1)) CovLi = np.cov(BetaLi) OutData["Cov" + str(foldID + 1)] = CovLi if Cov is None: Cov = CovLi.copy() else: if ui.rbAvg.isChecked(): Cov = np.add(Cov, CovLi) elif ui.rbMin.isChecked(): Cov = np.minimum(Cov, CovLi) else: Cov = np.maximum(Cov, CovLi) CoEff = len(UniqFold) - 1 if len(UniqFold) > 2 else 1 if ui.cbCov.isChecked(): if ui.rbAvg.isChecked(): Cov = Cov / CoEff 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(): if ui.rbAvg.isChecked(): Corr = Corr / CoEff 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() # 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] OutData["DistanceMatrix"] = dis Z = linkage(dis) OutData["Linkage"] = Z OutData["MSE"] = np.mean(AMSE) print("Average MSE: %f" % (OutData["MSE"])) OutData["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (OutData["RunTime"])) print("Saving results ...") mainIO_save(OutFile, OutData) print("Output is saved.") if ui.cbDiagram.isChecked(): if ui.cbCorr.isChecked(): 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=ui.txtXRotation.value()) ax.set_yticklabels(labels, minor=False, fontsize=FontSize, rotation=ui.txtYRotation.value()) 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('Group RSA: Correlation\nLevel: ' + FoldStr) plt.show() if ui.cbCov.isChecked(): 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=ui.txtXRotation.value()) ax.set_yticklabels(labels, minor=False, fontsize=FontSize, rotation=ui.txtYRotation.value()) 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('Group RSA: Covariance\nLevel: ' + FoldStr) plt.show() fig3 = plt.figure(figsize=(25, 10), ) if len(ui.txtTitleDen.text()): plt.title(ui.txtTitleDen.text()) else: plt.title('Group MP Gradient RSA: Similarity Analysis\nLevel: ' + FoldStr) dn = dendrogram(Z, labels=labels, leaf_font_size=FontSize, color_threshold=1, leaf_rotation=ui.txtXRotation.value()) plt.show() print("DONE.") msgBox.setText("Group Representational Similarity Analysis (RSA) is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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_()
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_()
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_()
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_()
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_()
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_()
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_()
def btnConvert_click(self): msgBox = QMessageBox() print("Loading...") 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 # Atlas InAtlas = ui.txtAtlas.text() if not len(InAtlas): msgBox.setText("Please enter atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InAtlas): msgBox.setText("Atlas file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: AtlasHdr = nb.load(InAtlas) AtlasImg = np.asanyarray(AtlasHdr.dataobj) AtlasShape = np.shape(AtlasImg) if np.shape(AtlasShape)[0] != 3: msgBox.setText("Atlas must be 3D image") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("Cannot load atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # 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("Class Filter is wrong!") msgBox.setText("Class filter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return MappingVectorRegion = None CodeText = ui.txtEvents.toPlainText() eCodeText = ui.txtEvents2.toPlainText() Fold = list() accuracy = list() precision = list() average_precision = list() f1score = list() recall = list() accuracyTr = list() precisionTr = list() average_precisionTr = list() f1scoreTr = list() recallTr = list() InFileList = list() OutData = dict() OutData["ModelAnalysis"] = "Atlas-based ensemble" 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)) InFileList.append(InFile) 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 try: InData = mainIO_load(InFile) except: print(f"FOLD {fold}: Cannot load file: {InFile}") return False # 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 # 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 try: TrX = InData[ui.txtITrData.currentText()] 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 try: TeX = InData[ui.txtITeData.currentText()] 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 try: TrL = InData[ui.txtITrLabel.currentText()][0] except: print(f"FOLD {fold}: Cannot load label") return try: TeL = InData[ui.txtITeLabel.currentText()][0] except: print(f"FOLD {fold}: Cannot load test label") return # Ref Filter Train if ui.cbFilterTrID.isChecked(): try: # Create content structure for training TrFilterContent = ui.txtFilterTrContent.text() TrFilterContent = TrFilterContent.replace(" ", "") if not len(TrFilterContent): print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TrFilterContent = TrFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TrFilterContent = np.int32(TrFilterContent) # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] if np.shape(TrX)[0] != np.shape(TrF)[0] or np.shape( TrL)[0] != np.shape(TrF)[0]: print( "Shape of reference for training must be the same as data and label" ) msgBox.setText( "Shape of reference for training must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for training is wrong!") msgBox.setText("Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove training set try: print( "Removing training set based on reference. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) for content in TrFilterContent: contIndex = np.where(TrF == content)[0] TrL = np.delete(TrL, contIndex, axis=0) TrX = np.delete(TrX, contIndex, axis=0) TrF = np.delete(TrF, contIndex, axis=0) print(" Content", content, "is removed from training set. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) except Exception as e: print("Cannot filter the training set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the training set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Create content structure for testing TeFilterContent = ui.txtFilterTeContent.text() TeFilterContent = TeFilterContent.replace(" ", "") if not len(TeFilterContent): print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TeFilterContent = TeFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TeFilterContent = np.int32(TeFilterContent) # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] if np.shape(TeX)[0] != np.shape(TeF)[0] or np.shape( TeL)[0] != np.shape(TeF)[0]: print( "Shape of reference for testing must be the same as data and label" ) msgBox.setText( "Shape of reference for testing must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for testing is wrong!") msgBox.setText("Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove testing set try: print( "Removing testing set based on reference. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) for content in TeFilterContent: contIndex = np.where(TeF == content)[0] TeL = np.delete(TeL, contIndex, axis=0) TeX = np.delete(TeX, contIndex, axis=0) TeF = np.delete(TeF, contIndex, axis=0) print(" Content", content, "is removed from testing set. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) except Exception as e: print("Cannot filter the testing set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the testing set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(TrL == fil)[0] TrL = np.delete(TrL, labelIndx, axis=0) TrX = np.delete(TrX, labelIndx, axis=0) # Remove Testing Set labelIndx = np.where(TeL == fil)[0] TeL = np.delete(TeL, labelIndx, axis=0) TeX = np.delete(TeX, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): TrX = preprocessing.scale(TrX) TeX = preprocessing.scale(TeX) print( "Whole of data is scaled Train~N(0,1) and Test~N(0,1)." ) except: print("Cannot load data or label") return # FoldID 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 try: currFID = InData[ui.txtFoldID.currentText()][0][0] Fold.append(currFID) except: print("Cannot load Fold ID!") return # Creating Mapping Dictionary for Atlas if MappingVectorRegion is None: # Coordinate 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()]) except: print("Cannot load coordinate") return # imgShape if not len(ui.txtImg.currentText()): msgBox.setText("Please enter Image Shape variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: DataShape = tuple(InData[ui.txtImg.currentText()][0]) except: print("Cannot load data image shape") return # Check shape if AtlasShape[0] != DataShape[0] or AtlasShape[1] != DataShape[ 1] or AtlasShape[2] != DataShape[2]: print( f'Atlas and data must have the same shape.\nAtlas: {AtlasShape} vs. Data: {DataShape}' ) msgBox.setText( f'Atlas and data must have the same shape.\nAtlas: {AtlasShape} vs. Data: {DataShape}' ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False print("Mapping 2D vector space to 3D region area...") MappingVectorRegion = dict() for cooIdx, coo in enumerate(Coord): reg = AtlasImg[coo[0], coo[1], coo[2]] if reg == 0: print(f'{tuple(coo)} is belong to black area.') else: try: MappingVectorRegion[str(reg)].append(cooIdx) except: MappingVectorRegion[str(reg)] = list() MappingVectorRegion[str(reg)].append(cooIdx) print( f'{tuple(coo)}:{cooIdx} is belong to region {reg}') print(f"FOLD {fold}: analyzing regions...") baseTrX = list() baseTeX = list() for reg in sorted(MappingVectorRegion.keys()): RegIndex = MappingVectorRegion[reg] SubXtr = TrX[:, RegIndex] SubXte = TeX[:, RegIndex] try: allvars = dict(locals(), **globals()) exec(CodeText, allvars, allvars) model = allvars['model'] model.fit(SubXtr, TrL) baseTrX.append(model.predict(SubXtr)) TeP = model.predict(SubXte) baseTeX.append(TeP) bacc = accuracy_score(TeL, TeP) 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 print(f"FOLD {fold}: Linear accuracy for {reg} is {bacc}") baseTrX = np.transpose(baseTrX) baseTeX = np.transpose(baseTeX) try: print(f"FOLD {fold}: Creating ensemble model ...") allvars = dict(locals(), **globals()) exec(eCodeText, allvars, allvars) emodel = allvars['emodel'] emodel.fit(baseTrX, TrL) PrL = emodel.predict(baseTrX) PeL = emodel.predict(baseTeX) except Exception as e: print(f"Cannot generate the final model\n{e}") msgBox.setText(f"Cannot generate the final model\n{e}") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if ui.cbAverage.isChecked(): acc = accuracy_score(TeL, PeL) accTr = accuracy_score(TrL, PrL) accuracy.append(acc) accuracyTr.append(accTr) print( "FoldID = {:d}, Average Train {:5.2f} Test {:5.2f}" .format(currFID, accTr * 100, acc * 100)) if ui.cbPrecision.isChecked(): pre = precision_score(TeL, PeL, average=ui.cbPrecisionAvg.currentData()) preTr = precision_score( TrL, PrL, average=ui.cbPrecisionAvg.currentData()) precision.append(pre) precisionTr.append(preTr) print( "FoldID = {:d}, Precision Train {:5.2f} Test {:5.2f}" .format(currFID, preTr * 100, pre * 100)) if ui.cbAPrecision.isChecked(): prA = average_precision_score( TeL, PeL, average=ui.cbAPrecisionAvg.currentData()) prATr = average_precision_score( TrL, PrL, average=ui.cbAPrecisionAvg.currentData()) average_precision.append(prA) average_precisionTr.append(prATr) print( "FoldID = {:d}, Average Precision: Train {:5.2f} Test {:5.2f}" .format(currFID, prATr * 100, prA * 100)) if ui.cbRecall.isChecked(): rec = recall_score(TeL, PeL, average=ui.cbRecallAvg.currentData()) recTr = recall_score(TrL, PrL, average=ui.cbRecallAvg.currentData()) recall.append(rec) recallTr.append(recTr) print( "FoldID = {:d}, Recall: Train {:5.2f} Test {:5.2f}" .format(currFID, recTr * 100, rec * 100)) if ui.cbF1.isChecked(): f1 = f1_score(TeL, PeL, average=ui.cbF1Avg.currentData()) f1Tr = f1_score(TrL, PrL, average=ui.cbF1Avg.currentData()) f1score.append(f1) f1scoreTr.append(f1Tr) print( "FoldID = {:d}, F1: Train {:5.2f} Test {:5.2f}" .format(currFID, f1Tr * 100, f1 * 100)) print("FoldID = " + str(currFID) + " is analyzed!") if ui.cbAverage.isChecked(): OutData["FoldAccuracy"] = accuracy MeanAcc = np.mean(accuracy) OutData["MeanTestAccuracy"] = MeanAcc STDAcc = np.std(accuracy) OutData["StdTestAccuracy"] = STDAcc MeanAccTr = np.mean(accuracyTr) OutData["MeanTrainAccuracy"] = MeanAccTr STDAccTr = np.std(accuracyTr) OutData["StdTrainAccuracy"] = STDAccTr print( "Accuracy: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAccTr * 100, STDAccTr, MeanAcc * 100, STDAcc)) if ui.cbPrecision.isChecked(): OutData["ModePrecision"] = ui.cbPrecisionAvg.currentText() OutData["FoldPrecision"] = precision MeanPre = np.mean(precision) OutData["MeanTrainPrecision"] = MeanPre STDPre = np.std(precision) OutData["StdTrainPrecision"] = STDPre MeanPreTr = np.mean(precisionTr) OutData["MeanTestPrecision"] = MeanPreTr STDPreTr = np.std(precisionTr) OutData["StdTestPrecision"] = STDPreTr print( "Precision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanPreTr * 100, STDPreTr, MeanPre * 100, STDPre)) if ui.cbAPrecision.isChecked(): OutData["ModeAveragePrecision"] = ui.cbAPrecisionAvg.currentText() OutData["FoldAveragePrecision"] = average_precision MeanAPre = np.mean(average_precision) OutData["MeanTrainAveragePrecision"] = MeanAPre STDAPre = np.std(average_precision) OutData["StdTestAveragePrecision"] = STDAPre MeanAPreTr = np.mean(average_precisionTr) OutData["MeanTrainAveragePrecision"] = MeanAPreTr STDAPreTr = np.std(average_precisionTr) OutData["StdTrainAveragePrecision"] = STDAPreTr print( "AveragePrecision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAPreTr * 100, STDAPreTr, MeanAPre * 100, STDAPre)) if ui.cbRecall.isChecked(): OutData["ModeRecall"] = ui.cbRecallAvg.currentText() OutData["FoldRecall"] = recall MeanRec = np.mean(recall) OutData["MeanTestRecall"] = MeanRec STDRec = np.std(recall) OutData["StdTestRecall"] = STDRec MeanRecTr = np.mean(recallTr) OutData["MeanTrainRecall"] = MeanRecTr STDRecTr = np.std(recallTr) OutData["StdTrainRecall"] = STDRecTr print( "Recall: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanRecTr * 100, STDRecTr, MeanRec * 100, STDRec)) if ui.cbF1.isChecked(): OutData["ModeF1"] = ui.cbF1Avg.currentText() OutData["FoldF1"] = f1score MeanF1 = np.mean(f1score) OutData["MeanTestF1"] = MeanF1 STDF1 = np.std(f1score) OutData["StdTestF1"] = STDF1 MeanF1Tr = np.mean(f1scoreTr) OutData["MeanTrainF1"] = MeanF1Tr STDF1Tr = np.std(f1scoreTr) OutData["StdTrainF1"] = STDF1Tr print( "F1: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanF1Tr * 100, STDF1Tr, MeanF1 * 100, STDF1)) OutData["InputFiles"] = InFileList print("Saving ...") mainIO_save(OutData, ui.txtOutFile.text()) print("DONE.") msgBox.setText("Atlas-based ensemble analysis is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() print("Loading...") 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 # Atlas InAtlas = ui.txtAtlas.text() if not len(InAtlas): msgBox.setText("Please enter atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InAtlas): msgBox.setText("Atlas file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: AtlasHdr = nb.load(InAtlas) AtlasImg = np.asanyarray(AtlasHdr.dataobj) #AtlasImg = AtlasHdr.get_data() AtlasShape = np.shape(AtlasImg) if np.shape(AtlasShape)[0] != 3: msgBox.setText("Atlas must be 3D image") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("Cannot load atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData = dict() MappingVectorRegion = 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 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 if MappingVectorRegion is None: # Coordinate 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()]) except: print("Cannot load coordinate") return # imgShape if not len(ui.txtImg.currentText()): msgBox.setText("Please enter Image Shape variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: DataShape = tuple(InData[ui.txtImg.currentText()][0]) except: print("Cannot load data image shape") return # Check shape if AtlasShape[0] != DataShape[0] or AtlasShape[1] != DataShape[ 1] or AtlasShape[2] != DataShape[2]: print( f'Atlas and data must have the same shape.\nAtlas: {AtlasShape} vs. Data: {DataShape}' ) msgBox.setText( f'Atlas and data must have the same shape.\nAtlas: {AtlasShape} vs. Data: {DataShape}' ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False print("Mapping 2D vector space to 3D region area...") MappingVectorRegion = dict() for cooIdx, coo in enumerate(Coord): reg = AtlasImg[coo[0], coo[1], coo[2]] if reg == 0: print(f'{tuple(coo)} is belong to black area.') else: try: MappingVectorRegion[str(reg)].append(cooIdx) except: MappingVectorRegion[str(reg)] = list() MappingVectorRegion[str(reg)].append(cooIdx) print( f'{tuple(coo)}:{cooIdx} is belong to region {reg}') print(f"FOLD {fold}: analyzing regions...") for reg in sorted(MappingVectorRegion.keys()): RegIndex = MappingVectorRegion[reg] SubXtr = Xtr[:, RegIndex] SubXte = Xte[:, RegIndex] try: allvars = dict(locals(), **globals()) exec(CodeText, allvars, allvars) model = allvars['model'] model.fit(SubXtr, Ytr) Pte = model.predict(SubXte) acc = accuracy_score(Yte, Pte) clr = classification_report(Yte, Pte) cfm = confusion_matrix(Yte, Pte) pre = precision_score(Yte, Pte) f1 = f1_score(Yte, Pte) rca = recall_score(Yte, Pte) 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 try: OutData[f"Region_{reg}_accuracy_score"].append(acc) OutData[f"Region_{reg}_count"] += 1 OutData[f"Region_{reg}_classification_report"].append(clr) OutData[f"Region_{reg}_precision_score"].append(pre) OutData[f"Region_{reg}_f1_score"].append(f1) OutData[f"Region_{reg}_recall_score"].append(rca) OutData[f"Region_{reg}_confusion_matrix"].append(cfm) except: OutData[f"Region_{reg}_count"] = 1 OutData[f"Region_{reg}_accuracy_score"] = list() OutData[f"Region_{reg}_accuracy_score"].append(acc) OutData[f"Region_{reg}_classification_report"] = list() OutData[f"Region_{reg}_classification_report"].append(clr) OutData[f"Region_{reg}_precision_score"] = list() OutData[f"Region_{reg}_precision_score"].append(pre) OutData[f"Region_{reg}_f1_score"] = list() OutData[f"Region_{reg}_f1_score"].append(f1) OutData[f"Region_{reg}_recall_score"] = list() OutData[f"Region_{reg}_recall_score"].append(rca) OutData[f"Region_{reg}_confusion_matrix"] = list() OutData[f"Region_{reg}_confusion_matrix"].append(cfm) print(f"FOLD {fold}: Linear accuracy for {reg} is {acc}") for reg in sorted(MappingVectorRegion.keys()): macc = np.mean(OutData[f"Region_{reg}_accuracy_score"]) mpre = np.mean(OutData[f"Region_{reg}_precision_score"]) mf1 = np.mean(OutData[f"Region_{reg}_f1_score"]) mrca = np.mean(OutData[f"Region_{reg}_recall_score"]) mcfm = None for ecmf in OutData[f"Region_{reg}_confusion_matrix"]: mcfm = ecmf if mcfm is None else mcfm + ecmf mcfm = mcfm / OutData[f"Region_{reg}_count"] OutData[f"Region_{reg}_mean_accuracy"] = macc OutData[f"Region_{reg}_mean_precision"] = mpre OutData[f"Region_{reg}_mean_recall"] = mrca OutData[f"Region_{reg}_mean_f1"] = mf1 OutData[f"Region_{reg}_mean_confusion_matrix"] = mcfm print(f"Mean accuracy of region {reg}: {macc}") print("Saving ...") mainIO_save(OutData, ui.txtOutFile.text()) print("DONE.") msgBox.setText("Wise area analysis: Atlas is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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_()
def btnConvert_click(self): msgBox = QMessageBox() # ComputeLabels ComputeLabels = ui.cbComputeLabels.isChecked() # Copy Copy = ui.cbCopy.isChecked() # NCluster try: NCluster = np.int32(ui.txtNCluster.text()) except: msgBox.setText("Number of Cluster is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Threshold try: Threshold = np.float(ui.txtThreshold.text()) except: msgBox.setText("Threshold is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # BranchingFactor try: BranchingFactor = np.int(ui.txtBranchingFactor.text()) except: msgBox.setText("Branching Factor is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # 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() OutData["ModelAnalysis"] = "Birch" # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None # 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) # Data if not len(ui.txtData.currentText()): msgBox.setText("Please enter Input Train 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 Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False X = InData[ui.txtData.currentText()] L = InData[ui.txtLabel.currentText()][0] try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(L == fil)[0] L = np.delete(L, labelIndx, axis=0) X = np.delete(X, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): X = preprocessing.scale(X) print("Whole of data is scaled to N(0,1).") except: print("Cannot load data or label") return try: cls = Birch(threshold=Threshold,branching_factor=BranchingFactor,n_clusters=NCluster,\ compute_labels=ComputeLabels,copy=Copy) print("Run Clustering ...") PeL = cls.fit_predict(X) except Exception as e: print(e) msgBox = QMessageBox() msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return OutData["predict"] = PeL if OutModel is not None: joblib.dump(cls, OutModel) print("Model is saved: " + OutModel) if ui.cbAverage.isChecked(): acc = accuracy_score(L, PeL) OutData["Accuracy"] = acc print("Average {:5.2f}".format(acc * 100)) if ui.cbNMI.isChecked(): NMI = normalized_mutual_info_score(L, PeL) OutData["NMI"] = NMI print("Normalized Mutual Information (NMI) {:7.6f}".format(NMI)) if ui.cbRIA.isChecked(): RIA = adjusted_rand_score(L, PeL) OutData["RIA"] = RIA print("Rand Index Adjusted (RIA) {:7.6f}".format(RIA)) if ui.cbAMI.isChecked(): AMI = adjusted_mutual_info_score(L, PeL) OutData["AMI"] = AMI print("Adjusted Mutual Information (AMI) {:7.6f}".format(AMI)) print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Birch Clustering is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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 method = ui.cbMethod.currentData() # Solver solver = ui.cbSolver.currentText() # Selection selection = ui.cbSelection.currentText() # Fit fit = ui.cbFit.isChecked() # normalize normalize = ui.cbNormalize.isChecked() try: alpha = np.float(ui.txtAlpha.text()) except: msgBox.setText("Alpha is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: iter = np.int(ui.txtMaxIter.text()) except: msgBox.setText("Max Iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False 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 try: l1 = np.float(ui.txtL1.text()) except: msgBox.setText("L1 is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: njob = np.float(ui.txtJobs.text()) except: msgBox.setText("Number of jobs is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # 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 print("Loading ...") InData = mainIO_load(InFile) # 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 # Design if not len(ui.txtDesign.currentText()): msgBox.setText("Please enter Input Design variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Design = InData[ui.txtDesign.currentText()] except: msgBox.setText("Design 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[ui.txtCond.currentText()] = Cond labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) 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] Design = np.delete(Design, labelIndx, axis=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"] = "SK.Group.RSA." + ui.cbMethod.currentText() print("Number of all levels is: " + str(len(UniqFold))) Cov = None Corr = None AMSE = list() Beta = None for foldID, fold in enumerate(GUFold): print("Analyzing level " + 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]] XLi = X[Index] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): XLi = preprocessing.scale(XLi) print("Whole of data is scaled X%d~N(0,1)." % (foldID + 1)) RegLi = np.insert(Design[Index], 0, 1, axis=1) if method == "ols": model = linmdl.LinearRegression(fit_intercept=fit, normalize=normalize, n_jobs=njob) elif method == "ridge": model = linmdl.Ridge(alpha=alpha, fit_intercept=fit, normalize=normalize, max_iter=iter, tol=tol, solver=solver) elif method == "lasso": model = linmdl.Lasso(alpha=alpha, fit_intercept=fit, normalize=normalize, max_iter=iter, tol=tol, selection=selection) elif method == "elast": model = linmdl.ElasticNet(alpha=alpha, l1_ratio=l1, fit_intercept=fit, normalize=normalize, \ max_iter=iter, tol=tol, selection=selection) model.fit(RegLi, XLi) BetaLi = np.transpose(model.coef_)[1:, :] Beta = BetaLi if Beta is None else Beta + BetaLi print("Calculating MSE for level %d ..." % (foldID + 1)) MSE = mean_squared_error(XLi, np.matmul(Design[Index], BetaLi)) print("MSE%d: %f" % (foldID + 1, MSE)) OutData["MSE" + str(foldID)] = MSE AMSE.append(MSE) if ui.cbBeta.isChecked(): OutData["BetaL" + str(foldID + 1)] = BetaLi # Calculate Results if ui.cbCorr.isChecked(): print("Calculating Correlation for level %d ..." % (foldID + 1)) CorrLi = np.corrcoef(BetaLi) OutData["Corr" + str(foldID + 1)] = CorrLi if Corr is None: Corr = CorrLi.copy() else: if ui.rbAvg.isChecked(): Corr = np.add(Corr, CorrLi) elif ui.rbMin.isChecked(): Corr = np.minimum(Corr, CorrLi) else: Corr = np.maximum(Corr, CorrLi) if ui.cbCov.isChecked(): print("Calculating Covariance for level %d ..." % (foldID + 1)) CovLi = np.cov(BetaLi) OutData["Cov" + str(foldID + 1)] = CovLi if Cov is None: Cov = CovLi.copy() else: if ui.rbAvg.isChecked(): Cov = np.add(Cov, CovLi) elif ui.rbMin.isChecked(): Cov = np.minimum(Cov, CovLi) else: Cov = np.maximum(Cov, CovLi) CoEff = len(UniqFold) - 1 if len(UniqFold) > 2 else 1 if ui.cbCov.isChecked(): if ui.rbAvg.isChecked(): Cov = Cov / CoEff 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(): if ui.rbAvg.isChecked(): Corr = Corr / CoEff 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() # 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] OutData["DistanceMatrix"] = dis Z = linkage(dis) OutData["Linkage"] = Z OutData["MSE"] = np.mean(AMSE) print("Average MSE: %f" % (OutData["MSE"])) OutData["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (OutData["RunTime"])) print("Saving results ...") mainIO_save(OutData, OutFile) print("Output is saved.") if ui.cbDiagram.isChecked(): if ui.cbCorr.isChecked(): 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=ui.txtXRotation.value()) ax.set_yticklabels(labels, minor=False, fontsize=FontSize, rotation=ui.txtYRotation.value()) 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('Group RSA: Correlation\nLevel: ' + FoldStr) plt.show() if ui.cbCov.isChecked(): 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=ui.txtXRotation.value()) ax.set_yticklabels(labels, minor=False, fontsize=FontSize, rotation=ui.txtYRotation.value()) 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('Group RSA: Covariance\nLevel: ' + FoldStr) plt.show() fig3 = plt.figure(figsize=(25, 10), ) if len(ui.txtTitleDen.text()): plt.title(ui.txtTitleDen.text()) else: plt.title( 'Group MP Gradient RSA: Similarity Analysis\nLevel: ' + FoldStr) dn = dendrogram(Z, labels=labels, leaf_font_size=FontSize, color_threshold=1, leaf_rotation=ui.txtXRotation.value()) plt.show() print("DONE.") msgBox.setText( "Group Representational Similarity Analysis (RSA) is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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_()
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_()
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_()
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_()
def btnConvert_click(self): msgBox = QMessageBox() tStart = time.time() Method = ui.cbMethod.currentData() LossType = ui.cbLossType.currentData() Optim = ui.cbOptim.currentData() try: Epoch = np.int32(ui.txtIter.text()) except: msgBox.setText("Number of iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: BatchSize = np.int32(ui.txtBatch.text()) except: msgBox.setText("Number of batch is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ReportStep = np.int32(ui.txtReportStep.text()) except: msgBox.setText("Number of Report Step is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: LearningRate = np.float32(ui.txtRate.text()) except: msgBox.setText("Number of Report Step is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: LassoAlpha = np.float32(ui.txtLParam.text()) except: msgBox.setText("Number of Lasso Parameter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ElasticLambda1 = np.float32(ui.txtEL1.text()) except: msgBox.setText("Number of Elastic Lambda 1 is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ElasticAlpha = np.float32(ui.txtEL2.text()) except: msgBox.setText("Number of Elastic Lambda 2 is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: RidgeReg = np.float32(ui.txtRRP.text()) except: msgBox.setText("Number of Ridge Regression Parameter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False 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 # 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 print("Loading ...") InData = mainIO_load(InFile) # 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 # Design if not len(ui.txtDesign.currentText()): msgBox.setText("Please enter Input Design variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Design = InData[ui.txtDesign.currentText()] except: msgBox.setText("Design 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[ui.txtCond.currentText()] = Cond labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) 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] 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 # Task Val if not len(ui.txtTaskVal.currentText()): msgBox.setText("Please enter Task value!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TaskIDTitle = ui.txtTaskVal.currentText() except: msgBox.setText("Task value is wrong!") 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 for ttlinx, ttl in enumerate(TaskTitleUnique): if TaskIDTitle == ttl: TaskID = ttlinx + 1 break OutData["Task"] = TaskIDTitle # 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 # Subject Val if not len(ui.txtSubjectVal.currentText()): msgBox.setText("Please enter Subject value!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: SubID = np.int32(ui.txtSubjectVal.currentText()) except: msgBox.setText("Subject value is wrong!") 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 OutData["SubjectID"] = SubID # 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 # Run Val if not len(ui.txtRunVal.currentText()): msgBox.setText("Please enter Run value!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: RunID = np.int32(ui.txtRunVal.currentText()) except: msgBox.setText("Run value is wrong!") 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 OutData["RunID"] = RunID # 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 # Counter Val if not len(ui.txtCounterVal.currentText()): msgBox.setText("Please enter Counter value!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: ConID = np.int32(ui.txtCounterVal.currentText()) except: msgBox.setText("Counter value is wrong!") 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 OutData["CounterID"] = ConID if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(L == fil)[0] Design = np.delete(Design, labelIndx, axis=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.") # Select Task TaskIndex = np.where(Task == TaskID) Design = Design[TaskIndex,:][0] X = X[TaskIndex,:][0] L = L[TaskIndex] Sub = Sub[TaskIndex] Run = Run[TaskIndex] Con = Con[TaskIndex] # Select Subject SubIndex = np.where(Sub == SubID) Design = Design[SubIndex,:][0] X = X[SubIndex,:][0] L = L[SubIndex] Run = Run[SubIndex] Con = Con[SubIndex] # Select Counter ConIndex = np.where(Con == ConID) Design = Design[ConIndex,:][0] X = X[ConIndex,:][0] L = L[ConIndex] Run = Run[ConIndex] # Select Run RunIndex = np.where(Run == RunID) Design = Design[RunIndex,:][0] X = X[RunIndex,:][0] L = L[RunIndex] # This will only use in supervised methods LUnique = np.unique(L) LNum = np.shape(LUnique)[0] OutData["Label"] = LUnique OutData["ModelAnalysis"] = "PyTorch.Session.Gradient.RSA." + ui.cbMethod.currentText() if np.shape(X)[0] == 0: msgBox.setText("The selected data is empty!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if ui.cbScale.isChecked(): X = preprocessing.scale(X) print("Data is scaled to N(0,1).") print("Running Gradient RSA ...") # RSA Method OutData['Method'] = dict() OutData['Method']['Method'] = Method OutData['Method']['LossType'] = LossType OutData['Method']['Optimization'] = Optim OutData['Method']['LearningRate'] = LearningRate OutData['Method']['Epoch'] = Epoch OutData['Method']['BatchSize'] = BatchSize OutData['Method']['ReportStep'] = ReportStep OutData['Method']['RidgeAlpha'] = RidgeReg OutData['Method']['ElaticLambda1'] = ElasticLambda1 OutData['Method']['ElaticAlpha'] = ElasticAlpha OutData['Method']['LassoAlpha'] = LassoAlpha OutData['Method']['Verbose'] = ui.cbVerbose.isChecked() rsa = GrRSA(method=Method,loss_type=LossType, optim=Optim, learning_rate=LearningRate, epoch=Epoch, \ batch_size=BatchSize, report_step=ReportStep, ridge_param=RidgeReg, elstnet_l1_ratio=ElasticLambda1,\ elstnet_alpha=ElasticAlpha, lasso_alpha=LassoAlpha, verbose=ui.cbVerbose.isChecked(),\ gpu_enable=ui.cbDevice.currentData(),normalization=False) Betas, Eps, loss_vec, MSE, Performacne, _ = rsa.fit(data_vals=X, design_vals=Design) OutData["LossVec"] = loss_vec OutData["MSE"] = MSE OutData["Performance"] = Performacne print("MSE: ", OutData["MSE"]) if ui.cbBeta.isChecked(): OutData["Betas"] = Betas OutData["Eps"] = Eps # Calculate Results if ui.cbCorr.isChecked(): print("Calculating Correlation ...") Corr = np.corrcoef(Betas) 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() if ui.cbCov.isChecked(): print("Calculating Covariance ...") Cov = np.cov(Betas) 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() # Calculating Distance Matrix dis = np.zeros((np.shape(Betas)[0], np.shape(Betas)[0])) for i in range(np.shape(Betas)[0]): for j in range(i + 1, np.shape(Betas)[0]): dis[i, j] = 1 - np.dot(Betas[i, :], Betas[j, :].T) dis[j, i] = dis[i, j] OutData["DistanceMatrix"] = dis Z = linkage(dis) OutData["Linkage"] = Z OutData["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (OutData["RunTime"])) print("Saving results ...") mainIO_save(OutData, OutFile) print("Output is saved.") if ui.cbDiagram.isChecked(): if ui.cbCorr.isChecked(): 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.txtTitleCorr.text()): plt.title(ui.txtTitleCorr.text()) else: plt.title('Correlation (' + ui.cbMethod.currentText() + \ ')\nTask: %s\nSub: %d, Counter: %d, Run: %d' % (TaskIDTitle, SubID, ConID, RunID)) plt.show() if ui.cbCov.isChecked(): 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.txtTitleCov.text()): plt.title(ui.txtTitleCov.text()) else: plt.title('Covariance (' + ui.cbMethod.currentText() + \ ')\nTask: %s\nSub: %d, Counter: %d, Run: %d' % (TaskIDTitle, SubID, ConID, RunID)) plt.show() fig3 = plt.figure(figsize=(25, 10), ) if len(ui.txtTitleDen.text()): plt.title(ui.txtTitleDen.text()) else: plt.title('Similarity Analysis (' + ui.cbMethod.currentText() + \ ')\nTask: %s\nSub: %d, Counter: %d, Run: %d' % (TaskIDTitle, SubID, ConID, RunID)) dn = dendrogram(Z, labels=labels, leaf_font_size=FontSize, color_threshold=1) plt.show() print("DONE.") msgBox.setText("Gradient Representational Similarity Analysis (RSA) is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): tme = time.time() 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 # Shuffle Shuffle = ui.cbShuffle.isChecked() # FitIntercept FitIntercept = ui.cbFitIntercept.isChecked() # WarmStart WarmStart = ui.cbWarmStart.isChecked() # NesterovsMomentum AverageParm = ui.cbAverageParm.isChecked() # Loss Loss = ui.cbLoss.currentText() # Penalty Penalty = ui.cbPenalty.currentText() # Learning Rate LearningRate = ui.cbLearningRate.currentText() # Alpha try: Alpha = np.float(ui.txtAlpha.text()) except: msgBox.setText("Alpha is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # L1Rate try: L1Rate = np.float(ui.txtL1Rate.text()) except: msgBox.setText("L1 Rate is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Eta0 try: Eta0 = np.double(ui.txtEta0.text()) except: msgBox.setText("Eta 0 Rate is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Powert try: Powert = np.double(ui.txtPowert.text()) except: msgBox.setText("Power_t is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Tol try: Tol = np.float(ui.txtTol.text()) if Tol == 0: Tol = None 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()) if MaxIter <= 0: msgBox.setText("Maximum number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("Maximum number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Epsilon try: Epsilon = np.float(ui.txtEpsilon.text()) except: msgBox.setText("Epsilon is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Epoach try: NIterNoChange = np.int32(ui.txtEpochs.text()) if NIterNoChange <= 0: msgBox.setText("N Iteration No Change must be positive!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("N Iteration No Change is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Verbose try: Verbose = np.int32(ui.txtVerbose.text()) except: msgBox.setText("Verbose is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # NJob try: NJobs = np.int32(ui.txtNJobs.text()) if NJobs <= 0: NJobs = -1 except: msgBox.setText("n_jobs is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # 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("Class filter is wrong!") msgBox.setText("Class filter is wrong!") 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 Fold = list() accuracy = list() precision = list() average_precision = list() f1score = list() recall = list() accuracyTr = list() precisionTr = list() average_precisionTr = list() f1scoreTr = list() recallTr = list() InFileList = list() OutData = dict() OutData[ "ModelAnalysis"] = "Stochastic Gradient Descent based Classifier" for fold in range(FoldFrom, FoldTo + 1): # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None else: OutModel = OutModel.replace("$FOLD$", str(fold)) # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold)) InFileList.append(InFile) 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) # 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 # 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 TrX = InData[ui.txtITrData.currentText()] TeX = InData[ui.txtITeData.currentText()] TrL = InData[ui.txtITrLabel.currentText()][0] TeL = InData[ui.txtITeLabel.currentText()][0] # Ref Filter Train if ui.cbFilterTrID.isChecked(): try: # Create content structure for training TrFilterContent = ui.txtFilterTrContent.text() TrFilterContent = TrFilterContent.replace(" ", "") if not len(TrFilterContent): print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TrFilterContent = TrFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TrFilterContent = np.int32(TrFilterContent) # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] if np.shape(TrX)[0] != np.shape(TrF)[0] or np.shape( TrL)[0] != np.shape(TrF)[0]: print( "Shape of reference for training must be the same as data and label" ) msgBox.setText( "Shape of reference for training must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for training is wrong!") msgBox.setText("Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove training set try: print( "Removing training set based on reference. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) for content in TrFilterContent: contIndex = np.where(TrF == content)[0] TrL = np.delete(TrL, contIndex, axis=0) TrX = np.delete(TrX, contIndex, axis=0) TrF = np.delete(TrF, contIndex, axis=0) print(" Content", content, "is removed from training set. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) except Exception as e: print("Cannot filter the training set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the training set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Create content structure for testing TeFilterContent = ui.txtFilterTeContent.text() TeFilterContent = TeFilterContent.replace(" ", "") if not len(TeFilterContent): print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TeFilterContent = TeFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TeFilterContent = np.int32(TeFilterContent) # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] if np.shape(TeX)[0] != np.shape(TeF)[0] or np.shape( TeL)[0] != np.shape(TeF)[0]: print( "Shape of reference for testing must be the same as data and label" ) msgBox.setText( "Shape of reference for testing must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for testing is wrong!") msgBox.setText("Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove testing set try: print( "Removing testing set based on reference. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) for content in TeFilterContent: contIndex = np.where(TeF == content)[0] TeL = np.delete(TeL, contIndex, axis=0) TeX = np.delete(TeX, contIndex, axis=0) TeF = np.delete(TeF, contIndex, axis=0) print(" Content", content, "is removed from testing set. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) except Exception as e: print("Cannot filter the testing set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the testing set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(TrL == fil)[0] TrL = np.delete(TrL, labelIndx, axis=0) TrX = np.delete(TrX, labelIndx, axis=0) # Remove Testing Set labelIndx = np.where(TeL == fil)[0] TeL = np.delete(TeL, labelIndx, axis=0) TeX = np.delete(TeX, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): TrX = preprocessing.scale(TrX) TeX = preprocessing.scale(TeX) print( "Whole of data is scaled Train~N(0,1) and Test~N(0,1)." ) except: print("Cannot load data or label") return # FoldID 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 try: currFID = InData[ui.txtFoldID.currentText()][0][0] Fold.append(currFID) except: print("Cannot load Fold ID!") return try: clf = SGDClassifier(loss=Loss,penalty=Penalty,alpha=Alpha,l1_ratio=L1Rate,fit_intercept=FitIntercept,\ max_iter=MaxIter,tol=Tol,shuffle=Shuffle,verbose=Verbose,epsilon=Epsilon,\ n_jobs=NJobs,learning_rate=LearningRate,eta0=Eta0,power_t=Powert,\ warm_start=WarmStart,average=AverageParm,n_iter_no_change=NIterNoChange) print("FoldID = " + str(currFID) + " is training ...") clf.fit(TrX, TrL) if OutModel is not None: joblib.dump(clf, OutModel) print("FoldID = " + str(currFID) + " Model is saved: " + OutModel) print("FoldID = " + str(currFID) + " is testing ...") PeL = clf.predict(TeX) PrL = clf.predict(TrX) OutData["fold" + str(currFID) + "_confusion_matrix"] = confusion_matrix( TeL, PeL, np.unique(TeL)) OutData["fold" + str(currFID) + "_classification_report"] = classification_report( TeL, PeL) print(OutData["fold" + str(currFID) + "_classification_report"]) except Exception as e: msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if ui.cbAverage.isChecked(): acc = accuracy_score(TeL, PeL) accTr = accuracy_score(TrL, PrL) accuracy.append(acc) accuracyTr.append(accTr) print( "FoldID = {:d}, Average Train {:5.2f} Test {:5.2f}" .format(currFID, accTr * 100, acc * 100)) if ui.cbPrecision.isChecked(): pre = precision_score(TeL, PeL, average=ui.cbPrecisionAvg.currentData()) preTr = precision_score( TrL, PrL, average=ui.cbPrecisionAvg.currentData()) precision.append(pre) precisionTr.append(preTr) print( "FoldID = {:d}, Precision Train {:5.2f} Test {:5.2f}" .format(currFID, preTr * 100, pre * 100)) if ui.cbAPrecision.isChecked(): prA = average_precision_score( TeL, PeL, average=ui.cbAPrecisionAvg.currentData()) prATr = average_precision_score( TrL, PrL, average=ui.cbAPrecisionAvg.currentData()) average_precision.append(prA) average_precisionTr.append(prATr) print( "FoldID = {:d}, Average Precision: Train {:5.2f} Test {:5.2f}" .format(currFID, prATr * 100, prA * 100)) if ui.cbRecall.isChecked(): rec = recall_score(TeL, PeL, average=ui.cbRecallAvg.currentData()) recTr = recall_score(TrL, PrL, average=ui.cbRecallAvg.currentData()) recall.append(rec) recallTr.append(recTr) print( "FoldID = {:d}, Recall: Train {:5.2f} Test {:5.2f}" .format(currFID, recTr * 100, rec * 100)) if ui.cbF1.isChecked(): f1 = f1_score(TeL, PeL, average=ui.cbF1Avg.currentData()) f1Tr = f1_score(TrL, PrL, average=ui.cbF1Avg.currentData()) f1score.append(f1) f1scoreTr.append(f1Tr) print( "FoldID = {:d}, F1: Train {:5.2f} Test {:5.2f}" .format(currFID, f1Tr * 100, f1 * 100)) print("FoldID = " + str(currFID) + " is analyzed!") if ui.cbAverage.isChecked(): OutData["FoldAccuracy"] = accuracy MeanAcc = np.mean(accuracy) OutData["MeanTestAccuracy"] = MeanAcc STDAcc = np.std(accuracy) OutData["StdTestAccuracy"] = STDAcc MeanAccTr = np.mean(accuracyTr) OutData["MeanTrainAccuracy"] = MeanAccTr STDAccTr = np.std(accuracyTr) OutData["StdTrainAccuracy"] = STDAccTr print( "Accuracy: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAccTr * 100, STDAccTr, MeanAcc * 100, STDAcc)) if ui.cbPrecision.isChecked(): OutData["ModePrecision"] = ui.cbPrecisionAvg.currentText() OutData["FoldPrecision"] = precision MeanPre = np.mean(precision) OutData["MeanTrainPrecision"] = MeanPre STDPre = np.std(precision) OutData["StdTrainPrecision"] = STDPre MeanPreTr = np.mean(precisionTr) OutData["MeanTestPrecision"] = MeanPreTr STDPreTr = np.std(precisionTr) OutData["StdTestPrecision"] = STDPreTr print( "Precision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanPreTr * 100, STDPreTr, MeanPre * 100, STDPre)) if ui.cbAPrecision.isChecked(): OutData["ModeAveragePrecision"] = ui.cbAPrecisionAvg.currentText() OutData["FoldAveragePrecision"] = average_precision MeanAPre = np.mean(average_precision) OutData["MeanTrainAveragePrecision"] = MeanAPre STDAPre = np.std(average_precision) OutData["StdTestAveragePrecision"] = STDAPre MeanAPreTr = np.mean(average_precisionTr) OutData["MeanTrainAveragePrecision"] = MeanAPreTr STDAPreTr = np.std(average_precisionTr) OutData["StdTrainAveragePrecision"] = STDAPreTr print( "AveragePrecision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAPreTr * 100, STDAPreTr, MeanAPre * 100, STDAPre)) if ui.cbRecall.isChecked(): OutData["ModeRecall"] = ui.cbRecallAvg.currentText() OutData["FoldRecall"] = recall MeanRec = np.mean(recall) OutData["MeanTestRecall"] = MeanRec STDRec = np.std(recall) OutData["StdTestRecall"] = STDRec MeanRecTr = np.mean(recallTr) OutData["MeanTrainRecall"] = MeanRecTr STDRecTr = np.std(recallTr) OutData["StdTrainRecall"] = STDRecTr print( "Recall: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanRecTr * 100, STDRecTr, MeanRec * 100, STDRec)) if ui.cbF1.isChecked(): OutData["ModeF1"] = ui.cbF1Avg.currentText() OutData["FoldF1"] = f1score MeanF1 = np.mean(f1score) OutData["MeanTestF1"] = MeanF1 STDF1 = np.std(f1score) OutData["StdTestF1"] = STDF1 MeanF1Tr = np.mean(f1scoreTr) OutData["MeanTrainF1"] = MeanF1Tr STDF1Tr = np.std(f1scoreTr) OutData["StdTrainF1"] = STDF1Tr print( "F1: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanF1Tr * 100, STDF1Tr, MeanF1 * 100, STDF1)) OutData["InputFiles"] = InFileList OutData["Runtime"] = time.time() - tme print("Runtime: ", OutData["Runtime"]) print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Stochastic Gradient Descent based Classifier is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
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_()
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_()
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_()