oprunner 0.1.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oprunner/__init__.py +49 -0
- oprunner/_adapt.py +198 -0
- oprunner/_codec.py +126 -0
- oprunner/_tiling.py +400 -0
- oprunner/discovery.py +126 -0
- oprunner/host.py +281 -0
- oprunner/runner.py +901 -0
- oprunner/worker.py +158 -0
- oprunner-0.1.0.dev0.dist-info/METADATA +33 -0
- oprunner-0.1.0.dev0.dist-info/RECORD +12 -0
- oprunner-0.1.0.dev0.dist-info/WHEEL +4 -0
- oprunner-0.1.0.dev0.dist-info/licenses/LICENSE +24 -0
oprunner/worker.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""The worker-side half of oprunner.
|
|
2
|
+
|
|
3
|
+
This module is loaded inside every op environment, via an Appose service init
|
|
4
|
+
script. It must therefore stay austere: standard library, numpy, and appose
|
|
5
|
+
only -- never magicgui, napari, or anything an op happens to depend on.
|
|
6
|
+
|
|
7
|
+
Its whole job is to turn an Appose task into an ordinary Python function call
|
|
8
|
+
and turn the return value back into Appose outputs.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import importlib
|
|
14
|
+
import sys
|
|
15
|
+
from enum import Enum
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Any, get_args, get_origin
|
|
18
|
+
|
|
19
|
+
from opspec.op import FUNCTION, OpSpec
|
|
20
|
+
from opspec.runtime import bind_task, unbind_task
|
|
21
|
+
|
|
22
|
+
from . import _adapt, _codec
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def invoke(
|
|
26
|
+
task: Any,
|
|
27
|
+
module: str,
|
|
28
|
+
function: str,
|
|
29
|
+
kwargs: dict,
|
|
30
|
+
plans: list | None = None,
|
|
31
|
+
origin: dict | None = None,
|
|
32
|
+
) -> dict:
|
|
33
|
+
"""Run an op in this worker process, on behalf of a host.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
task: The Appose task, used for progress and cancellation.
|
|
37
|
+
module: Module to import the op from, e.g. ``"skop.ops.segment.starfun3d"``.
|
|
38
|
+
function: Name of the op function within that module.
|
|
39
|
+
kwargs: Op arguments, as decoded by Appose.
|
|
40
|
+
plans: Serialized ``AdaptationPlan``s, one per parameter needing one.
|
|
41
|
+
Iteration runs here rather than on the host so that forty planes
|
|
42
|
+
cost one task and one trip through shared memory, not forty.
|
|
43
|
+
origin: Where the host found the op's module, for an op that lives
|
|
44
|
+
outside any installed package: ``{"root": ...}``, the folder to
|
|
45
|
+
import it from.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
A dict of task outputs. Computer- and inplace-form ops return an
|
|
49
|
+
empty dict, since their results land in buffers the host already
|
|
50
|
+
owns.
|
|
51
|
+
"""
|
|
52
|
+
fn = getattr(_import(module, origin), function)
|
|
53
|
+
spec = OpSpec.from_op(fn)
|
|
54
|
+
adaptations = {
|
|
55
|
+
plan["param"]: _adapt.AdaptationPlan.from_dict(plan) for plan in plans or []
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
refs: list = []
|
|
59
|
+
outbound: list = []
|
|
60
|
+
try:
|
|
61
|
+
args = _coerce(spec, _codec.decode(kwargs, refs))
|
|
62
|
+
_check_args(spec, args)
|
|
63
|
+
|
|
64
|
+
token = bind_task(task)
|
|
65
|
+
try:
|
|
66
|
+
result = _adapt.execute(spec, fn, args, adaptations)
|
|
67
|
+
finally:
|
|
68
|
+
unbind_task(token)
|
|
69
|
+
|
|
70
|
+
return _pack(spec, result, outbound)
|
|
71
|
+
finally:
|
|
72
|
+
# Release our mappings of the host's shared memory. Per Appose's
|
|
73
|
+
# rule, only the host process unlinks; we merely close our view.
|
|
74
|
+
_codec.release(refs, unlink=False)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _import(module: str, origin: dict | None) -> Any:
|
|
78
|
+
"""Import the op's module, from where the host found it if need be."""
|
|
79
|
+
root = (origin or {}).get("root")
|
|
80
|
+
if root and root not in sys.path:
|
|
81
|
+
if (origin or {}).get("append"):
|
|
82
|
+
# The host's installed packages: last, so the worker uses its own
|
|
83
|
+
# environment's copy, and only falls back to the host's.
|
|
84
|
+
sys.path.append(root)
|
|
85
|
+
else:
|
|
86
|
+
# A checkout: in front, so the worker runs the code being edited.
|
|
87
|
+
sys.path.insert(0, root)
|
|
88
|
+
return importlib.import_module(module)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _coerce(spec: OpSpec, args: dict) -> dict:
|
|
92
|
+
"""Rebuild the rich argument types that JSON flattened in transit.
|
|
93
|
+
|
|
94
|
+
An Enum travels as its value and a Path as a string, so an op called
|
|
95
|
+
remotely would otherwise receive something different from what an op
|
|
96
|
+
called directly receives. The declared parameter types say how to put
|
|
97
|
+
them back.
|
|
98
|
+
"""
|
|
99
|
+
for param in spec.params:
|
|
100
|
+
if param.name not in args:
|
|
101
|
+
continue
|
|
102
|
+
value = args[param.name]
|
|
103
|
+
if value is None:
|
|
104
|
+
# An optional parameter left empty. Nothing to rebuild, and
|
|
105
|
+
# Path(None) would raise.
|
|
106
|
+
continue
|
|
107
|
+
declared = _unwrap_optional(param.type)
|
|
108
|
+
if isinstance(declared, type) and issubclass(declared, Enum):
|
|
109
|
+
if not isinstance(value, declared):
|
|
110
|
+
args[param.name] = declared(value)
|
|
111
|
+
elif declared is Path and not isinstance(value, Path):
|
|
112
|
+
args[param.name] = Path(value)
|
|
113
|
+
return args
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _unwrap_optional(annotation: Any) -> Any:
|
|
117
|
+
"""``X`` from ``X | None``, and anything else unchanged.
|
|
118
|
+
|
|
119
|
+
A parameter that may be left empty is spelled ``X | None``, which is a
|
|
120
|
+
union rather than ``X`` -- so without this, an optional Path or Enum
|
|
121
|
+
would travel as a string and arrive as one, while the same op called
|
|
122
|
+
directly received the real type.
|
|
123
|
+
"""
|
|
124
|
+
if get_origin(annotation) is None:
|
|
125
|
+
return annotation
|
|
126
|
+
args = [a for a in get_args(annotation) if a is not type(None)]
|
|
127
|
+
return args[0] if len(args) == 1 else annotation
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _check_args(spec: OpSpec, args: dict) -> None:
|
|
131
|
+
if spec.form == FUNCTION:
|
|
132
|
+
return
|
|
133
|
+
missing = [
|
|
134
|
+
p.name for p in spec.params if p.direction is not None and p.name not in args
|
|
135
|
+
]
|
|
136
|
+
if missing:
|
|
137
|
+
raise TypeError(
|
|
138
|
+
f"Op {spec.name} is {spec.form} form, so its buffers must be "
|
|
139
|
+
f"supplied by the caller. Missing: {', '.join(missing)}. "
|
|
140
|
+
"Automatic allocation is not implemented yet."
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _pack(spec: OpSpec, result: Any, refs: list) -> dict:
|
|
145
|
+
names = tuple(o.name for o in spec.outputs)
|
|
146
|
+
if spec.form != FUNCTION or not names:
|
|
147
|
+
# The host owns the output buffers; nothing to send back.
|
|
148
|
+
return {}
|
|
149
|
+
if names == ("result",):
|
|
150
|
+
return {"result": _codec.encode(result, refs)}
|
|
151
|
+
if result is None:
|
|
152
|
+
raise TypeError(f"Op {spec.name} declares outputs {names} but returned None")
|
|
153
|
+
if len(result) != len(names):
|
|
154
|
+
raise TypeError(
|
|
155
|
+
f"Op {spec.name} declares {len(names)} outputs {names} "
|
|
156
|
+
f"but returned {len(result)} values"
|
|
157
|
+
)
|
|
158
|
+
return {name: _codec.encode(value, refs) for name, value in zip(names, result)}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: oprunner
|
|
3
|
+
Version: 0.1.0.dev0
|
|
4
|
+
Summary: Run image-processing ops in their own environments, with Appose.
|
|
5
|
+
Author: scikit-ops developers
|
|
6
|
+
License-Expression: BSD-2-Clause
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Requires-Dist: appose>=0.12
|
|
15
|
+
Requires-Dist: numpy
|
|
16
|
+
Requires-Dist: opspec>=0.1.0.dev0
|
|
17
|
+
Requires-Dist: psutil
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# oprunner
|
|
21
|
+
|
|
22
|
+
Runs ops -- Python functions declared with [opspec](../opspec) -- in their own
|
|
23
|
+
environments, built with [Appose](https://github.com/apposed/appose). It can
|
|
24
|
+
also tile a call to fit memory, and loop a 2-D op over a stack.
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from oprunner import Runner
|
|
28
|
+
|
|
29
|
+
with Runner() as runner:
|
|
30
|
+
result = runner.run(some_op, image=image, sigma=2.0)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
An op's environment is found beside the op's own files, in `envs/<id>/pixi.toml`.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
oprunner/__init__.py,sha256=vkU84LHsBdIMhEoI2mo4y9xhVlURbOM6sup9sCqpdsE,1920
|
|
2
|
+
oprunner/_adapt.py,sha256=3k8tufgIlCdXNjP0MQZi2DPcPjxF2VUk_U7EGX7rkmI,6978
|
|
3
|
+
oprunner/_codec.py,sha256=0j7F5FDKfNlvZmMOwO4kV6wVKPHFMUfPPALWabhbLZI,4456
|
|
4
|
+
oprunner/_tiling.py,sha256=ofh_wxVT2MeDD1XpzwbvkefWJ6rX3XahnWt1RiaWUv0,16512
|
|
5
|
+
oprunner/discovery.py,sha256=odv78i7Lf_WRw6v7JEnKLy0tCGXoQJwvPhTtSDRFdBo,4575
|
|
6
|
+
oprunner/host.py,sha256=Vzlbwzv20M3J634m4BdneiwQpiMS4OQlaTh62CVN0Ys,10981
|
|
7
|
+
oprunner/runner.py,sha256=oVsvd4Rieb5FVNTYm1pJYbdNQvRBGtXdrn4sb5XCH3g,35480
|
|
8
|
+
oprunner/worker.py,sha256=VOFXzxmfMuppwfV9YeKPBx1qJHouA5wSDLa5x5WZZJg,5808
|
|
9
|
+
oprunner-0.1.0.dev0.dist-info/METADATA,sha256=ues12QCwl3Jvksm2jFIBCDo5RX2hlgjNmRSmeKAg5Mo,1089
|
|
10
|
+
oprunner-0.1.0.dev0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
11
|
+
oprunner-0.1.0.dev0.dist-info/licenses/LICENSE,sha256=OVmXAsSBJBi_elQtTc9S-H02MbKDRyFy7_ZPE95OJkA,1299
|
|
12
|
+
oprunner-0.1.0.dev0.dist-info/RECORD,,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
BSD 2-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, scikit-ops developers
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
16
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
17
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
19
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
20
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
21
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
22
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
23
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING OUT OF THE USE OF THIS
|
|
24
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|