Open Each Lif (Files in Folder) and Convert Each Series to Image Sequence

Hi,

I’m relatively new so feel free to redirect me to a more applicable post.
I’ve repurposed a common macro that opens lif files by series and saves each series to a variable and pre-created folder for each series. My code works until the last series iteration then an error is caused at the Ext.setSeries step.
Error:
“Caused by: java.lang.IllegalArgumentException: Invalid series: 5”

  • Full Error Code Included with post

Attempted corrections:
-Iterating the “for” loop from zero & adjusting series variable accordingly. - Failed
-Iterating the “for” loop from 1 & adjusting series variable accordingly. - Failed
-Iterating the “for” loop from zero but excluding final series. - Successful file iteration but without final series
LIF2TIF_Prompt5.ijm (1.7 KB) LIFtoTIF Error Code.txt (2.0 KB)

Just throwing some things out there…

Have you verified that the first four series that are export are working correctly (not just completing, the output is what is expected)?

Is there any chance the 5th is corrupted? Have you tried starting from 5 and counting down, for instance?

And are you sure the series does not start from 0? In that case it would not matter what you did with your for loop since you add 1 inside the for loop.

Hi! Thanks for your response and suggestions. These are great questions.

-Yes, the first 4 series and are working correctly as well as other series(n-1) lif files that I’ve tested.

-I don’t believe the 5th is corrupted because I can preform the task manually with bioformats without any problem. I will try counting down though.

-I doesn’t start from zero from what I can tell. I’ve attached a photo of how the series are numbered in bio-formats.

Thanks again!

Lawrence

I guess it would be easy enough to verify which of these two
image
show up as the series1 that you saved.

Yes, that’s the only way I knew it was partially working.

1 Like

Hello, I just tried the code iterating in reverse order and it still does not recognize the final series.

1 Like

So the error is Invalid series: 1 now?

Anyway, if you can strip the script down to ONLY opening each series with the bioformats (3-4 line script) and there is still an error, then there might be a bioformats problem.

But try that, try printing what “l+1” is at the beginning of the loop.

When using the Bio-Formats API via a macro the indexes will always be zero based. When using the user interface the series names are listed as starting from 1 to make it more user friendly. So changing Ext.setSeries(l+1); to Ext.setSeries(l); should be enough to correct the problem.

2 Likes

Hi David, Thanks for your message. I’ve tried that. I’ve attached forward and reverse iteration code that indexes from zero to n-1. The code adjustment’s output iterates the first series as Series 000 and duplicates the same series as Series 001. I’ve attached the adjusted code.

Coming back to the API, could the problem be my line:
run(“Bio-Formats Importer”, “open=[”+dirPath+fileName+"] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_"+(l));

Does this line toggle from typical indexing (0 to n-1) to indexing from (1 to n)?

Many thanks,
LawrenceLIF2TIF_Reverse Iteration.ijm (1.7 KB) LIF2TIF_Forward Iteration.ijm (1.7 KB)

Ok, so the confusion seems to be because the macro has a mix of the macro extensions (using the API indexing to get the series count for example) and then using run(“Bio-Formats Importer”, which uses the values from the user interface. It is still valid to do this and your updated macro would then be something like below. If its helpful there are also some examples of using either method to convert to tif at https://github.com/ome/bioformats/tree/develop/components/bio-formats-plugins/utils/macros:

setBatchMode(true);
fs=File.separator;


pathFile=File.openDialog("Select LIF File to Process");
dirFiles=File.getDirectory(pathFile);
allFiles=getFileList(dirFiles);
print(allFiles.length);
for(f=0; f<allFiles.length; f++) {
if (endsWith(allFiles[f], ".lif"))
   fileName=allFiles[f];
   folderName=substring(fileName,0,lastIndexOf(fileName, "."));
   condName=folderName;
   dirPath= File.getParent(pathFile)+fs;
   savePath=dirPath + folderName + fs;
   run("Bio-Formats Macro Extensions");
   Ext.setId(dirFiles+fileName);
   Ext.getSeriesCount(seriesCount);
   sCount=seriesCount;

for(l=1;l<=sCount;l++){
   run("Bio-Formats Importer", "open=["+dirPath+fileName+"] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_"+(l));
   nameStore=getTitle();
   getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
   print(hour+":"+minute+":"+second+" - Processing Series "+(l)+" of "+seriesCount);
   //imageName=getInfo("image.filename");
   currentCount=l+1;
   currentCount=d2s(currentCount,0);
   currentLength=lengthOf(currentCount);
   if(currentLength==1){
      folderPath= condName+"-00"+currentCount+fs;
   }
   if(currentLength==2){
      folderPath= condName+"-0"+currentCount+fs;
   }
   if(currentLength>=3){
      folderPath= condName+currentCount+fs;
   }
fullsave=savePath+folderPath;
   run("8-bit");
print(folderPath);
print(fullsave);
//Ext.setId(fullsave);
run("Image Sequence... ","format=TIFF save=["+fullsave+"] name=["+condName+"]-");
run("Close All");

}
}
print("DONE!");

//dir="+savePath+folderPath+"
//dir=["+fullsave+"] 
//] name=["+ condName + "-]
1 Like

Hi David, apologies for my delay. Thank you!!

Your answer is the solution to opening each series including the last series. There was a minor issue where the final file is called repeatedly i.e. for 10 Lif files in 22 files, on the 10th/last Lif file, the loop reiterates and repeats until the count reaches 22 (13x). I would suggest an initial “for loop” to count lif files then iterate through the folder as “f” less than count number.

Yeah, it looks as though I missed a set of brackets on the if statement as the enclosed for loop should also have been included as part it and not executed every time. An initial loop to count the number of LIF files present would also work just fine.

When using the user interface the series name are listed as starting from 1 to make it more user friendly.

Hi Alex, many thanks. David said the same thing (Jan12). The solution provided allows UI indexing and native indexing to transition smoothly - David improved my code instead of rewriting to one indexing so I’m grateful.