Bio-formats importer in Jython for czi files

Hello everybody,

I have a question regarding the import in BioFormats of huge czi files. The files are composed as follows:
Channels: 2
Stack: 40 slices
Time: 100 frames

I want to write a Jython script to iterate through the files in a directory and open them as virtual stack to avoid to get an OutOfMemory error. It would then iterate through timepoints, one by one, to open a stack as an ImagePlus and perform some processing on the stack. Finally, the script should save the new processed file as a tiff (possibly without overwriting the original czi file).

That is what I was trying to do so so far:

import os
from os import path
from ij import IJ, ImagePlus, ImageStack, VirtualStack, WindowManager
from ij.io import DirectoryChooser, FileSaver
from ij.gui import GenericDialog
from ij.process import ImageProcessor
from loci.plugins import BF
from loci.plugins.in import ImporterOptions
from ij.plugin import Duplicator, ChannelSplitter, ImageCalculator, ZProjector

# Functions

def getMIP(astack, start, end):
	mip = ZProjector(); 
	mip.setMethod(ZProjector.MAX_METHOD);
	mip.setImage(astack);
	mip.setStartSlice(start); mip.setStopSlice(end);
	mip.doHyperStackProjection(True);
	return mip.getProjection()
	
# Process -------------------------------------------------------------------------------------

def run():
	srcDir = DirectoryChooser("Input directory").getDirectory()
	if not srcDir:
		raise Exception("You need to provide a source directory")

	for root, directories, filenames in os.walk(srcDir):
		for filename in filenames:
			if not filename.startswith("MAX") and filename.endswith(".czi"):
				print "Processing ", filename
				reader = ImageReader()
				reader.setId(os.path.join(srcDir, filename))
				n = reader.getSeriesCount()
				print("Series: ", n)
				for i in range(n):
					options = ImporterOptions()
					options.setColorMode(ImporterOptions.COLOR_MODE_GRAYSCALE)
					options.setId(os.path.join(srcDir, filename))
					options.isVirtual() #set True?
					options.setTStep(1, i+1) #what is s?
					imps = BF.openImagePlus(options)
                                        slides = imps[0].getNSlices()
					L = [(j, j+20) for j in range(0, slides-19, 10)]
				
					for el in L:
						mip = getMIP(imps[0], el[0], el[1])
						prefix = "MAX_{}-{}_".format(str(el[0]),str(el[1]))
						mip.setTitle(prefix)
						fs = FileSaver(mip)
						fs.saveAsTiff(srcDir+"/"+prefix+filename)


run()

I still get an error “java.lang.OutOfMemoryError: Java heap space” at BF.openImagePlus(options). What am I missing?

Lucrezia

How much memory is assigned to Fiji?

Hi @LucreziaF
Does it crash immediately or only after processing a couple of images? There is a known issue with BioFormats potentially not releasing the memory. Does this thread help you?

@sebi06
48Gb, then increased to 60Gb.
The movie is 368Gb in total.

I have changed line 12 cause I realised I was counting the wrong value for timepoints:

n = reader.getSizeT()

@Christian_Tischer
It starts processing and it crashes only after several minutes. I am gonna check out the thread, thanks!

1 Like

Then it makes sense to check out the thread. I hope you find an answer there.