How to programatically add metadata to a multi scene czi

Discuss questions and projects related to processing of imaging data here
user-6244
Posts: 11
Joined: Thu Jan 01, 1970 1:00 am

How to programatically add metadata to a multi scene czi

Post by user-6244 »

I would like to add metadata to each scene in a multiscene czi file. The czi is for a 96-well plate and the new metadata should contain the treatment condition of each well, which I want to display as an annotation on the images (similar to how the carrier well name and the channel can be displayed currently). What is the recommended way of doing this via the Python scripting interface? I have been suing getMetaDataFromString to retrieve metadata attributes, but I have not seen a similar method for setting metadata instead of retrieving it.

Is this the recommended way of adding an annotation with the treatment condition or would it be simpler to iterate over the scenes and add a separate annotation for each scene rather than using the metadata in the same way as the current add carrier container name annotation?
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Setting any metadata is not possible at the moment.

If it makes sense in your case an alternative is to position graphics, e.g. TextBoxes.

Something like:

Code: Select all

image1 = Zen.Application.Documents.ActiveDocument

rectangle1 = image1.Graphics.Add(ZenGraphicCategory.TextBox)
rectangle1.SetBounds(1000, 1000, 181, 179)
rectangle1.Text = "MyText"
Where x and y coordinates have to be set according to the positions of the scenes.

Another alternative is to use 'Split Scenes' e.g. on the processing tab of Zen to get single images.
user-6244
Posts: 11
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6244 »

Thanks for the reply,
I tried your suggestion, but it only works for adding an annotation to a single scene in the active document, how do I add the annotation to all scenes while keeping them together in the same czi file? I have tried looping through the scenes with SplitScenesToArray, but no visible annotations are added to the images. This is the script I have tried:

Code: Select all

conditions = {
    'B2': 'treatment_a',
    'B3': 'treatment_b'}
active_doc = Zen.Application.Documents.ActiveDocument
imgs = Zen.Processing.Utilities.SplitScenesToArray(active_doc)
for img in imgs:
    well_name = img.Metadata.GetMetadataStringWithPath('Metadata/Information/Image/Dimensions/S/Scenes[current]/Name')
    condition = conditions[well_name]
    print(well_name, condition, img)
    rectangle1 = img.Graphics.Add(ZenGraphicCategory.TextBox)
    rectangle1.SetBounds(25, 25, 481, 179)
    rectangle1.Text = condition
    rectangle1.FontSize = 12
    rectangle1.TextColor = ZenColors.Red
The script prints the following as expected, so the loop is working, just not the annotations.

Code: Select all

('B2', 'treatment_a', <Zeiss.Micro.Scripting.ZenImage object at 0x000000000000003D [Zeiss.Micro.Scripting.ZenImage]>)
('B3', 'treatment_b', <Zeiss.Micro.Scripting.ZenImage object at 0x000000000000003E [Zeiss.Micro.Scripting.ZenImage]>)
Could you please advice on how I can add annotations to each scene in a multiscene czi file.
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello joelostblom,

Maybe there is some basic misunderstanding.

First of all, as most of the processing functions SplitScenesToArray returns an array of ‘new’ images.
So if you work with this array of images and add annotations etc. all this will accordingly not show up in the ‘original’ image.

To add some graphical elements to a scene it helps a lot if you get an impression what your image is ‘really’ like.

When a multi scene image is opened in Zen it is most probably presented it in the ‘2D’-View (the tabs on the left hand side of the image).
On the ‘Dimensions’ tab (at the bottom of the image in the 2D-View) deactivate ‘Scenes’ so that you see the ‘whole image’.
Stay in this view of the image and add graphical elements like Textboxes etc. to the image.
If you position the graphical elements in the areas of the scenes these elements should also show up when you (re)activate ‘Scenes’ on the ‘Dimensions’ tab and then move from one scene to the next one.

I hope this helps ...
user-6244
Posts: 11
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6244 »

Thanks for the clarifications!

I now understand how I can create an annotation in each scene (in my case a well in a 96-well plate) by calculating position of that well in the "whole image" (in my case, the enitre 96-well plate) from the pixelsize of each tile and the number of tiles in each image.

One major concern with this approach is that my main usage is looking at images in the "Split" view/tab rather than the "2D" view/tab. I have noticed that annotations that I add in the 2D tab disappear when I change back and fourth between scenes in the split view, e.g. if I add an annotation to scene 1 and then go to split view I can initially see the same annotation, but if I change to scene 2 and then back to scene 1, the annotation is gone. It will reappear if I click the 2D tab and then click back to the split view tab.

