Fiji/imageJ for loop for z-stack in subfolders

Hello!

I am currently trying to make a script to recursively create Z-stack on images in subfolders. I have my main folder that contains subfolders (different animals) each of this subfolders contain 3 subfolders of 3 differen channels. I need to Z-stack by channel so I need 2 loops: 1)for each folder in main folder 2)for each channel subfolder.

I need that because the stack need to be done by animal and by channel of the animal.

Summary of the needed loop

1)Load image series, divided by subfolder of each animal’s folder

  1. For all the image in the animal subfolder (for ex. channel one) run(“Z Project…”, “projection=[Max Intensity]”);

  2. save the output in the same folder with the name +“processed” (TIF format)

  3. close all

I am trying since forever but I can’t program in Java and with Python I can’t make work the extension import so that’s not an option. My main issue is: I define “path” the path of my main folder, inside path I have other folders and inside that folders I have 3 subfolders (one for each channel) and THEN I have the files… so it’s really complicated for me to think about the loop like this.

I don’t know if it helps but in python I did it like this but no idea about how to translate it in Java!

root = “MAIN DIRECTORY PATH”

path = os.path.join(root, “.”)

for path, subdirs, files in os.walk(root):

for sub_files in files:

Hi,

seeing that you’re comfortable with python, you could try some Jython scripting. To do this

  1. File > New > Script....
  2. Select Language > Python.
  3. Paste the attached code into text window
  4. Press the Run button in the script editor

The first line is a bit of imglib magic to prompt for a directory. The code is assumes in the directory there is a collection of files named like *_z.tif where z is the slice number. Change for your files.

Cheers,

Chris

#@ File (label="Select a directory", style="directory") myDir
from __future__ import print_function
from ij import IJ

def main():
	root = myDir.getPath() # get the root out the java file object
	
	import os, glob

	# set up bioformats
	from loci.plugins import BF
	from loci.plugins.in import ImporterOptions
	options = ImporterOptions()
	options.setColorMode(ImporterOptions.COLOR_MODE_COMPOSITE)
	options.setGroupFiles(True)  # if the files are named logically if will group them into a stack
  	
	# this will do the maximum intensity projection
	from ij.plugin import ZProjector
	Zproj = ZProjector()

	for path, subdirs, files in os.walk(root):
		# just get the one of the files that matches your image pattern
		flist = glob.glob(os.path.join(path,"*_?.tif"))
		if( flist ):
			file = flist[0]
			print("Processing {}".format(file))
			options.setId(file)
			imp = BF.openImagePlus(options)[0]

			# show the image if you want to see it
			imp.show()

			imp_max = Zproj.run(imp,'max')
			imp_max.show()

			# save the Z projection
			IJ.save(imp_max,file.rsplit('_',1)[0]+'_processed.tif')

			# closes the windows if they are open
			imp.close()
			imp_max.close()
if( __name__ == '__builtin__'):
	main()

Dear Chris,
Thanks a lot for the help! :slightly_smiling_face:
Unfortunately it looks like for some reason this script is not actually working on my data (even after I adjusted the code to fix some errors like “UnboundLocalError: local variable ‘imp’ referenced before assignment”) in a way that I don’t actually have any output even if the script runs correctly.
Another issue I would like to discuss with you is that this code assumes that I have the image in a folder that I select. I would like to recursively do the z-stack starting from a main folder and apply it by subfolders but still maintaining them separate (as I did in python) in a way that the z-stack is of only the images in the same subfolders and not of all of them.

I am trying to understand why the function per-sé is not working (is not making Z-stack and saving the output) but I guess I need to study more as well this part “# set up bioformats”.

Did the script worked in your hands?

Thanks a lot!

Flavia

Hi Flavia

It looks like Bio-Formats is unable to open your files for some reason. The list of files that the script is expecting is empty which is what causes the error you see.

Can you open the image sequence as a stack though the Bio-Formats importer? There is an option to group files on the first screen:


That brings up the series opener dialog:

If that works sometimes it’s easier talk to Bioformats through strings rather than using the API. To get the an example string record opening the series with the macro recorder Plugins > Macro > Record... and copy commands. It will look something like:

run("Bio-Formats Importer", "open=/Users/evenhuis/Downloads/test_os_walk/frog/CF1/exp_1.tif 
autoscale color_mode=Default group_files rois_import=[ROI manager] 
view=Hyperstack stack_order=XYCZT dimensions 
axis_1_number_of_images=5 
axis_1_axis_first_image=1 axis_1_axis_increment=1 
contains=[] name=/Users/evenhuis/Downloads/test_os_walk/frog/CF1/exp_<1-5>.tif");

This needs to converted for the script to some more manageable

BF_string="open=[{}]  autoscale color_mode=Default group_files axis_1_number_of_images={}".format(file,len(flist)) 
IJ.run("Bio-Formats Importer", BF_string)
imp = IJ.getImage()

Here’s a small test set that runs for me:
test_os_walk.zip (36.5 KB)

Cheers,

Chris

1 Like

Dear Chris,

You ARE a life saver, it is working beautifully and you have been SO helpful!

I really wish you a nice day and I will start to learn better how to make this kind of scripts for ImageJ, but for sure I will use a lot the scaffold you kindly made for me!

Flavia

Hi Chris,

Sorry for bother you again but now I am having technical issues with the Java virtual machine.
I run the script on my files and it worked good except for the last 10 folders. I tried to run it again on that folders and now I am having problem with the virtual machine that says “console: Failed to install ‘’: java.nio.charset.UnsupportedCharsetException: cp0.” so it’s not running the script at all. I tried to check online and I read they recommend to insert “-Dpython.console.encoding=UTF-8” in the script but it’s not very clear to me where… Script editor error when working with python scripts

Sorry for the silly questions but I am gonna need more time to get used to this new environment!

Thanks

Flavia

Hi Flavia,

Check if the directory or file names have any exotic characters in them. I have a vague memory of python 2.7 having problems with unicode characters, maybe this page might help.

I think we dealt with by telling users to use only UTF8 characters and no spaces if they want scripts to work.

Hope it helps,

Chris

Hi,

I checked and all the folders and files have the same type of characters and before it crushed the script analyzed most of them, the one that are left out don’t have any specific different character so I believe this may be only a technical general issue with the JMV in this moment. I tried to run it in debug mode but still nothing. The previous folders had only one space and still they were processed, I tried to remove the space but still gives me the same “console: Failed to install ‘’: java.nio.charset.UnsupportedCharsetException: cp0.” even with the old folder that have been correctly analyzed…

Flavia