here I present a collection of (hopefully) useful example on how to start various external applications from a ZEN OAD macro directly using different ways.
- Start (normal) Python Script directly to plot a CSV data file (saved ZEN data table)
- Start (normal) Python Script using a little tool script to plot the same CSV data file
- Open an CZI File, Start Fiji, Analyse the image data in Fiji and import the results back into ZEN
- Open an CZI File and open the image data in ICY
All those example illustrate the general approaches that can be used to start various external application from with a OAD macro. For plotting the data using matplotlib one must have the required python modules installed --> see PythonXY, WinPython, .... (this is outside the ZEN world and not possible in IronPython).
All the scripts (only the CZI for the cell count is attached) are attached. It contains also an image analysis pipeline that can be used to create the CSV table and the required Fiji macro for counting cells.
So please replace 3CH_Z-Stack.czi inside the macro with a similar CZI (please do not try Gigabyte-sized multiposition files right away .
Good luck!
Code: Select all
"""
Author: SRh
Date: 2013_12_02
Version: 1.0
This macro illustrates the possibility to start an external application directly from a Zen OAD Python script.
1) Load CZI File (or Execute a Zen Experiment) or Data File
2) Define parameters to hand over to the file location
3) Use .NET power inside IronPython... --> start external process = Python Script, Fiji or ICY
Remark: With this method one can start any external application from with an Zen Macro.
It works for Fiji, ImageJ, ICY, Python Scripts, ...
"""
import clr
from System.Diagnostics import Process
from System.IO import Directory, Path, File
import sys
# place all the example files and scripts here ...
scriptfolder = r'MyFolder\Start_External_Application_Example'
sys.path.append(scriptfolder)
# import tools scripts
import FijiTableTools as ft
import StartExternal as stext
# please set to True if one wants the example to be executed
start_PlotData = True
start_Simple_Plot = False
start_Fiji = False
start_Icy = False
# Remove all open images
Zen.Application.Documents.RemoveAll()
# example csv table created from Zen table
csvfile = r'MyFolder\Start_External_Application_Example\96well_all_objects.csv'
# example - Time Series CZI file
imagefile1 = r'MyFolder\\Count_Cells_DAPI_96frames.czi'
# example - 2 Channel Z-Stack
imagefile2 = r'MyFolder\\Start_External_Application_Example\3CH_Z-Stack.czi'
######## Example 1 - Start Python Script and pass command line parameters #######
# check if set to run
if (start_PlotData == True):
# this is the location of the script use to plot the data
# use this version for a general approach --> pass filename, X-column, Y-column and rows2skip to plot directly from the OAD macro
script1 = r'MyFolder\\Start_External_Application_Example\PlotData_Example.py'
# define the parameters --> the respective python script must be configure to be able to read command line parameters
# requires PythonXY, WinPython or required Python modules to be installed
params1 = ' -f ' + csvfile + ' -x 2 -y 0 -r 2'
# start the data display script as an external application
app1 = Process();
app1.StartInfo.FileName = script1
app1.StartInfo.Arguments = params1
app1.Start()
######## Example 2 - Start Python Script using a little tool function and pass parameters #######
# check if set to run
if (start_Simple_Plot == True):
# this is easy version, where one has define the columns manually inside the data display script
script2 = r'MyFolder\\simple_plot.py'
# this one for the easy version --> only the filename is passed as an argument
params2 = ' -f ' + csvfile
# start the data display script as an external application using a tool script
stext.StartApp(script2, params2)
######## Example 3 - Start Fiji, Open the CZI file, Run an analysis macros inside Fiji and re-import the results into Zen #######
# check if set to run
if (start_Fiji == True):
# load the time series example CZI file
image1 = Zen.Application.LoadImage(imagefile1)
Zen.Application.Documents.Add(image1)
# define Fiji analysis macro
filename = image1.FileName # get the name of the current image data set
exeloc = 'C:\Users\Public\Documents\Fiji\ImageJ-win64.exe' # use the absolute path --> this works always
macro = '-macro Zen_Test\Count_Cells_EXP2.ijm' # specify the Fiji macro one wants to use
option = macro + ' ' + filename # 'glue' together the options
# define status of Fiji
Fiji_Finished = False
## start Fiji and execute the macro
app = Process();
app.StartInfo.FileName = exeloc
app.StartInfo.Arguments = option
app.Start()
# check if Fiji already saved the data tables
while (Fiji_Finished == False):
Fiji_Finished = File.Exists(filename + '_Summary_Fiji.txt')
#print Fiji_Finished
# read the data tables and convert them into Zen tables
rowoffset = 1 # skip ... lines
filename_R = filename + '_Results_Fiji.txt'
filename_S = filename + '_Summary_Fiji.txt'
# Results Table
[ValuesArray_R, Legends_R, numvar_R, entries_R, coltypelist] = ft.Conv2Array(filename_R, rowoffset, '\t')
table_R = ft.CreateTable(ValuesArray_R, numvar_R, entries_R, Legends_R, 1, 'Result Table',coltypelist)
# read the sliceor frame lables separately since those are strings
labels = ft.Conv2List(filename_R, rowoffset, '\t', 1)
# add them to the Zen table to replace the NaNs
ft.AddLabels(table_R, labels, entries_R, 0)
Zen.Application.Documents.Add(table_R)
# Summary Table
[ValuesArray_S, Legends_S, numvar_S, entries_S,coltypelist] = ft.Conv2Array(filename_S, rowoffset, '\t')
table_S = ft.CreateTable(ValuesArray_S, numvar_S, entries_S, Legends_S, 0, 'Summary Table',coltypelist)
Zen.Application.Documents.Add(table_S)
######## Example 4 - Start ICY and open the CZI file inside ICY #######
# check if set to run
if (start_Icy == True):
icylocation = r'C:\Users\Public\Documents\Icy\icy.exe'
## start application as an external process
app = Process();
app.StartInfo.FileName = icylocation
app.StartInfo.Arguments = imagefile2
app.Start()