|
| 1 | +from scyjava import jimport |
| 2 | +import scyjava.config |
| 3 | +from types import MethodType |
| 4 | +jars=scyjava.config.find_jars("C:\\Users\\hiner\\code\\scijava\\incubator\\imagej\\imagej-ops2\\target\\dependency\\") |
| 5 | +scyjava.config.add_classpath(*jars) |
| 6 | +scyjava.config.add_classpath("C:\\Users\\hiner\\code\\scijava\\incubator\\imagej\\imagej-ops2\\target\\imagej-ops2-0-SNAPSHOT.jar") |
| 7 | +env=jimport('org.scijava.ops.engine.DefaultOpEnvironment')() |
| 8 | + |
| 9 | +op_names={str(name) for info in env.infos() for name in info.names()} |
| 10 | + |
| 11 | +#ns={op[:op.index('.')] for op in op_names if op.find('.') >= 0} |
| 12 | + |
| 13 | +class OpNamespace: |
| 14 | + def __init__(self, env, ns): |
| 15 | + self.env = env |
| 16 | + self.ns = ns |
| 17 | + |
| 18 | +class OpGateway(OpNamespace): |
| 19 | + def __init__(self, env): |
| 20 | + super().__init__(env, 'global') |
| 21 | + |
| 22 | +def nary(env, fqop, arity): |
| 23 | + 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] |
| 24 | + return arities[arity](fqop) |
| 25 | + |
| 26 | +def add_op(c, op_name): |
| 27 | + if hasattr(c, op_name): |
| 28 | + return |
| 29 | + |
| 30 | + def f(self, *args, **kwargs): |
| 31 | + print(type(self)) |
| 32 | + fqop = op_name if self.ns == 'global' else self.ns + "." + op_name |
| 33 | + run = kwargs.get('run', True) |
| 34 | + b = nary(self.env, fqop, len(args)).input(*args) |
| 35 | + # inplace |
| 36 | + # ops.filter.gauss(image, 5, inplace=0) |
| 37 | + if (inplace:=kwargs.get('inplace', None)) is not None: |
| 38 | + return b.mutate(inplace) if run else b.inplace(inplace) |
| 39 | + |
| 40 | + # computer |
| 41 | + # ops.filter.gauss(image, 5, out=result) |
| 42 | + if (out:=kwargs.get('out', None)) is not None: |
| 43 | + b=b.output(out) |
| 44 | + return b.compute() if run else b.computer() |
| 45 | + |
| 46 | + # function |
| 47 | + # gauss_op = ops.filter.gauss(image, 5) |
| 48 | + # result = ops.filter.gauss(image, 5) |
| 49 | + return b.apply() if run else b.function() |
| 50 | + |
| 51 | + if c == OpGateway: |
| 52 | + # op_name is a global. |
| 53 | + setattr(c, op_name, f) |
| 54 | + else: |
| 55 | +# if isinstance(c, MethodType): |
| 56 | +# print("oh no! - race condition happened") |
| 57 | + m=MethodType(f, c) |
| 58 | + setattr(c, op_name, m) |
| 59 | + |
| 60 | +def add_namespace(c, ns, op_name): |
| 61 | + if not hasattr(c, ns): |
| 62 | + setattr(c, ns, OpNamespace(env, ns)) |
| 63 | + add_op(getattr(c, ns), op_name) |
| 64 | + |
| 65 | +for op in op_names: |
| 66 | + print(op) |
| 67 | + dot = op.find('.') |
| 68 | + if dot >= 0: |
| 69 | + ns=op[:dot] |
| 70 | + op_name=op[dot+1:] |
| 71 | + add_namespace(OpGateway, ns, op_name) |
| 72 | + else: |
| 73 | + print(f"registering global op: {op}") |
| 74 | + add_op(OpGateway, op) |
| 75 | + |
0 commit comments