Skip to content

Commit

Permalink
actor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Sep 1, 2023
1 parent 3bcc227 commit f79424c
Show file tree
Hide file tree
Showing 38 changed files with 2,363 additions and 1,101 deletions.
4 changes: 4 additions & 0 deletions .arkitekt/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Hiding Arkitekt Credential files from git
*.json
*.temp
cache/
4 changes: 4 additions & 0 deletions .arkitekt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Hiding Arkitekt Credential files from git
*.json
*.temp
cache/
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@
"**/__pycache__": true,
"**/.pytest_cache": true,
},
"mypy.runUsingActiveInterpreter": true
"mypy.runUsingActiveInterpreter": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none",
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
22 changes: 12 additions & 10 deletions build-mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

APP_NAME = "KARL"

PyInstaller.__main__.run([
'test.py',
'--clean',
'--windowed',
f'--name={APP_NAME}',
'--noconfirm',
#'--add-data=share;share',
'--additional-hooks-dir=hooks',
'--icon=mikroj-logo.ico'
])
PyInstaller.__main__.run(
[
"test.py",
"--clean",
"--windowed",
f"--name={APP_NAME}",
"--noconfirm",
#'--add-data=share;share',
"--additional-hooks-dir=hooks",
"--icon=mikroj-logo.ico",
]
)
2 changes: 1 addition & 1 deletion entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mikroj.main import main

if __name__ == "__main__":
main()
main()
8 changes: 6 additions & 2 deletions hooks/hook-arkitekt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from PyInstaller.utils.hooks import collect_data_files, collect_dynamic_libs, collect_submodules
from PyInstaller.utils.hooks import (
collect_data_files,
collect_dynamic_libs,
collect_submodules,
)

datas = collect_data_files('arkitekt')
datas = collect_data_files("arkitekt")
9 changes: 6 additions & 3 deletions hooks/hook-mikroj.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from PyInstaller.utils.hooks import (
collect_data_files,
collect_dynamic_libs,
collect_submodules,
)

from PyInstaller.utils.hooks import collect_data_files, collect_dynamic_libs, collect_submodules

datas = collect_data_files('mikroj')
datas = collect_data_files("mikroj")
3 changes: 1 addition & 2 deletions hooks/hook-numcodecs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from PyInstaller.utils.hooks import collect_dynamic_libs, collect_submodules

hiddenimports = collect_submodules('numcodecs')
hiddenimports = collect_submodules("numcodecs")
binaries = collect_dynamic_libs("numcodecs")
179 changes: 52 additions & 127 deletions mikroj/actors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,164 +3,89 @@
from rekuest.actors.functional import (
ThreadedFuncActor,
)
from rekuest.api.schema import ProvisionFragment, ProvisionFragmentTemplate, DefinitionFragment
from rekuest.api.schema import (
ProvisionFragment,
ProvisionFragmentTemplate,
DefinitionFragment,
DefinitionInput,
)
from mikro.api.schema import (
RepresentationFragment,
RepresentationVarietyInput,
from_xarray,
from_df
from_df,
)
from rekuest.definition.validate import auto_validate
from mikroj.macro_helper import ImageJMacroHelper
from mikroj.registries.base import Macro, RESULTS_KEY, ROIS_KEY, ACTIVE_OUT_KEY, ACTIVE_IN_KEY
from mikroj.language.types import Macro
from mikro.traits import Representation
import xarray as xr
from pydantic import Field
from typing import Protocol, TypeVar, runtime_checkable, Any
from mikroj import constants, structures

T = TypeVar("T")

def jtranspile(
instance,
helper: ImageJMacroHelper,
):
if isinstance(instance, Representation):
x = helper.py.to_java(instance.data.squeeze().compute())
properties = x.getProperties()
properties.put("name", instance.name)
properties.put("representation_id", instance.id)
return x

return instance


def ptranspile(
instance,
kwargs: dict,
helper: ImageJMacroHelper,
macro: Macro,
definition: DefinitionFragment,
):

if (
instance.__class__.__name__ == "ij.ImagePlus"
or instance.__class__.__name__ == "net.imagej.DefaultDataset"
or instance.__class__.__name__ == "ij.CompositeImage"
):
print(instance)
xarray: xr.DataArray = helper.py.from_java(instance)
print(xarray)
if "row" in xarray.dims:
xarray = xarray.rename(row="x")
if "col" in xarray.dims:
xarray = xarray.rename(col="y")
if "Channel" in xarray.dims:
xarray = xarray.rename(Channel="c")

