Skip to content

Conversation

@elevans
Copy link
Member

@elevans elevans commented Aug 11, 2025

With commit b1965c6 I fixed the outputs of run_script and run_macro by returning the promised Java HashMap instead of an instance of a ScriptModule. This change however (1) changes the API behavior and (2) breaks any script that depends on the getOutput() and getOutputs() calls. To alleviate this headache I made, we agreed to implement these methods on the Java HashMap, thereby not breaking scripts calling for outputs. However the getInput() and getInputs() functions are not implemented as they would exist in a different Hash map. Calling these will raise a NotImplementedError.

I also added the appropriate tests for the JavaHashMapAddons.

Here is a minimal script to test/play with this behavior:

import imagej
import numpy as np

# initialize imagej2
ij = imagej.init()

# create test data
arr = np.random.randint(0, 65536, size=(512, 512), dtype=np.uint16)

# create script
sc = """
#@ Img img
#@output Integer a
#@output Float b

dims = img.dimensionsAsLongArray()
a = dims[0] + dims[1]
b = dims[0] * dims[1]
"""

# create arg map
args = {"img": arr}
output = ij.py.run_script("Python (Jython)", sc, args)

# this should FAIL on main without this patch
# this should PASS with release because we return a ScriptModule
a = output.getOutput("a")
b = output.getOutput("b")
map = output.getOutputs()

One question is if raising a NotImplementedError is appropriate here or if we should do something more fancy.

With commit b1965c6 I fixed the outputs of `run_script`
and `run_macro` by returning the promised Java HashMap
instead of an instance of a `ScriptModule`. This change
however (1) changes the API behavior and (2) breaks any
script that depends on the `getOutput()` and `getOutputs`
calls. To alleviate this headache I made, we agreed to
implement these methods on the Java HashMap, thereby
not breaking scripts calling for outputs. However the
`getInput` and `getInputs` functions are not implemented
as they would exisit in a different Hash map. Calling
these will raise a `NotImplementedError`.

This commit also adds the appropriate tests.
@elevans elevans requested a review from ctrueden August 11, 2025 17:54
@imagejan
Copy link
Member

Maybe calling getOutput() and getOutputs() could print some kind of deprecation warning, with an explanation that you can directly use the map?
This would help script maintainers to simplify their code.

@elevans
Copy link
Member Author

elevans commented Aug 11, 2025

Great idea, I'll that in!

@elevans
Copy link
Member Author

elevans commented Aug 11, 2025

The JavaHashMapAddons methods are now depreciated and produce/print warnings:

getOutput() is deprecated. Use the Java HashMap itself (e.g. data.get(key) or data[key]).
getOutputs() is deprecated. Use the Java HashMap itself.

The SciJava ScriptModule interface hierarchy has quite a few methods.
Of course, many of them do not make sense to call anymore after actually
running the module. But for the sake of compatibility, this commit
changes run_script to return a dict-extending object that also supports
all the public non-deprecated ScriptModule methods, delegating to the
linked ScriptModule instance while also issuing a deprecation warning.
Copy link
Member

@ctrueden ctrueden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following through on this, @elevans!

I took the liberty of pushing this a bit farther: with the commit I just pushed, the run_script function now returns a dict-extending object that offers the entire public ScriptModule API, delegating to the linked ScriptModule object's corresponding method, but also printing a deprecation warning in so doing. On my side, this is now good to go, as it avoids backwards-incompatible API breakage. Could you take a look, and merge if you like it? Thanks.

@elevans
Copy link
Member Author

elevans commented Aug 12, 2025

Awesome thank you! I'm looking now and will merge after (unless something jumps out).

@elevans elevans merged commit 3287bac into main Aug 12, 2025
6 checks passed
@elevans elevans deleted the java-hashmap-addons branch August 13, 2025 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants