Hello everyone,
I have been struggling with a Results table overwrite issue that I have been unable to debug. The attached piece of code came from a much larger macro that worked fine until a month ago. The code in Test.ijm consists of a for loop iterating 3 times. During each iteration, it opens one z-stack, puts it through a few plugins (including 3D Spot Segmentation from Thomas Boudier, and Find Connected Regions from Mark Longair) and prints spot counts to the Results table. In the end, there should be 3 z-stacks processed by the macro, with their spot counts displayed in individual rows in the Results table. However, every time the loop carries out an iteration, all results for that z-stack are printed to the first line (i.e. Row 0) of the Results table. This means that the first z-stack has its results displayed on Row 0, then the second z-stack overwrites its results to the same row, and this gets overwritten by the third. According to the workflow in the code, the first piece of data sent to the Results table comes from the command *run("Find Maxima...", "noise="+noiseTol+" output=Count");* where noiseTol is the variable for the intensity threshold above which to count maxima/spots. Once the Count output is sent to the Results table, a few more pieces of data (e.g. spot count from 3D Spot Segmentation, Image name and number labels, etc) get sent to new columns in that same row, with the help of setResult() functions. To my knowledge, the *run("Find Maxima...", "noise="+noiseTol+" output=Count");* function should create a new row in the Results table each time, but within this loop structure, it only seems to write to row 0. This may be due to bad code on my part that I have been unable to find and correct, but I wonder if it has something to do with some change in Find Maxima's behavior? If anyone can read through/run the code with the attached Example.tiff z-stack, and help me debug this Results line overwrite issue, I would be most grateful. Currently running v1.50i of ImageJ and have the latest versions/releases of Bioformats, 3D Image Suite, etc plugins. Note: I have modified the code so that the one example z-stack has its intensity increased in the second and third iterations of the loop, so that the macro will be able to generate new higher spot counts from the second and third iterations. Thanks for the help! -- Feriel Presswalla Kellogg Eye Center University of Michigan ------------------------------------------------------------------------------------------------------------------------------ Example.tiff z-stack: https://www.dropbox.com/s/v5gtx4z5hemp4my/Example.tif?dl=0 Code for Test.ijm: https://www.dropbox.com/s/s4y6k39g6cfya4q/Test.ijm?dl=0 // example1=1; example2=2; example3=3; filterSize = 4; threshold = 5000; noiseTol = 5000; gaussianRadius = 5; titles = "Title"; //setBatchMode(true); for(i=1; i<=3; i++) { if (i==1) { //Dialog box allows you to open the Example file run("Bio-Formats Macro Extensions"); file = File.openDialog("Choose a .tif file"); Ext.setId(file); print(file); run("Bio-Formats Importer", "open=[" +file+"] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT"); example=example1; } if (i==2) { //Dialog box to open the same Example file run("Bio-Formats Macro Extensions"); file = File.openDialog("Choose a .tif file"); Ext.setId(file); print(file); run("Bio-Formats Importer", "open=[" +file+"] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT"); //Increases the intensity of the Example file so that the macro will treat it as a different image //and generate a higher ROI count above the same fixed threshold run("Add...", "value=1500 stack"); example=example2; } if (i==3) { //Dialog box to open the same Example file run("Bio-Formats Macro Extensions"); file = File.openDialog("Choose a .tif file"); Ext.setId(file); print(file); run("Bio-Formats Importer", "open=[" +file+"] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT"); //Increases the intensity of the Example file so that the macro will treat it as a different image //and generate a higher ROI count above the same fixed threshold run("Add...", "value=2500 stack"); example=example3; } rename("rawBackSubtracted"+example); run("Z Project...", "projection=[Max Intensity]"); //Collects greatest number of local maxima without collecting noise. Can be increased to 8^3 if stack contains bright artifacts even after background subtraction. selectWindow("rawBackSubtracted"+example); run("3D Fast Filters","filter=MaximumLocal radius_x_pix="+filterSize+" radius_y_pix="+filterSize+" radius_z_pix="+filterSize+" Nb_cpus=4"); print("Segmenting loop in Complete; filter size ="+ filterSize); selectWindow("3D_MaximumLocal"); rename("3D_MaximumLocal"+example); run("Z Project...", "projection=[Max Intensity]"); rename("MAX_seeds"+example); //Error messages displayed in the Console Window come after Watershedding and before 3D segmentation begins run("3D Spot Segmentation", "seeds_threshold="+threshold+" local_background=0 radius_0=2 radius_1=4 radius_2=6 weigth=0.50 radius_max="+gaussianRadius+" sd_value=1.17 local_threshold=[Gaussian fit] seg_spot=Maximum watershed volume_min=1 volume_max=1000000 seeds=3D_MaximumLocal"+example+" spots=rawBackSubtracted"+example+" radius_for_seeds=2 output=Both 32-bits"); print("Segmenting thresh ="+threshold+"; gaussian radius ="+gaussianRadius); selectWindow("seg"); //These window names are generated with each run, so they must be renamed to avoid using an old one in the next iteration. None of them will be closed, allowing user to look at them all, compare, and evaluate settings rename("seg"+example); //"seg"+example is currently 32 bit. To be binarized by dividing by itself it needs to be something other than 32bit, and we chose 8bit because the Find Connected Regions plugin needs 8bit run("8-bit"); //Divide the segmented stack by itself to create a binary stack imageCalculator("Divide create stack", "seg"+example,"seg"+example); selectWindow("Result of seg"+example); //Invert the intensities so that the ROIs are highest intensity and the rest is black and 0 run("Invert", "stack"); //Using the Find Connected regions plugin to eliminate stray 1-2 pix ROIs so that they do not unnecessarily inflate the ROI count run("Find Connected Regions", "allow_diagonal display_one_image regions_for_values_over=1 minimum_number_of_points=3 stop_after=-1"); objCount=0; for (s=1;s<=nSlices;s++) { setSlice(s); List.setMeasurements; max=List.getValue("Max"); if (max>objCount) { objCount=max; } if (max<objCount) { s=nSlices; } } //Generated All Connected Regions stack is always 16bit. Since it is a count mask we divide it by itself to make it binary imageCalculator("Divide create stack", "All connected regions","All connected regions"); selectWindow("Result of All connected regions"); rename("Result of All connected regions"+example); run("16-bit"); run("Invert", "stack"); run("Subtract...", "value=65533 stack"); close("All connected regions"); //Segmented stack needs to be same bit depth as raw stack so that the max projections can be merged //7000 is a good noise tolerance; a good modification would be to stop and let the user examine the point selection Complete to get a better sense of Find Maxima...countrun("Z Project...", "projection=[Max Intensity]"); selectWindow("MAX_rawBackSubtracted"+example); run("Select None"); run("Find Maxima...", "noise="+noiseTol+" output=Count"); print("NoiseTol after Count is done = "+noiseTol); //To beginn with 0th row of the Results table and end with (seriesCount-1) to get Find Maxima..., 3D ROI Count, and %Difference in Results window //Store Find Maxima... count from Results Window in variable and use to calculate error with objCount selectWindow("Result of All connected regions"+example); //run("3D Objects Counter", "threshold=1 slice=1 min.=1 max.=65024 summary"); updateResults(); j=nResults-1; setResult("Series #",j,example); //Title number here will show the row you intended it to be on. i-1 SHOULD be the row number, but it keeps printing on row 0 t=titles+(i-1); setResult("Series/Condition Name",j,t); setResult("3D ROI Count", j, objCount); updateResults(); //This works after the two setResult commands to refresh the Results window with new values objCount=0; maxCount=0; /*selectWindow("Connected seg"+example+"-3Dseg"); rename("Connected 3DROI"+example); imageCalculator("Divide create stack", "Connected 3DROI"+example,"Connected 3DROI"+example);*/ //Create a max projection of the binary Connected 3DROI stack. We do it this way because you cannot do a Max projection of the seg stack directly, since the ROIs have pxel values of the object labells, not their intensities selectWindow("Result of All connected regions"+example); run("Z Project...", "projection=[Max Intensity]"); rename("MAX_Connected 3DROI"+example); //Create a composite where the red channel is the max projection of seg, and the green channel is the max projection of raw. This allows user to compare colocalization, to see if the segmented ROIS match the raw spots, or if there are false positives, or missed true spots selectWindow("MAX_rawBackSubtracted"+example); run("Enhance Contrast", "saturated=0.35"); run("Merge Channels...", "c1=[MAX_Connected 3DROI"+example+"] c2=MAX_rawBackSubtracted"+example+" create keep ignore"); rename("Composite"+example); //close("seg"+example); close("Connected seg"+example); close("Connected 3DROI"+example); close("Result of seg"+example); close("Result of All connected regions"+example); close("3D_MaximumLocal"+example); close("MAX_raw"+example); close("rawBackSubtracted"+example); } -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |