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 btnRedraw_click(self): msgBox = QMessageBox() ofile = LoadFile("Save result file ...", ['Result files (*.ezx *.mat)'], 'ezx',\ os.path.dirname(ui.txtOutFile.text())) FontSize = ui.txtFontSize.value() if len(ofile): try: Res = mainIO_load(ofile) except: print("Cannot load result file!") msgBox.setText("Cannot load result file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False HasDefaultCond = False # Condition if not len(ui.txtCond.currentText()): try: Cond = Res["condition"] HasDefaultCond = True except: msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not HasDefaultCond: try: Cond = Res[ui.txtCond.currentText()] except: msgBox.setText("Condition value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) if ui.cbCorr.isChecked(): try: Corr = Res["Correlation"] except: print("Cannot load Correlation variable!") msgBox.setText("Cannot load Correlation variable!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False NumData = np.shape(Corr)[0] fig1 = plt.figure(num=None, figsize=(NumData, NumData), dpi=100) plt.pcolor(Corr, vmin=np.min(Corr), vmax=np.max(Corr)) plt.xlim([0, NumData]) plt.ylim([0, NumData]) cbar = plt.colorbar() cbar.ax.tick_params(labelsize=FontSize) ax = plt.gca() ax.invert_yaxis() ax.set_aspect(1) ax.set_yticks(np.arange(NumData) + 0.5, minor=False) ax.set_xticks(np.arange(NumData) + 0.5, minor=False) ax.set_xticklabels(labels, minor=False, fontsize=FontSize, rotation=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: Correlation\nLevel: ' + str(Res["FoldInfo"]["Order"][0][0][0])) plt.show() if ui.cbCov.isChecked(): try: Cov = Res["Covariance"] except: print("Cannot load Covariance variable!") msgBox.setText("Cannot load Covariance variable!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False NumData = np.shape(Cov)[0] fig2 = plt.figure(num=None, figsize=(NumData, NumData), dpi=100) plt.pcolor(Cov, vmin=np.min(Cov), vmax=np.max(Cov)) plt.xlim([0, NumData]) plt.ylim([0, NumData]) cbar = plt.colorbar() cbar.ax.tick_params(labelsize=FontSize) ax = plt.gca() ax.invert_yaxis() ax.set_aspect(1) ax.set_yticks(np.arange(NumData) + 0.5, minor=False) ax.set_xticks(np.arange(NumData) + 0.5, minor=False) ax.set_xticklabels(labels, minor=False, fontsize=FontSize, rotation=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: Covariance\nLevel: ' + str(Res["FoldInfo"]["Order"][0][0][0])) plt.show() fig3 = plt.figure(figsize=(25, 10), ) dn = dendrogram(Res["Linkage"], labels=labels, leaf_font_size=FontSize, color_threshold=1, leaf_rotation=ui.txtXRotation.value()) if len(ui.txtTitleDen.text()): plt.title(ui.txtTitleDen.text()) else: plt.title( 'Group MP Gradient RSA: Similarity Analysis\nLevel: ' + str(Res["FoldInfo"]["Order"][0][0][0])) plt.show()
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() 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 btnRefresh_click(self): filename = ui.txtInFile.text() if len(filename): if os.path.isfile(filename): try: data = mainIO_load(filename) Keys = data.keys() # Data ui.txtData.clear() HasDefualt = False for key in Keys: ui.txtData.addItem(key) if key == "data": HasDefualt = True if HasDefualt: ui.txtData.setCurrentText("data") # Label ui.txtLabel.clear() HasDefualt = False for key in Keys: ui.txtLabel.addItem(key) if key == "label": HasDefualt = True if HasDefualt: ui.txtLabel.setCurrentText("label") Labels = data[ui.txtLabel.currentText()] Labels = np.unique(Labels) ui.txtClass.clear() for lbl in Labels: ui.txtClass.append(str(lbl)) # Design ui.txtDesign.clear() HasDefualt = False for key in Keys: ui.txtDesign.addItem(key) if key == "design": HasDefualt = True if HasDefualt: ui.txtDesign.setCurrentText("design") # Subject ui.txtSubject.clear() ui.txtSubjectVal.clear() HasDefualt = False for key in Keys: ui.txtSubject.addItem(key) if key == "subject": HasDefualt = True if HasDefualt: ui.txtSubject.setCurrentText("subject") values = np.unique(data["subject"]) for val in values: ui.txtSubjectVal.addItem(str(val)) # Run ui.txtRun.clear() ui.txtRunVal.clear() HasDefualt = False for key in Keys: ui.txtRun.addItem(key) if key == "run": HasDefualt = True if HasDefualt: ui.txtRun.setCurrentText("run") values = np.unique(data["run"]) for val in values: ui.txtRunVal.addItem(str(val)) # Counter ui.txtCounter.clear() ui.txtCounterVal.clear() HasDefualt = False for key in Keys: ui.txtCounter.addItem(key) if key == "counter": HasDefualt = True if HasDefualt: ui.txtCounter.setCurrentText("counter") values = np.unique(data["counter"]) for val in values: ui.txtCounterVal.addItem(str(val)) # Condition ui.txtCond.clear() HasDefualt = False for key in Keys: ui.txtCond.addItem(key) if key == "condition": HasDefualt = True if HasDefualt: ui.txtCond.setCurrentText("condition") # Task ui.txtTask.clear() ui.txtTaskVal.clear() HasDefualt = False for key in Keys: ui.txtTask.addItem(key) if key == "task": HasDefualt = True if HasDefualt: ui.txtTask.setCurrentText("task") values = np.unique(data["task"]) for val in values: ui.txtTaskVal.addItem(str(reshape_condition_cell(val))) ui.txtInFile.setText(filename) except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
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 btnInFile_click(self): filename = LoadFile("Load data file ...",['Data files (*.ezx *.mat *.ezdata)'],'ezx',\ os.path.dirname(ui.txtInFile.text())) if len(filename): if os.path.isfile(filename): try: print("Loading ...") data = mainIO_load(filename) Keys = data.keys() # Data ui.txtData.clear() HasDefualt = False for key in Keys: ui.txtData.addItem(key) if key == "data": HasDefualt = True if HasDefualt: ui.txtData.setCurrentText("data") print("Data Shape: ", np.shape(data["data"])) # Label ui.txtLabel.clear() HasDefualt = False for key in Keys: ui.txtLabel.addItem(key) if key == "label": HasDefualt = True if HasDefualt: ui.txtLabel.setCurrentText("label") Labels = data[ui.txtLabel.currentText()] Labels = np.unique(Labels) print("Number of labels: ", np.shape(Labels)[0]) print("Labels: ", Labels) ui.txtClass.clear() for lbl in Labels: ui.txtClass.append(str(lbl)) # Design ui.txtDesign.clear() HasDefualt = False for key in Keys: ui.txtDesign.addItem(key) if key == "design": HasDefualt = True if HasDefualt: ui.txtDesign.setCurrentText("design") # Subject ui.txtSubject.clear() ui.txtSubjectVal.clear() HasDefualt = False for key in Keys: ui.txtSubject.addItem(key) if key == "subject": HasDefualt = True if HasDefualt: ui.txtSubject.setCurrentText("subject") print("Number of subjects: ", np.shape(np.unique(data["subject"]))[0]) values = np.unique(data["subject"]) for val in values: ui.txtSubjectVal.addItem(str(val)) # Run ui.txtRun.clear() ui.txtRunVal.clear() HasDefualt = False for key in Keys: ui.txtRun.addItem(key) if key == "run": HasDefualt = True if HasDefualt: ui.txtRun.setCurrentText("run") values = np.unique(data["run"]) for val in values: ui.txtRunVal.addItem(str(val)) # Counter ui.txtCounter.clear() ui.txtCounterVal.clear() HasDefualt = False for key in Keys: ui.txtCounter.addItem(key) if key == "counter": HasDefualt = True if HasDefualt: ui.txtCounter.setCurrentText("counter") values = np.unique(data["counter"]) for val in values: ui.txtCounterVal.addItem(str(val)) # Condition ui.txtCond.clear() HasDefualt = False for key in Keys: ui.txtCond.addItem(key) if key == "condition": HasDefualt = True if HasDefualt: ui.txtCond.setCurrentText("condition") # Task ui.txtTask.clear() ui.txtTaskVal.clear() HasDefualt = False for key in Keys: ui.txtTask.addItem(key) if key == "task": HasDefualt = True if HasDefualt: ui.txtTask.setCurrentText("task") values = np.unique(np.asarray(data["task"])) for val in values: ui.txtTaskVal.addItem(str(reshape_condition_cell(val))) ui.txtInFile.setText(filename) print("DONE.") except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
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() 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() 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() 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 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"] = "SK.Session.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 RSA ...") # RSA Method Reg = np.insert(Design, 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(Reg, X) Betas = np.transpose(model.coef_)[1:, :] print("Calculating MSE ...") MSE = mean_squared_error(X, np.matmul(Design, Betas)) print("MSE: %f" % (MSE)) OutData["MSE"] = MSE # 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 if ui.cbBeta.isChecked(): OutData["Betas"] = Betas # 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() OutData["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (OutData["RunTime"])) print("Saving results ...") mainIO_save(OutData, OutFile) print("Output is saved.") FontSize = ui.txtFontSize.value() 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('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=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('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, leaf_rotation=ui.txtXRotation.value()) plt.show() print("DONE.") msgBox.setText("Representational Similarity Analysis (RSA) is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()