Error running BaSiC plugin from ICB in Python script in FiJi

Hi everyone,

I am trying to run the background correction plugin BaSiC from ICB on a stack of fluorescence images in a Python script in FiJi, but I’m facing issues, being new to scripting in FiJi.

The original data file is of format nd2 (Nikon Elements proprietary format), and contains 256 positions in space x 15 times points for each positions x 3 fluorescence channels per positions (CFP, YFP and DsRed).

In the script below, I first select the first series “imp” corresponding to the 1st position (45 frames), then decompose it in three new stacks corresponding to each channel.

Then, I would like to apply the BaSiC plugin to each of these channels independently. I first try running the plugin on the first channel corresponding to CFP (15 frames), with the command
IJ.run("BaSiC ", "processing_stack=imp2 flat-field=None dark-field=None shading_estimation=[Estimate shading profiles] shading_model=[Estimate both flat-field and dark-field] setting_regularisationparametes=Automatic temporal_drift=Ignore correction_options=[Compute shading and correct images] lambda_flat=0.50 lambda_dark=0.50")
obtained from the Macro recorder.

When I run this script, I get a pop-up window from BaSiC warning me twice that

“imp2” is not a valid choice for “processing_stack”

After clicking “OK”, the plugin runs nevertheless, but I noticed that it will run on whichever stack was open first, and not necessarily “imp2”.

How can I provide a suitable input to the BaSiC plugin so that it runs smoothly on the stack I want? Any help would be welcome!

==============

The full example script is below, the data file “test_1.nd2” can be found here.

from loci.plugins import BF
from loci.plugins.in import ImporterOptions
from loci.formats import MetadataTools
from loci.formats import ImageReader
from ij import ImagePlus, ImageStack, IJ

file = "Z:/case_imagesc/test_1.nd2"

# load position by position
options = ImporterOptions()
options.setId(file)

# Choose first position for now
s =0

options.setSeriesOn(s, True)
imp = BF.openImagePlus(options)[0]
stack = imp.getImageStack()
print 'Number of frames in stack  = ', stack.size()

# three new stacks for each fluorescence channel
CFP_stack = ImageStack(stack.width, stack.height)  
YFP_stack = ImageStack(stack.width, stack.height)
DsRed_stack = ImageStack(stack.width, stack.height)
    
# Iterate each frame in the stack and sort + convert to float
for i in range(stack.size()):  
     if i%3==0:
         cp = stack.getProcessor(i+1)
         fp = cp.convertToFloat() 
         CFP_stack.addSlice(None,fp)

     if i%3==1:
         cp = stack.getProcessor(i+1)
         fp = cp.convertToFloat() 
         YFP_stack.addSlice(None,fp)

     if i%3==2:
         cp = stack.getProcessor(i+1)
         fp = cp.convertToFloat() 
         DsRed_stack.addSlice(None,fp)

options.setSeriesOn(s, False)

# first channel CFP is the one on which I want to run BaSiC
imp2 = ImagePlus("CFP stack", CFP_stack)
imp2.show()

# run BaSiC plugin
IJ.run("BaSiC ", 
"processing_stack=imp2 flat-field=None dark-field=None shading_estimation=[Estimate shading profiles] shading_model=[Estimate both flat-field and dark-field] setting_regularisationparametes=Automatic temporal_drift=Ignore correction_options=[Compute shading and correct images] lambda_flat=0.50 lambda_dark=0.50")

If you run the macro recorder while starting BaSiC interactively, you’ll notice that it records the title of the image in the processing_stack parameter of the option string.

I’d assume that if you change your code to something like:

# first channel CFP is the one on which I want to run BaSiC
imp2 = ImagePlus("CFP stack", CFP_stack)
imp2.show()

# run BaSiC plugin
IJ.run("BaSiC ", "processing_stack=[CFP stack] flat-field=None dark-field=None shading_estimation=[Estimate shading profiles] shading_model=[Estimate both flat-field and dark-field] setting_regularisationparametes=Automatic temporal_drift=Ignore correction_options=[Compute shading and correct images] lambda_flat=0.50 lambda_dark=0.50")
                                   ^^^^^^^^^^^

… it should work as expected.

It does work, thanks!