Writing clean code with functions#
In our macro, you can see that we have repeated lines of code. A fundamental paradigm of programming is DRY: Don't repeat yourself.
Code that does not contain a lot of repeated lines is called "dry". Code that repeats a lot of lines is called "wet", and right now, our code is looking pretty wet.
But we can fix this by writing functions to do some of this processing that we know is going to happen on every image.
For example, the code to select arrays from the ROI manager, make the intersection, and perform the measurement is repeat twice, with the only difference being which indices we select.
Write a function that performs this job, and replace your code with calls to this function
function measureIntersect(roi_1, roi_2) {
// Create an array with the two rois
rois = newArray(RoiManager.getIndex(roi_1), RoiManager.getIndex(roi_2));
roiManager("Select", rois);
roiManager("AND");
run("Set Measurements...", "area mean standard integrated redirect=None decimal=3");
run("Measure");
}
open("path/to/file/bax_DAPI_overlay.tif");
run("Split Channels");
selectWindow("C1-bax_DAPI_overlay.tif");
rename("nuclei");
selectWindow("C2-bax_DAPI_overlay.tif");
rename("bax");
selectWindow("nuclei");
run("Duplicate...", "title=nuclei_thresholded");
run("Auto Threshold", "method=Default white");
selectWindow("bax");
run("Duplicate...", "title=bax_thresholded");
run("Auto Threshold", "method=Default white");
selectWindow("nuclei_thresholded");
run("Create Selection");
roiManager("Add");
run("Make Inverse");
roiManager("Add");
selectWindow("bax_thresholded");
run("Create Selection");
roiManager("Add");
name_array = newArray("nuclei", "nuclei_inverted", "bax")
for (i=0; i<roiManager("count"); i++) {
roiManager("Select", i);
roiManager("rename", name_array[i]);
}
selectWindow("bax");
measureIntersect("nuclei", "bax");
measureIntersect("nuclei_inverted", "bax");
Now, the last three lines of our script look much easier to understand!
Question
Can you see anywhere else where we repeat code that could otherside be a function?