def LoadClinicalRef(self): dlg = QFileDialog() file_name, _ = dlg.getOpenFileName(self, 'Open SCV file', filter="csv files (*.csv)") if file_name: try: self.__clinical_ref = pd.read_csv(file_name, index_col=0) if list(self.__clinical_ref.index) != list( self.data_container.GetFrame().index): QMessageBox.information( self, 'Error', 'The index of clinical features is not consistent to the data' ) return None self.loadClinicRef.setEnabled(False) self.clearClinicRef.setEnabled(True) except OSError as reason: eclog(self._filename).GetLogger().error( 'Load Clinical Ref Error: {}'.format(reason)) QMessageBox.information(self, 'Error', 'Can not Open the Files') except ValueError: eclog(self._filename).GetLogger().error( 'OpenCSV Error: {}'.format(file_name)) QMessageBox.information(self, 'Error', 'The selected data file mismatch.') return None
def GenerateDescription(self): if (self.comboNormalizer.count() == 0) or \ (self.comboDimensionReduction.count() == 0) or \ (self.comboFeatureSelector.count() == 0) or \ (self.comboClassifier.count() == 0) or \ (self.spinBoxFeatureNumber.value() == 0): return dlg = QFileDialog() dlg.setFileMode(QFileDialog.DirectoryOnly) dlg.setOption(QFileDialog.ShowDirsOnly) if dlg.exec_(): store_folder = dlg.selectedFiles()[0] roc_path = os.path.join(store_folder, 'ROC.jpg') self.canvasROC.getFigure().savefig(roc_path, dpi=300) report = Description() try: pipeline_name = self._fae.GetStoreName( self.comboNormalizer.currentText(), self.comboDimensionReduction.currentText(), self.comboFeatureSelector.currentText(), str(self.spinBoxFeatureNumber.value()), self.comboClassifier.currentText()) report.Run(pipeline_name, self._root_folder, store_folder) os.system("explorer.exe {:s}".format( os.path.normpath(store_folder))) except Exception as ex: QMessageBox.about(self, 'Description Generate Error: ', ex.__str__()) print(eclog(self._filename)) eclog(self._filename).GetLogger().error( 'Description Generate Error: ' + ex.__str__())
def __init__(self, parent=None): super(QWidget, self).__init__(parent) self.ui = Ui_FeatureExtraction() self._source_path = '' self._cur_pattern_item = 0 self.logger = eclog(os.path.split(__file__)[-1]).GetLogger() folder, _ = os.path.split(os.path.abspath(sys.argv[0])) self.radiomics_config_path = folder + '/RadiomicsParams.yaml' self.ui.setupUi(self) self.ui.buttonBrowseSourceFolder.clicked.connect( self.BrowseSourceFolder) self.ui.buttonBrowseRoiFile.clicked.connect(self.BrowseRoiFile) self.ui.buttonAdd.clicked.connect(self.onButtonAddClicked) self.ui.listWidgetImageFiles.setContextMenuPolicy( QtCore.Qt.CustomContextMenu) self.ui.listWidgetImageFiles.customContextMenuRequested[ QtCore.QPoint].connect(self.onListImageFilesContextMenuRequested) self.ui.buttonBrowseFile.clicked.connect(self.BrowsePatternFile) self.ui.buttonBrowseOutputFile.clicked.connect(self.BrowseOutputFile) self.radiomics_params = RadiomicsParamsConfig( self.radiomics_config_path) self.ui.buttonGo.clicked.connect(self.Go) self.InitUi() self.check_background_color = "background-color:rgba(255,0,0,64)" self.raw_background_color = "background-color:rgba(25,35,45,255)"
def LoadTestingReferenceDataContainer(self): dlg = QFileDialog() file_name, _ = dlg.getOpenFileName(self, 'Open SCV file', filter="csv files (*.csv)") if file_name: try: self.__testing_ref_data_container.Load(file_name) self.loadTestingReference.setEnabled(False) self.clearTestingReference.setEnabled(True) self.spinBoxSeparate.setEnabled(False) except OSError as reason: eclog(self._filename).GetLogger().error( 'Load Testing Ref Error: {}'.format(reason)) print('Error!' + str(reason)) except ValueError: eclog(self._filename).GetLogger().error( 'Open CSV Error: {}'.format(file_name)) QMessageBox.information(self, 'Error', 'The selected data file mismatch.')
def LoadData(self): dlg = QFileDialog() file_name, _ = dlg.getOpenFileName(self, 'Open SCV file', filter="csv files (*.csv)") if file_name: try: if self.data_container.Load(file_name, is_update=False): self.UpdateTable() self.SetButtonsState(True) except OSError as reason: eclog(self._filename).GetLogger().error( 'Load CSV Error: {}'.format(reason)) QMessageBox.about(self, 'Load data Error', reason.__str__()) print('Error!' + str(reason)) except ValueError: eclog(self._filename).GetLogger().error( 'Open CSV Error: {}'.format(file_name)) QMessageBox.information(self, 'Error', 'The selected data file mismatch.')
def __IterateCase(self, root_folder, key_name_list, roi_key, show_key_name_list, store_path=''): case_name_list = os.listdir(root_folder) case_name_list.sort() for case_name in case_name_list: case_path = os.path.join(root_folder, case_name) if not os.path.isdir(case_path): continue print(case_name) try: if self.feature_name_list != [] and self.feature_values != []: feature_values = self.__GetFeatureValues( case_path, key_name_list, show_key_name_list, roi_key) if not self.__MergeCase(case_name, feature_values): self.error_list.append(case_name) else: self.__InitialFeatureValues(case_path, key_name_list, show_key_name_list, roi_key) self.case_list.append(case_name) except Exception as e: content = 'In FeatureExtractor, {} extracted failed: '.format( case_name) eclog(self._file_name).LogError('{}{}'.format(content, str(e))) self.error_list.append(case_name) print('{} \n{}'.format(content, e.__str__())) raise e if store_path: self.Save(store_path) with open(os.path.join(store_path + '_error.csv'), 'w', newline='') as csvfile: writer = csv.writer(csvfile) for error_case in self.error_list: writer.writerow([error_case])
def __GetFeatureValues(self, case_folder, key_name_list, show_key_name_list, roi_key): feature_dict = {} if isinstance(roi_key, str): roi_key = [roi_key] roi_key_path = '*' for one_roi_key in roi_key: roi_key_path += '{}*'.format(one_roi_key) roi_candidate = glob.glob(os.path.join(case_folder, roi_key_path)) roi_candidate = [one for one in roi_candidate if 'nii' in one] if len(roi_candidate) != 1: eclog( self._file_name).LogError('Check the ROI file path of case: ' + case_folder) return None roi_file_path = roi_candidate[0] quality_feature_path = os.path.join(case_folder, 'QualityFeature.csv') if os.path.exists(quality_feature_path): with open(quality_feature_path, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in reader: feature_dict['Quality_' + row[0]] = row[1] for sequence_key, show_key in zip(key_name_list, show_key_name_list): if isinstance(sequence_key, str): sequence_key = [sequence_key] sequence_key_path = '*' for one_sequence_key in sequence_key: sequence_key_path += '{}*'.format(one_sequence_key) sequence_candidate = glob.glob( os.path.join(case_folder, sequence_key_path)) for one_roi_key in roi_key: sequence_candidate = self._RemoveKeyFromPathList( sequence_candidate, one_roi_key) if len(sequence_candidate) != 1: eclog(self._file_name).LogError( 'Check the data file path of case: ' + sequence_key_path) return None sequence_file_path = sequence_candidate[0] feature_names_each_modality, feature_values_each_modality = \ self.__GetFeatureValuesEachModality(sequence_file_path, roi_file_path, show_key) for feature_name, feature_value in zip( feature_names_each_modality, feature_values_each_modality): if feature_name in self.feature_name_list: feature_dict[feature_name] = feature_value else: print('Check the feature name in the feature name list') feature_dict = collections.OrderedDict(sorted(feature_dict.items())) feature_values = list(feature_dict.values()) if self.__has_label: label_value = 0 label_path = os.path.join(case_folder, 'label.csv') if os.path.exists(label_path): with open(label_path, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in reader: label_value = row[0] feature_values.insert(0, label_value) else: print('No label file!: ', label_path) return feature_values
def __init__(self): self.__model = None self._x = np.array([]) self._y = np.array([]) self._data_container = DataContainer() self.logger = eclog(os.path.split(__file__)[-1]).GetLogger()
def CheckAndSave(self): if self.data_container.IsEmpty(): QMessageBox.warning(self, "Warning", "There is no data", QMessageBox.Ok) return None if self.data_container.HasInvalidNumber(): QMessageBox.warning(self, "Warning", "There are nan items", QMessageBox.Ok) non_valid_number_index = self.data_container.FindInvalidNumberIndex( ) old_edit_triggers = self.tableFeature.editTriggers() self.tableFeature.setEditTriggers(QAbstractItemView.CurrentChanged) self.tableFeature.setCurrentCell(non_valid_number_index[0], non_valid_number_index[1]) self.tableFeature.setEditTriggers(old_edit_triggers) return None self.data_container.UpdateDataByFrame() if not self.data_container.IsBinaryLabel(): QMessageBox.warning(self, "Warning", "There are not 2 Labels", QMessageBox.Ok) return None remove_features_with_same_value = RemoveSameFeatures() self.data_container = remove_features_with_same_value.Run( self.data_container) if self.radioSplitRandom.isChecked() or self.radioSplitRef.isChecked(): folder_name = QFileDialog.getExistingDirectory(self, "Save data") if folder_name != '': data_separate = DataSeparate.DataSeparate() try: if self.__testing_ref_data_container.IsEmpty(): testing_data_percentage = self.spinBoxSeparate.value() if self.__clinical_ref.size == 0: training_data_container, _, = \ data_separate.RunByTestingPercentage(self.data_container, testing_data_percentage, store_folder=folder_name) else: training_data_container, _, = \ data_separate.RunByTestingPercentage(self.data_container, testing_data_percentage, clinic_df=self.__clinical_ref, store_folder=folder_name) else: training_data_container, _, = \ data_separate.RunByTestingReference(self.data_container, self.__testing_ref_data_container, folder_name) if training_data_container.IsEmpty(): QMessageBox.information( self, 'Error', 'The testing data does not mismatch, please check the testing data ' 'really exists in current data') return None os.system("explorer.exe {:s}".format( os.path.normpath(folder_name))) except Exception as e: content = 'PrepareConnection, splitting failed: ' eclog(self._filename).GetLogger().error('Split Error: ' + e.__str__()) QMessageBox.about(self, content, e.__str__()) else: file_name, _ = QFileDialog.getSaveFileName( self, "Save data", filter="csv files (*.csv)") if file_name: self.data_container.Save(file_name)