Skip to content

Commit 4b1c4e3

Browse files
committed
Copy gateways.py to notebooks
We want to use it in both. Originally it was symlinked... that turned out to be a bad idea! Eventually this will be replaced with a proper module for the gateway functionality.
1 parent 3f3e556 commit 4b1c4e3

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'''
2+
Create namespace convenience methods for all discovered ops.
3+
Assumes we're running in an environment with scyjava configured to have a classpath with some SciJava Ops implementations (i.e. org.scijava:scijava-ops-engine plus implementations)
4+
5+
Classes:
6+
OpNamespace
7+
OpGateway
8+
9+
Variables:
10+
env - OpEnvironment used to populate the OpGateway and OpNamespaces
11+
ops - OpGateway entry point
12+
'''
13+
14+
from scyjava import jimport
15+
from types import MethodType
16+
env=jimport('org.scijava.ops.engine.DefaultOpEnvironment')()
17+
18+
op_names={str(name) for info in env.infos() for name in info.names()}
19+
20+
class OpNamespace:
21+
'''
22+
Represents intermediate ops categories. For example, "math.add" and "math.sub" are in both in the "math" namespace.
23+
'''
24+
def __init__(self, env, ns):
25+
self.env = env
26+
self.ns = ns
27+
28+
def help(self, op_name=None):
29+
'''
30+
Convenience wrapper for OpEnvironment.description(), for information about available ops. Prints all returned information, line-by-line.
31+
'''
32+
print(*self.env.descriptions(op_name), sep = "\n")
33+
34+
class OpGateway(OpNamespace):
35+
'''
36+
Global base specialization of OpNamespace. Contains all other namespaces, in addition to all ops in the "global" namespace.
37+
'''
38+
def __init__(self, env):
39+
super().__init__(env, 'global')
40+
41+
def nary(env, fqop, arity):
42+
'''
43+
Helper method to convert a numerical arity to the corresponding OpEnvironment method
44+
45+
Parameters:
46+
env (OpEnvironment): the environment being used
47+
fqop (str): the fully-qualified op name
48+
arity (int): the desired arity of the op
49+
50+
Returns:
51+
The appropriate OpBuilder.Arity instance for invoking the given op with the indicated number of parameters
52+
'''
53+
arities=[env.nullary, env.unary, env.binary, env.ternary, env.quaternary, env.quinary, env.senary, env.septenary, env.octonary, env.nonary, env.decenary, env.arity11, env.arity12, env.arity13, env.arity14, env.arity15, env.arity16]
54+
return arities[arity](fqop)
55+
56+
def add_op(c, op_name):
57+
'''
58+
Helper method patch in a given op name as its corresponding function call within the given class
59+
60+
Parameters:
61+
c (class): the OpNamespace/OpGateway to add a new function with the given op_name that calls the op
62+
op_name (str): the actual name of the op we're adding
63+
'''
64+
if hasattr(c, op_name):
65+
return
66+
67+
def f(self, *args, **kwargs):
68+
'''
69+
Instance method to attach to our OpNamespace/OpGateway that does the actual op call
70+
'''
71+
fqop = op_name if self.ns == 'global' else self.ns + "." + op_name
72+
run = kwargs.get('run', True)
73+
b = nary(self.env, fqop, len(args)).input(*args)
74+
# inplace
75+
# ops.filter.gauss(image, 5, inplace=0)
76+
if (inplace:=kwargs.get('inplace', None)) is not None:
77+
return b.mutate(inplace) if run else b.inplace(inplace)
78+
79+
# computer
80+
# ops.filter.gauss(image, 5, out=result)
81+
if (out:=kwargs.get('out', None)) is not None:
82+
b=b.output(out)
83+
return b.compute() if run else b.computer()
84+
85+
# function
86+
# gauss_op = ops.filter.gauss(image, 5)
87+
# result = ops.filter.gauss(image, 5)
88+
return b.apply() if run else b.function()
89+
90+
if c == OpGateway:
91+
# op_name is a global.
92+
setattr(c, op_name, f)
93+
else:
94+
m=MethodType(f, c)
95+
setattr(c, op_name, m)
96+
97+
def add_namespace(c, ns, op_name):
98+
'''
99+
Helper method to add an op call with nested OpNamespace instances if needed
100+
101+
Parameters:
102+
c (class): the OpNamespace/OpGateway to add the given op and namespace
103+
ns(str): the namespace to nest the given op within
104+
op_name (str): the actual name of the op we're adding
105+
'''
106+
if not hasattr(c, ns):
107+
setattr(c, ns, OpNamespace(env, ns))
108+
add_op(getattr(c, ns), op_name)
109+
110+
# Entry point to do the namespace population.
111+
# This modifies the OpGateway class to add a call for each op, with intermediate accessors by namespace
112+
for op in op_names:
113+
dot = op.find('.')
114+
if dot >= 0:
115+
ns=op[:dot]
116+
op_name=op[dot+1:]
117+
add_namespace(OpGateway, ns, op_name)
118+
else:
119+
add_op(OpGateway, op)
120+
121+
# Make an instance of our modified OpGateway class as a global for external use
122+
ops = OpGateway(env)

0 commit comments

Comments
 (0)