Skip to content

Instantly share code, notes, and snippets.

@lacan
Created July 8, 2024 06:52
Show Gist options
  • Save lacan/eb2bbb3453b078e553ec3d7f81d1c39f to your computer and use it in GitHub Desktop.
Save lacan/eb2bbb3453b078e553ec3d7f81d1c39f to your computer and use it in GitHub Desktop.
[FRC Batch fix] Running FRC in batch with proper saving of plots and results #groovy #frc #fiji
#@ File image_dir1 ( label="First directory", style="directory" )
#@ File image_dir2 ( label="Second directory", style="directory" )
#@ Boolean save_plot ( label="Save plot?" )
#@ Boolean save_data ( label="Save Data points?" )
#@ String threshold_method (label="Threshold method", choices={"Fixed 1/7", "Half-bit", "Three sigma"})
import ch.epfl.biop.frc.FRC
import ch.epfl.biop.frc.FRC.ThresholdMethod
import ij.IJ
import ij.ImagePlus
import java.io.FilenameFilter
import ij.gui.Plot
import ij.measure.ResultsTable
def rt = new ResultsTable()
def tm = FRC.ThresholdMethod.values().find{ it.toString() == threshold_method}
batchFolders( image_dir1, image_dir2, tm, rt, save_plot, save_data )
def batchFolders(File directory1, File directory2, ThresholdMethod method, ResultsTable rt, boolean is_save_plot, boolean is_save_data) {
def frc = new FRC()
// Navigate folder for tiffs
String[] the_files = directory1.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// check extensions
return true;
}
});
// Prepare saving if necessary
File save_dir = new File(directory1.getParentFile(), "Graphs");
save_dir.mkdir();
// For each file, open one in each directory
for(String the_file : the_files) {
File f1 = new File(directory1.getAbsolutePath()+File.separator+the_file);
File f2 = new File(directory2.getAbsolutePath()+File.separator+the_file);
if(f2.exists()) {
ImagePlus i1 = IJ.openImage(f1.getAbsolutePath());
ImagePlus i2 = IJ.openImage(f2.getAbsolutePath());
// Finally calculate FRC
double[][] frc_curve = frc.calculateFrcCurve(i1.getProcessor(), i2.getProcessor());
double[][] smooth_frc = frc.getSmoothedCurve(frc_curve);
// Fourier Image REsolution number ("FIRE")
double fire = frc.calculateFireNumber(smooth_frc, method);
rt.incrementCounter();
rt.addLabel(i1.getTitle());
rt.addValue("FRC ["+method+"]", fire);
rt.addValue("FRC ["+method+"] Calibrated ["+i1.getCalibration().getUnit()+"]", fire*i1.getCalibration().pixelHeight);
rt.show("FRC Results");
if(is_save_plot) {
Plot p = frc.doPlot(frc_curve, smooth_frc, method, fire, i1.getTitle());
plotWindow = p.show()
ImagePlus plot_image = p.makeHighResolution("FRC", 3, true, false);
String plot_name = save_dir.getAbsolutePath()+File.separator+f1.getName().substring(0,f1.getName().lastIndexOf("."));
plot_name += "_"+method.toString().replaceAll("/", " over ")+".tif";
IJ.save(plot_image, plot_name);
if( is_save_data ) {
def rt2 = p.getResultsTableWithLabels()
String res_name = save_dir.getAbsolutePath()+File.separator+f1.getName().substring(0,f1.getName().lastIndexOf("."));
res_name += "_"+method.toString().replaceAll("/", " over ")+".csv";
rt2.saveAs( res_name )
}
plotWindow.close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment