napari-imagej icon indicating copy to clipboard operation
napari-imagej copied to clipboard

Programmatically calling search results

Open kephale opened this issue 2 years ago • 6 comments

I was hoping to programmatically make calls to commands/scripts with napari-imagej.

I've read up to ResultRunner, jc.SearchResult, and python_actions_for but it looks like search is pretty directly connected to the UI.

Is there a way to do something like:

endpoint = "sc.fiji:fiji:2.13.0+org.morphonets:SNT:MANAGED"

...

search_results = get_matching_commands("tracing")
command_result = run_command(search_results[0], <some args>)

kephale avatar May 24 '23 14:05 kephale

@kephale you can get much of this functionality from pyimagej, but there's of course additional value in the processing napari-imagej adds on top of pyimagej. And @ctrueden asks for a lot of this functionality in #208 - the functionality is, like you say, too tied in to the GUI. A better separation of functionality from UI is already on the docket!

What's your use case here? I really want to support stuff like this - I'm just not sure whether it is napari-imagej's job or pyimagej's job to do it.

gselzer avatar May 24 '23 15:05 gselzer

I'm trying to port workflows from purely being run within Fiji to being run as Python scripts, so I'm starting with a skeleton script that sets up an endpoint, then incrementally adding/tweaking code line-by-line.

I think the main thing I want is to have Python functions that correspond to IJ2 Commands which have the logic for taking napari layers as input. It is definitely possible to do this stuff with pyimagej, but it seems as though napari-imagej is already providing the logic for mapping from IJ2 types to napari layers.

More on using pyimagej+napari-imagej in another issue.

kephale avatar May 24 '23 15:05 kephale

@gselzer Is it possible to make use of napari-imagej's layer conversion logic programmatically by using ij.py.to/from_java, since napari-imagej registers that conversion logic as scyjava converters?

ctrueden avatar May 24 '23 17:05 ctrueden

@gselzer Is it possible to make use of napari-imagej's layer conversion logic programmatically by using ij.py.to/from_java, since napari-imagej registers that conversion logic as scyjava converters?

Yup, all you have to do is register the converters (either by letting napari-imagej install them for you by calling init_ij, or manually by calling install_converters)

gselzer avatar May 24 '23 17:05 gselzer

OK, so @kephale's script could use napari_imagej.java.init_ij to synchronously initialize PyImageJ according to its current settings, and then he's good to go? I guess that still doesn't resolve this issue though, which is about exposing programmatic access to the search functions. You could do that now by using the Java API by accessing the SearchService, but there's nothing more Pythonic yet. What else (if anything) does napari-imagej add on top of the search service that would be good to expose?

ctrueden avatar May 24 '23 19:05 ctrueden

As part of my work on #265, I'm creating a model.py with NapariImageJ central class holding all the non-Qt-specific business logic. This will include the REPL of course, but I'll also see about making the search logic part of that as well. Then maybe this issue can be closed out at the same time as #265, assuming my plan is successful. API calls for everyone! :birthday:

ctrueden avatar Jul 25 '23 16:07 ctrueden