I am trying to get a home made Fiji script to sun inside Python by calling Fiji, but there's little documentation on how to do it.
What I need is something like this:
def myfijiscript:
[CODE]
and then in Python:
fiji(myfijiscript)
is there a way to do this?
Python (or, to be precise, Jython) scripts within Fiji are executed using the org.python.util.PythonInterpreter class (see source code).
It doesn't make much sense to run a Jython script within a Java instance that is started from with Python, but have a look at those two questions concerning how to run external commands in python. You can save your script in a file myscript.py and then do:
call(["./ImageJ-linux64", "myscript.py"])
using the ImageJ launcher from the command line.
The other way is to use ImageJ as a library and just import the classes you need for your script, as others have suggested:
from ij import IJ
Related
I would like to run MSC NASTRAN using python. I have seen a similiar function in MATLAB using
system('nastran.exe file_name.bdf') #where file_name.bdf is the input file to Run using nastran.
Hence i tried below using python code, but it did not work,
import os
os.system('nastran.exe file_name.bdf')
Could you tell me where i going wrong?
Also, how to give the command line in NASTRAN thru python? Like for example memory allocation for the run, number of cores need to be used for run etc.
some NASTRAN command lines include,
1. scr=yes delete=f04,log,xdb pause=yes
2. mem=10gb bpool=3gb memorymaximum=14gb sscr=500gb sdball=500gb mode=i8
...etc.
I can't speak directly for MSC Nastran, its been a while since I've used it. But most modern FEA programs have an API (application program interface) to allow you to call commands from a external program like python or matlab.
Without an API, you may be limited to using python to start the program from the command line, which is what your code is trying to do. As for how to launch a program from within python, check out this question/answer:
How to run application with parameters in Python?
Easy way to run a MSC NASTRAN file is to create a .bat file and run it from python.
The format for .bat file is:
<nastran.exe location> <Python script file location> <Nastran command line>
An example can be :
C:\MSC.Software\MSC_Nastran\20141\bin\nastran.exe C:\py_nastran_run\example.bdf scr=yes old=no delete=f04,log,xdb
Then include the below line in python script,
status=subprocess.call("runBatch.bat")
I am using Python 2.7
I have interactive Python script, named A, written using Cmd module.
However, sometimes there is a need to activate script A from other Python scripts.
It also could be nice if the interactive script A wouldn't exit in between commands given to them.
I tried to search for solutions and I found couple here
One is to pass a file from which the interactive script will read commands. It works well for me but it is not much liked.
Another is to use echo which doesn't work for me (still trying).
I wonder whether there are other solutions.....
I've got a bash script that's starting to cross that grey area where it should probably be rewritten in a scripting language. Since I'm constantly tweaking the script bash is perfect because of how concise things are. Mostly I'm manipulating output from one program to put into another program (i.e. using cat, grep, sed, tail, head). I'm familiar with Python so I'm looking for a module that can essentially perform these commands. I've found some stuff that uses subprocess to call bash commands but I don't want a wrapper for bash commands. I also realize I could simply take the time to write most of these commands in python and even chain them fairly easily if I encapsulated them all into one class. It seems like such an obvious thing though I find it hard to believe these utilities don't exist in a module that already exists.
You can use the os module in python to have a command executed exactly as it would be in a command shell.
For example, if you have a file named "junk.txt" and want to use the "head" command on it, you would write head junk.txt in your shell.
In Python, it's
import os
os.system("head junk.txt")
I have a Python 2.7 script that among others contains the following piece of code:
import spss
columns = []
spss.StartDataStep()
dataset = spss.Dataset()
for column in dataset.varlist:
columns.append(column.name)
spss.EndDataStep()
print columns
When running this code inside a SPSS syntax (so between BEGIN PROGRAM. and END PROGRAM), it runs as expected and I end up with the variables in the active dataset.
However, when running the same code as part of a script (so from Utilities > Run script...) will return me no results.
It looks as if the SPSS session context is not taken into consideration when running a script.
Is there a way around this problem, or am I doing something wrong?
I don't want to run my code as part of Syntax file, I just want to use vanilla Python scripts.
This is, unfortunately, a complicated issue. I don't think Statistics is working as documented. I will take this up with Development.
It appears that in V24, when you run a Python script via Utilities > Run Script (which is the same as issuing the SCRIPT command), your script is connected to the Statistics Viewer process but not to the Statistics backend (the spssengine process), which is where the data live. There are typically three processes running - the stats.exe process, the spssengine process, and, for Python code, the startx process. Your script can issue commands via the spss.Submit api and can use other spss apis, but they go against a new copy of the backend, so the expected backend context is not present.
To get around this, you can run a trivial program like
begin program.
import ascript
end program.
where ascript.py is a Python module on the Python search path. (You could put these lines in an sps file and use INSERT to execute it, too.)
Another way to approach this would be to run Statistics in external mode. In that mode, you run a Python program that uses SPSS apis but the Python program is on top, and no Statistics user interface appears. You can read about this in the Python scripting help.
An advantage of external mode is that you can use your favorite Python IDE to build and debug your code. That's a big advantage if you are basically a Python person. I use Wing IDE, but any Python IDE should work. You can also set up an alternative IDE as your default by editing the clientscriptingcfg.ini file in the Statistics installation directory. See the scripting help for details. With a tool like Wing, this lets you debug your scripts or other Python code even if run within Statistics.
How do I use exec() in imageJ, http://fiji.sc/ImageJ_tricks#Execute_external_programs? Can I use the exec() function to launch an external python program with something like:
exec("python", d:\py\program1.py");
Is it possible to return an image array to exec from python?
The exec() macro function is hard coded to return STDOUT, as outlined in Curtis Rueden's answer to a similar question on the ImageJ mailing list.
However, you can run Python (or, for that matter, Jython) scripts directly from within Fiji via the script editor or by placing them into the Fiji.app/plugins/ folder.
For anyone interested in using additional python libraries with imageJ, exec() can get the job done. Use:
exec("python", "d:\\pathtoyour\\pythonprogram.py");
Save this using the imageJ script editor as .ijm file. This can then be installed as a plugin to launch your python program from imageJ.