Algorithms

Here you will find some Excel VBA codes and Fiji macros.

Folders tools:

List folders and subfolders in a folder (code)

Plot tools:

Label point in a scatter plot: here
Do a polar chart in Excel: here and download the excel file.

WEKA analysis on multiple large images

After having saved a WEKA classifier, this macro (beanshell language) will divide your images in a chosen number of tiles and run the analysis using your classifier. Found at : https://imagej.net/Scripting_the_Trainable_Weka_Segmentation
// @File(label=”Input directory”, description=”Select the directory with input images”, style=”directory”) inputDir
// @File(label=”Output directory”, description=”Select the output directory”, style=”directory”) outputDir
// @File(label=”Weka model”, description=”Select the Weka model to apply”) modelPath
// @String(label=”Result mode”,choices={“Labels”,”Probabilities”}) resultMode
// @Integer(label=”Number of tiles in X:”, description=”Number of image subdivisions in the X direction”, value=3) xTiles
// @Integer(label=”Number of tiles in Y:”, description=”Number of image subdivisions in the Y direction”, value=3) yTiles
// @Integer(label=”Number of tiles in Z (set to 0 for 2D processing):”, description=”Number of image subdivisions in the Z direction (ignored when using 2D images)”, value=3) zTiles

import trainableSegmentation.WekaSegmentation;
import trainableSegmentation.utils.Utils;
import ij.io.FileSaver;
import ij.IJ;
import ij.ImagePlus;

// starting time
startTime = System.currentTimeMillis();

// caculate probabilities?
getProbs = resultMode.equals( “Probabilities” );

// create segmentator
segmentator = new WekaSegmentation( zTiles > 0 );
// load classifier
segmentator.loadClassifier( modelPath.getCanonicalPath() );

// get list of input images
listOfFiles = inputDir.listFiles();
for ( i = 0; i < listOfFiles.length; i++ )
{
// process only files (do not go into sub-folders)
if( listOfFiles[ i ].isFile() )
{
// try to read file as image
image = IJ.openImage( listOfFiles[i].getCanonicalPath() );
if( image != null )
{
tilesPerDim = new int[ 2 ];
if( image.getNSlices() > 1 )
{
tilesPerDim = new int[ 3 ];
tilesPerDim[ 2 ] = zTiles;
}
tilesPerDim[ 0 ] = xTiles;
tilesPerDim[ 1 ] = yTiles;

// apply classifier and get results (0 indicates number of threads is auto-detected)
result = segmentator.applyClassifier( image, tilesPerDim, 0, getProbs );

if( !getProbs )
// assign same LUT as in GUI
result.setLut( Utils.getGoldenAngleLUT() );

// save result as TIFF in output folder
outputFileName = listOfFiles[ i ].getName().replaceFirst(“[.][^.]+$”, “”) + “.tif”;
new FileSaver( result ).saveAsTiff( outputDir.getPath() + File.separator + outputFileName );

// force garbage collection (important for large images)
result = null;
image = null;
System.gc();
}
}
}
// print elapsed time
estimatedTime = System.currentTimeMillis() – startTime;
IJ.log( “** Finished processing folder in ” + estimatedTime + ” ms **” );
System.gc();

Merge Excel files into one book:

Sub MergeExcelFiles()
Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook
fnameList = Application.GetOpenFilename(FileFilter:=”Microsoft Excel Workbooks (.xls;.xlsx;.xlsm;.csv),.xls;.xlsx;.xlsm;.csv”, Title:=”Choose Excel files to merge”, MultiSelect:=True)
If (vbBoolean <> VarType(fnameList)) Then
If (UBound(fnameList) > 0) Then
countFiles = 0
countSheets = 0
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wbkCurBook = ActiveWorkbook
For Each fnameCurFile In fnameList
countFiles = countFiles + 1
Set wbkSrcBook = Workbooks.Open(Filename:=fnameCurFile)
For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
wksCurSheet.Copy after:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)
Next
wbkSrcBook.Close SaveChanges:=False
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox “Processed ” & countFiles & ” files” & vbCrLf & “Merged ” & countSheets & ” worksheets”, Title:=”Merge Excel files”
End If
Else
MsgBox “No files selected”, Title:=”Merge Excel files”
End If
End Sub

%d bloggers like this: