Analyze Z stack images and choose Z plane to image

Post your acquisition-related questions and macros here
Anand Ranjan
Posts: 14
Joined: Wed Dec 02, 2020 7:53 pm

Analyze Z stack images and choose Z plane to image

Post by Anand Ranjan »

Hi,
We are trying to take advantage of Zen OAD to smoothen the acquisition of single-molecule data from live yeast cells. So far before doing fluorescent imaging of single molecules, we image cells under phase contrast and manually adjust the focus. Recently we were able to take a series of Z stack images under phase contrast, analyze those images by a Fiji macro, and select the Z plane for single-molecule imaging. I need help with stringing these steps together for faster image acquisition.

Query # 1.
While acquiring Z stack images, is it possible to generate a table with image names along with corresponding Z positions?
Here's the script we use in Zen for collecting images of the Z stack.
-----------------------------------------------------------------------------------------------------------------------------------------------------
## Z stack DIC images
focPos = Zen.Devices.Focus.ActualPosition

newfocPos = focPos - 5
Zen.Devices.Focus.MoveTo(newfocPos)

experiment1 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image1 = Zen.Acquisition.AcquireImage(experiment1)
Zen.Application.Documents.Add(image1)

## Show new focus position
newfocPos = focPos - 4
Zen.Devices.Focus.MoveTo(newfocPos)

experiment2 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image2 = Zen.Acquisition.AcquireImage(experiment2)
Zen.Application.Documents.Add(image2)

## Show new focus position
newfocPos = focPos -3
Zen.Devices.Focus.MoveTo(newfocPos)

experiment3 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image3 = Zen.Acquisition.AcquireImage(experiment3)
Zen.Application.Documents.Add(image3)

## Show new focus position
newfocPos = focPos - 2
Zen.Devices.Focus.MoveTo(newfocPos)

experiment4 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image4 = Zen.Acquisition.AcquireImage(experiment4)
Zen.Application.Documents.Add(image4)

## Show new focus position
newfocPos = focPos - 1
Zen.Devices.Focus.MoveTo(newfocPos)

experiment5 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image5 = Zen.Acquisition.AcquireImage(experiment5)
Zen.Application.Documents.Add(image5)

## Show new focus position
newfocPos = focPos
Zen.Devices.Focus.MoveTo(newfocPos)

experiment6 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image6 = Zen.Acquisition.AcquireImage(experiment6)
Zen.Application.Documents.Add(image6)

## Show new focus position
newfocPos = focPos +1
Zen.Devices.Focus.MoveTo(newfocPos)

experiment7 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image7 = Zen.Acquisition.AcquireImage(experiment7)
Zen.Application.Documents.Add(image7)

## Show new focus position
newfocPos = focPos +2
Zen.Devices.Focus.MoveTo(newfocPos)

experiment8 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image8 = Zen.Acquisition.AcquireImage(experiment8)
Zen.Application.Documents.Add(image8)

## Show new focus position
newfocPos = focPos +3
Zen.Devices.Focus.MoveTo(newfocPos)

experiment9 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image9 = Zen.Acquisition.AcquireImage(experiment9)
Zen.Application.Documents.Add(image9)

## Show new focus position
newfocPos = focPos +4
Zen.Devices.Focus.MoveTo(newfocPos)

experiment10 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image10 = Zen.Acquisition.AcquireImage(experiment10)
Zen.Application.Documents.Add(image10)

## Show new focus position
newfocPos = focPos +5
Zen.Devices.Focus.MoveTo(newfocPos)

experiment11 = Zen.Acquisition.Experiments.GetByName("DIC.czexp")
image11 = Zen.Acquisition.AcquireImage(experiment11)
Zen.Application.Documents.Add(image11)

newfocPos = focPos
Zen.Devices.Focus.MoveTo(newfocPos)

--------------------------------------------------------------------------------------------------------------------------------------------

Query # 2
From Zen we run a Fiji macro to analyze Z stack images for variance in pixel value. The Fiji macro saves the name of the Z stack image with the lowest pixel variance in a file named 'minVar.txt'. From the Zen OAD, is it possible to get the name of the image in 'minVar.txt' file. Right now when I open the 'minVar.txt' file, all I see is the name of the image file 'DIC-05' and the question is how to access the 'minVar.txt' from Zen and get the image name 'DIC-05'.

Query # 3
This might be too many questions- but once we have the image file name such as 'DIC-05' available in Zen, would it be possible to access the table generated above (query #1) and access information of the Z position for 'DIC-05' for further single molecule flouresent imaging at the same Z plane as 'DIC-05'.

Thank you very much for your pateince and support.
Regards,
Anand
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Analyze Z stack images and choose Z plane to image

Post by CarlZeissMicroscopy3 »

Hello Anand Ranjan,

first of all it seems to me that you can size down your code by a loop like

Code: Select all

focPos = Zen.Devices.Focus.ActualPosition

experiment = Zen.Acquisition.Experiments.GetByName("DIC.czexp")

for i in range(-5, 5):
    newfocPos = focPos - i
    Zen.Devices.Focus.MoveTo(newfocPos)
    image = Zen.Acquisition.AcquireImage(experiment)
    Zen.Application.Documents.Add(image)
Query # 1
is it possible to generate a table with image names along with corresponding Z positions?

Code: Select all

focPos = Zen.Devices.Focus.ActualPosition

experiment = Zen.Acquisition.Experiments.GetByName("DIC.czexp")

table = ZenTable()
table.Columns.Add("name", str)
table.Columns.Add("focus", float)

for i in range(-5, 5):
    newfocPos = focPos - i
    Zen.Devices.Focus.MoveTo(newfocPos)
    image = Zen.Acquisition.AcquireImage(experiment)
    table.Rows.Add(image.NameWithoutExtension, newfocPos)
    Zen.Application.Documents.Add(image)

Zen.Application.Documents.Add(table)
Query # 2
how to access the 'minVar.txt' from Zen and get the image name?

Code: Select all

from System.IO import File

fileName = r'C:\YourPath\minVar.txt'
fileContent = File.ReadAllText(fileName)
Query # 3
once we have the image file name available in Zen, would it be possible to access the table?

Code: Select all

focus = None

for row in table.Rows:
    if row[0] == fileContent:
        focus = row[1]
I hope these code snippets help to build a macro according to your needs.
Anand Ranjan
Posts: 14
Joined: Wed Dec 02, 2020 7:53 pm

Re: Analyze Z stack images and choose Z plane to image

Post by Anand Ranjan »

Hi,
thank you for your quick response. The codes are super helpful.

I got an error – AttibuteError: 'Zenimage' object has no attribute 'NameWithoutExtenstion' in line 13, when running the following code-
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
focPos = Zen.Devices.Focus.ActualPosition

experiment = Zen.Acquisition.Experiments.GetByName("DIC.czexp")

table = ZenTable()
table.Columns.Add("name", str)
table.Columns.Add("focus", float)

for i in range(-5, 5):
newfocPos = focPos - i
Zen.Devices.Focus.MoveTo(newfocPos)
image = Zen.Acquisition.AcquireImage(experiment)
table.Rows.Add(image.NameWithoutExtension, newfocPos)
Zen.Application.Documents.Add(image)

Zen.Application.Documents.Add(table)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The code worked when line 13 was changed to -
table.Rows.Add(image, newfocPos)

In this case, all steps work except the name of image files is incorrectly shown in the table as 'Zeiss.Micro.Scripting.ZenImage'. The Z positions are correct in the table. Can you please help with saving the image files names in the table.
Thanks,
Anand
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Analyze Z stack images and choose Z plane to image

Post by CarlZeissMicroscopy3 »

Hello Anand Ranjan,

The version of Zen blue you use seems to be an older version.
The function image.NameWithoutExtension is available since 3.2, current version is 3.4.

To overcome your problem simply use:

Code: Select all

from System.IO import Path

focPos = Zen.Devices.Focus.ActualPosition

experiment = Zen.Acquisition.Experiments.GetByName("DIC.czexp")

table = ZenTable()
table.Columns.Add("name", str)
table.Columns.Add("focus", float)

for i in range(-5, 5):
    newfocPos = focPos - i
    Zen.Devices.Focus.MoveTo(newfocPos)
    image = Zen.Acquisition.AcquireImage(experiment)
    table.Rows.Add(Path.GetFileNameWithoutExtension(image.FileName), newfocPos)
    Zen.Application.Documents.Add(image)

Zen.Application.Documents.Add(table)
Good luck ; - )
Anand Ranjan
Posts: 14
Joined: Wed Dec 02, 2020 7:53 pm

Re: Analyze Z stack images and choose Z plane to image

Post by Anand Ranjan »

Thanks a lot for your help.

The revised code for Query #1 works great. Now a table is created with the correct image names and corresponding Z positions. The table is displayed as an untitled window.

The code for Query #2, related to getting the image name from the saved text file, seems to work fine as well.

However, when running the following code for query # 3, I get the error "NameError: name 'table' is not defined in line 3”
Query #3
--------------------------------------------------------
focus = None

for row in table.Rows:
if row[0] == fileContent:
focus = row[1]
-------------------------------------------------------------

I suspect the error is again due to the older version of Zen Blue. We are using Zen Blue 2012 and the module for macro environment was added two years back.

For quick reference here’s the revised code you sent for generating the table-
Query # 1
------------------------------------------------------------------------------------
from System.IO import Path

focPos = Zen.Devices.Focus.ActualPosition

experiment = Zen.Acquisition.Experiments.GetByName("DIC.czexp")

table = ZenTable()
table.Columns.Add("name", str)
table.Columns.Add("focus", float)

for i in range(-5, 5):
newfocPos = focPos - i
Zen.Devices.Focus.MoveTo(newfocPos)
image = Zen.Acquisition.AcquireImage(experiment)
table.Rows.Add(Path.GetFileNameWithoutExtension(image.FileName), newfocPos)
Zen.Application.Documents.Add(image)

Zen.Application.Documents.Add(table)

-------------------------------------------------------------------------------------------

The table was created and displayed, but I get an error- ’table’ is not defined. Can you please help resolve this?

I would like to request one more modification in the code for Query #1. Currently, the images are saved as per settings in 'Auto Save' window of Zen. Would it be possible to modify the code in order to save the Z stack images as DIC-1, DIC-2, DIC-3....DIC-10 in a specified folder on the computer. The exact name of the files is not important for this purpose. I'm doing something similar with 'Auto Save' settings, but that does not allow overwriting the files and over time too many phase-contrast images would be accumulated.

Thanks again for your patience and support.
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Analyze Z stack images and choose Z plane to image

Post by CarlZeissMicroscopy3 »

Hello Anand Ranjan,

However,... I get the error "NameError: name 'table' is not defined in line 3

Code: Select all

focus = None

for row in table.Rows:  <= THIS IS LINE 3!
	if row[0] == fileContent:
		focus = row[1]
What I mean is, when you get the error "table is not defined in line 3" then your macro just consists of these lines of code and table does simply not exist.

The code will work if it is executed 'after' the main macro e.g. after Zen.Application.Documents.Add(table) and thus has to be in the same macro.
It does not work as a separate or isolated macro because then table is unknown.
But this has nothing to do with your version of Zen blue.

To save an image with a new filename you can use

image.Save(r'C:\MyPath\MyName.czi')

You can use the code when the image has been acquired.

I hope this helps ...
Anand Ranjan
Posts: 14
Joined: Wed Dec 02, 2020 7:53 pm

Re: Analyze Z stack images and choose Z plane to image

Post by Anand Ranjan »

Hi,
thank you for your suggestions and help. The following code you sent works well but acquiring ten individual phase-contrast images of the Z plane takes time and slows things down.
-------------------------------------------------------------------------------
from System.IO import Path
focPos = Zen.Devices.Focus.ActualPosition

experiment = Zen.Acquisition.Experiments.GetByName("DIC.czexp")

table = ZenTable()
table.Columns.Add("name", str)
table.Columns.Add("focus", float)

for i in range(-5, 5):
newfocPos = focPos - i
Zen.Devices.Focus.MoveTo(newfocPos)
image = Zen.Acquisition.AcquireImage(experiment)
table.Rows.Add(Path.GetFileNameWithoutExtension(image.FileName), newfocPos)
Zen.Application.Documents.Add(image)

