From 3bcc22733baf55bb56d60d16ac6aa5fc12098ed9 Mon Sep 17 00:00:00 2001 From: jhnnsrs Date: Thu, 18 May 2023 19:40:35 +0200 Subject: [PATCH] update with new actor model --- mikroj/main.py | 77 +++++++++++++++++++++----------------- mikroj/registries/macro.py | 28 ++++++++++---- mikroj/registries/utils.py | 33 ++++++++++------ pyproject.toml | 2 +- 4 files changed, 86 insertions(+), 54 deletions(-) diff --git a/mikroj/main.py b/mikroj/main.py index 64bab02..732c332 100644 --- a/mikroj/main.py +++ b/mikroj/main.py @@ -27,16 +27,17 @@ from arkitekt.apps.fakts import ArkitektFakts from arkitekt.apps.rekuest import ArkitektAgent from arkitekt.apps.rekuest import ArkitektRekuest -from rekuest.agents.stateful import StatefulAgent from arkitekt import easy from herre import Herre from herre.grants import CacheGrant as HerreCacheGrant from herre.grants.oauth2.refresh import RefreshGrant from herre.grants.fakts import FaktsGrant -from herre.grants.fakts.fakts_login_screen import FaktsQtLoginScreen, LoginWidget +from herre.grants.fakts.fakts_login_screen import FaktsQtLoginScreen +from fakts.grants.remote.base import Manifest +from herre.grants.qt.login_screen import LoginWidget +from qtpy.QtWidgets import QMessageBox - -packaged = True +packaged = False identifier = "github.io.jhnnsrs.mikroj" version = "v0.0.1" @@ -82,9 +83,11 @@ def __init__(self, *args, **kwargs) -> None: grant=FailsafeGrant( grants=[ RetrieveGrant( - identifier=identifier, - version=version, - redirect_uri="http://localhost:6767", + manifest=Manifest( + identifier=identifier, + version=version, + scopes=["read"], + ), discovery=QtSelectableDiscovery( widget=self.selectBeaconWidget, ), @@ -95,16 +98,16 @@ def __init__(self, *args, **kwargs) -> None: assert_groups={"mikro", "rekuest"}, ), herre=Herre( - grant=HerreCacheGrant( - cache_file=f"{identifier}-{version}_herre_cache.json", - hash=f"{identifier}-{version}", - grant=FaktsQtLoginScreen( - widget=self.loginWindow, - auto_login=True, - grant=RefreshGrant(grant=FaktsGrant()), + grant=HerreCacheGrant( + cache_file=f"{identifier}-{version}_herre_cache.json", + hash=f"{identifier}-{version}", + grant=FaktsQtLoginScreen( + widget=self.loginWindow, + auto_login=True, + grant=RefreshGrant(grant=FaktsGrant()), + ), ), ), - ), ) self.app.enter() @@ -130,9 +133,6 @@ def __init__(self, *args, **kwargs) -> None: if self.image_j_path and self.auto_initialize: self.initialize() - - - def request_imagej_dir(self): dir = QtWidgets.QFileDialog.getExistingDirectory( parent=self, caption="Select ImageJ directory" @@ -160,26 +160,38 @@ def initialize(self): self.request_imagej_dir() if self.plugins_dir: - scyjava.config.add_option(f"-Dplugins.dir={self.plugins_dir}") + # scyjava.config.add_option(f"-Dplugins.dir={self.plugins_dir}") + pass - self.imagej_button.setDisabled(True) - self.imagej_button.setText("Initializing...") - self._ij = imagej.init(self.image_j_path, headless=self.headless) - self.imagej_button.setText("ImageJ Initialized") - self.magic_bar.magicb.setDisabled(False) + try: + self.imagej_button.setDisabled(True) + self.imagej_button.setText("Initializing...") + self._ij = imagej.init(self.image_j_path, mode="interactive") + self.imagej_button.setText("ImageJ Initialized") + self.magic_bar.magicb.setDisabled(False) + self.vlayout.update() + self.macro_registry.helper.set_ij_instance(self._ij) - self.vlayout.update() - - - self.macro_registry.helper.set_ij_instance(self._ij) + if not self.headless: + self._ij.ui().showUI() + except Exception as e: + self.image_j_path = None + self.imagej_button.setText("ImageJ Failed to Initialize") + self.magic_bar.magicb.setDisabled(True) + self.imagej_button.setDisabled(False) + self.show_exception(e) - if not self.headless: - self._ij.ui().showUI() + def show_exception(self, exception: Exception): + msg = QMessageBox() + msg.setIcon(QMessageBox.Critical) + msg.setText("Error") + msg.setInformativeText(str(exception)) + msg.setWindowTitle("Error") + msg.exec_() def add_testing_ui(self): - self.get_rois_button = QtWidgets.QPushButton("Get ROIs") self.vlayout.addWidget(self.get_rois_button) @@ -190,12 +202,9 @@ def add_testing_ui(self): self.get_results_button.clicked.connect(self.get_results) - def main(**kwargs): - app = QtWidgets.QApplication(sys.argv) try: - from qt_material import apply_stylesheet apply_stylesheet(app, theme="dark_teal.xml") diff --git a/mikroj/registries/macro.py b/mikroj/registries/macro.py index 22d98e8..86bd8e4 100644 --- a/mikroj/registries/macro.py +++ b/mikroj/registries/macro.py @@ -20,7 +20,12 @@ from mikroj.registries.base import Macro from qtpy import QtCore from mikroj.registries.utils import load_macro, define_macro -from rekuest.structures.registry import StructureRegistry, get_current_structure_registry +from rekuest.structures.registry import ( + StructureRegistry, + get_current_structure_registry, +) +from rekuest.structures.default import get_default_structure_registry + logger = logging.getLogger(__name__) @@ -29,17 +34,21 @@ class MacroBuilder: MacroBuilder is a builder for FuncMacroActor. """ - __definition__: DefinitionInput - def __init__(self, definition: DefinitionInput, macro: Macro, helper: ImageJMacroHelper, structure_registry: StructureRegistry) -> None: - self.__definition__ = definition + def __init__( + self, + definition: DefinitionInput, + macro: Macro, + helper: ImageJMacroHelper, + structure_registry: StructureRegistry, + ) -> None: + self.definition = definition self.macro = macro self.helper = helper self.structure_registry = structure_registry def __call__(self, *args, **kwargs): return FuncMacroActor( - definition=self.__definition__, structure_registry=self.structure_registry, macro=self.macro, helper=self.helper, @@ -96,9 +105,12 @@ def load_macros(self): # because path is object not string path_in_str = str(path) macro = load_macro(path_in_str) - structure_registry = self.structure_registry or get_current_structure_registry() + structure_registry = get_default_structure_registry() + interface = macro.name definition = define_macro(macro) - actorBuilder = MacroBuilder(definition, macro, self.helper, structure_registry) + actorBuilder = MacroBuilder( + definition, macro, self.helper, structure_registry + ) - self.register_actorBuilder(actorBuilder) + self.register_at_interface(interface, definition, actorBuilder) diff --git a/mikroj/registries/utils.py b/mikroj/registries/utils.py index 0b224ea..8516515 100644 --- a/mikroj/registries/utils.py +++ b/mikroj/registries/utils.py @@ -7,11 +7,18 @@ DefinitionInput, NodeKind, PortKindInput, - ChildPortInput + ChildPortInput, + Scope, ) from pydantic.main import BaseModel from mikro.widgets import MY_TOP_REPRESENTATIONS -from mikroj.registries.base import Macro, RESULTS_KEY, ROIS_KEY, ACTIVE_IN_KEY, ACTIVE_OUT_KEY +from mikroj.registries.base import ( + Macro, + RESULTS_KEY, + ROIS_KEY, + ACTIVE_IN_KEY, + ACTIVE_OUT_KEY, +) doc = re.compile("\/\*(?P(.|\n)*)\*\/*") @@ -26,11 +33,12 @@ activein_re = re.compile(".*\@setactivein.*") interfaces_re = re.compile(".*@interface:(\w*)\n") activeout_re = re.compile(".*\@takeactiveout*") -getroisout_re = re.compile(".*\@getroisout*") # should we extract the rois from the roi manager? -getresults_re = re.compile(".*\@getresults*") # should we extract the results from the results table? - - - +getroisout_re = re.compile( + ".*\@getroisout*" +) # should we extract the rois from the roi manager? +getresults_re = re.compile( + ".*\@getresults*" +) # should we extract the results from the results table? donecloseactive_re = re.compile(".*\@donecloseactive*") @@ -38,8 +46,6 @@ rgb_re = re.compile(".*\@rgb*") - - params_re = re.compile(r"#@[^\(]*\((?P[^\)]*)\)") # line has params @@ -90,6 +96,7 @@ def define_macro(macro: Macro) -> DefinitionInput: description="Image to be processed", assignWidget=MY_TOP_REPRESENTATIONS, nullable=False, + scope=Scope.GLOBAL, ) ] @@ -101,6 +108,7 @@ def define_macro(macro: Macro) -> DefinitionInput: identifier="@mikro/representation", description="Image to be processed", nullable=False, + scope=Scope.GLOBAL, ) ] @@ -115,7 +123,9 @@ def define_macro(macro: Macro) -> DefinitionInput: identifier="@mikro/roi", kind=PortKindInput.STRUCTURE, nullable=False, - ) + scope=Scope.GLOBAL, + ), + scope=Scope.GLOBAL, ) ] @@ -127,6 +137,7 @@ def define_macro(macro: Macro) -> DefinitionInput: nullable=False, description="The results table", identifier="@mikro/table", + scope=Scope.GLOBAL, ) ] @@ -136,6 +147,6 @@ def define_macro(macro: Macro) -> DefinitionInput: args=args, interfaces=macro.interfaces, returns=returns, - interface=macro.name, kind=NodeKind.FUNCTION, + portGroups=[], ) diff --git a/pyproject.toml b/pyproject.toml index 2a99737..cde082e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "mikroj" -version = "0.1.19" +version = "0.1.20" description = "" authors = ["jhnnsrs "] license = "CC BY-NC 3.0"