From 052c481e887b07377b6fcf7d0e04b957531b3f70 Mon Sep 17 00:00:00 2001 From: Christina Bukas Date: Thu, 21 Mar 2024 13:49:56 +0100 Subject: [PATCH] formatting --- src/client/dcp_client/gui/main_window.py | 187 +++++++++++++------- src/client/dcp_client/gui/welcome_window.py | 6 +- 2 files changed, 122 insertions(+), 71 deletions(-) diff --git a/src/client/dcp_client/gui/main_window.py b/src/client/dcp_client/gui/main_window.py index e03125b..889e833 100644 --- a/src/client/dcp_client/gui/main_window.py +++ b/src/client/dcp_client/gui/main_window.py @@ -3,10 +3,40 @@ import os from typing import TYPE_CHECKING -from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QFileSystemModel, QHBoxLayout, QLabel, QTreeView, QProgressBar, QShortcut, QApplication, QStyledItemDelegate -from PyQt5.QtCore import Qt, QModelIndex, QThread, pyqtSignal, QRect, QSize, QVariant, QDir -from PyQt5.QtGui import QKeySequence, QIcon, QPixmap, QPainter, QImage, QBrush, QPen, QFont -from PySide2.QtCore import QModelIndex +from PyQt5.QtWidgets import ( + QPushButton, + QVBoxLayout, + QFileSystemModel, + QHBoxLayout, + QLabel, + QTreeView, + QProgressBar, + QShortcut, + QApplication, + QStyledItemDelegate, +) +from PyQt5.QtCore import ( + Qt, + QModelIndex, + QThread, + pyqtSignal, + QRect, + QSize, + QVariant, + QDir, +) +from PyQt5.QtGui import ( + QKeySequence, + QIcon, + QPixmap, + QPainter, + QImage, + QBrush, + QPen, + QFont, +) + +# from PySide2.QtCore import QModelIndex from dcp_client.utils import settings @@ -71,6 +101,7 @@ def run(self) -> None: self.task_finished.emit((message_text, message_title)) + class MyQFileSystemModel(QFileSystemModel): def __init__(self, app): """ @@ -78,7 +109,7 @@ def __init__(self, app): """ super().__init__() self.app = app - + def setFilter(self, filters): """ Sets filters for the model. @@ -109,7 +140,7 @@ def headerData(self, section, orientation, role): :rtype: QVariant """ if section == 0 and role == Qt.DisplayRole: - return "" + return "" else: return super().headerData(section, orientation, role) @@ -123,19 +154,18 @@ def data(self, index, role=Qt.DisplayRole): """ if not index.isValid(): return QVariant() - - if '_seg' in self.filePath(index): + + if "_seg" in self.filePath(index): return QVariant() if role == Qt.DisplayRole: filepath_img = self.filePath(index) - if role == Qt.DecorationRole: filepath_img = self.filePath(index) - - if filepath_img.endswith('.tiff') or filepath_img.endswith('.png'): - if '_seg' not in filepath_img: + + if filepath_img.endswith(".tiff") or filepath_img.endswith(".png"): + if "_seg" not in filepath_img: painter = QPainter() img_x, img_y = 64, 64 @@ -146,30 +176,46 @@ def data(self, index, role=Qt.DisplayRole): if os.path.exists(filepath_mask): mask = tiff.imread(filepath_mask)[0] - num_objects = len(self.app.fs_image_storage.search_segs( - os.path.dirname(filepath_img), - filepath_img - )) - - mask = cv2.resize(mask, (int(round(1.5*img_x)), int(round(1.5*img_y))), interpolation=cv2.INTER_NEAREST) - + num_objects = len( + self.app.fs_image_storage.search_segs( + os.path.dirname(filepath_img), filepath_img + ) + ) + + mask = cv2.resize( + mask, + (int(round(1.5 * img_x)), int(round(1.5 * img_y))), + interpolation=cv2.INTER_NEAREST, + ) + mask = label2rgb(mask) - mask = (255 * np.transpose(mask,(1,0,2)).copy()).astype(np.uint8) - - mask = QImage(mask, mask.shape[1], mask.shape[0], QImage.Format_RGB888)\ - .scaled(123, 123, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) - - img = img.scaled(82, 82, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) + mask = (255 * np.transpose(mask, (1, 0, 2)).copy()).astype( + np.uint8 + ) + + mask = QImage( + mask, mask.shape[1], mask.shape[0], QImage.Format_RGB888 + ).scaled( + 123, 123, Qt.IgnoreAspectRatio, Qt.SmoothTransformation + ) + + img = img.scaled( + 82, 82, Qt.IgnoreAspectRatio, Qt.SmoothTransformation + ) img_x, img_y = 82, 82 painter.begin(mask) - - rect_image = QRect(0, img_x//2, img_x, img_y) - rect_corner_left = QRect(0, 0, img_x//2, img_y//2) - rect_corner_bottom = QRect(img_x, img_y, img_x//2, img_y//2) + rect_image = QRect(0, img_x // 2, img_x, img_y) + + rect_corner_left = QRect(0, 0, img_x // 2, img_y // 2) + rect_corner_bottom = QRect(img_x, img_y, img_x // 2, img_y // 2) - painter.fillRect(rect_corner_left, QBrush(Qt.white, Qt.SolidPattern)) - painter.fillRect(rect_corner_bottom, QBrush(Qt.white, Qt.SolidPattern)) + painter.fillRect( + rect_corner_left, QBrush(Qt.white, Qt.SolidPattern) + ) + painter.fillRect( + rect_corner_bottom, QBrush(Qt.white, Qt.SolidPattern) + ) painter.drawImage(rect_image, img) @@ -177,16 +223,20 @@ def data(self, index, role=Qt.DisplayRole): painter.setPen(pen) font = QFont() - font.setFamily('Arial') + font.setFamily("Arial") font.setPointSize(6) painter.setFont(font) - painter.drawText(int(round((5/4)*img_x))-19, int(round((5/4)*img_x)), f"{str(num_objects)} masks") + painter.drawText( + int(round((5 / 4) * img_x)) - 19, + int(round((5 / 4) * img_x)), + f"{str(num_objects)} masks", + ) painter.end() - pixmap = QPixmap.fromImage(mask) + pixmap = QPixmap.fromImage(mask) return pixmap @@ -199,7 +249,6 @@ def data(self, index, role=Qt.DisplayRole): return super().data(index, role) - class ImageDelegate(QStyledItemDelegate): """ Custom delegate for displaying images in Qt views. @@ -213,7 +262,7 @@ def paint(self, painter, option, index): :param option: The style options for the item. (QStyleOptionViewItem) :param index: The model index of the item. (QModelIndex) """ - if '_seg' in index.data(): + if "_seg" in index.data(): return if index.isValid() and index.data(Qt.DecorationRole): pixmap = index.data(Qt.DecorationRole) @@ -262,15 +311,14 @@ def main_window(self) -> None: self.uncurated_layout = QVBoxLayout() self.inprogress_layout = QVBoxLayout() self.curated_layout = QVBoxLayout() - - self.eval_dir_layout = QVBoxLayout() - self.eval_dir_layout.setContentsMargins(0,0,0,0) + self.eval_dir_layout = QVBoxLayout() + self.eval_dir_layout.setContentsMargins(0, 0, 0, 0) self.label_eval = QLabel(self) self.label_eval.setText("Uncurated Dataset") - self.label_eval.setMinimumHeight(50) - self.label_eval.setMinimumWidth(200) + self.label_eval.setMinimumHeight(50) + self.label_eval.setMinimumWidth(200) self.label_eval.setAlignment(Qt.AlignCenter) self.label_eval.setStyleSheet( """ @@ -283,25 +331,24 @@ def main_window(self) -> None: ) self.eval_dir_layout.addWidget(self.label_eval) - + model_eval = MyQFileSystemModel(app=self.app) model_eval.setIconProvider(IconProvider()) model_eval.sort(0, Qt.AscendingOrder) - - + self.list_view_eval = QTreeView(self) self.list_view_eval.setItemDelegate(ImageDelegate()) self.list_view_eval.setToolTip("Select an image, click it, then press Enter") - self.list_view_eval.setIconSize(QSize(300,300)) + self.list_view_eval.setIconSize(QSize(300, 300)) self.list_view_eval.setStyleSheet("background-color: #ffffff") self.list_view_eval.setModel(model_eval) - model_eval.setRootPath('/') + model_eval.setRootPath("/") delegate = CustomItemDelegate() self.list_view_eval.setItemDelegate(delegate) - for i in range(1,4): + for i in range(1, 4): self.list_view_eval.hideColumn(i) # self.list_view_eval.setFixedSize(600, 600) self.list_view_eval.setRootIndex( @@ -315,7 +362,7 @@ def main_window(self) -> None: # add buttons self.inference_button = QPushButton("Generate Labels", self) self.inference_button.setStyleSheet( - """QPushButton + """QPushButton { background-color: #3d81d1; font-size: 12px; @@ -326,30 +373,31 @@ def main_window(self) -> None: "QPushButton:hover { background-color: #7bc432; }" "QPushButton:pressed { background-color: #7bc432; }" ) - self.inference_button.clicked.connect(self.on_run_inference_button_clicked) # add selected image + self.inference_button.clicked.connect( + self.on_run_inference_button_clicked + ) # add selected image self.uncurated_layout.addWidget(self.inference_button, alignment=Qt.AlignCenter) dir_layout.addLayout(self.uncurated_layout) # In progress layout - self.inprogr_dir_layout = QVBoxLayout() - self.inprogr_dir_layout.setContentsMargins(0,0,0,0) + self.inprogr_dir_layout = QVBoxLayout() + self.inprogr_dir_layout.setContentsMargins(0, 0, 0, 0) self.label_inprogr = QLabel(self) - self.label_inprogr.setMinimumHeight(50) - self.label_inprogr.setMinimumWidth(200) + self.label_inprogr.setMinimumHeight(50) + self.label_inprogr.setMinimumWidth(200) self.label_inprogr.setAlignment(Qt.AlignCenter) self.label_inprogr.setStyleSheet( "font-size: 20px; font-weight: bold; background-color: #015998; color: #ffffff; border-radius: 5px; padding: 8px 16px;" ) - self.label_inprogr.setText("Curation in progress") self.inprogr_dir_layout.addWidget(self.label_inprogr) # add in progress dir list model_inprogr = MyQFileSystemModel(app=self.app) - - #self.list_view = QListView(self) + + # self.list_view = QListView(self) self.list_view_inprogr = QTreeView(self) self.list_view_inprogr.setToolTip("Select an image, click it, then press Enter") @@ -358,11 +406,11 @@ def main_window(self) -> None: model_inprogr.setIconProvider(IconProvider()) self.list_view_inprogr.setModel(model_inprogr) - model_inprogr.setRootPath('/') + model_inprogr.setRootPath("/") delegate = CustomItemDelegate() self.list_view_inprogr.setItemDelegate(delegate) - for i in range(1,4): + for i in range(1, 4): self.list_view_inprogr.hideColumn(i) # self.list_view_inprogr.setFixedSize(600, 600) self.list_view_inprogr.setRootIndex( @@ -374,12 +422,13 @@ def main_window(self) -> None: self.launch_nap_button = QPushButton() self.launch_nap_button.setStyleSheet( - "QPushButton { background-color: transparent; border: none; border-radius: 5px; padding: 8px 16px; }" + "QPushButton { background-color: transparent; border: none; border-radius: 5px; padding: 8px 16px; }" ) - self.launch_nap_button.setEnabled(False) - self.inprogress_layout.addWidget(self.launch_nap_button, alignment=Qt.AlignCenter) + self.inprogress_layout.addWidget( + self.launch_nap_button, alignment=Qt.AlignCenter + ) # Create a shortcut for the Enter key to click the button enter_shortcut = QShortcut(QKeySequence(Qt.Key_Return), self) @@ -392,8 +441,8 @@ def main_window(self) -> None: self.train_dir_layout.setContentsMargins(0, 0, 0, 0) self.label_train = QLabel(self) self.label_train.setText("Curated dataset") - self.label_train.setMinimumHeight(50) - self.label_train.setMinimumWidth(200) + self.label_train.setMinimumHeight(50) + self.label_train.setMinimumWidth(200) self.label_train.setAlignment(Qt.AlignCenter) self.label_train.setStyleSheet( "font-size: 20px; font-weight: bold; background-color: #015998; color: #ffffff; border-radius: 5px; padding: 8px 16px;" @@ -402,7 +451,7 @@ def main_window(self) -> None: # add train dir list model_train = MyQFileSystemModel(app=self.app) # model_train.setNameFilters(["*_seg.tiff"]) - #self.list_view = QListView(self) + # self.list_view = QListView(self) self.list_view_train = QTreeView(self) self.list_view_train.setToolTip("Select an image, click it, then press Enter") @@ -411,11 +460,11 @@ def main_window(self) -> None: model_train.setIconProvider(IconProvider()) self.list_view_train.setModel(model_train) - model_train.setRootPath('/') + model_train.setRootPath("/") delegate = CustomItemDelegate() self.list_view_train.setItemDelegate(delegate) - for i in range(1,4): + for i in range(1, 4): self.list_view_train.hideColumn(i) # self.list_view_train.setFixedSize(600, 600) self.list_view_train.setRootIndex( @@ -438,7 +487,9 @@ def main_window(self) -> None: "QPushButton:hover { background-color: #7bc432; }" "QPushButton:pressed { background-color: #7bc432; }" ) - self.train_button.clicked.connect(self.on_train_button_clicked) # add selected image + self.train_button.clicked.connect( + self.on_train_button_clicked + ) # add selected image self.curated_layout.addWidget(self.train_button, alignment=Qt.AlignCenter) dir_layout.addLayout(self.curated_layout) @@ -451,7 +502,7 @@ def main_window(self) -> None: self.progress_bar = QProgressBar(self) self.progress_bar.setMinimumWidth(1000) self.progress_bar.setAlignment(Qt.AlignCenter) - self.progress_bar.setRange(0,1) + self.progress_bar.setRange(0, 1) progress_layout.addWidget(self.progress_bar) main_layout.addLayout(progress_layout) diff --git a/src/client/dcp_client/gui/welcome_window.py b/src/client/dcp_client/gui/welcome_window.py index fe112f3..3789454 100644 --- a/src/client/dcp_client/gui/welcome_window.py +++ b/src/client/dcp_client/gui/welcome_window.py @@ -112,7 +112,7 @@ def __init__(self, app: Application) -> None: self.path_layout.addWidget(self.val_textbox) self.path_layout.addWidget(self.inprogr_textbox) self.path_layout.addWidget(self.train_textbox) - + ''' self.file_open_button_val = QPushButton("Browse", self) self.file_open_button_val.show() self.file_open_button_val.clicked.connect(self.browse_eval_clicked) @@ -125,7 +125,7 @@ def __init__(self, app: Application) -> None: self.button_layout.addWidget(self.file_open_button_val) self.button_layout.addWidget(self.file_open_button_prog) self.button_layout.addWidget(self.file_open_button_train) - + ''' input_layout.addLayout(self.text_layout) input_layout.addLayout(self.path_layout) input_layout.addLayout(self.button_layout) @@ -215,7 +215,7 @@ def eventFilter(self, obj, event): def browse_inprogr_clicked(self): - ''' + """" Activates when the user clicks the button to choose the curation in progress directory (QFileDialog) and displays the name of the evaluation directory chosen in the validation textbox line (QLineEdit). """