-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathops.py
More file actions
74 lines (60 loc) · 2.07 KB
/
ops.py
File metadata and controls
74 lines (60 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from types import MethodType
env = ij.get("org.scijava.legacy.service.OpEnvironmentService").env()
op_names={str(name) for info in env.infos() for name in info.names()}
#ns={op[:op.index('.')] for op in op_names if op.find('.') >= 0}
class OpNamespace:
def __init__(self, env, ns):
self.env = env
self.ns = ns
class OpGateway(OpNamespace):
def __init__(self, env):
super().__init__(env, 'global')
def add_op(c, op_name):
if hasattr(c, op_name):
return
def f(self, *args, **kwargs):
print(type(self))
fqop = op_name if self.ns == 'global' else self.ns + "." + op_name
run = kwargs.get('run', True)
b = self.env.op(fqop).input(*args)
# inplace
# ops.filter.gauss(image, 5, inplace=0)
if (inplace:=kwargs.get('inplace', None)) is not None:
return b.mutate(inplace) if run else b.inplace(inplace)
# computer
# ops.filter.gauss(image, 5, out=result)
if (out:=kwargs.get('out', None)) is not None:
b=b.output(out)
return b.compute() if run else b.computer()
# function
# gauss_op = ops.filter.gauss(image, 5)
# result = ops.filter.gauss(image, 5)
return b.apply() if run else b.function()
if c == OpGateway:
# op_name is a global.
setattr(c, op_name, f)
else:
#if isinstance(c, MethodType):
#print("oh no! - race condition happened")
m=MethodType(f, c)
setattr(c, op_name, m)
# ipython HACKS
globals()['add_op'] = add_op
globals()['OpNamespace'] = OpNamespace
globals()['OpGateway'] = OpGateway
globals()['MethodType'] = MethodType
def add_namespace(c, ns, env, op_name):
if not hasattr(c, ns):
setattr(c, ns, OpNamespace(env, ns))
add_op(getattr(c, ns), op_name)
for op in op_names:
print(op)
dot = op.find('.')
if dot >= 0:
ns=op[:dot]
op_name=op[dot+1:]
add_namespace(OpGateway, ns, env, op_name)
else:
print(f"registering global op: {op}")
add_op(OpGateway, op)
ops = OpGateway(env)