def btnLoadCondition_click(self): msgBox = QMessageBox() # InFile InFile = ui.txtInFile.text() if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) try: Cond = InData[ui.txtCond.currentText()] except: msgBox.setText("Cannot find Condition ID!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False ui.txtClassName.clear() ui.txtClassName.addItem("") for condition in Cond: ui.txtClassName.addItem(condition[0][0])
def btnReport_click(self): msgBox = QMessageBox() Vars = str(ui.txtVars.text()).replace(",", " ").split() if not len(Vars): msgBox.setText("Please enter variables!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if ui.lvFile.count() <= 0: msgBox.setText("Please enter files!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if ui.cbClear.isChecked(): ui.txtReport.clear() Len = ui.txtChar.value() str1 = "" for var in Vars: str1 = str1 + "\t" + var if str != "" else var ui.txtReport.append(str1 + "\n") for itemIndx in range(ui.lvFile.count()): filename = ui.lvFile.item(itemIndx).text() print("Reading " + filename) data = mainIO_load(filename) dat1 = "" if ui.rbAddress.isChecked(): str1 = filename elif ui.rbNone.isChecked(): str1 = "" elif ui.rbIndex.isChecked(): str1 = str(itemIndx) else: str1 = os.path.split(os.path.abspath(filename))[1] if not ui.rbNone.isChecked(): if len(str1) < Len: for _ in range(Len - len(str1)): str1 = str1 + ' ' else: str1 = str1[0:Len] for var in Vars: try: dat1 = str(data[var]) except: dat1 = 'None' str1 = str1 + "\t" + dat1 if str1 != "" else dat1 ui.txtReport.append(str1 + "\n")
def btnAnalysisFile_click(self): filename = LoadFile("Load data file ...",['Data files (*.ezx *.mat *.ezdata)'],'ezx',\ os.path.dirname(ui.txtAnFile.text())) # Load File dat = None try: dat = mainIO_load(filename) except: print("Cannot load Analysis file!") return # Load Result results = None try: results = dat["results"] except: print( "Cannot find results field --- i.e., 2D matrix including coordinates and accuracy" ) return # Clear Views ui.vwSele.clear() ui.vwSele.setColumnCount(2) ui.vwSele.setRowCount(0) ui.vwSele.setColumnWidth(0, 120) ui.vwSele.setColumnWidth(1, 100) ui.vwSele.setHorizontalHeaderLabels(['Coordinate', 'Accuracy']) ui.vwSele.setSortingEnabled(True) ui.vwSele.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection) ui.vwSele.setSelectionBehavior(QAbstractItemView.SelectRows) ui.vwAvai.clear() ui.vwAvai.setColumnCount(2) ui.vwSele.setRowCount(0) ui.vwAvai.setColumnWidth(0, 120) ui.vwAvai.setColumnWidth(1, 100) ui.vwAvai.setHorizontalHeaderLabels(['Coordinate', 'Accuracy']) ui.vwAvai.setSortingEnabled(True) ui.vwAvai.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection) ui.vwAvai.setSelectionBehavior(QAbstractItemView.SelectRows) # Draw ui.vwAvai.setRowCount(np.shape(results)[0]) ui.vwAvai.setRowCount(np.shape(results)[0]) for rowId, (coo, acc) in enumerate(results): try: item = QTableWidgetItem('{:5};{:5};{:5}'.format( coo[0][0], coo[0][1], coo[0][2])) item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) ui.vwAvai.setItem(rowId, 0, item) item = QTableWidgetItem('{:8f}'.format(acc[0][0] * 100)) item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) ui.vwAvai.setItem(rowId, 1, item) except: print(f'Item: {rowId} cannot add to the list --- format error') print(f"We have added {np.shape(results)[0]} items") ui.txtAnFile.setText(filename) ui.tabWidget.setCurrentIndex(1)
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 # Load Variables try: labels = list(Res["labels"]) Z = Res["Linkage"] if ui.cbCorr.isChecked(): Corr = Res["Correlation"] if ui.cbCov.isChecked(): Cov = Res["Covariance"] except Exception as e: print(str(e)) msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False 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())
def btnInData_click(self): filename = LoadFile("Load data file ...",['Data files (*.ezx *.mat *.ezdata)'],'ezx',\ os.path.dirname(ui.txtInData.text())) if len(filename): if os.path.isfile(filename): try: print("Data is loading ...") data = mainIO_load(filename) Keys = data.keys() # Test Data ui.txtITeData.clear() HasDefualt = False for key in Keys: ui.txtITeData.addItem(key) if key == "test_data": HasDefualt = True if HasDefualt: ui.txtITeData.setCurrentText("test_data") # Test Label ui.txtITeLabel.clear() ui.txtClass.clear() HasDefualt = False for key in Keys: ui.txtITeLabel.addItem(key) if key == "test_label": HasDefualt = True if HasDefualt: ui.txtITeLabel.setCurrentText("test_label") # set number of features Labels = data[ui.txtITeLabel.currentText()] Labels = np.unique(Labels) lblString = None for lbl in Labels: lblString = str( lbl ) if lblString is None else lblString + ", " + str( lbl) ui.txtClass.setText(lblString.strip()) ui.txtInData.setText(filename) print("Data is ready.") except: print("Cannot load data file!") return
def OpenFile(self, ifile): global data global root if len(ifile): if os.path.isfile(ifile): try: data = mainIO_load(ifile) assert data is not None except: print("Cannot load file!") return root = queue.Queue() frmDataEditor.DrawData(self) ui.txtInFile.setText(ifile) print(ifile + " is loaded!") ui.btnSave.setEnabled(False)
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: data = mainIO_load(filename) Keys = data.keys() # Train 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") # Train Label ui.txtLabel.clear() ui.txtClass.clear() HasDefualt = False for key in Keys: ui.txtLabel.addItem(key) if key == "label": HasDefualt = True if HasDefualt: ui.txtLabel.setCurrentText("label") # set number of features Labels = data[ui.txtLabel.currentText()] Labels = np.unique(Labels) ui.txtClass.clear() for lbl in Labels: ui.txtClass.append(str(lbl)) ui.txtInFile.setText(filename) except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
def btnRedraw_click(self): msgBox = QMessageBox() 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 ofile = LoadFile("Load result file ...", ['Result files (*.ezx *.mat)'], 'ezx',\ os.path.dirname(ui.txtOutFile.text())) if len(ofile): Out = {} try: Out = 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 Net = Out["Networks"] ThrNet = Out["ThresholdNetworks"] ACoord = Out["Atlas_parcellation"] for nnIndex, (nn, tnn) in enumerate(zip(Net, ThrNet)): try: Cond = Out["condition"] 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()
def btnMerge_click(self): global data global root if len(ui.txtInFile.text()): ifile = LoadFile("Open data files ...",\ ['Data files (*.ezx *.mat *.ezdata *.ezmat, *.model)', "easyX files (*.ezx)", 'MatLab files (*.mat)','EasyData files (*.ezdata)', \ 'EasyMat (*.ezmat)', 'All files (*.*)'],'mat') if len(ifile): if os.path.isfile(ifile): try: data2 = mainIO_load(ifile) except: print("Cannot load file!") return for key in data2.keys(): if key != "__header__" and key != "__version__" and key != "__globals__": data[key] = data2[key] root = queue.Queue() frmDataEditor.DrawData(self) ui.btnSave.setEnabled(True)
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: data = mainIO_load(filename) Keys = data.keys() # Train Data ui.txtITrData.clear() HasDefualt = False for key in Keys: ui.txtITrData.addItem(key) if key == "train_data": HasDefualt = True if HasDefualt: ui.txtITrData.setCurrentText("train_data") # Test Data ui.txtITeData.clear() HasDefualt = False for key in Keys: ui.txtITeData.addItem(key) if key == "test_data": HasDefualt = True if HasDefualt: ui.txtITeData.setCurrentText("test_data") # Train Label ui.txtITrLabel.clear() HasDefualt = False for key in Keys: ui.txtITrLabel.addItem(key) if key == "train_label": HasDefualt = True if HasDefualt: ui.txtITrLabel.setCurrentText("train_label") # Test Label ui.txtITeLabel.clear() HasDefualt = False for key in Keys: ui.txtITeLabel.addItem(key) if key == "test_label": HasDefualt = True if HasDefualt: ui.txtITeLabel.setCurrentText("test_label") # Train mLabel ui.txtITrmLabel.clear() HasDefualt = False for key in Keys: ui.txtITrmLabel.addItem(key) if key == "train_mlabel": HasDefualt = True if HasDefualt: ui.txtITrmLabel.setCurrentText("train_mlabel") ui.cbmLabel.setChecked(HasDefualt) # Test mLabel ui.txtITemLabel.clear() HasDefualt = False for key in Keys: ui.txtITemLabel.addItem(key) if key == "test_mlabel": HasDefualt = True if HasDefualt: ui.txtITemLabel.setCurrentText("test_mlabel") if ui.cbmLabel.isChecked(): ui.cbmLabel.setChecked(HasDefualt) # Coordinate ui.txtCol.clear() HasDefualt = False for key in Keys: ui.txtCol.addItem(key) if key == "coordinate": HasDefualt = True if HasDefualt: ui.txtCol.setCurrentText("coordinate") ui.cbCol.setChecked(HasDefualt) # Train Design ui.txtITrDM.clear() HasDefualt = False for key in Keys: ui.txtITrDM.addItem(key) if key == "train_design": HasDefualt = True if HasDefualt: ui.txtITrDM.setCurrentText("train_design") ui.cbDM.setChecked(HasDefualt) # Test Design ui.txtITeDM.clear() HasDefualt = False for key in Keys: ui.txtITeDM.addItem(key) if key == "test_design": HasDefualt = True if HasDefualt: ui.txtITeDM.setCurrentText("test_design") if ui.cbDM.isChecked(): ui.cbDM.setChecked(HasDefualt) # Train Subject ui.txtITrSubject.clear() HasDefualt = False for key in Keys: ui.txtITrSubject.addItem(key) if key == "train_subject": HasDefualt = True if HasDefualt: ui.txtITrSubject.setCurrentText("train_subject") # Test Subject ui.txtITeSubject.clear() HasDefualt = False for key in Keys: ui.txtITeSubject.addItem(key) if key == "test_subject": HasDefualt = True if HasDefualt: ui.txtITeSubject.setCurrentText("test_subject") # Train Task ui.txtITrTask.clear() HasDefualt = False for key in Keys: ui.txtITrTask.addItem(key) if key == "train_task": HasDefualt = True if HasDefualt: ui.txtITrTask.setCurrentText("train_task") ui.cbTask.setChecked(HasDefualt) # Test Task ui.txtITeTask.clear() HasDefualt = False for key in Keys: ui.txtITeTask.addItem(key) if key == "test_task": HasDefualt = True if HasDefualt: ui.txtITeTask.setCurrentText("test_task") if ui.cbTask.isChecked(): ui.cbTask.setChecked(HasDefualt) # Train Run ui.txtITrRun.clear() HasDefualt = False for key in Keys: ui.txtITrRun.addItem(key) if key == "train_run": HasDefualt = True if HasDefualt: ui.txtITrRun.setCurrentText("train_run") ui.cbRun.setChecked(HasDefualt) # Test Run ui.txtITeRun.clear() HasDefualt = False for key in Keys: ui.txtITeRun.addItem(key) if key == "test_run": HasDefualt = True if HasDefualt: ui.txtITeRun.setCurrentText("test_run") if ui.cbRun.isChecked(): ui.cbRun.setChecked(HasDefualt) # Train Counter ui.txtITrCounter.clear() HasDefualt = False for key in Keys: ui.txtITrCounter.addItem(key) if key == "train_counter": HasDefualt = True if HasDefualt: ui.txtITrCounter.setCurrentText("train_counter") ui.cbCounter.setChecked(HasDefualt) # Test Counter ui.txtITeCounter.clear() HasDefualt = False for key in Keys: ui.txtITeCounter.addItem(key) if key == "test_counter": HasDefualt = True if HasDefualt: ui.txtITeCounter.setCurrentText("test_counter") if ui.cbCounter.isChecked(): ui.cbCounter.setChecked(HasDefualt) # 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") ui.cbCond.setChecked(HasDefualt) # Train NScan ui.txtITrScan.clear() HasDefualt = False for key in Keys: ui.txtITrScan.addItem(key) if key == "train_nscan": HasDefualt = True if HasDefualt: ui.txtITrScan.setCurrentText("train_nscan") ui.cbNScan.setChecked(HasDefualt) # Test NScan ui.txtITeScan.clear() HasDefualt = False for key in Keys: ui.txtITeScan.addItem(key) if key == "test_nscan": HasDefualt = True if HasDefualt: ui.txtITeScan.setCurrentText("test_nscan") if ui.cbNScan.isChecked(): ui.cbNScan.setChecked(HasDefualt) # FoldID ui.txtFoldID.clear() HasDefualt = False for key in Keys: ui.txtFoldID.addItem(key) if key == "FoldID": HasDefualt = True if HasDefualt: ui.txtFoldID.setCurrentText("FoldID") ui.cbFoldID.setChecked(HasDefualt) # FoldInfo ui.txtFoldInfo.clear() HasDefualt = False for key in Keys: ui.txtFoldInfo.addItem(key) if key == "FoldInfo": HasDefualt = True if HasDefualt: ui.txtFoldInfo.setCurrentText("FoldInfo") ui.cbFoldInfo.setChecked(HasDefualt) # set number of features XShape = np.shape(data[ui.txtITrData.currentText()]) ui.txtNumFea.setMaximum(0) ui.txtNumFea.setMaximum(XShape[1]) ui.txtNumFea.setValue(0) ui.lblFeaNum.setText("1 ... " + str(XShape[1]) + ", 0 = auto") if ui.cbFoldID.isChecked(): ui.lbFoldID.setText( "ID=" + str(data[ui.txtFoldID.currentText()][0][0])) 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() try: FoldFrom = np.int32(ui.txtFoldFrom.text()) FoldTo = np.int32(ui.txtFoldTo.text()) except: print("Please check fold parameters!") return if FoldTo < FoldFrom: print("Please check fold parameters!") return # Filter try: Filter = ui.txtFilter.text() if not len(Filter): Filter = None else: Filter = Filter.replace("\'", " ").replace(",", " ").replace( "[", "").replace("]", "").split() Filter = np.int32(Filter) except: print("Class filter is wrong!") msgBox.setText("Class filter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False Fold = list() accuracy = list() precision = list() average_precision = list() f1score = list() recall = list() accuracyTr = list() precisionTr = list() average_precisionTr = list() f1scoreTr = list() recallTr = list() InFileList = list() OutData = dict() OutData["ModelAnalysis"] = "Gaussian Naive Bayes" for fold in range(FoldFrom, FoldTo + 1): # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None else: OutModel = OutModel.replace("$FOLD$", str(fold)) # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold)) InFileList.append(InFile) if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) # Data if not len(ui.txtITrData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeData.currentText()): msgBox.setText("Please enter Input Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtITrLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeLabel.currentText()): msgBox.setText("Please enter Test Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrX = InData[ui.txtITrData.currentText()] TeX = InData[ui.txtITeData.currentText()] TrL = InData[ui.txtITrLabel.currentText()][0] TeL = InData[ui.txtITeLabel.currentText()][0] # Ref Filter Train if ui.cbFilterTrID.isChecked(): try: # Create content structure for training TrFilterContent = ui.txtFilterTrContent.text() TrFilterContent = TrFilterContent.replace(" ", "") if not len(TrFilterContent): print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TrFilterContent = TrFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TrFilterContent = np.int32(TrFilterContent) # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] if np.shape(TrX)[0] != np.shape(TrF)[0] or np.shape( TrL)[0] != np.shape(TrF)[0]: print( "Shape of reference for training must be the same as data and label" ) msgBox.setText( "Shape of reference for training must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for training is wrong!") msgBox.setText("Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove training set try: print( "Removing training set based on reference. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) for content in TrFilterContent: contIndex = np.where(TrF == content)[0] TrL = np.delete(TrL, contIndex, axis=0) TrX = np.delete(TrX, contIndex, axis=0) TrF = np.delete(TrF, contIndex, axis=0) print(" Content", content, "is removed from training set. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) except Exception as e: print("Cannot filter the training set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the training set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Create content structure for testing TeFilterContent = ui.txtFilterTeContent.text() TeFilterContent = TeFilterContent.replace(" ", "") if not len(TeFilterContent): print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TeFilterContent = TeFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TeFilterContent = np.int32(TeFilterContent) # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] if np.shape(TeX)[0] != np.shape(TeF)[0] or np.shape( TeL)[0] != np.shape(TeF)[0]: print( "Shape of reference for testing must be the same as data and label" ) msgBox.setText( "Shape of reference for testing must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for testing is wrong!") msgBox.setText("Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove testing set try: print( "Removing testing set based on reference. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) for content in TeFilterContent: contIndex = np.where(TeF == content)[0] TeL = np.delete(TeL, contIndex, axis=0) TeX = np.delete(TeX, contIndex, axis=0) TeF = np.delete(TeF, contIndex, axis=0) print(" Content", content, "is removed from testing set. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) except Exception as e: print("Cannot filter the testing set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the testing set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(TrL == fil)[0] TrL = np.delete(TrL, labelIndx, axis=0) TrX = np.delete(TrX, labelIndx, axis=0) # Remove Testing Set labelIndx = np.where(TeL == fil)[0] TeL = np.delete(TeL, labelIndx, axis=0) TeX = np.delete(TeX, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): TrX = preprocessing.scale(TrX) TeX = preprocessing.scale(TeX) print( "Whole of data is scaled Train~N(0,1) and Test~N(0,1)." ) except: print("Cannot load data or label") return # FoldID if not len(ui.txtFoldID.currentText()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: currFID = InData[ui.txtFoldID.currentText()][0][0] Fold.append(currFID) except: print("Cannot load Fold ID!") return try: clf = GaussianNB() print("FoldID = " + str(currFID) + " is training ...") clf.fit(TrX, TrL) if OutModel is not None: joblib.dump(clf, OutModel) print("FoldID = " + str(currFID) + " Model is saved: " + OutModel) print("FoldID = " + str(currFID) + " is testing ...") PeL = clf.predict(TeX) PrL = clf.predict(TrX) OutData["fold" + str(currFID) + "_confusion_matrix"] = confusion_matrix( TeL, PeL, np.unique(TeL)) OutData["fold" + str(currFID) + "_classification_report"] = classification_report( TeL, PeL) print(OutData["fold" + str(currFID) + "_classification_report"]) except Exception as e: print(e) msgBox = QMessageBox() msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if ui.cbAverage.isChecked(): acc = accuracy_score(TeL, PeL) accTr = accuracy_score(TrL, PrL) accuracy.append(acc) accuracyTr.append(accTr) print( "FoldID = {:d}, Average Train {:5.2f} Test {:5.2f}" .format(currFID, accTr * 100, acc * 100)) if ui.cbPrecision.isChecked(): pre = precision_score(TeL, PeL, average=ui.cbPrecisionAvg.currentData()) preTr = precision_score( TrL, PrL, average=ui.cbPrecisionAvg.currentData()) precision.append(pre) precisionTr.append(preTr) print( "FoldID = {:d}, Precision Train {:5.2f} Test {:5.2f}" .format(currFID, preTr * 100, pre * 100)) if ui.cbAPrecision.isChecked(): prA = average_precision_score( TeL, PeL, average=ui.cbAPrecisionAvg.currentData()) prATr = average_precision_score( TrL, PrL, average=ui.cbAPrecisionAvg.currentData()) average_precision.append(prA) average_precisionTr.append(prATr) print( "FoldID = {:d}, Average Precision: Train {:5.2f} Test {:5.2f}" .format(currFID, prATr * 100, prA * 100)) if ui.cbRecall.isChecked(): rec = recall_score(TeL, PeL, average=ui.cbRecallAvg.currentData()) recTr = recall_score(TrL, PrL, average=ui.cbRecallAvg.currentData()) recall.append(rec) recallTr.append(recTr) print( "FoldID = {:d}, Recall: Train {:5.2f} Test {:5.2f}" .format(currFID, recTr * 100, rec * 100)) if ui.cbF1.isChecked(): f1 = f1_score(TeL, PeL, average=ui.cbF1Avg.currentData()) f1Tr = f1_score(TrL, PrL, average=ui.cbF1Avg.currentData()) f1score.append(f1) f1scoreTr.append(f1Tr) print( "FoldID = {:d}, F1: Train {:5.2f} Test {:5.2f}" .format(currFID, f1Tr * 100, f1 * 100)) print("FoldID = " + str(currFID) + " is analyzed!") if ui.cbAverage.isChecked(): OutData["FoldAccuracy"] = accuracy MeanAcc = np.mean(accuracy) OutData["MeanTestAccuracy"] = MeanAcc STDAcc = np.std(accuracy) OutData["StdTestAccuracy"] = STDAcc MeanAccTr = np.mean(accuracyTr) OutData["MeanTrainAccuracy"] = MeanAccTr STDAccTr = np.std(accuracyTr) OutData["StdTrainAccuracy"] = STDAccTr print( "Accuracy: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAccTr * 100, STDAccTr, MeanAcc * 100, STDAcc)) if ui.cbPrecision.isChecked(): OutData["ModePrecision"] = ui.cbPrecisionAvg.currentText() OutData["FoldPrecision"] = precision MeanPre = np.mean(precision) OutData["MeanTrainPrecision"] = MeanPre STDPre = np.std(precision) OutData["StdTrainPrecision"] = STDPre MeanPreTr = np.mean(precisionTr) OutData["MeanTestPrecision"] = MeanPreTr STDPreTr = np.std(precisionTr) OutData["StdTestPrecision"] = STDPreTr print( "Precision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanPreTr * 100, STDPreTr, MeanPre * 100, STDPre)) if ui.cbAPrecision.isChecked(): OutData["ModeAveragePrecision"] = ui.cbAPrecisionAvg.currentText() OutData["FoldAveragePrecision"] = average_precision MeanAPre = np.mean(average_precision) OutData["MeanTrainAveragePrecision"] = MeanAPre STDAPre = np.std(average_precision) OutData["StdTestAveragePrecision"] = STDAPre MeanAPreTr = np.mean(average_precisionTr) OutData["MeanTrainAveragePrecision"] = MeanAPreTr STDAPreTr = np.std(average_precisionTr) OutData["StdTrainAveragePrecision"] = STDAPreTr print( "AveragePrecision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAPreTr * 100, STDAPreTr, MeanAPre * 100, STDAPre)) if ui.cbRecall.isChecked(): OutData["ModeRecall"] = ui.cbRecallAvg.currentText() OutData["FoldRecall"] = recall MeanRec = np.mean(recall) OutData["MeanTestRecall"] = MeanRec STDRec = np.std(recall) OutData["StdTestRecall"] = STDRec MeanRecTr = np.mean(recallTr) OutData["MeanTrainRecall"] = MeanRecTr STDRecTr = np.std(recallTr) OutData["StdTrainRecall"] = STDRecTr print( "Recall: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanRecTr * 100, STDRecTr, MeanRec * 100, STDRec)) if ui.cbF1.isChecked(): OutData["ModeF1"] = ui.cbF1Avg.currentText() OutData["FoldF1"] = f1score MeanF1 = np.mean(f1score) OutData["MeanTestF1"] = MeanF1 STDF1 = np.std(f1score) OutData["StdTestF1"] = STDF1 MeanF1Tr = np.mean(f1scoreTr) OutData["MeanTrainF1"] = MeanF1Tr STDF1Tr = np.std(f1scoreTr) OutData["StdTrainF1"] = STDF1Tr print( "F1: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanF1Tr * 100, STDF1Tr, MeanF1 * 100, STDF1)) OutData["InputFiles"] = InFileList print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Gaussian Naive Bayes is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() totalTime = 0 Alg = ui.cbAlg.currentText() # Tol try: Tol = np.float(ui.txtTole.text()) except: msgBox.setText("Tolerance is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # MaxIte try: MaxIter = np.int32(ui.txtMaxIter.text()) except: msgBox.setText("Maximum number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if MaxIter < 1: msgBox.setText("Maximum number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: FoldFrom = np.int32(ui.txtFoldFrom.text()) FoldTo = np.int32(ui.txtFoldTo.text()) except: print("Please check fold parameters!") return if FoldTo < FoldFrom: print("Please check fold parameters!") return for fold_all in range(FoldFrom, FoldTo+1): tic = time.time() # OutFile OutFile = ui.txtOutFile.text() OutFile = OutFile.replace("$FOLD$", str(fold_all)) if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold_all)) if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) OutData = dict() OutData["imgShape"] = reshape_1Dvector(InData["imgShape"]) # Data if not len(ui.txtITrData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeData.currentText()): msgBox.setText("Please enter Input Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrData.text()): msgBox.setText("Please enter Output Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeData.text()): msgBox.setText("Please enter Output Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: XTr = InData[ui.txtITrData.currentText()] XTe = InData[ui.txtITeData.currentText()] if ui.cbScale.isChecked(): XTr = preprocessing.scale(XTr) XTe = preprocessing.scale(XTe) print("Whole of data is scaled X~N(0,1).") except: print("Cannot load data") return try: NumFea = np.int32(ui.txtNumFea.text()) except: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea < 1: msgBox.setText("Number of features must be greater than zero!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea > np.shape(XTr)[1]: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea > np.shape(XTe)[1]: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtITrLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeLabel.currentText()): msgBox.setText("Please enter Test Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrLabel.text()): msgBox.setText("Please enter Train Output Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeLabel.text()): msgBox.setText("Please enter Test Output Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(InData[ui.txtITrLabel.currentText()]) OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(InData[ui.txtITeLabel.currentText()]) except: print("Cannot load labels!") # Subject if not len(ui.txtITrSubject.currentText()): msgBox.setText("Please enter Train Input Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeSubject.currentText()): msgBox.setText("Please enter Test Input Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrSubject.text()): msgBox.setText("Please enter Train Output Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeSubject.text()): msgBox.setText("Please enter Test Output Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrSubject = InData[ui.txtITrSubject.currentText()] OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject) TeSubject = InData[ui.txtITeSubject.currentText()] OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject) except: print("Cannot load Subject IDs") return # Task if ui.cbTask.isChecked(): if not len(ui.txtITrTask.currentText()): msgBox.setText("Please enter Input Train Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeTask.currentText()): msgBox.setText("Please enter Input Test Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrTask.text()): msgBox.setText("Please enter Output Train Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeTask.text()): msgBox.setText("Please enter Output Test Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrTask = np.asarray(InData[ui.txtITrTask.currentText()]) OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask) TeTask = np.asarray(InData[ui.txtITeTask.currentText()]) OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask) TrTaskIndex = TrTask.copy() for tasindx, tas in enumerate(np.unique(TrTask)): TrTaskIndex[TrTask == tas] = tasindx + 1 TeTaskIndex = TeTask.copy() for tasindx, tas in enumerate(np.unique(TeTask)): TeTaskIndex[TeTask == tas] = tasindx + 1 except: print("Cannot load Tasks!") return # Run if ui.cbRun.isChecked(): if not len(ui.txtITrRun.currentText()): msgBox.setText("Please enter Train Input Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeRun.currentText()): msgBox.setText("Please enter Test Input Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrRun.text()): msgBox.setText("Please enter Train Output Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeRun.text()): msgBox.setText("Please enter Test Output Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrRun = InData[ui.txtITrRun.currentText()] OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun) TeRun = InData[ui.txtITeRun.currentText()] OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun) except: print("Cannot load Runs!") return # Counter if ui.cbCounter.isChecked(): if not len(ui.txtITrCounter.currentText()): msgBox.setText("Please enter Train Input Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeCounter.currentText()): msgBox.setText("Please enter Test Input Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrCounter.text()): msgBox.setText("Please enter Train Output Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeCounter.text()): msgBox.setText("Please enter Test Output Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrCounter = InData[ui.txtITrCounter.currentText()] OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(TrCounter) TeCounter = InData[ui.txtITeCounter.currentText()] OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(TeCounter) except: print("Cannot load Counters!") return # Matrix Label if ui.cbmLabel.isChecked(): if not len(ui.txtITrmLabel.currentText()): msgBox.setText("Please enter Train Input Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITemLabel.currentText()): msgBox.setText("Please enter Test Input Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrmLabel.text()): msgBox.setText("Please enter Train Output Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTemLabel.text()): msgBox.setText("Please enter Test Output Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrmLabel.text()] = InData[ui.txtITrmLabel.currentText()] OutData[ui.txtOTemLabel.text()] = InData[ui.txtITemLabel.currentText()] except: print("Cannot load matrix lables!") return # Design if ui.cbDM.isChecked(): if not len(ui.txtITrDM.currentText()): msgBox.setText("Please enter Train Input Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeDM.currentText()): msgBox.setText("Please enter Test Input Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrDM.text()): msgBox.setText("Please enter Train Output Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeDM.text()): msgBox.setText("Please enter Test Output Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrDM.text()] = InData[ui.txtITrDM.currentText()] OutData[ui.txtOTeDM.text()] = InData[ui.txtITeDM.currentText()] except: print("Cannot load design matrices!") return # Coordinate if ui.cbCol.isChecked(): if not len(ui.txtCol.currentText()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOCol.text()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()] except: print("Cannot load coordinator!") return # Condition if ui.cbCond.isChecked(): if not len(ui.txtCond.currentText()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOCond.text()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()] except: print("Cannot load conditions!") return # FoldID if ui.cbFoldID.isChecked(): if not len(ui.txtFoldID.currentText()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOFoldID.text()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOFoldID.text()] = reshape_1Dvector(InData[ui.txtFoldID.currentText()]) except: print("Cannot load Fold ID!") return # FoldInfo if ui.cbFoldInfo.isChecked(): if not len(ui.txtFoldInfo.currentText()): msgBox.setText("Please enter FoldInfo variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOFoldInfo.text()): msgBox.setText("Please enter FoldInfo variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOFoldInfo.text()] = InData[ui.txtFoldInfo.currentText()] except: print("Cannot load Fold Info!") return pass # Number of Scan if ui.cbNScan.isChecked(): if not len(ui.txtITrScan.currentText()): msgBox.setText("Please enter Number of Scan variable name for Input Train!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeScan.currentText()): msgBox.setText("Please enter Number of Scan variable name for Input Test!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrScan.text()): msgBox.setText("Please enter Number of Scan variable name for Output Train!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeScan.text()): msgBox.setText("Please enter Number of Scan variable name for Output Test!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrScan.text()] = reshape_1Dvector(InData[ui.txtITrScan.currentText()]) OutData[ui.txtOTeScan.text()] = reshape_1Dvector(InData[ui.txtITeScan.currentText()]) except: print("Cannot load NScan!") return try: model = FastICA(n_components=NumFea, algorithm=Alg, max_iter=MaxIter, tol=Tol) print("Running ICA Functional Alignment on Training Data ...") OutData[ui.txtOTrData.text()] = model.fit_transform(XTr) print("Running ICA Functional Alignment on Testing Data ...") OutData[ui.txtOTeData.text()] = model.fit_transform(XTe) except Exception as e: print(str(e)) HAParam = dict() HAParam["Method"] = "FastICA" HAParam["NumFea"] = NumFea HAParam["Algorithm"]= Alg OutData["FunctionalAlignment"] = HAParam OutData["Runtime"] = time.time() - tic totalTime += OutData["Runtime"] print("Saving ...") mainIO_save(OutData, OutFile) print("Fold " + str(fold_all) + " is DONE: " + OutFile) print("Runtime: ", totalTime) print("ICA Functional Alignment is done.") msgBox.setText("ICA Functional Alignment is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): totalTime = 0 msgBox = QMessageBox() TrFoldErr = list() TeFoldErr = list() try: FoldFrom = np.int32(ui.txtFoldFrom.text()) FoldTo = np.int32(ui.txtFoldTo.text()) except: print("Please check fold parameters!") return if FoldTo < FoldFrom: print("Please check fold parameters!") return for fold_all in range(FoldFrom, FoldTo + 1): tic = time.time() # Regularization try: Regularization = np.float(ui.txtRegularization.text()) except: msgBox.setText("Regularization value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # OutFile OutFile = ui.txtOutFile.text() OutFile = OutFile.replace("$FOLD$", str(fold_all)) if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold_all)) if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) OutData = dict() OutData["imgShape"] = reshape_1Dvector(InData["imgShape"]) # Data if not len(ui.txtITrData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeData.currentText()): msgBox.setText("Please enter Input Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrData.text()): msgBox.setText("Please enter Output Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeData.text()): msgBox.setText("Please enter Output Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: XTr = InData[ui.txtITrData.currentText()] XTe = InData[ui.txtITeData.currentText()] if ui.cbScale.isChecked() and not ui.rbScale.isChecked(): XTr = preprocessing.scale(XTr) XTe = preprocessing.scale(XTe) print("Whole of data is scaled X~N(0,1).") except: print("Cannot load data") return # NComponent try: NumFea = np.int32(ui.txtNumFea.text()) except: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea < 0: msgBox.setText("Number of features must be greater than zero!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea > np.shape(XTr)[1]: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea == 0: NumFea = None # Label if not len(ui.txtITrLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeLabel.currentText()): msgBox.setText("Please enter Test Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrLabel.text()): msgBox.setText( "Please enter Train Output Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeLabel.text()): msgBox.setText("Please enter Test Output Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrLabel.text()] = reshape_1Dvector( InData[ui.txtITrLabel.currentText()]) OutData[ui.txtOTeLabel.text()] = reshape_1Dvector( InData[ui.txtITeLabel.currentText()]) except: print("Cannot load labels!") # Subject if not len(ui.txtITrSubject.currentText()): msgBox.setText( "Please enter Train Input Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeSubject.currentText()): msgBox.setText( "Please enter Test Input Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrSubject.text()): msgBox.setText( "Please enter Train Output Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeSubject.text()): msgBox.setText( "Please enter Test Output Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrSubject = InData[ui.txtITrSubject.currentText()] OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject) TeSubject = InData[ui.txtITeSubject.currentText()] OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject) except: print("Cannot load Subject IDs") return # Task if ui.cbTask.isChecked(): if not len(ui.txtITrTask.currentText()): msgBox.setText( "Please enter Input Train Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeTask.currentText()): msgBox.setText( "Please enter Input Test Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrTask.text()): msgBox.setText( "Please enter Output Train Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeTask.text()): msgBox.setText( "Please enter Output Test Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrTask = np.asarray(InData[ui.txtITrTask.currentText()]) OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask) TeTask = np.asarray(InData[ui.txtITeTask.currentText()]) OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask) TrTaskIndex = TrTask.copy() for tasindx, tas in enumerate(np.unique(TrTask)): TrTaskIndex[TrTask == tas] = tasindx + 1 TeTaskIndex = TeTask.copy() for tasindx, tas in enumerate(np.unique(TeTask)): TeTaskIndex[TeTask == tas] = tasindx + 1 except: print("Cannot load Tasks!") return # Run if ui.cbRun.isChecked(): if not len(ui.txtITrRun.currentText()): msgBox.setText( "Please enter Train Input Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeRun.currentText()): msgBox.setText( "Please enter Test Input Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrRun.text()): msgBox.setText( "Please enter Train Output Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeRun.text()): msgBox.setText( "Please enter Test Output Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrRun = InData[ui.txtITrRun.currentText()] OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun) TeRun = InData[ui.txtITeRun.currentText()] OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun) except: print("Cannot load Runs!") return # Counter if ui.cbCounter.isChecked(): if not len(ui.txtITrCounter.currentText()): msgBox.setText( "Please enter Train Input Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeCounter.currentText()): msgBox.setText( "Please enter Test Input Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrCounter.text()): msgBox.setText( "Please enter Train Output Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeCounter.text()): msgBox.setText( "Please enter Test Output Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrCounter = InData[ui.txtITrCounter.currentText()] OutData[ui.txtOTrCounter.text()] = reshape_1Dvector( TrCounter) TeCounter = InData[ui.txtITeCounter.currentText()] OutData[ui.txtOTeCounter.text()] = reshape_1Dvector( TeCounter) except: print("Cannot load Counters!") return # Matrix Label if ui.cbmLabel.isChecked(): if not len(ui.txtITrmLabel.currentText()): msgBox.setText( "Please enter Train Input Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITemLabel.currentText()): msgBox.setText( "Please enter Test Input Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrmLabel.text()): msgBox.setText( "Please enter Train Output Matrix Label variable name!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTemLabel.text()): msgBox.setText( "Please enter Test Output Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrmLabel.text()] = InData[ ui.txtITrmLabel.currentText()] OutData[ui.txtOTemLabel.text()] = InData[ ui.txtITemLabel.currentText()] except: print("Cannot load matrix lables!") return # Design if ui.cbDM.isChecked(): if not len(ui.txtITrDM.currentText()): msgBox.setText( "Please enter Train Input Design Matrix variable name!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeDM.currentText()): msgBox.setText( "Please enter Test Input Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrDM.text()): msgBox.setText( "Please enter Train Output Design Matrix variable name!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeDM.text()): msgBox.setText( "Please enter Test Output Design Matrix variable name!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrDM.text()] = InData[ ui.txtITrDM.currentText()] OutData[ui.txtOTeDM.text()] = InData[ ui.txtITeDM.currentText()] except: print("Cannot load design matrices!") return # Coordinate if ui.cbCol.isChecked(): if not len(ui.txtCol.currentText()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOCol.text()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOCol.text()] = InData[ ui.txtCol.currentText()] except: print("Cannot load coordinator!") return # Condition if ui.cbCond.isChecked(): if not len(ui.txtCond.currentText()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOCond.text()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOCond.text()] = InData[ ui.txtCond.currentText()] except: print("Cannot load conditions!") return # FoldID if ui.cbFoldID.isChecked(): if not len(ui.txtFoldID.currentText()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOFoldID.text()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOFoldID.text()] = reshape_1Dvector( InData[ui.txtFoldID.currentText()]) except: print("Cannot load Fold ID!") return # FoldInfo if ui.cbFoldInfo.isChecked(): if not len(ui.txtFoldInfo.currentText()): msgBox.setText("Please enter FoldInfo variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOFoldInfo.text()): msgBox.setText("Please enter FoldInfo variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOFoldInfo.text()] = InData[ ui.txtFoldInfo.currentText()] except: print("Cannot load Fold Info!") return pass # Number of Scan if ui.cbNScan.isChecked(): if not len(ui.txtITrScan.currentText()): msgBox.setText( "Please enter Number of Scan variable name for Input Train!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeScan.currentText()): msgBox.setText( "Please enter Number of Scan variable name for Input Test!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrScan.text()): msgBox.setText( "Please enter Number of Scan variable name for Output Train!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeScan.text()): msgBox.setText( "Please enter Number of Scan variable name for Output Test!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrScan.text()] = reshape_1Dvector( InData[ui.txtITrScan.currentText()]) OutData[ui.txtOTeScan.text()] = reshape_1Dvector( InData[ui.txtITeScan.currentText()]) except: print("Cannot load NScan!") return # Train Analysis Level print("Calculating Analysis Level for Training Set ...") TrGroupFold = None FoldStr = "" if ui.cbFSubject.isChecked(): if not ui.rbFRun.isChecked(): TrGroupFold = TrSubject FoldStr = "Subject" else: TrGroupFold = np.concatenate((TrSubject, TrRun)) FoldStr = "Subject+Run" if ui.cbFTask.isChecked(): TrGroupFold = np.concatenate( (TrGroupFold, TrTaskIndex)) if TrGroupFold is not None else TrTaskIndex FoldStr = FoldStr + "+Task" if ui.cbFCounter.isChecked(): TrGroupFold = np.concatenate( (TrGroupFold, TrCounter)) if TrGroupFold is not None else TrCounter FoldStr = FoldStr + "+Counter" TrGroupFold = np.transpose(TrGroupFold) TrUniqFold = np.array( list(set(tuple(i) for i in TrGroupFold.tolist()))) TrFoldIDs = np.arange(len(TrUniqFold)) + 1 TrListFold = list() for gfold in TrGroupFold: for ufoldindx, ufold in enumerate(TrUniqFold): if (ufold == gfold).all(): currentID = TrFoldIDs[ufoldindx] break TrListFold.append(currentID) TrListFold = np.int32(TrListFold) TrListFoldUniq = np.unique(TrListFold) # Test Analysis Level print("Calculating Analysis Level for Testing Set ...") TeGroupFold = None if ui.cbFSubject.isChecked(): if not ui.rbFRun.isChecked(): TeGroupFold = TeSubject else: TeGroupFold = np.concatenate((TeSubject, TeRun)) if ui.cbFTask.isChecked(): TeGroupFold = np.concatenate( (TeGroupFold, TeTaskIndex)) if TeGroupFold is not None else TeTaskIndex if ui.cbFCounter.isChecked(): TeGroupFold = np.concatenate( (TeGroupFold, TeCounter)) if TeGroupFold is not None else TeCounter TeGroupFold = np.transpose(TeGroupFold) TeUniqFold = np.array( list(set(tuple(i) for i in TeGroupFold.tolist()))) TeFoldIDs = np.arange(len(TeUniqFold)) + 1 TeListFold = list() for gfold in TeGroupFold: for ufoldindx, ufold in enumerate(TeUniqFold): if (ufold == gfold).all(): currentID = TeFoldIDs[ufoldindx] break TeListFold.append(currentID) TeListFold = np.int32(TeListFold) TeListFoldUniq = np.unique(TeListFold) # Train Partition print("Partitioning Training Data ...") TrX = list() TrShape = None for foldindx, fold in enumerate(TrListFoldUniq): dat = XTr[np.where(TrListFold == fold)] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): dat = preprocessing.scale(dat) print("Data belong to View " + str(foldindx + 1) + " is scaled X~N(0,1).") TrX.append(dat) if TrShape is None: TrShape = np.shape(dat) else: if not (TrShape == np.shape(dat)): print("ERROR: Train, Reshape problem for Fold " + str(foldindx + 1) + ", Shape: " + str(np.shape(dat))) return print("Train: View " + str(foldindx + 1) + " is extracted. Shape: " + str(np.shape(dat))) print("Training Shape: " + str(np.shape(TrX))) # Test Partition print("Partitioning Testing Data ...") TeX = list() TeShape = None for foldindx, fold in enumerate(TeListFoldUniq): dat = XTe[np.where(TeListFold == fold)] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): dat = preprocessing.scale(dat) print("Data belong to View " + str(foldindx + 1) + " is scaled X~N(0,1).") TeX.append(dat) if TeShape is None: TeShape = np.shape(dat) else: if not (TeShape == np.shape(dat)): print("Test: Reshape problem for Fold " + str(foldindx + 1)) return print("Test: View " + str(foldindx + 1) + " is extracted.") print("Testing Shape: " + str(np.shape(TeX))) model = RHA(regularization=Regularization, Dim=NumFea) print("Running Hyperalignment on Training Data ...") model.fit(TrX) G = model.get_G() TrU = model.get_U() print("Running Hyperalignment on Testing Data ...") model.project(TeX) TeU = model.get_U_tilde() # Train Dot Product print("Producting Training Data ...") TrHX = None TrErr = None for foldindx, fold in enumerate(TrListFoldUniq): mapping = np.dot(TrX[foldindx], TrU[foldindx]) TrErr = TrErr + (G - mapping) if TrErr is not None else G - mapping TrHX = np.concatenate( (TrHX, mapping)) if TrHX is not None else mapping OutData[ui.txtOTrData.text()] = TrHX foldindx = foldindx + 1 TrErr = TrErr / foldindx print("Train: alignment error ", np.linalg.norm(TrErr)) TrFoldErr.append(np.linalg.norm(TrErr)) # Train Dot Product print("Producting Testing Data ...") TeHX = None TeErr = None for foldindx, fold in enumerate(TeListFoldUniq): mapping = np.dot(TeX[foldindx], TeU[foldindx]) TeErr = TeErr + (G - mapping) if TeErr is not None else G - mapping TeHX = np.concatenate( (TeHX, mapping)) if TeHX is not None else mapping OutData[ui.txtOTeData.text()] = TeHX foldindx = foldindx + 1 TeErr = TeErr / foldindx print("Test: alignment error ", np.linalg.norm(TeErr)) TeFoldErr.append(np.linalg.norm(TeErr)) HAParam = dict() HAParam["Share"] = G HAParam["Train"] = TrU HAParam["Test"] = TeU HAParam["Level"] = FoldStr OutData["FunctionalAlignment"] = HAParam OutData["Runtime"] = time.time() - tic totalTime += OutData["Runtime"] print("Saving ...") mainIO_save(OutData, OutFile) print("Fold " + str(fold_all) + " is DONE: " + OutFile) print("Training -> Alignment Error: mean " + str(np.mean(TrFoldErr)) + " std " + str(np.std(TrFoldErr))) print("Testing -> Alignment Error: mean " + str(np.mean(TeFoldErr)) + " std " + str(np.std(TeFoldErr))) print("Runtime: ", totalTime) print("Regularized Hyperalignment is done.") msgBox.setText("Regularized Hyperalignment is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def 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() # 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) # Subject ui.txtSubject.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]) # Task ui.txtTask.clear() HasDefualt = False for key in Keys: ui.txtTask.addItem(key) if key == "task": HasDefualt = True if HasDefualt: ui.txtTask.setCurrentText("task") # Run ui.txtRun.clear() HasDefualt = False for key in Keys: ui.txtRun.addItem(key) if key == "run": HasDefualt = True if HasDefualt: ui.txtRun.setCurrentText("run") # Counter ui.txtCounter.clear() HasDefualt = False for key in Keys: ui.txtCounter.addItem(key) if key == "counter": HasDefualt = True if HasDefualt: ui.txtCounter.setCurrentText("counter") ui.txtInFile.setText(filename) ui.tbReport.clear() ui.tbReport.setRowCount(0) ui.tbReport.setColumnCount(0) ui.tbReport2.clear() ui.tbReport2.setRowCount(0) ui.tbReport2.setColumnCount(0) ui.tabWidget2.setCurrentIndex(0) ui.tabWidget.setCurrentIndex(2) except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
def btnConvert_click(self): totalTime = 0 msgBox = QMessageBox() Model = ui.cbMethod.currentText() try: FoldFrom = np.int32(ui.txtFoldFrom.text()) FoldTo = np.int32(ui.txtFoldTo.text()) except: print("Please check fold parameters!") return if FoldTo < FoldFrom: print("Please check fold parameters!") return for fold_all in range(FoldFrom, FoldTo+1): tic = time.time() # Regularization try: NIter = np.int32(ui.txtIter.text()) except: msgBox.setText("Number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Gamma = np.float(ui.txtGamma.text()) except: msgBox.setText("Gamma is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # AR Rank try: Rank = np.int32(ui.txtRank.text()) except: msgBox.setText("Rank value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # AR Rank try: Rho = np.float(ui.txtRho.text()) except: msgBox.setText("Rho value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # OutFile OutFile = ui.txtOutFile.text() OutFile = OutFile.replace("$FOLD$", str(fold_all)) if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold_all)) if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) OutData = dict() OutData["imgShape"] = reshape_1Dvector(InData["imgShape"]) # Data if not len(ui.txtITrData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeData.currentText()): msgBox.setText("Please enter Input Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrData.text()): msgBox.setText("Please enter Output Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeData.text()): msgBox.setText("Please enter Output Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: XTr = InData[ui.txtITrData.currentText()] XTe = InData[ui.txtITeData.currentText()] if ui.cbScale.isChecked() and not ui.rbScale.isChecked(): XTr = preprocessing.scale(XTr) XTe = preprocessing.scale(XTe) print("Whole of data is scaled X~N(0,1).") except: print("Cannot load data") return # NComponent try: NumFea = np.int32(ui.txtNumFea.text()) except: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea < 0: msgBox.setText("Number of features must be greater than zero!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if NumFea > np.shape(XTr)[1]: msgBox.setText("Number of features is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtITrLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeLabel.currentText()): msgBox.setText("Please enter Test Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrLabel.text()): msgBox.setText("Please enter Train Output Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeLabel.text()): msgBox.setText("Please enter Test Output Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrLabel.text()] = reshape_1Dvector(InData[ui.txtITrLabel.currentText()]) OutData[ui.txtOTeLabel.text()] = reshape_1Dvector(InData[ui.txtITeLabel.currentText()]) except: print("Cannot load labels!") # Subject if not len(ui.txtITrSubject.currentText()): msgBox.setText("Please enter Train Input Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeSubject.currentText()): msgBox.setText("Please enter Test Input Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrSubject.text()): msgBox.setText("Please enter Train Output Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeSubject.text()): msgBox.setText("Please enter Test Output Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrSubject = InData[ui.txtITrSubject.currentText()] OutData[ui.txtOTrSubject.text()] = reshape_1Dvector(TrSubject) TeSubject = InData[ui.txtITeSubject.currentText()] OutData[ui.txtOTeSubject.text()] = reshape_1Dvector(TeSubject) except: print("Cannot load Subject IDs") return # Task if ui.cbTask.isChecked(): if not len(ui.txtITrTask.currentText()): msgBox.setText("Please enter Input Train Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeTask.currentText()): msgBox.setText("Please enter Input Test Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrTask.text()): msgBox.setText("Please enter Output Train Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeTask.text()): msgBox.setText("Please enter Output Test Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrTask = np.asarray(InData[ui.txtITrTask.currentText()]) OutData[ui.txtOTrTask.text()] = reshape_1Dvector(TrTask) TeTask = np.asarray(InData[ui.txtITeTask.currentText()]) OutData[ui.txtOTeTask.text()] = reshape_1Dvector(TeTask) TrTaskIndex = TrTask.copy() for tasindx, tas in enumerate(np.unique(TrTask)): TrTaskIndex[TrTask == tas] = tasindx + 1 TeTaskIndex = TeTask.copy() for tasindx, tas in enumerate(np.unique(TeTask)): TeTaskIndex[TeTask == tas] = tasindx + 1 except: print("Cannot load Tasks!") return # Run if ui.cbRun.isChecked(): if not len(ui.txtITrRun.currentText()): msgBox.setText("Please enter Train Input Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeRun.currentText()): msgBox.setText("Please enter Test Input Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrRun.text()): msgBox.setText("Please enter Train Output Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeRun.text()): msgBox.setText("Please enter Test Output Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrRun = InData[ui.txtITrRun.currentText()] OutData[ui.txtOTrRun.text()] = reshape_1Dvector(TrRun) TeRun = InData[ui.txtITeRun.currentText()] OutData[ui.txtOTeRun.text()] = reshape_1Dvector(TeRun) except: print("Cannot load Runs!") return # Counter if ui.cbCounter.isChecked(): if not len(ui.txtITrCounter.currentText()): msgBox.setText("Please enter Train Input Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeCounter.currentText()): msgBox.setText("Please enter Test Input Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrCounter.text()): msgBox.setText("Please enter Train Output Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeCounter.text()): msgBox.setText("Please enter Test Output Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrCounter = InData[ui.txtITrCounter.currentText()] OutData[ui.txtOTrCounter.text()] = reshape_1Dvector(TrCounter) TeCounter = InData[ui.txtITeCounter.currentText()] OutData[ui.txtOTeCounter.text()] = reshape_1Dvector(TeCounter) except: print("Cannot load Counters!") return # Matrix Label if ui.cbmLabel.isChecked(): if not len(ui.txtITrmLabel.currentText()): msgBox.setText("Please enter Train Input Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITemLabel.currentText()): msgBox.setText("Please enter Test Input Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrmLabel.text()): msgBox.setText("Please enter Train Output Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTemLabel.text()): msgBox.setText("Please enter Test Output Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrmLabel.text()] = InData[ui.txtITrmLabel.currentText()] OutData[ui.txtOTemLabel.text()] = InData[ui.txtITemLabel.currentText()] except: print("Cannot load matrix lables!") return # Design if ui.cbDM.isChecked(): if not len(ui.txtITrDM.currentText()): msgBox.setText("Please enter Train Input Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeDM.currentText()): msgBox.setText("Please enter Test Input Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrDM.text()): msgBox.setText("Please enter Train Output Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeDM.text()): msgBox.setText("Please enter Test Output Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrDM.text()] = InData[ui.txtITrDM.currentText()] OutData[ui.txtOTeDM.text()] = InData[ui.txtITeDM.currentText()] except: print("Cannot load design matrices!") return # Coordinate if ui.cbCol.isChecked(): if not len(ui.txtCol.currentText()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOCol.text()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()] except: print("Cannot load coordinator!") return # Condition if ui.cbCond.isChecked(): if not len(ui.txtCond.currentText()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOCond.text()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()] except: print("Cannot load conditions!") return # FoldID if ui.cbFoldID.isChecked(): if not len(ui.txtFoldID.currentText()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOFoldID.text()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOFoldID.text()] = reshape_1Dvector(InData[ui.txtFoldID.currentText()]) except: print("Cannot load Fold ID!") return # FoldInfo if ui.cbFoldInfo.isChecked(): if not len(ui.txtFoldInfo.currentText()): msgBox.setText("Please enter FoldInfo variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOFoldInfo.text()): msgBox.setText("Please enter FoldInfo variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOFoldInfo.text()] = InData[ui.txtFoldInfo.currentText()] except: print("Cannot load Fold Info!") return pass # Number of Scan if ui.cbNScan.isChecked(): if not len(ui.txtITrScan.currentText()): msgBox.setText("Please enter Number of Scan variable name for Input Train!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeScan.currentText()): msgBox.setText("Please enter Number of Scan variable name for Input Test!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTrScan.text()): msgBox.setText("Please enter Number of Scan variable name for Output Train!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtOTeScan.text()): msgBox.setText("Please enter Number of Scan variable name for Output Test!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: OutData[ui.txtOTrScan.text()] = reshape_1Dvector(InData[ui.txtITrScan.currentText()]) OutData[ui.txtOTeScan.text()] = reshape_1Dvector(InData[ui.txtITeScan.currentText()]) except: print("Cannot load NScan!") return # Train Analysis Level print("Calculating Analysis Level for Training Set ...") TrGroupFold = None FoldStr = "" if ui.cbFSubject.isChecked(): if not ui.rbFRun.isChecked(): TrGroupFold = TrSubject FoldStr = "Subject" else: TrGroupFold = np.concatenate((TrSubject,TrRun)) FoldStr = "Subject+Run" if ui.cbFTask.isChecked(): TrGroupFold = np.concatenate((TrGroupFold,TrTaskIndex)) if TrGroupFold is not None else TrTaskIndex FoldStr = FoldStr + "+Task" if ui.cbFCounter.isChecked(): TrGroupFold = np.concatenate((TrGroupFold,TrCounter)) if TrGroupFold is not None else TrCounter FoldStr = FoldStr + "+Counter" TrGroupFold = np.transpose(TrGroupFold) TrUniqFold = np.array(list(set(tuple(i) for i in TrGroupFold.tolist()))) TrFoldIDs = np.arange(len(TrUniqFold)) + 1 TrListFold = list() for gfold in TrGroupFold: for ufoldindx, ufold in enumerate(TrUniqFold): if (ufold == gfold).all(): currentID = TrFoldIDs[ufoldindx] break TrListFold.append(currentID) TrListFold = np.int32(TrListFold) TrListFoldUniq = np.unique(TrListFold) # Test Analysis Level print("Calculating Analysis Level for Testing Set ...") TeGroupFold = None if ui.cbFSubject.isChecked(): if not ui.rbFRun.isChecked(): TeGroupFold = TeSubject else: TeGroupFold = np.concatenate((TeSubject,TeRun)) if ui.cbFTask.isChecked(): TeGroupFold = np.concatenate((TeGroupFold,TeTaskIndex)) if TeGroupFold is not None else TeTaskIndex if ui.cbFCounter.isChecked(): TeGroupFold = np.concatenate((TeGroupFold,TeCounter)) if TeGroupFold is not None else TeCounter TeGroupFold = np.transpose(TeGroupFold) TeUniqFold = np.array(list(set(tuple(i) for i in TeGroupFold.tolist()))) TeFoldIDs = np.arange(len(TeUniqFold)) + 1 TeListFold = list() for gfold in TeGroupFold: for ufoldindx, ufold in enumerate(TeUniqFold): if (ufold == gfold).all(): currentID = TeFoldIDs[ufoldindx] break TeListFold.append(currentID) TeListFold = np.int32(TeListFold) TeListFoldUniq = np.unique(TeListFold) # Train Partition print("Partitioning Training Data ...") TrX = list() TrShape = None for foldindx, fold in enumerate(TrListFoldUniq): dat = XTr[np.where(TrListFold == fold)] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): dat = preprocessing.scale(dat) print("Data belong to View " + str(foldindx + 1) + " is scaled X~N(0,1).") AR_MAT = AR(np.shape(dat)[0], rho=Rho, rank=Rank) TrX.append(np.dot(np.transpose(dat), AR_MAT)) if TrShape is None: TrShape = np.shape(dat) else: if not(TrShape == np.shape(dat)): print("ERROR: Train, Reshape problem for Fold " + str(foldindx + 1) + ", Shape: " + str(np.shape(dat))) return print("Train: View " + str(foldindx + 1) + " is extracted. Shape: " + str(np.shape(dat))) print("Training Shape (sub x voxel x time): " + str(np.shape(TrX))) # Test Partition print("Partitioning Testing Data ...") TeX = list() TeShape = None for foldindx, fold in enumerate(TeListFoldUniq): dat = XTe[np.where(TeListFold == fold)] if ui.cbScale.isChecked() and ui.rbScale.isChecked(): dat = preprocessing.scale(dat) print("Data belong to View " + str(foldindx + 1) + " is scaled X~N(0,1).") AR_MAT = AR(np.shape(dat)[0], rho=Rho, rank=Rank) TeX.append(np.dot(np.transpose(dat), AR_MAT)) if TeShape is None: TeShape = np.shape(dat) else: if not(TeShape == np.shape(dat)): print("Test: Reshape problem for Fold " + str(foldindx + 1)) return print("Test: View " + str(foldindx + 1) + " is extracted.") print("Testing Shape (sub x voxel x time): " + str(np.shape(TeX))) if NumFea == 0: NumFea = np.min(np.shape(TrX)[1:3]) print("Number of features are automatically selected as ", NumFea) MessageString = "" SharedR = None try: if Model == "Probabilistic SRM": model = SRM(n_iter=NIter,features=NumFea) MessageString = "Probabilistic " elif Model == "Deterministic SRM": model = DetSRM(n_iter=NIter,features=NumFea) MessageString = "Deterministic " else: model = RSRM(n_iter=NIter, features=NumFea, gamma=Gamma) SharedR = True MessageString = "Robust " print("Running Hyperalignment on Training Data ...") model.fit(TrX) if SharedR == True: S = model.r_ Spec_train = list() for spec in model.s_: Spec_train.append(np.transpose(spec)) else: S = model.s_ WTr = list() for wtri in model.w_ : WTr.append(wtri) # Train Dot Product print("Producting Training Data ...") TrHX = None for mapping, viewi in zip(WTr, TrX): TrHX = np.concatenate((TrHX, np.transpose(np.dot(np.transpose(mapping),viewi)))) if TrHX is not None else np.transpose(np.dot(np.transpose(mapping),viewi)) OutData[ui.txtOTrData.text()] = TrHX print("Running Hyperalignment on Testing Data ...") TeHX = None WTe = list() if SharedR: Specific_test = np.zeros(np.shape(TeX)) for ijk in range(NIter): # Considering Specific_Test is fixed and Updating Wi WTe = list() for vid, view in enumerate(TeX): product = np.dot(view - Specific_test[vid], np.transpose(S)) U, _, V = lg.svd(product, full_matrices=False, check_finite=False) WTe.append(np.dot(U, V)) # Considering Wi is fixed and Updating Specific_test Specific_test = list() for vid, (view, Wtest) in enumerate(zip(TeX, WTe)): NewSpec = view - np.dot(Wtest, S) posIndex = NewSpec > Gamma negIndex = NewSpec < -Gamma NewSpec[posIndex] -= Gamma NewSpec[negIndex] += Gamma NewSpec[np.logical_and(~posIndex, ~negIndex)] = .0 Specific_test.append(NewSpec) # Generating the concatenate results Spec_test = list() for (Wtest, spec) in zip(WTe, Specific_test): TeHX = np.concatenate((TeHX, np.transpose(np.dot(np.transpose(Wtest), view - spec)))) if TeHX is not None else np.transpose(np.dot(np.transpose(Wtest),view - spec)) Spec_test.append(np.transpose(spec)) else: for vid, view in enumerate(TeX): product = np.dot(view, np.transpose(S)) U, _, V = lg.svd(product, full_matrices=False, check_finite=False) Wtest = np.dot(U,V) WTe.append(Wtest) TeHX = np.concatenate((TeHX, np.transpose(np.dot(np.transpose(Wtest),view)))) if TeHX is not None else np.transpose(np.dot(np.transpose(Wtest),view)) OutData[ui.txtOTeData.text()] = TeHX except Exception as e: print(e) HAParam = dict() if ui.cbSaveShare.isChecked(): HAParam["Share"] = S if SharedR: HAParam["Specific_train"] = Spec_train HAParam["Specific_test"] = Spec_test if ui.cbSaveMap.isChecked(): HAParam["WTrain"] = WTr HAParam["WTest"] = WTe HAParam["Model"] = Model OutData["FunctionalAlignment"] = HAParam OutData["Runtime"] = time.time() - tic totalTime += OutData["Runtime"] print("Saving ...") mainIO_save(OutData, OutFile) print("Fold " + str(fold_all) + " is DONE: " + OutFile) print("Runtime: ", totalTime) print(MessageString + "Autoregressive Shared Response Model is done.") msgBox.setText(MessageString + "Autoregressive Shared Response Model is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def 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:") print(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") # 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") # Subject ui.txtSubject.clear() HasDefualt = False for key in Keys: ui.txtSubject.addItem(key) if key == "subject": HasDefualt = True if HasDefualt: ui.txtSubject.setCurrentText("subject") # Run ui.txtRun.clear() HasDefualt = False for key in Keys: ui.txtRun.addItem(key) if key == "run": HasDefualt = True if HasDefualt: ui.txtRun.setCurrentText("run") # Counter ui.txtCounter.clear() HasDefualt = False for key in Keys: ui.txtCounter.addItem(key) if key == "counter": HasDefualt = True if HasDefualt: ui.txtCounter.setCurrentText("counter") # Task ui.txtTask.clear() HasDefualt = False for key in Keys: ui.txtTask.addItem(key) if key == "task": HasDefualt = True if HasDefualt: ui.txtTask.setCurrentText("task") 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() try: Threshold = np.float(ui.txtThre.text()) assert Threshold <= 1 assert Threshold >= 0 except: msgBox.setText("Threshold is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: EdgeThreshold = np.float(ui.txtEdgeThre.text()) assert EdgeThreshold <= 1 assert EdgeThreshold >= 0 except: msgBox.setText("Edge threshold is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: MetricCode = ui.txtMetric.toPlainText() allvars = dict(locals(), **globals()) exec(MetricCode, allvars, allvars) Metric = allvars['metric'] except Exception as e: msgBox.setText("Metric is wrong!\n" + str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: IntegrationCode = ui.txtIntegration.toPlainText() allvars = dict(locals(), **globals()) exec(IntegrationCode, allvars, allvars) Integration = allvars['integration'] except Exception as e: msgBox.setText("Integration is wrong!\n" + str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False AtlasPath = ui.txtAtlasPath.text() # Filter try: Filter = ui.txtFilter.text() if not len(Filter): Filter = None else: Filter = Filter.replace("\'", " ").replace(",", " ").replace( "[", "").replace("]", "").split() Filter = np.int32(Filter) except: print("Filter is wrong!") return # Region Filter try: RegionFilter = ui.txtRegions.text() if not len(RegionFilter): RegionFilter = [] else: RegionFilter = RegionFilter.replace("\'", " ").replace( ",", " ").replace("[", "").replace("]", "").split() RegionFilter = np.int32(RegionFilter) except: print("Region filter is wrong!") return # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData = dict() # AtlasFile AtlasFile = ui.txtAtlasFile.text() if not len(AtlasFile): msgBox.setText("Please enter atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(AtlasFile): msgBox.setText("Atlas file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: AtlasHDR = nb.load(AtlasFile) AtlasImg = np.asanyarray(AtlasHDR.dataobj) AtlasReg = np.unique(AtlasImg) if not 0 in RegionFilter: AtlasReg = AtlasReg[np.where(AtlasReg != 0)[0]] print("Region 0 is considered as rest mode!") AtlasShape = np.shape(AtlasImg) except: msgBox.setText("Cannot load atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # InFile InFile = ui.txtInFile.text() if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: print("Loading ...") InData = mainIO_load(InFile) imgShape = InData["imgShape"][0] except: print("Cannot load data!") return try: assert imgShape[0] == AtlasShape[0] assert imgShape[1] == AtlasShape[1] assert imgShape[2] == AtlasShape[2] except: msgBox.setText("Input file (" + str(imgShape).replace("]", "").replace("[", "") + ") and Atlas file " + str(AtlasShape).replace(",", "") + " must have the same shape!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Data if not len(ui.txtData.currentText()): msgBox.setText("Please enter Input Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtLabel.currentText()): msgBox.setText("Please enter Train Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Condition try: if not len(ui.txtCond.currentText()): Cond = 0 else: Cond = InData[ui.txtCond.currentText()] OutData[ui.txtCond.currentText()] = Cond labels = list() for con in Cond: labels.append(reshape_condition_cell(con[1])) labels = np.array(labels) except: msgBox.setText("Condition value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: X = InData[ui.txtData.currentText()] L = InData[ui.txtLabel.currentText()][0] if ui.cbScale.isChecked(): X = preprocessing.scale(X) print("Whole of data is scaled X~N(0,1).") except: print("Cannot load data or label") return try: if len(ui.txtCoord.currentText()): Coord = np.transpose(InData[ui.txtCoord.currentText()]) else: Coord = None except: msgBox.setText("Coordinate variable is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(L == fil)[0] X = np.delete(X, labelIndx, axis=0) L = np.delete(L, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") Net, ThrNet, ActiveRegions, A, ACoord, listL = ClassicNetworkAnalysis( X=X, L=L, Coord=Coord, Integration=Integration, Metric=Metric, AtlasImg=AtlasImg, affine=AtlasHDR.affine, KeepRegions=RegionFilter, AtlasPath=AtlasPath, Threshold=Threshold) Out = {} Out["Networks"] = Net Out["ThresholdNetworks"] = ThrNet Out["ActiveRegions"] = ActiveRegions Out["Atlas"] = A Out["Atlas_parcellation"] = ACoord Out["Atlas_affine"] = AtlasHDR.affine Out["condition"] = Cond Out["labels"] = listL Out["RunTime"] = time.time() - tStart print("Runtime (s): %f" % (Out["RunTime"])) print("Saving results ...") mainIO_save(Out, OutFile) print("Output is saved.") if ui.cbDiagram.isChecked(): for nnIndex, (nn, tnn) in enumerate(zip(Net, ThrNet)): try: Title = f"Label: {reshape_condition_cell(Cond[nnIndex][1])}" except: Title = f"Label: {nnIndex}" PlotConnectome(tnn, ACoord, Title, EdgeThreshold) fig = plt.figure() plt.imshow(np.transpose(nn)) plt.title(Title) plt.show() print("DONE.") msgBox.setText("Network analysis is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # InFile InFile = ui.txtInFile.text() if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) OutData = dict() OutData["imgShape"] = reshape_1Dvector(InData["imgShape"]) # Subject if not len(ui.txtSubject.currentText()): msgBox.setText("Please enter Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Subject = InData[ui.txtSubject.currentText()] OutData[ui.txtOSubject.text()] = reshape_1Dvector(Subject) except: print("Cannot load Subject ID") return if not len(ui.txtData.currentText()): msgBox.setText("Please enter Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: X = InData[ui.txtData.currentText()] if not ui.rbScale.isChecked(): X_new = preprocessing.scale(X) print("Whole of data is scaled X~N(0,1).") else: print("Partition data to subject level ...") SubjectUniq = np.unique(Subject) X_Sub = list() for subj in SubjectUniq: X_Sub.append(preprocessing.scale(X[np.where(Subject == subj)[1], :])) print("Data in subject level is scaled, X_" + str(subj) + "~N(0,1).") lenPCA = len(X_Sub) print("Data integration ... ") X_new = None for xsubindx, xsub in enumerate(X_Sub): X_new = np.concatenate((X_new, xsub)) if X_new is not None else xsub print("Integration: ", xsubindx + 1, " of ", lenPCA, " is done.") OutData[ui.txtOData.text()] = X_new except: print("Cannot load data") return # Label if not len(ui.txtLabel.currentText()): msgBox.setText("Please enter Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOLabel.text()] = reshape_1Dvector(InData[ui.txtLabel.currentText()]) # Task if ui.cbTask.isChecked(): if not len(ui.txtTask.currentText()): msgBox.setText("Please enter Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOTask.text()] = reshape_1Dvector(InData[ui.txtTask.currentText()]) # Run if ui.cbRun.isChecked(): if not len(ui.txtRun.currentText()): msgBox.setText("Please enter Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtORun.text()] = reshape_1Dvector(InData[ui.txtRun.currentText()]) # Counter if ui.cbCounter.isChecked(): if not len(ui.txtCounter.currentText()): msgBox.setText("Please enter Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOCounter.text()] = reshape_1Dvector(InData[ui.txtCounter.currentText()]) # Matrix Label if ui.cbmLabel.isChecked(): if not len(ui.txtmLabel.currentText()): msgBox.setText("Please enter Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()] # Design if ui.cbDM.isChecked(): if not len(ui.txtDM.currentText()): msgBox.setText("Please enter Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()] # Coordinate if ui.cbCol.isChecked(): if not len(ui.txtCol.currentText()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()] # Condition if ui.cbCond.isChecked(): if not len(ui.txtCond.currentText()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()] # Number of Scan if ui.cbNScan.isChecked(): if not len(ui.txtScan.currentText()): msgBox.setText("Please enter Number of Scan variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOScan.text()] = reshape_1Dvector(InData[ui.txtScan.currentText()]) print("Saving ...") mainIO_save(OutData, ui.txtOutFile.text()) #io.savemat(ui.txtOutFile.text(), mdict=OutData) print("DONE.") msgBox.setText("Normalization is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def 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: data = mainIO_load(filename) Keys = data.keys() # Train Filter ui.txtFilterTrID.clear() for key in Keys: ui.txtFilterTrID.addItem(key) # Test Filter ui.txtFilterTeID.clear() for key in Keys: ui.txtFilterTeID.addItem(key) # Train Data ui.txtITrData.clear() HasDefualt = False for key in Keys: ui.txtITrData.addItem(key) if key == "train_data": HasDefualt = True if HasDefualt: ui.txtITrData.setCurrentText("train_data") # Test Data ui.txtITeData.clear() HasDefualt = False for key in Keys: ui.txtITeData.addItem(key) if key == "test_data": HasDefualt = True if HasDefualt: ui.txtITeData.setCurrentText("test_data") # Train Label ui.txtITrLabel.clear() HasDefualt = False for key in Keys: ui.txtITrLabel.addItem(key) if key == "train_label": HasDefualt = True if HasDefualt: ui.txtITrLabel.setCurrentText("train_label") # Test Label ui.txtITeLabel.clear() ui.txtClass.clear() HasDefualt = False for key in Keys: ui.txtITeLabel.addItem(key) if key == "test_label": HasDefualt = True if HasDefualt: ui.txtITeLabel.setCurrentText("test_label") # set number of features Labels = data[ui.txtITrLabel.currentText()] Labels = np.unique(Labels) for lbl in Labels: ui.txtClass.append(str(lbl)) # FoldID ui.txtFoldID.clear() HasDefualt = False for key in Keys: ui.txtFoldID.addItem(key) if key == "FoldID": HasDefualt = True if HasDefualt: ui.txtFoldID.setCurrentText("FoldID") ui.lbFoldID.setText( "ID=" + str(data[ui.txtFoldID.currentText()][0][0])) # Coordinate ui.txtCol.clear() HasDefualt = False for key in Keys: ui.txtCol.addItem(key) if key == "coordinate": HasDefualt = True if HasDefualt: ui.txtCol.setCurrentText("coordinate") # ImgShape ui.txtImg.clear() HasDefualt = False for key in Keys: ui.txtImg.addItem(key) if key == "imgShape": HasDefualt = True if HasDefualt: ui.txtImg.setCurrentText("imgShape") try: print( f'Input image shape is {tuple(data["imgShape"][0])}' ) except: print("Cannot find ImgShape") ui.txtInFile.setText(filename) except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
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: 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") # mLabel ui.txtmLabel.clear() HasDefualt = False for key in Keys: ui.txtmLabel.addItem(key) if key == "mlabel": HasDefualt = True if HasDefualt: ui.txtmLabel.setCurrentText("mlabel") ui.cbmLabel.setChecked(HasDefualt) # Coordinate ui.txtCol.clear() HasDefualt = False for key in Keys: ui.txtCol.addItem(key) if key == "coordinate": HasDefualt = True if HasDefualt: ui.txtCol.setCurrentText("coordinate") ui.cbCol.setChecked(HasDefualt) # Design ui.txtDM.clear() HasDefualt = False for key in Keys: ui.txtDM.addItem(key) if key == "design": HasDefualt = True if HasDefualt: ui.txtDM.setCurrentText("design") ui.cbDM.setChecked(HasDefualt) # Subject ui.txtSubject.clear() HasDefualt = False for key in Keys: ui.txtSubject.addItem(key) if key == "subject": HasDefualt = True if HasDefualt: ui.txtSubject.setCurrentText("subject") # Task ui.txtTask.clear() HasDefualt = False for key in Keys: ui.txtTask.addItem(key) if key == "task": HasDefualt = True if HasDefualt: ui.txtTask.setCurrentText("task") ui.cbTask.setChecked(HasDefualt) # Run ui.txtRun.clear() HasDefualt = False for key in Keys: ui.txtRun.addItem(key) if key == "run": HasDefualt = True if HasDefualt: ui.txtRun.setCurrentText("run") ui.cbRun.setChecked(HasDefualt) # Counter ui.txtCounter.clear() HasDefualt = False for key in Keys: ui.txtCounter.addItem(key) if key == "counter": HasDefualt = True if HasDefualt: ui.txtCounter.setCurrentText("counter") ui.cbCounter.setChecked(HasDefualt) # 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") ui.cbCond.setChecked(HasDefualt) # NScan ui.txtScan.clear() HasDefualt = False for key in Keys: ui.txtScan.addItem(key) if key == "nscan": HasDefualt = True if HasDefualt: ui.txtScan.setCurrentText("nscan") ui.cbNScan.setChecked(HasDefualt) # set number of features XShape = np.shape(data[ui.txtData.currentText()]) print("Data shape: ", XShape) if len(XShape) != 4: msgBox = QMessageBox() msgBox.setText( "This method just supports 4D datasets!") msgBox.setIcon(QMessageBox.Warning) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() else: print("Data shape is okay.") 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() print("Loading...") try: FoldFrom = np.int32(ui.txtFoldFrom.text()) FoldTo = np.int32(ui.txtFoldTo.text()) except: print("Please check fold parameters!") return # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Atlas InAtlas = ui.txtAtlas.text() if not len(InAtlas): msgBox.setText("Please enter atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InAtlas): msgBox.setText("Atlas file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: AtlasHdr = nb.load(InAtlas) AtlasImg = np.asanyarray(AtlasHdr.dataobj) AtlasShape = np.shape(AtlasImg) if np.shape(AtlasShape)[0] != 3: msgBox.setText("Atlas must be 3D image") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("Cannot load atlas file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Filter try: Filter = ui.txtFilter.text() if not len(Filter): Filter = None else: Filter = Filter.replace("\'", " ").replace(",", " ").replace( "[", "").replace("]", "").split() Filter = np.int32(Filter) except: print("Class Filter is wrong!") msgBox.setText("Class filter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return MappingVectorRegion = None CodeText = ui.txtEvents.toPlainText() eCodeText = ui.txtEvents2.toPlainText() Fold = list() accuracy = list() precision = list() average_precision = list() f1score = list() recall = list() accuracyTr = list() precisionTr = list() average_precisionTr = list() f1scoreTr = list() recallTr = list() InFileList = list() OutData = dict() OutData["ModelAnalysis"] = "Atlas-based ensemble" for fold in range(FoldFrom, FoldTo + 1): print(f"Analyzing Fold: {fold}...") # InFile InFile = ui.txtInFile.text() if not len(InFile): msgBox.setText(f"FOLD {fold}: Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InFile = InFile.replace("$FOLD$", str(fold)) InFileList.append(InFile) if not os.path.isfile(InFile): msgBox.setText(f"FOLD {fold}: Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Load InData try: InData = mainIO_load(InFile) except: print(f"FOLD {fold}: Cannot load file: {InFile}") return False # Data if not len(ui.txtITrData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeData.currentText()): msgBox.setText("Please enter Input Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtITrLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeLabel.currentText()): msgBox.setText("Please enter Test Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrX = InData[ui.txtITrData.currentText()] except: print(f"FOLD {fold}: Cannot load train data") msgBox.setText(f"FOLD {fold}: Cannot load train data!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TeX = InData[ui.txtITeData.currentText()] except: print(f"FOLD {fold}: Cannot load test data") msgBox.setText(f"FOLD {fold}: Cannot load train data!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: TrL = InData[ui.txtITrLabel.currentText()][0] except: print(f"FOLD {fold}: Cannot load label") return try: TeL = InData[ui.txtITeLabel.currentText()][0] except: print(f"FOLD {fold}: Cannot load test label") return # Ref Filter Train if ui.cbFilterTrID.isChecked(): try: # Create content structure for training TrFilterContent = ui.txtFilterTrContent.text() TrFilterContent = TrFilterContent.replace(" ", "") if not len(TrFilterContent): print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TrFilterContent = TrFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TrFilterContent = np.int32(TrFilterContent) # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] if np.shape(TrX)[0] != np.shape(TrF)[0] or np.shape( TrL)[0] != np.shape(TrF)[0]: print( "Shape of reference for training must be the same as data and label" ) msgBox.setText( "Shape of reference for training must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for training is wrong!") msgBox.setText("Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove training set try: print( "Removing training set based on reference. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) for content in TrFilterContent: contIndex = np.where(TrF == content)[0] TrL = np.delete(TrL, contIndex, axis=0) TrX = np.delete(TrX, contIndex, axis=0) TrF = np.delete(TrF, contIndex, axis=0) print(" Content", content, "is removed from training set. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) except Exception as e: print("Cannot filter the training set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the training set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Create content structure for testing TeFilterContent = ui.txtFilterTeContent.text() TeFilterContent = TeFilterContent.replace(" ", "") if not len(TeFilterContent): print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TeFilterContent = TeFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TeFilterContent = np.int32(TeFilterContent) # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] if np.shape(TeX)[0] != np.shape(TeF)[0] or np.shape( TeL)[0] != np.shape(TeF)[0]: print( "Shape of reference for testing must be the same as data and label" ) msgBox.setText( "Shape of reference for testing must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for testing is wrong!") msgBox.setText("Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove testing set try: print( "Removing testing set based on reference. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) for content in TeFilterContent: contIndex = np.where(TeF == content)[0] TeL = np.delete(TeL, contIndex, axis=0) TeX = np.delete(TeX, contIndex, axis=0) TeF = np.delete(TeF, contIndex, axis=0) print(" Content", content, "is removed from testing set. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) except Exception as e: print("Cannot filter the testing set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the testing set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(TrL == fil)[0] TrL = np.delete(TrL, labelIndx, axis=0) TrX = np.delete(TrX, labelIndx, axis=0) # Remove Testing Set labelIndx = np.where(TeL == fil)[0] TeL = np.delete(TeL, labelIndx, axis=0) TeX = np.delete(TeX, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): TrX = preprocessing.scale(TrX) TeX = preprocessing.scale(TeX) print( "Whole of data is scaled Train~N(0,1) and Test~N(0,1)." ) except: print("Cannot load data or label") return # FoldID if not len(ui.txtFoldID.currentText()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: currFID = InData[ui.txtFoldID.currentText()][0][0] Fold.append(currFID) except: print("Cannot load Fold ID!") return # Creating Mapping Dictionary for Atlas if MappingVectorRegion is None: # Coordinate if not len(ui.txtCol.currentText()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Coord = np.transpose(InData[ui.txtCol.currentText()]) except: print("Cannot load coordinate") return # imgShape if not len(ui.txtImg.currentText()): msgBox.setText("Please enter Image Shape variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: DataShape = tuple(InData[ui.txtImg.currentText()][0]) except: print("Cannot load data image shape") return # Check shape if AtlasShape[0] != DataShape[0] or AtlasShape[1] != DataShape[ 1] or AtlasShape[2] != DataShape[2]: print( f'Atlas and data must have the same shape.\nAtlas: {AtlasShape} vs. Data: {DataShape}' ) msgBox.setText( f'Atlas and data must have the same shape.\nAtlas: {AtlasShape} vs. Data: {DataShape}' ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False print("Mapping 2D vector space to 3D region area...") MappingVectorRegion = dict() for cooIdx, coo in enumerate(Coord): reg = AtlasImg[coo[0], coo[1], coo[2]] if reg == 0: print(f'{tuple(coo)} is belong to black area.') else: try: MappingVectorRegion[str(reg)].append(cooIdx) except: MappingVectorRegion[str(reg)] = list() MappingVectorRegion[str(reg)].append(cooIdx) print( f'{tuple(coo)}:{cooIdx} is belong to region {reg}') print(f"FOLD {fold}: analyzing regions...") baseTrX = list() baseTeX = list() for reg in sorted(MappingVectorRegion.keys()): RegIndex = MappingVectorRegion[reg] SubXtr = TrX[:, RegIndex] SubXte = TeX[:, RegIndex] try: allvars = dict(locals(), **globals()) exec(CodeText, allvars, allvars) model = allvars['model'] model.fit(SubXtr, TrL) baseTrX.append(model.predict(SubXtr)) TeP = model.predict(SubXte) baseTeX.append(TeP) bacc = accuracy_score(TeL, TeP) except Exception as e: print(f'Cannot generate model\n{e}') msgBox.setText(f'Cannot generate model\n{e}') msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False print(f"FOLD {fold}: Linear accuracy for {reg} is {bacc}") baseTrX = np.transpose(baseTrX) baseTeX = np.transpose(baseTeX) try: print(f"FOLD {fold}: Creating ensemble model ...") allvars = dict(locals(), **globals()) exec(eCodeText, allvars, allvars) emodel = allvars['emodel'] emodel.fit(baseTrX, TrL) PrL = emodel.predict(baseTrX) PeL = emodel.predict(baseTeX) except Exception as e: print(f"Cannot generate the final model\n{e}") msgBox.setText(f"Cannot generate the final model\n{e}") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if ui.cbAverage.isChecked(): acc = accuracy_score(TeL, PeL) accTr = accuracy_score(TrL, PrL) accuracy.append(acc) accuracyTr.append(accTr) print( "FoldID = {:d}, Average Train {:5.2f} Test {:5.2f}" .format(currFID, accTr * 100, acc * 100)) if ui.cbPrecision.isChecked(): pre = precision_score(TeL, PeL, average=ui.cbPrecisionAvg.currentData()) preTr = precision_score( TrL, PrL, average=ui.cbPrecisionAvg.currentData()) precision.append(pre) precisionTr.append(preTr) print( "FoldID = {:d}, Precision Train {:5.2f} Test {:5.2f}" .format(currFID, preTr * 100, pre * 100)) if ui.cbAPrecision.isChecked(): prA = average_precision_score( TeL, PeL, average=ui.cbAPrecisionAvg.currentData()) prATr = average_precision_score( TrL, PrL, average=ui.cbAPrecisionAvg.currentData()) average_precision.append(prA) average_precisionTr.append(prATr) print( "FoldID = {:d}, Average Precision: Train {:5.2f} Test {:5.2f}" .format(currFID, prATr * 100, prA * 100)) if ui.cbRecall.isChecked(): rec = recall_score(TeL, PeL, average=ui.cbRecallAvg.currentData()) recTr = recall_score(TrL, PrL, average=ui.cbRecallAvg.currentData()) recall.append(rec) recallTr.append(recTr) print( "FoldID = {:d}, Recall: Train {:5.2f} Test {:5.2f}" .format(currFID, recTr * 100, rec * 100)) if ui.cbF1.isChecked(): f1 = f1_score(TeL, PeL, average=ui.cbF1Avg.currentData()) f1Tr = f1_score(TrL, PrL, average=ui.cbF1Avg.currentData()) f1score.append(f1) f1scoreTr.append(f1Tr) print( "FoldID = {:d}, F1: Train {:5.2f} Test {:5.2f}" .format(currFID, f1Tr * 100, f1 * 100)) print("FoldID = " + str(currFID) + " is analyzed!") if ui.cbAverage.isChecked(): OutData["FoldAccuracy"] = accuracy MeanAcc = np.mean(accuracy) OutData["MeanTestAccuracy"] = MeanAcc STDAcc = np.std(accuracy) OutData["StdTestAccuracy"] = STDAcc MeanAccTr = np.mean(accuracyTr) OutData["MeanTrainAccuracy"] = MeanAccTr STDAccTr = np.std(accuracyTr) OutData["StdTrainAccuracy"] = STDAccTr print( "Accuracy: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAccTr * 100, STDAccTr, MeanAcc * 100, STDAcc)) if ui.cbPrecision.isChecked(): OutData["ModePrecision"] = ui.cbPrecisionAvg.currentText() OutData["FoldPrecision"] = precision MeanPre = np.mean(precision) OutData["MeanTrainPrecision"] = MeanPre STDPre = np.std(precision) OutData["StdTrainPrecision"] = STDPre MeanPreTr = np.mean(precisionTr) OutData["MeanTestPrecision"] = MeanPreTr STDPreTr = np.std(precisionTr) OutData["StdTestPrecision"] = STDPreTr print( "Precision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanPreTr * 100, STDPreTr, MeanPre * 100, STDPre)) if ui.cbAPrecision.isChecked(): OutData["ModeAveragePrecision"] = ui.cbAPrecisionAvg.currentText() OutData["FoldAveragePrecision"] = average_precision MeanAPre = np.mean(average_precision) OutData["MeanTrainAveragePrecision"] = MeanAPre STDAPre = np.std(average_precision) OutData["StdTestAveragePrecision"] = STDAPre MeanAPreTr = np.mean(average_precisionTr) OutData["MeanTrainAveragePrecision"] = MeanAPreTr STDAPreTr = np.std(average_precisionTr) OutData["StdTrainAveragePrecision"] = STDAPreTr print( "AveragePrecision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAPreTr * 100, STDAPreTr, MeanAPre * 100, STDAPre)) if ui.cbRecall.isChecked(): OutData["ModeRecall"] = ui.cbRecallAvg.currentText() OutData["FoldRecall"] = recall MeanRec = np.mean(recall) OutData["MeanTestRecall"] = MeanRec STDRec = np.std(recall) OutData["StdTestRecall"] = STDRec MeanRecTr = np.mean(recallTr) OutData["MeanTrainRecall"] = MeanRecTr STDRecTr = np.std(recallTr) OutData["StdTrainRecall"] = STDRecTr print( "Recall: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanRecTr * 100, STDRecTr, MeanRec * 100, STDRec)) if ui.cbF1.isChecked(): OutData["ModeF1"] = ui.cbF1Avg.currentText() OutData["FoldF1"] = f1score MeanF1 = np.mean(f1score) OutData["MeanTestF1"] = MeanF1 STDF1 = np.std(f1score) OutData["StdTestF1"] = STDF1 MeanF1Tr = np.mean(f1scoreTr) OutData["MeanTrainF1"] = MeanF1Tr STDF1Tr = np.std(f1scoreTr) OutData["StdTrainF1"] = STDF1Tr print( "F1: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanF1Tr * 100, STDF1Tr, MeanF1 * 100, STDF1)) OutData["InputFiles"] = InFileList print("Saving ...") mainIO_save(OutData, ui.txtOutFile.text()) print("DONE.") msgBox.setText("Atlas-based ensemble analysis is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnInFile_click(self): msgBox = QMessageBox() 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: data = mainIO_load(filename) Keys = data.keys() ui.lbMatrix.setText("Image Shape=" + str(data["imgShape"][0])) cooShape = None dataShape = None # Train Data ui.txtMatrix.clear() HasDefualt = None for key in Keys: ui.txtMatrix.addItem(key) if key == "train_data" or key == "data" or key == "Beta" or key == "sharedvoxelspace": HasDefualt = key dataShape = np.shape(data[key]) ui.txtTime.setText("[0-" + str(dataShape[0] - 1) + "]") if HasDefualt is not None: ui.txtMatrix.setCurrentText(HasDefualt) # Test Data ui.txtCoord.clear() HasDefualt = False for key in Keys: ui.txtCoord.addItem(key) if key == "coordinate": ui.txtCoord.setCurrentText("coordinate") cooShape = np.shape(data[key][0])[0] ui.lbCoord.setText("ROI Size=" + str(cooShape)) if not cooShape == dataShape[1]: print( "WARNING: Coordinate size is not matched by data size!" ) msgBox.setText( "Coordinate size is not matched by data size!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() if cooShape is None: print("WARNING: Cannot find coordinate!") if dataShape is None: print("WARNING: Cannot find data matrix!") ui.txtInFile.setText(filename) except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
def btnShowFilterContent_click(self): msgBox = QMessageBox() filename = ui.txtInFile.text() if len(filename): if os.path.isfile(filename): try: InData = mainIO_load(filename) if ui.cbFilterTrID.isChecked(): try: # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] fstr = "" fconnect = "" for filter in np.unique(TrF): fstr += fconnect fstr += str(filter) fconnect = ", " print("Training Reference Content:", fstr) except: print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] fstr = "" fconnect = "" for filter in np.unique(TeF): fstr += fconnect fstr += str(filter) fconnect = ", " print("Testing Reference Content:", fstr) except: print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
def btnConvert_click(self): msgBox = QMessageBox() # CType CType = ui.cbCType.currentData() # IParams IParams = ui.cbIParams.currentData() # Warm Start WStart = ui.cbWarmStart.isChecked() # NCluster try: NComponent = np.int32(ui.txtNComponent.text()) except: msgBox.setText("Number of Component is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # NInit try: NInit = np.int32(ui.txtNInit.text()) except: msgBox.setText("Number of init is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Tol try: Tol = np.float(ui.txtTole.text()) except: msgBox.setText("Tolerance is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # MaxIter try: MaxIter = np.int32(ui.txtMaxIter.text()) except: msgBox.setText("Max Iteration is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Regularization try: Regularization = np.float(ui.txtRegularization.text()) except: msgBox.setText("Regularization is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Verbose try: Verbose = np.int(ui.txtVerbose.text()) except: msgBox.setText("Verbose is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # IVerbose try: IVerbose = np.int(ui.txtIVerbose.text()) except: msgBox.setText("Verbose Interval is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Filter try: Filter = ui.txtFilter.text() if not len(Filter): Filter = None else: Filter = Filter.replace("\'", " ").replace(",", " ").replace( "[", "").replace("]", "").split() Filter = np.int32(Filter) except: print("Filter is wrong!") return # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData = dict() OutData["ModelAnalysis"] = "Gaussian Mixture" # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None # InFile InFile = ui.txtInFile.text() if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) # Data if not len(ui.txtData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False X = InData[ui.txtData.currentText()] L = InData[ui.txtLabel.currentText()][0] try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(L == fil)[0] L = np.delete(L, labelIndx, axis=0) X = np.delete(X, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): X = preprocessing.scale(X) print("Whole of data is scaled to N(0,1).") except: print("Cannot load data or label") return try: cls = GaussianMixture(n_components=NComponent,covariance_type=CType,tol=Tol,reg_covar=Regularization,\ max_iter=MaxIter,n_init=NInit,init_params=IParams,\ warm_start=WStart,verbose=Verbose,verbose_interval=IVerbose) print("Run Clustering ...") cls.fit(X) PeL = cls.predict(X) except Exception as e: print(e) msgBox = QMessageBox() msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return OutData["predict"] = PeL if OutModel is not None: joblib.dump(cls, OutModel) print("Model is saved: " + OutModel) if ui.cbAverage.isChecked(): acc = accuracy_score(L, PeL) OutData["Accuracy"] = acc print("Average {:5.2f}".format(acc * 100)) if ui.cbNMI.isChecked(): NMI = normalized_mutual_info_score(L, PeL) OutData["NMI"] = NMI print("Normalized Mutual Information (NMI) {:7.6f}".format(NMI)) if ui.cbRIA.isChecked(): RIA = adjusted_rand_score(L, PeL) OutData["RIA"] = RIA print("Rand Index Adjusted (RIA) {:7.6f}".format(RIA)) if ui.cbAMI.isChecked(): AMI = adjusted_mutual_info_score(L, PeL) OutData["AMI"] = AMI print("Adjusted Mutual Information (AMI) {:7.6f}".format(AMI)) print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Gaussian Mixture Clustering is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): tme = time.time() msgBox = QMessageBox() try: FoldFrom = np.int32(ui.txtFoldFrom.text()) FoldTo = np.int32(ui.txtFoldTo.text()) except: print("Please check fold parameters!") return if FoldTo < FoldFrom: print("Please check fold parameters!") return # Shuffle Shuffle = ui.cbShuffle.isChecked() # FitIntercept FitIntercept = ui.cbFitIntercept.isChecked() # WarmStart WarmStart = ui.cbWarmStart.isChecked() # NesterovsMomentum AverageParm = ui.cbAverageParm.isChecked() # Loss Loss = ui.cbLoss.currentText() # Penalty Penalty = ui.cbPenalty.currentText() # Learning Rate LearningRate = ui.cbLearningRate.currentText() # Alpha try: Alpha = np.float(ui.txtAlpha.text()) except: msgBox.setText("Alpha is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # L1Rate try: L1Rate = np.float(ui.txtL1Rate.text()) except: msgBox.setText("L1 Rate is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Eta0 try: Eta0 = np.double(ui.txtEta0.text()) except: msgBox.setText("Eta 0 Rate is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Powert try: Powert = np.double(ui.txtPowert.text()) except: msgBox.setText("Power_t is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Tol try: Tol = np.float(ui.txtTol.text()) if Tol == 0: Tol = None except: msgBox.setText("Tolerance is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # MaxIte try: MaxIter = np.int32(ui.txtMaxIter.text()) if MaxIter <= 0: msgBox.setText("Maximum number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("Maximum number of iterations is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Epsilon try: Epsilon = np.float(ui.txtEpsilon.text()) except: msgBox.setText("Epsilon is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Epoach try: NIterNoChange = np.int32(ui.txtEpochs.text()) if NIterNoChange <= 0: msgBox.setText("N Iteration No Change must be positive!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False except: msgBox.setText("N Iteration No Change is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Verbose try: Verbose = np.int32(ui.txtVerbose.text()) except: msgBox.setText("Verbose is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # NJob try: NJobs = np.int32(ui.txtNJobs.text()) if NJobs <= 0: NJobs = -1 except: msgBox.setText("n_jobs is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Filter try: Filter = ui.txtFilter.text() if not len(Filter): Filter = None else: Filter = Filter.replace("\'", " ").replace(",", " ").replace( "[", "").replace("]", "").split() Filter = np.int32(Filter) except: print("Class filter is wrong!") msgBox.setText("Class filter is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False Fold = list() accuracy = list() precision = list() average_precision = list() f1score = list() recall = list() accuracyTr = list() precisionTr = list() average_precisionTr = list() f1scoreTr = list() recallTr = list() InFileList = list() OutData = dict() OutData[ "ModelAnalysis"] = "Stochastic Gradient Descent based Classifier" for fold in range(FoldFrom, FoldTo + 1): # OutModel OutModel = ui.txtOutModel.text() if not len(OutModel): OutModel = None else: OutModel = OutModel.replace("$FOLD$", str(fold)) # InFile InFile = ui.txtInFile.text() InFile = InFile.replace("$FOLD$", str(fold)) InFileList.append(InFile) if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) # Data if not len(ui.txtITrData.currentText()): msgBox.setText("Please enter Input Train Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeData.currentText()): msgBox.setText("Please enter Input Test Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # Label if not len(ui.txtITrLabel.currentText()): msgBox.setText("Please enter Train Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not len(ui.txtITeLabel.currentText()): msgBox.setText("Please enter Test Input Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrX = InData[ui.txtITrData.currentText()] TeX = InData[ui.txtITeData.currentText()] TrL = InData[ui.txtITrLabel.currentText()][0] TeL = InData[ui.txtITeLabel.currentText()][0] # Ref Filter Train if ui.cbFilterTrID.isChecked(): try: # Create content structure for training TrFilterContent = ui.txtFilterTrContent.text() TrFilterContent = TrFilterContent.replace(" ", "") if not len(TrFilterContent): print("Reference filter for training is wrong!") msgBox.setText( "Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TrFilterContent = TrFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TrFilterContent = np.int32(TrFilterContent) # Check Filter ID for training if not len(ui.txtFilterTrID.currentText()): msgBox.setText( "Please enter variable name for training filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TrF = InData[ui.txtFilterTrID.currentText()][0] if np.shape(TrX)[0] != np.shape(TrF)[0] or np.shape( TrL)[0] != np.shape(TrF)[0]: print( "Shape of reference for training must be the same as data and label" ) msgBox.setText( "Shape of reference for training must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for training is wrong!") msgBox.setText("Reference filter for training is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove training set try: print( "Removing training set based on reference. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) for content in TrFilterContent: contIndex = np.where(TrF == content)[0] TrL = np.delete(TrL, contIndex, axis=0) TrX = np.delete(TrX, contIndex, axis=0) TrF = np.delete(TrF, contIndex, axis=0) print(" Content", content, "is removed from training set. Data shape: ", np.shape(TrX), "Label shape: ", np.shape(TrL)) except Exception as e: print("Cannot filter the training set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the training set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Ref Filter Test if ui.cbFilterTeID.isChecked(): try: # Create content structure for testing TeFilterContent = ui.txtFilterTeContent.text() TeFilterContent = TeFilterContent.replace(" ", "") if not len(TeFilterContent): print("Reference filter for testing is wrong!") msgBox.setText( "Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return else: TeFilterContent = TeFilterContent.replace( "\'", " ").replace(",", " ").replace("[", "").replace("]", "").split() TeFilterContent = np.int32(TeFilterContent) # Check Filter ID for testing if not len(ui.txtFilterTeID.currentText()): msgBox.setText( "Please enter variable name for testing filter!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False TeF = InData[ui.txtFilterTeID.currentText()][0] if np.shape(TeX)[0] != np.shape(TeF)[0] or np.shape( TeL)[0] != np.shape(TeF)[0]: print( "Shape of reference for testing must be the same as data and label" ) msgBox.setText( "Shape of reference for testing must be the same as data and label" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return except: print("Reference filter for testing is wrong!") msgBox.setText("Reference filter for testing is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # Remove testing set try: print( "Removing testing set based on reference. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) for content in TeFilterContent: contIndex = np.where(TeF == content)[0] TeL = np.delete(TeL, contIndex, axis=0) TeX = np.delete(TeX, contIndex, axis=0) TeF = np.delete(TeF, contIndex, axis=0) print(" Content", content, "is removed from testing set. Data shape: ", np.shape(TeX), "Label shape: ", np.shape(TeL)) except Exception as e: print("Cannot filter the testing set based on Reference") print(str(e)) msgBox.setText( "Cannot filter the testing set based on Reference") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: if Filter is not None: for fil in Filter: # Remove Training Set labelIndx = np.where(TrL == fil)[0] TrL = np.delete(TrL, labelIndx, axis=0) TrX = np.delete(TrX, labelIndx, axis=0) # Remove Testing Set labelIndx = np.where(TeL == fil)[0] TeL = np.delete(TeL, labelIndx, axis=0) TeX = np.delete(TeX, labelIndx, axis=0) print("Class ID = " + str(fil) + " is removed from data.") if ui.cbScale.isChecked(): TrX = preprocessing.scale(TrX) TeX = preprocessing.scale(TeX) print( "Whole of data is scaled Train~N(0,1) and Test~N(0,1)." ) except: print("Cannot load data or label") return # FoldID if not len(ui.txtFoldID.currentText()): msgBox.setText("Please enter FoldID variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: currFID = InData[ui.txtFoldID.currentText()][0][0] Fold.append(currFID) except: print("Cannot load Fold ID!") return try: clf = SGDClassifier(loss=Loss,penalty=Penalty,alpha=Alpha,l1_ratio=L1Rate,fit_intercept=FitIntercept,\ max_iter=MaxIter,tol=Tol,shuffle=Shuffle,verbose=Verbose,epsilon=Epsilon,\ n_jobs=NJobs,learning_rate=LearningRate,eta0=Eta0,power_t=Powert,\ warm_start=WarmStart,average=AverageParm,n_iter_no_change=NIterNoChange) print("FoldID = " + str(currFID) + " is training ...") clf.fit(TrX, TrL) if OutModel is not None: joblib.dump(clf, OutModel) print("FoldID = " + str(currFID) + " Model is saved: " + OutModel) print("FoldID = " + str(currFID) + " is testing ...") PeL = clf.predict(TeX) PrL = clf.predict(TrX) OutData["fold" + str(currFID) + "_confusion_matrix"] = confusion_matrix( TeL, PeL, np.unique(TeL)) OutData["fold" + str(currFID) + "_classification_report"] = classification_report( TeL, PeL) print(OutData["fold" + str(currFID) + "_classification_report"]) except Exception as e: msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if ui.cbAverage.isChecked(): acc = accuracy_score(TeL, PeL) accTr = accuracy_score(TrL, PrL) accuracy.append(acc) accuracyTr.append(accTr) print( "FoldID = {:d}, Average Train {:5.2f} Test {:5.2f}" .format(currFID, accTr * 100, acc * 100)) if ui.cbPrecision.isChecked(): pre = precision_score(TeL, PeL, average=ui.cbPrecisionAvg.currentData()) preTr = precision_score( TrL, PrL, average=ui.cbPrecisionAvg.currentData()) precision.append(pre) precisionTr.append(preTr) print( "FoldID = {:d}, Precision Train {:5.2f} Test {:5.2f}" .format(currFID, preTr * 100, pre * 100)) if ui.cbAPrecision.isChecked(): prA = average_precision_score( TeL, PeL, average=ui.cbAPrecisionAvg.currentData()) prATr = average_precision_score( TrL, PrL, average=ui.cbAPrecisionAvg.currentData()) average_precision.append(prA) average_precisionTr.append(prATr) print( "FoldID = {:d}, Average Precision: Train {:5.2f} Test {:5.2f}" .format(currFID, prATr * 100, prA * 100)) if ui.cbRecall.isChecked(): rec = recall_score(TeL, PeL, average=ui.cbRecallAvg.currentData()) recTr = recall_score(TrL, PrL, average=ui.cbRecallAvg.currentData()) recall.append(rec) recallTr.append(recTr) print( "FoldID = {:d}, Recall: Train {:5.2f} Test {:5.2f}" .format(currFID, recTr * 100, rec * 100)) if ui.cbF1.isChecked(): f1 = f1_score(TeL, PeL, average=ui.cbF1Avg.currentData()) f1Tr = f1_score(TrL, PrL, average=ui.cbF1Avg.currentData()) f1score.append(f1) f1scoreTr.append(f1Tr) print( "FoldID = {:d}, F1: Train {:5.2f} Test {:5.2f}" .format(currFID, f1Tr * 100, f1 * 100)) print("FoldID = " + str(currFID) + " is analyzed!") if ui.cbAverage.isChecked(): OutData["FoldAccuracy"] = accuracy MeanAcc = np.mean(accuracy) OutData["MeanTestAccuracy"] = MeanAcc STDAcc = np.std(accuracy) OutData["StdTestAccuracy"] = STDAcc MeanAccTr = np.mean(accuracyTr) OutData["MeanTrainAccuracy"] = MeanAccTr STDAccTr = np.std(accuracyTr) OutData["StdTrainAccuracy"] = STDAccTr print( "Accuracy: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAccTr * 100, STDAccTr, MeanAcc * 100, STDAcc)) if ui.cbPrecision.isChecked(): OutData["ModePrecision"] = ui.cbPrecisionAvg.currentText() OutData["FoldPrecision"] = precision MeanPre = np.mean(precision) OutData["MeanTrainPrecision"] = MeanPre STDPre = np.std(precision) OutData["StdTrainPrecision"] = STDPre MeanPreTr = np.mean(precisionTr) OutData["MeanTestPrecision"] = MeanPreTr STDPreTr = np.std(precisionTr) OutData["StdTestPrecision"] = STDPreTr print( "Precision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanPreTr * 100, STDPreTr, MeanPre * 100, STDPre)) if ui.cbAPrecision.isChecked(): OutData["ModeAveragePrecision"] = ui.cbAPrecisionAvg.currentText() OutData["FoldAveragePrecision"] = average_precision MeanAPre = np.mean(average_precision) OutData["MeanTrainAveragePrecision"] = MeanAPre STDAPre = np.std(average_precision) OutData["StdTestAveragePrecision"] = STDAPre MeanAPreTr = np.mean(average_precisionTr) OutData["MeanTrainAveragePrecision"] = MeanAPreTr STDAPreTr = np.std(average_precisionTr) OutData["StdTrainAveragePrecision"] = STDAPreTr print( "AveragePrecision: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanAPreTr * 100, STDAPreTr, MeanAPre * 100, STDAPre)) if ui.cbRecall.isChecked(): OutData["ModeRecall"] = ui.cbRecallAvg.currentText() OutData["FoldRecall"] = recall MeanRec = np.mean(recall) OutData["MeanTestRecall"] = MeanRec STDRec = np.std(recall) OutData["StdTestRecall"] = STDRec MeanRecTr = np.mean(recallTr) OutData["MeanTrainRecall"] = MeanRecTr STDRecTr = np.std(recallTr) OutData["StdTrainRecall"] = STDRecTr print( "Recall: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanRecTr * 100, STDRecTr, MeanRec * 100, STDRec)) if ui.cbF1.isChecked(): OutData["ModeF1"] = ui.cbF1Avg.currentText() OutData["FoldF1"] = f1score MeanF1 = np.mean(f1score) OutData["MeanTestF1"] = MeanF1 STDF1 = np.std(f1score) OutData["StdTestF1"] = STDF1 MeanF1Tr = np.mean(f1scoreTr) OutData["MeanTrainF1"] = MeanF1Tr STDF1Tr = np.std(f1scoreTr) OutData["StdTrainF1"] = STDF1Tr print( "F1: Train {:5.2f} +/- {:4.2f} Test {:5.2f} +/- {:4.2f}" .format(MeanF1Tr * 100, STDF1Tr, MeanF1 * 100, STDF1)) OutData["InputFiles"] = InFileList OutData["Runtime"] = time.time() - tme print("Runtime: ", OutData["Runtime"]) print("Saving ...") mainIO_save(OutData, OutFile) print("DONE.") msgBox.setText("Stochastic Gradient Descent based Classifier is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False ThresholdType = ui.cbThType.currentData() MinTh = None MaxTh = None if (ThresholdType == "min") or (ThresholdType == "ext"): try: MinTh = np.double(ui.txtThMin.text()) except: msgBox.setText("Min Threshold must be a number") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False print("Min Threshold is valid") if (ThresholdType == "max") or (ThresholdType == "ext"): try: MaxTh = np.double(ui.txtThMax.text()) except: msgBox.setText("Max Threshold must be a number") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False print("Max Threshold is valid") # OutFile AFNI = ui.txtAFNI.text() if not len(AFNI): AFNI = None else: CopyFile = ui.txtFAFNI.text() if not len(CopyFile): msgBox.setText("Please select 3dcopy command!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if not os.path.isfile(CopyFile): msgBox.setText("Please select 3dcopy command!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return RefitFile = ui.txtFSUMA.text() if not len(RefitFile): msgBox.setText("Please select 3drefit command!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if not os.path.isfile(RefitFile): msgBox.setText("Please select 3drefit command!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return # 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 if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if not len(ui.txtMatrix.currentText()): msgBox.setText("Matrix name not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if not len(ui.txtCoord.currentText()): msgBox.setText("Coordinate name not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if not len(ui.txtTime.text()): msgBox.setText("Time points not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return AffineFile = ui.txtSSSpace.currentText() if not len(AffineFile): msgBox.setText("Please enter affine reference!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return if not os.path.isfile(AffineFile): msgBox.setText("Affine reference not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return AffineHDR = nb.load(AffineFile) Affine = AffineHDR.affine Time = strRange(ui.txtTime.text()) if Time is None: msgBox.setText("Time points is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return try: InData = mainIO_load(InFile) Mat = InData[ui.txtMatrix.currentText()] Coord = InData[ui.txtCoord.currentText()] imgShape = InData["imgShape"][0] except: print("Cannot load data!") msgBox.setText("Cannot load data!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return ImgData = None # Filter Mat for t in Time: vox = Mat[t, :] if ui.cbScale.isChecked(): vox = preprocessing.scale(vox) print("Time Point: " + str(t) + " is normalized.") if MinTh is not None: vox[np.where(vox < MinTh)] = 0 print("Time Point: " + str(t) + ", Lower band thresholding is done!") if MaxTh is not None: vox[np.where(vox > MaxTh)] = 0 print("Time Point: " + str(t) + ", Upper band thresholding is done!") img = np.zeros([imgShape[0], imgShape[1], imgShape[2], 1]) for coIndx, _ in enumerate(Coord[0]): img[Coord[0][coIndx], Coord[1][coIndx], Coord[2][coIndx], 0] = vox[coIndx] ImgData = img if ImgData is None else np.concatenate( (ImgData, img), axis=3) print("Time point: " + str(t) + " is done.") print("Saving image ...") NIF = nb.Nifti1Image(ImgData, Affine) nb.save(NIF, OutFile) if AFNI is not None: cmd = CopyFile + " " + OutFile + " " + AFNI print("Running: " + cmd) os.system(cmd) cmd = RefitFile + " -view tlrc -space MNI " + AFNI + " " + AFNI + "+tlrc." print("Running: " + cmd) os.system(cmd) print("DONE!") msgBox.setText("Image file is generated.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() try: Threshold = np.float64(ui.txtThreshold.text()) except: msgBox.setText("Standard deviation threshold value is wrong!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: net_model = createModel(ui.tbModel) except Exception as e: msgBox.setText(str(e)) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if net_model is None: msgBox.setText("Please create a network model!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # OutFile OutFile = ui.txtOutFile.text() if not len(OutFile): msgBox.setText("Please enter out file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False # InFile InFile = ui.txtInFile.text() if not len(InFile): msgBox.setText("Please enter input file!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if not os.path.isfile(InFile): msgBox.setText("Input file not found!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False if ui.rbScale.isChecked() == True and ui.rbALScale.isChecked( ) == False: msgBox.setText( "Subject Level Normalization is just available for Subject Level Analysis!" ) msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False InData = mainIO_load(InFile) OutData = dict() OutData["imgShape"] = reshape_1Dvector(InData["imgShape"]) if not len(ui.txtData.currentText()): msgBox.setText("Please enter Data variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: X = InData[ui.txtData.currentText()] if ui.cbScale.isChecked() and (not ui.rbScale.isChecked()): X = X - X.mean() X = X / X.std() print("Whole of data is scaled X~N(0,1).") except: print("Cannot load data") return # Subject if not len(ui.txtSubject.currentText()): msgBox.setText("Please enter Subject variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False try: Subject = InData[ui.txtSubject.currentText()] OutData[ui.txtOSubject.text()] = reshape_1Dvector(Subject) except: print("Cannot load Subject ID") return # Label if not len(ui.txtLabel.currentText()): msgBox.setText("Please enter Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOLabel.text()] = reshape_1Dvector( InData[ui.txtLabel.currentText()]) # Task if ui.cbTask.isChecked(): if not len(ui.txtTask.currentText()): msgBox.setText("Please enter Task variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOTask.text()] = reshape_1Dvector( InData[ui.txtTask.currentText()]) # Run if ui.cbRun.isChecked(): if not len(ui.txtRun.currentText()): msgBox.setText("Please enter Run variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtORun.text()] = reshape_1Dvector( InData[ui.txtRun.currentText()]) # Counter if ui.cbCounter.isChecked(): if not len(ui.txtCounter.currentText()): msgBox.setText("Please enter Counter variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOCounter.text()] = reshape_1Dvector( InData[ui.txtCounter.currentText()]) # Matrix Label if ui.cbmLabel.isChecked(): if not len(ui.txtmLabel.currentText()): msgBox.setText("Please enter Matrix Label variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()] # Design if ui.cbDM.isChecked(): if not len(ui.txtDM.currentText()): msgBox.setText("Please enter Design Matrix variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()] # Coordinate if ui.cbCol.isChecked(): if not len(ui.txtCol.currentText()): msgBox.setText("Please enter Coordinator variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()] # Condition if ui.cbCond.isChecked(): if not len(ui.txtCond.currentText()): msgBox.setText("Please enter Condition variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()] # Number of Scan if ui.cbNScan.isChecked(): if not len(ui.txtScan.currentText()): msgBox.setText("Please enter Number of Scan variable name!") msgBox.setIcon(QMessageBox.Critical) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_() return False OutData[ui.txtOScan.text()] = reshape_1Dvector( InData[ui.txtScan.currentText()]) if ui.rbALScale.isChecked(): print("Partition data to subject level ...") SubjectUniq = np.unique(Subject) X_Sub = list() for subj in SubjectUniq: if ui.cbScale.isChecked() and ui.rbScale.isChecked(): extractXsub = X[np.where(Subject == subj)[1], :] extractXsub = extractXsub - extractXsub.mean() extractXsub = extractXsub / extractXsub.std() X_Sub.append(extractXsub) print("Data in subject level is scaled, X_" + str(subj) + "~N(0,1).") else: X_Sub.append(X[np.where(Subject == subj)[1], :]) print("Subject ", subj, " is extracted from data.") print("Running CNN in subject level ...") X_Sub_PCA = list() lenPCA = len(X_Sub) for xsubindx, xsub in enumerate(X_Sub): net = CNN(net_model) if ui.rbMat.isChecked(): xsub_tran = net.tonumpy( net.vectorize( net(torch.Tensor(np.expand_dims(xsub, axis=1))))) else: xsub_tran = net.tonumpy( net(torch.Tensor(np.expand_dims(xsub, axis=1)))) X_Sub_PCA.append(xsub_tran) print("CNN: ", xsubindx + 1, " of ", lenPCA, " is done. Shape: " + str(np.shape(xsub_tran))) print("Data integration ... ") X_new = None for xsubindx, xsub in enumerate(X_Sub_PCA): X_new = np.concatenate( (X_new, xsub)) if X_new is not None else xsub print("Integration: ", xsubindx + 1, " of ", lenPCA, " is done.") if not ui.rbMat.isChecked(): X_new = X_new[:, 0, :, :, :] # Apply Thresholding print("Data shape before thresholding: ", np.shape(X_new)) X_new_th = X_new if ui.rbMat.isChecked() and ui.cbThreshold.isChecked(): index = np.where(np.std(X_new, axis=0) >= Threshold) X_new_index = X_new[:, index] print("Standard deviation thresholding ...") X_new_th = None for xx_new in X_new_index: X_new_th = xx_new if X_new_th is None else np.concatenate( (X_new_th, xx_new), axis=0) # Apply Normalization if ui.rbMat.isChecked() and ui.cbOutNormal.isChecked(): X_new_th = X_new_th - np.mean(X_new_th) X_new_th = X_new_th / np.std(X_new_th) print("Output data is normalized!") OutData[ui.txtOData.text()] = X_new_th else: print("Running CNN ...") net = CNN(net_model) if ui.rbMat.isChecked(): X_new = net.tonumpy( net.vectorize(net(torch.Tensor(np.expand_dims(X, axis=1))))) else: X_new = net.tonumpy( net(torch.Tensor(np.expand_dims(X, axis=1)))) print("CNN is done. Shape: " + str(np.shape(X_new))) # Apply Thresholding print("Data shape before thresholding: ", np.shape(X_new)) X_new_th = X_new if ui.rbMat.isChecked() and ui.cbThreshold.isChecked(): index = np.where(np.std(X_new, axis=0) >= Threshold) X_new_index = X_new[:, index] print("Standard deviation thresholding ...") X_new_th = None for xx_new in X_new_index: X_new_th = xx_new if X_new_th is None else np.concatenate( (X_new_th, xx_new), axis=0) # Apply Normalization if ui.rbMat.isChecked() and ui.cbOutNormal.isChecked(): X_new_th = X_new_th - np.mean(X_new_th) X_new_th = X_new_th / np.std(X_new_th) print("Output data is normalized!") OutData[ui.txtOData.text()] = X_new_th print("Saving ...") print("Final data shape: ", np.shape(OutData[ui.txtOData.text()])) mainIO_save(OutData, ui.txtOutFile.text()) print("DONE.") msgBox.setText("CNN is done.") msgBox.setIcon(QMessageBox.Information) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec_()
def btnConvert_click(self): msgBox = QMessageBox() 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 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: 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") # mLabel ui.txtmLabel.clear() HasDefualt = False for key in Keys: ui.txtmLabel.addItem(key) if key == "mlabel": HasDefualt = True if HasDefualt: ui.txtmLabel.setCurrentText("mlabel") ui.cbmLabel.setChecked(HasDefualt) # Coordinate ui.txtCol.clear() HasDefualt = False for key in Keys: ui.txtCol.addItem(key) if key == "coordinate": HasDefualt = True if HasDefualt: ui.txtCol.setCurrentText("coordinate") ui.cbCol.setChecked(HasDefualt) # Design ui.txtDM.clear() HasDefualt = False for key in Keys: ui.txtDM.addItem(key) if key == "design": HasDefualt = True if HasDefualt: ui.txtDM.setCurrentText("design") ui.cbDM.setChecked(HasDefualt) # Subject ui.txtSubject.clear() HasDefualt = False for key in Keys: ui.txtSubject.addItem(key) if key == "subject": HasDefualt = True if HasDefualt: ui.txtSubject.setCurrentText("subject") # Task ui.txtTask.clear() HasDefualt = False for key in Keys: ui.txtTask.addItem(key) if key == "task": HasDefualt = True if HasDefualt: ui.txtTask.setCurrentText("task") ui.cbTask.setChecked(HasDefualt) # Run ui.txtRun.clear() HasDefualt = False for key in Keys: ui.txtRun.addItem(key) if key == "run": HasDefualt = True if HasDefualt: ui.txtRun.setCurrentText("run") ui.cbRun.setChecked(HasDefualt) # Counter ui.txtCounter.clear() HasDefualt = False for key in Keys: ui.txtCounter.addItem(key) if key == "counter": HasDefualt = True if HasDefualt: ui.txtCounter.setCurrentText("counter") ui.cbCounter.setChecked(HasDefualt) # 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") ui.cbCond.setChecked(HasDefualt) # NScan ui.txtScan.clear() HasDefualt = False for key in Keys: ui.txtScan.addItem(key) if key == "nscan": HasDefualt = True if HasDefualt: ui.txtScan.setCurrentText("nscan") ui.cbNScan.setChecked(HasDefualt) ui.txtInFile.setText(filename) except Exception as e: print(e) print("Cannot load data file!") return else: print("File not found!")
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()