Call Java (ImageJ plugin BoneJ) from a Jython script - java

**Sorry, I am very new to both python and java so I might not recognize that this has been answered and only needs adjusted for my situation.
Essentially I have a script manager that operates in a python environment and I need to pass data off to the bonej plugin for analysis, which should then return an array that will then be used by another python script for further analysis.
I have been directed to jython as a way to do this, but I can't figure out how to import imageJ as if it were a module.
I know that I can simply open imagej from a command line and direct it to analyze an image, but this is not what I need because it simply returns an average in a segment of bone.
Basically I need to tell imagej that it should analyze this segment of bone, return the array, then this next segment of bone. An additional python script interpolates the arrays onto a predefined background grid.
If anyone has some insight on how to achieve this back and fort from jython I would appreciate it.

You can indeed use Jython to call ImageJ. See the Jython scripting page on the Fiji wiki for an overview and lots of example code. One easy way to get started from within ImageJ is to use the Script Editor. You can then invoke your Jython scripts from the command line using the ImageJ launcher [instructions]. Or you can run them via the jython executable by adding the needed Java libraries to the classpath.
But beware: this does not provide total integration between Python libraries and Java ones. The problem is that many Python libraries are backed by C code, making them accessible only from CPython, and not from Jython. For example, the very popular SciPy and NumPy libraries cannot be called directly from Jython code.

Related

How to use NLTK in Jython, or PY4J cross platforn?

I have two questions, this could go into different directions based on opinion, but as of right now I am writing a Java/Kotlin API. So far it is compatible across all platforms, excluding IOS(have not tested).
A few of the tasks it runs is python using PY4J but the way the API calls the python script to start the PY4J connection is
Runtime.getRuntime().exec("python script.py") # Is there a better way to do this?
Which is fine until it comes to Android. Will Android be able to use this method? If so, the question is answered and finished there.
If not, is there a way to embed a python interpreter into a JAR file? Java project?
I like Jython, but I could not get NLTK to work with it.
1) Is there a way to make Jython work with NLTK : If not,
2) Is there a way to use PY4J in Python?
3) Is there a way to embed a custom Python interpreter in Java/JAR?
Feel free to edit my question, code and title. I don't do this much, so it is probably sloppy.
This was silly, but I will keep it up since this was a real question. You can install python3 directly into the JAR. When the python installation screen shows up you can just install it into a folder inside your Java project.
For the Android part, there is a python package called p4a, python-for-android. You can create a python interpreter with dependencies. This should work.

How do you call C++ and\or Java functions from Python 2.7?

I am creating a Windows program that so far has a .bat file calling a .pyw file, and I need functions from Java and C++. How can I do this?(I don't mind creating a new batch or python file, and I already have the header file for the C++ section and a .jar file for my java components. (For Java I use Eclipse Java Mars, and it's Java 8u101)) Thanks!!!
This is rather simple for C++: you have to compile a library with the function, import it in Python and... call it! Python has a powerful ctypes module in the standard library to handle this kind of tasks.
Here is an example of loading print() function from hypothetical libc.dll.
from ctypes import *
libc = cdll.LoadLibrary("libc.dll")
>>> print(libc.time(None))
1150640792
Calling Java from Python is covered here: How to call a java function from python/numpy?
You can load C++ function and execute it from Python like BasicWolf explained in his answer. For Java, Jython might be a good approach. But then there's a problem - you will need to be dependent on Jython which is not up to date with the latest versions of Python. You will face compatibility issues with different libraries too.
I would recommend compiling your C++ and Java functions to create individual binaries out of them. Then execute these binaries from within Python, passing the arguments as command line parameters. This way you can keep using CPython. You can interoperate with programs written in any language.

do you need jython for calling a python script from java?

I have seen in answered here that if you want to call a python script from java, you can use jython as a option.
But have seen other answers as well saying, you can use Process or ProcessBuilder and call their exec or start methods to run the python script.
As I understand jython allows you to program python in you java code, but it allows you to call python scripts as well via, PythonInterpreter.execfile.
So I'm wondering what are my options if I want to call a python script (e.g. text processing script which uses nltk) from my java code and along with some arguments and return the results back to my java programme? which option should I use?
Jython is an implementation of the Python language for the Java platform
(source)
You can simply use ProcessBuilder if there is a regular python script on a machine that has regular python installed. This saves you from having to include a whole python runtime in your java application. You can pass parameters quite easily, the returned text is a little trickier: See http://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/ or other examples. Basically anything that the python program prints can be captured that way.
However if you don't like external dependencies and prefer shipping everything in a single application you can try jython. It has some limitations though since it doesn't come with all those modules and it seems it may have difficulties with C modules. Getting Python's nltk.wordnet module working for Jython looks like an explanation on how to get nltk to run with it. Arguments and return values should be simpler with jython since the integration is better. And if you want more than just printed text, Jython can even call into java code directly from python. That would give you interesting options for hybrid code.

Pass a File object to a Python script from Java, then get the outputted result as a new File object

I'm working on a big Java project right now, and I decided that the easiest way to "fix" an issue I have with the data in a File object would be to parse and edit it with a Python script I wrote. I tested the Python script on its own on this particular file (outside of Java) and it works fine. Now I'm just wondering how I integrate my Python script with the rest of my Java code.
I basically want to somehow pass this File object to my Python script, which has to be called from Java. Then I want to take the file that the Python script outputs, read it back in as a File object, and return that from this particular Java method.
From researching a bit, I've heard Jython come up a bit, as well as the built-in Python interpreter in Java 6 and later. I'm just not sure where to start. Or alternatively, I guess I could port this Python script over to Java, but that's kind of a last resort. Any help is appreciated!
Look at these classes
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
You can use them in Java to call your Python process.

Building Ruby Documentation for Java Classes/Source

As I've recently started using JRuby, more specifically building Java applications using Ruby code, I've started using Java Libraries such as LWJGL and Slick.
I know JRuby changes Java method names to a more ruby-esque structure, my question is, is there any current way to generate Ruby documentation, either from source or by conversion of current documentation or even based on what JRuby exposes the methods themselves as?
At some point when JRuby 1.6.7 is out I plan on cleaning this really rough project for just such a thing:
https://github.com/enebo/noridoc
The intention is it will weave both Java and Ruby syntax together and give reasonable documentation for a Rubyist wanting to know all Ruby methods. In it's current form you should be able to generate a reasonable set of HTML docs of Java code showing you all ruby aliased methods. You will need to screw around with the javadoc comment.
Oh and we plan on making this tool available out of the box in JRuby once it is good enough to merge.

Categories