if "c" in xarray.dims:
if xarray.sizes["c"] == 3:
type = (
RepresentationVarietyInput.RGB
if macro.rgb
else RepresentationVarietyInput.VOXEL
)
else:
type = RepresentationVarietyInput.VOXEL
else:
type = RepresentationVarietyInput.VOXEL

origins = [
arg for arg in kwargs.values() if isinstance(arg, RepresentationFragment)
]
tags = []
if macro.filter:
tags = tags.append("filtered")

name = "Output of" + definition.name

if len(origins) > 0:
name = (
definition.name
+ " of "
+ " , ".join(map(lambda x: x.name, origins))
)
@runtime_checkable
class TranspileAble(Protocol):
@classmethod
def from_jreturn(cls: T, value, Any) -> T:
...

rep = from_xarray(xarray, name=name, variety=type, origins=origins)
def to_jarg(self, helper: ImageJMacroHelper) -> Any:
...

return rep
if instance.__class__.__name__ == "net.imagej.legacy.convert.ResultsTableWrapper":
pdDataFrame = helper.py.from_java(instance)

rep_origins = [
arg for arg in kwargs.values() if isinstance(arg, RepresentationFragment)
]
def convert_inputs(kwargs, helper: ImageJMacroHelper, definition: DefinitionInput):
transpile_inputs = {}
for key, value in kwargs.items():
if key == "active_in":
value.set_active(helper)
continue

if isinstance(value, TranspileAble):
transpile_inputs[key] = value.to_jarg(helper)
else:
transpile_inputs[key] = helper.py.to_java(value)

table = from_df(pdDataFrame, name=definition.name, rep_origins=rep_origins)
return table
return transpile_inputs


def convert_outputs(
macro_output, helper: ImageJMacroHelper, definition: DefinitionInput
):
transpile_outputs = []
for port in definition.returns:
if port.key == "active_out":
transpile_outputs.append(
structures.ImageJPlus(helper.py.active_imageplus())
)
continue
if port.identifier == constants.IMAGEJ_PLUS_IDENTIFIER:
transpile_outputs.append(
structures.ImageJPlus.from_jreturn(
macro_output.getOutput(port.key), helper
)
)
continue

transpile_outputs.append(helper.py.to_python(macro_output.getOutput(port.key)))

return instance
return transpile_outputs


class FuncMacroActor(ThreadedFuncActor):
macro: Macro
helper: ImageJMacroHelper
threadpool: ThreadPoolExecutor = Field(
default_factory=lambda: ThreadPoolExecutor(max_workers=1)
)
_validated_definition: DefinitionFragment

async def on_provide(self, provision: ProvisionFragment):
self._validated_definition = auto_validate(self.definition)
return await super().on_provide(provision)

def assign(self, **kwargs):
logging.info("Being assigned")

transpiled_args = {
key: jtranspile(kwarg, self.helper) for key, kwarg in kwargs.items()
}

if self.macro.setactivein:
image = transpiled_args.pop(self._validated_definition.args[0].key)
self.helper.ui.show(
kwargs[self._validated_definition.args[0].key].name, image
)
transpiled_args = convert_inputs(kwargs, self.helper, self.definition)
macro_output = self.helper.py.run_macro(self.macro.code, {**transpiled_args})
print(macro_output)

imagej_returns = []

outkeys = [re.key for re in self._validated_definition.returns]

for re in self._validated_definition.returns:
if re.key == RESULTS_KEY:
imagej_returns.append(self.helper.get_results_table())
continue
if re.key == ROIS_KEY:
imagej_returns.append(self.helper.get_rois())
continue
if re.key == ACTIVE_OUT_KEY:
imagej_returns.append(self.helper.py.active_imageplus())
continue

imagej_returns.append(macro_output.getOutput(re.key))


transpiled_returns = [
ptranspile(value, kwargs, self.helper, self.macro, self._validated_definition)
for value in imagej_returns
]

print(transpiled_returns)
transpiled_returns = convert_outputs(macro_output, self.helper, self.definition)

if len(transpiled_returns) == 0:
return None
if len(transpiled_returns) == 1:
return transpiled_returns[0]

return transpiled_returns
return tuple(transpiled_returns) if transpiled_returns else None

class Config:
underscore_attrs_are_private = True
underscore_attrs_are_private = True
8 changes: 8 additions & 0 deletions mikroj/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMAGEJ_PLUS_IDENTIFIER = "imageplus"
IMAGEJ_DATASET_IDENTIFIER = "imagejdataset"
IMAGEJ_FILE_IDENTIFIER = "imagejfile"

ACTIVE_OUT_KEY = "active_out"
ACTIVE_IN_KEY = "active_in"
RESULTS_KEY = "results"
ROIS_KEY = "rois"
Loading

0 comments on commit f79424c

Please sign in to comment.