Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…lSeg3d into v0.0.1rc3
  • Loading branch information
MMathisLab committed Jul 21, 2022
2 parents 7ac4ccc + ec8ab8b commit a65f0d7
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 214 deletions.
1 change: 1 addition & 0 deletions napari_cellseg3d/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Union
from typing import List


from qtpy.QtCore import Qt
from qtpy.QtCore import QUrl
from qtpy.QtGui import QDesktopServices
Expand Down
51 changes: 48 additions & 3 deletions napari_cellseg3d/log_utility.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import threading
import warnings

from qtpy import QtCore
from qtpy.QtGui import QTextCursor
from qtpy.QtWidgets import QTextEdit

Expand All @@ -22,23 +24,66 @@ def __init__(self, parent):

# def receive_log(self, text):
# self.print_and_log(text)
def write(self, message):
self.lock.acquire()
try:
if not hasattr(self, "flag"):
self.flag = False
message = message.replace("\r", "").rstrip()
if message:
method = "replace_last_line" if self.flag else "append"
QtCore.QMetaObject.invokeMethod(
self,
method,
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(str, message),
)
self.flag = True
else:
self.flag = False

finally:
self.lock.release()

def print_and_log(self, text):
@QtCore.Slot(str)
def replace_last_line(self, text):
self.lock.acquire()
try:
cursor = self.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.select(QTextCursor.BlockUnderCursor)
cursor.removeSelectedText()
cursor.insertBlock()
self.setTextCursor(cursor)
self.insertPlainText(text)
finally:
self.lock.release()

def print_and_log(self, text, printing=True):
"""Utility used to both print to terminal and log text to a QTextEdit
item in a thread-safe manner. Use only for important user info.
Args:
text (str): Text to be printed and logged
printing (bool): Whether to print the message as well or not using print(). Defaults to True.
"""
self.lock.acquire()
try:
print(text)
# causes issue if you clik on terminal (tied to CMD QuickEdit mode)
if printing:
print(text)
# causes issue if you clik on terminal (tied to CMD QuickEdit mode on Windows)
self.moveCursor(QTextCursor.End)
self.insertPlainText(f"\n{text}")
self.verticalScrollBar().setValue(
self.verticalScrollBar().maximum()
)
finally:
self.lock.release()

def warn(self, warning):
self.lock.acquire()
try:
warnings.warn(warning)
finally:
self.lock.release()
6 changes: 3 additions & 3 deletions napari_cellseg3d/model_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from napari_cellseg3d.models import model_SegResNet as SegResNet
from napari_cellseg3d.models import model_TRAILMAP as TRAILMAP
from napari_cellseg3d.models import model_VNet as VNet
from napari_cellseg3d.models import TRAILMAP_MS as TMAP
from napari_cellseg3d.models import model_TRAILMAP_MS as TRAILMAP_MS
from napari_cellseg3d.plugin_base import BasePluginFolder

warnings.formatwarning = utils.format_Warning
Expand Down Expand Up @@ -62,8 +62,8 @@ def __init__(self, viewer: "napari.viewer.Viewer"):
self.models_dict = {
"VNet": VNet,
"SegResNet": SegResNet,
"TRAILMAP pre-trained": TRAILMAP,
"TRAILMAP_MS": TMAP,
"TRAILMAP": TRAILMAP,
"TRAILMAP_MS": TRAILMAP_MS,
}
"""dict: dictionary of available models, with string for widget display as key
Expand Down
Loading

0 comments on commit a65f0d7

Please sign in to comment.