|
| 1 | +# Reasoning about Ops |
| 2 | + |
| 3 | +The declarative nature of Ops is convenient for matching and execution, but sometimes it is necessary to know where an Op comes from. This page describes the tools provided by SciJava Ops to understand the code underneath. |
| 4 | + |
| 5 | +Naturally, to understand where an Op came from, you first need an Op: |
| 6 | + |
| 7 | +```java |
| 8 | +var img = ...; |
| 9 | +var out = ...; |
| 10 | +var sigma = 5.0; |
| 11 | + |
| 12 | +// This Op call finds a "filter.gauss Op" that blurs an image using a provided |
| 13 | +// sigma, placing the result in the output container "out". |
| 14 | +// |
| 15 | +// NB the Op is saved within the variable "op" |
| 16 | +var op = ops.op("filter.gauss").input(img, sigma).output(out).computer(); |
| 17 | +``` |
| 18 | + |
| 19 | +## Introduction |
| 20 | + |
| 21 | +Each Op is generated from an `OpInfo` object, describing an Op's source code, version, and other associated metadata. If you've matched an Op `op`, you can obtain its `OpInfo` using the method `Ops.info(op)`: |
| 22 | + |
| 23 | +```java |
| 24 | +import org.scijava.ops.api.Ops; |
| 25 | + |
| 26 | +var info = Ops.info(op); |
| 27 | +``` |
| 28 | + |
| 29 | +The `OpInfo` often provides all needed information, but if `op` utilizes dependencies or other framework features, you may wish to introspect the "rich" Op instead, using the method `Ops.rich(op)`. This method returns a `RichOp` object which wraps the Op up with its `OpInfo` and the `OpInfo`s of its dependencies, along with other metadata. |
| 30 | + |
| 31 | +```java |
| 32 | +var richOp = Ops.rich(op); |
| 33 | +``` |
| 34 | + |
| 35 | +These two methods provide gateways into learning more about where your Op came from! |
| 36 | + |
| 37 | +## Implementations and Versions |
| 38 | + |
| 39 | +For reproducibility, it is important to know not only the underlying implementations, or discrete lines of code, backing each executed Op, but additionally the *version* of that implementation if it changes over time. Fortunately, SciJava Ops provides mechanisms to determine both! |
| 40 | + |
| 41 | +A unique identifier to an Op's implementation can be obtained from its `OpInfo` using the method `OpInfo.implementationName()`. For example, the following |
| 42 | + |
| 43 | +```java |
| 44 | +System.out.println(Ops.info(op).implementationName()); |
| 45 | +``` |
| 46 | + |
| 47 | +might print out (if you're using `scijava-ops-image` version `1.0.0`): |
| 48 | + |
| 49 | +```{code-block} |
| 50 | +:class: no-copybutton |
| 51 | +org.scijava.ops.image.filter.gauss.Gaussians.gaussRAISingleSigma(net.imglib2.RandomAccessibleInterval<I>,double,net.imglib2.outofbounds.OutOfBoundsFactory<I, net.imglib2.RandomAccessibleInterval<I>>,net.imglib2.RandomAccessibleInterval<O>)Reduction1 |
| 52 | +``` |
| 53 | + |
| 54 | +Note that in many cases, including the case above, the implementation name may not itself be an explicit piece of code, however it must always explain how an explicit piece of code became the Op you've matched. |
| 55 | + |
| 56 | +Similarly, the method `OpInfo.version()` describes **the version of the Op**, which is set by whichever component declares the Op. This version might change due to: |
| 57 | +* An update in the version of the underlying implementation (such as a bump in the version of `scijava-ops-image`) |
| 58 | +* An update to the Op itself (such as the inclusion of an additional alias, or a change in the Op priority) |
| 59 | + |
| 60 | +SciJava Ops guarantees identical outputs from an Op execution given identical inputs **and** an identical `OpEnvironment`. Your results may differ if you: |
| 61 | +* Add new Ops into your `OpEnvironment`, if a new Op takes precedence over an existing Op |
| 62 | +* Change the version of an Op within the `OpEnvironment` by updating a component |
| 63 | +* Pass different inputs to the Op |
| 64 | + |
| 65 | +## Op Signatures |
| 66 | + |
| 67 | +While SciJava Ops requires the aforementioned constraints to ensure reproducible Op *matches*, the concept of **Op Signatures** enables SciJava Ops to **reproducibly reconstruct prior matches in new environments** (assuming all required Ops are still available). |
| 68 | + |
| 69 | +Given a matched Op `op`, its signature can be obtained using the static method `Ops.signature(op)`, which distills all Op implementation names and versions into a single string. For example, the following |
| 70 | + |
| 71 | +```java |
| 72 | +System.out.println(Ops.signature(op)); |
| 73 | +``` |
| 74 | + |
| 75 | +might print out (again using `scijava-ops-image` version `1.0.0`): |
| 76 | + |
| 77 | +```{code-block} |
| 78 | +:class: no-copybutton |
| 79 | +|Reduction:|ParamsReduced:1|OriginalInfo:|Info:org.scijava.ops.image.filter.gauss.Gaussians.gaussRAISingleSigma(net.imglib2.RandomAccessibleInterval<I>,double,net.imglib2.outofbounds.OutOfBoundsFactory<I, net.imglib2.RandomAccessibleInterval<I>>,net.imglib2.RandomAccessibleInterval<O>)@1.0.0{} |
| 80 | +``` |
| 81 | + |
| 82 | +**Assuming all utilized Ops and Op features are still available**, the Op can then be reconstructed using the method `OpEnvironment.opFromSignature(signature, opType)`: |
| 83 | + |
| 84 | +```java |
| 85 | +// The value of signature could be saved within a script for future reconstruction |
| 86 | +String signature = Ops.signature(op); |
| 87 | + |
| 88 | +... |
| 89 | + |
| 90 | +// NB: This generic type construct is needed for the OpEnvironment to provide a Computer Op. |
| 91 | +var opType = new Nil<Computers.Arity2<Img<DoubleType>, Double, Img<DoubleType>>>() {}; |
| 92 | +// Reconstruct the Op |
| 93 | +var sameOp = ops.opFromSignature(signature, opType); |
| 94 | +// We could then call this Op with our input image, real-valued sigma, and output image container |
| 95 | +sameOp.compute(img, sigma, out); |
| 96 | +``` |
| 97 | + |
| 98 | +There still exist limitations with reconstructive features, including: |
| 99 | +* [scijava/scijava#220](https://github.com/scijava/scijava/issues/220) - The inability to reconstruct an Op using simpler `opType`s. |
| 100 | +* [scijava/scijava#219](https://github.com/scijava/scijava/issues/219) - The inability to reconstruct Op matches with omitted optional parameters. |
| 101 | + |
| 102 | +## Info Trees |
| 103 | + |
| 104 | +Ops often utilize Op dependencies, which may have dependencies themselves. As such, the Ops returned by SciJava Ops are best thought of as a [tree](https://en.wikipedia.org/wiki/Tree_(data_structure)). While an Op's signature, described above, represents the entire Tree, it is often more convenient to interrogate dependencies using the `InfoTree` data structure, obtained using the method `Ops.infoTree(op)`: |
| 105 | + |
| 106 | +```java |
| 107 | +var tree = Ops.infoTree(op); |
| 108 | +``` |
| 109 | + |
| 110 | +The `InfoTree` API allows users to interrogate: |
| 111 | +* The root `OpInfo`, accessible using `InfoTree.info()` |
| 112 | +* All dependencies, which are each themselves an `InfoTree`, using `InfoTree.dependencies()`. |
| 113 | + |
| 114 | +## Op History |
| 115 | + |
| 116 | +It is often useful to determine the list of Ops responsible for the current state of a given Object. To aid in this task, each `OpEnvironment` records Op executions within an `OpHistory` object, accessible using the method `OpEnvironment.history()`. |
| 117 | + |
| 118 | +```java |
| 119 | +// OpEnvironment.history() provides access to a OpHistory object |
| 120 | +var history = ops.history(); |
| 121 | + |
| 122 | +// Since an Op was responsible for the results written into the "out" object, it is listed in OpHistory.executionsUpon() |
| 123 | +var usedOps = history.executionsUpon(out); |
| 124 | +``` |
| 125 | + |
| 126 | +Using the `OpHistory` object, we can see that our gaussian blur Op `op` is at the end of the list, since it wrote to `out` last. If `out` was additionally created using SciJava Ops, the Op(s) responsible for creating the image would also be included. |
0 commit comments