The only annotations that are persistent when changing scenes in split view are the ZenGraphicCategory.FrequentAnnotations, such as "Carrier Container Name" and "MultiChannel Name" that can be added from the Graphics section under Split view. These also have the advantage of automatically positioning themselves in a non overlapping manner in the top left corner.

For next steps, I have three questions:

1. You said that it is not possible to add new metadata, but is it possible to overwrite existing metadata? E.g. can I change the scene name from "B3" to "B3-TreatmentA"? Or is there a comments/notes section in the metadata that I could write to?

2. The MultiChannel Name annotation is not added via metadata, but rather via FrequentAnnotationDataSource.MultiChannelName. If the metadata cannot be modified, is it possible for me to add the dictionary mapping of well to treatment by creating a new FrequentAnnotationDataSource instead?

3. In your opinion, what is the best way to achieve my goal of having the treatment show up next to the well/scene name in the Split tab? Maybe I am going about this in the wrong fashion and there is an easier way of doing it? We explore many different treatments in our 96 well plates and it would be tremendously helpful to have the images annotated with these treatments instead of referring back to our lab books for each scene/well.
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello joelostblom,

to give a very short answer:

The only way to insert information is via graphical elements.

The missing capability of the Split-View is a bug and will be reported to the development team.
user-7130
Posts: 1
Joined: Thu Jan 01, 1970 1:00 am

Post by user-7130 »

Carl Zeiss Microscopy 3 wrote:Hello joelostblom,

Maybe there is some basic misunderstanding.

First of all, as most of the processing functions SplitScenesToArray returns an array of ‘new’ images.
So if you work with this array of images and add annotations etc. all this will accordingly not show up in the ‘original’ image.

To add some graphical elements to a scene it helps a lot if you get an impression what your image is ‘really’ like.

When a multi scene image is opened in Zen it is most probably presented it in the ‘2D’-View (the tabs on the left hand side of the image).
On the ‘Dimensions’ tab (at the bottom of the image in the 2D-View) deactivate ‘Scenes’ so that you see the ‘whole image’.
Stay in this view of the image and add graphical elements like Textboxes etc. to the image.
If you position the graphical elements in the areas of the scenes these elements should also show up when you (re)activate ‘Scenes’ on the ‘Dimensions’ tab and then move from one scene to the next one.

I hope this helps ...
Yeah it did help me. Thanks a ton
laura lorde
Posts: 4
Joined: Thu Jan 19, 2023 4:59 am

Re:

Post by laura lorde »

user-4 wrote: Wed Jul 17, 2019 11:33 am Setting any metadata is not possible at the moment.

If it makes sense in your case an alternative is to position graphics, e.g. TextBoxes.

Something like: shell shockers

Code: Select all

image1 = Zen.Application.Documents.ActiveDocument

rectangle1 = image1.Graphics.Add(ZenGraphicCategory.TextBox)
rectangle1.SetBounds(1000, 1000, 181, 179)
rectangle1.Text = "MyText"
Where x and y coordinates have to be set according to the positions of the scenes.

Another alternative is to use 'Split Scenes' e.g. on the processing tab of Zen to get single images.
Thank you! I will try.
Jim Hamilton
Posts: 1
Joined: Tue Jan 31, 2023 2:14 pm

Re:

Post by Jim Hamilton »

user-4 wrote: Wed Jul 17, 2019 11:33 am

Code: Select all

image1 = Zen.Application.Documents.ActiveDocument

rectangle1 = image1.Graphics.Add(ZenGraphicCategory.TextBox)
rectangle1.SetBounds(1000, 1000, 181, 179)
rectangle1.Text = "MyText"
Good code, it helped me to add an annotation to a single scene in the active document, I searched for several hours and finally found it. Thanks.
Saint Otis
Posts: 1
Joined: Mon Jun 12, 2023 9:11 am

Re: Re:

Post by Saint Otis »

user-4 wrote: Wed Jul 17, 2019 11:33 am

Code: Select all

image1 = Zen.Application.Documents.ActiveDocument

rectangle1 = image1.Graphics.Add(ZenGraphicCategory.TextBox)
rectangle1.SetBounds(1000, 1000, 181, 179)
rectangle1.Text = "MyText"
This is a very useful piece of code. thank you for sharing!
Minesweeper
Post Reply