Zen.Application.Documents.Add(table)
----------------------------------------------------------------------------------
Is there a way to programmatically acquire a movie of Z-plane images? Ideally, it should contain eleven un-labeled frames with 1-micron increments, and the current Z-plane should form the middle stack of the movie.

Currently, we achieve something similar using the Z-Stack window/setting in the acquisition tab of Zen. The macro for running Z-stack is recorded as a .czxp file and then run from Zen OAD. However, this way the Center Z-plane remains fixed as defined in the .czxp file. From Zen OAD we would like to assign the current Z-plane as the Center of the Z-stack video.

All other steps of the acquisition pipeline are now working smoothly.
Thanks a lot for your support.
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Analyze Z stack images and choose Z plane to image

Post by CarlZeissMicroscopy3 »

Currently, we achieve something similar using the Z-Stack window/setting in the acquisition tab of Zen. The macro for running Z-stack is recorded as a .czexp file and then run from Zen OAD. However, this way the Center Z-plane remains fixed as defined in the .czexp file.
Hello Anand Ranjan,

yes, it is a good step to use the built in Z-Stack capabilities!

You can overcome the limitation mentioned in two ways.

First:
There is also a 'Focus Strategy' window which allows to change form 'Fixed Z' to 'Current Z'.
If you load an experiment with 'Focus Strategy' set to 'Current Z' you can move to the z position with Zen.Devices.Focus.MoveTo(MyPos) and then execute the experiment.

Second:
You can programmatically change the relevant value(s) within the .czexp file, load the experiment (again) and finally execute it.

I hope this helps.
Anand Ranjan
Posts: 14
Joined: Wed Dec 02, 2020 7:53 pm

Re: Analyze Z stack images and choose Z plane to image

Post by Anand Ranjan »

The Focus Strategy method worked very well. Thank you
Anand Ranjan
Posts: 14
Joined: Wed Dec 02, 2020 7:53 pm

Re: Analyze Z stack images and choose Z plane to image

Post by Anand Ranjan »

Sorry, this has become a long chain. I hope it's alright to ask one more question regarding this project.

Overall the ZEN OAD script is working great. We generate a Z-stack of phase-contrast images and from within ZEN OAD, we run Fiji macro to analyze plane with the correct focus. Fiji stores the focal plane number in a text file, with the same name as Z-stack image file and _minVar.txt added at end.

An unexpected problem occurred in the last sentence of ZEN OAD script pasted below. This part of the script checks if Fiji macro finished data analysis and it has generated text file with focal plane information. Upon running the script we get the Scripting Error (NameError: name'File' is not defined in line #corresponding to the last line in the script below), which is the line –
Fiji_Finished = File.Exists(filename + '_minVar.txt')

The script runs fine if we do not have lines for checking Fiji status. As I understand, File.Exists is a command in ZEN OAD. So it's not clear why this line is giving an error 'File' not defined.



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
# *************** Recorded Code Block ***************
# Create and save a Zstack of DIC images

experiment1 = Zen.Acquisition.Experiments.GetByName("DIC_Zstack.czexp")
outputexperiment1 = Zen.Acquisition.Execute(experiment1)
Zen.Application.Documents.Add(outputexperiment1)
Zen.Application.Pause('Zstack done')

# Run a Fiji Macro to analyze Z stack images and choose the frame with correct focus
import clr
from System.Diagnostics import Process

def StartApp(exeloc, option):
app = Process();
app.StartInfo.FileName = exeloc
app.StartInfo.Arguments = option
app.Start()

# define the status of Fiji
Fiji_Finished = False

exeloc = 'D:\Fiji.app\ImageJ-win64.exe'
filename = outputexperiment1.FileName
option = '-macro ' + 'Z:\Anand\Zen_Automation_AR\get_minimal_variance_Zstack_newFileEachTime_PC_filename_minvar.ijm' +' ' + filename
StartApp(exeloc, option)
Zen.Application.Pause('Wait for FIJI')


# check if Fiji already analyzed the data
while (Fiji_Finished == False):
Fiji_Finished = File.Exists(filename + '_minVar.txt')
---------------------------------------------------------------------------------------------------------------------------------------------------------------

Any help will be much appreciated.
Many thanks,
Anand
Post Reply