From aa3ad49ec0dad9fb88bfab9b198c12f1ed952d33 Mon Sep 17 00:00:00 2001 From: AnniekStok Date: Mon, 7 Aug 2023 09:09:07 +0200 Subject: [PATCH] updated output_saving scripting module to fit with multiactivity layout --- .../exercises/output_saving_imagej-jython.md | 50 ------------------- .../exercises/output_saving_imagej-macro.md | 36 ------------- ...output_saving.md => output_saving_act1.md} | 8 +-- ...md => output_saving_act1_imagej-jython.md} | 4 +- ...put_saving_act1_imagej-jython_template.py} | 0 ....md => output_saving_act1_imagej-macro.md} | 4 +- ...put_saving_act1_imagej-macro_template.ijm} | 0 _includes/output_saving/output_saving_act2.md | 5 ++ .../output_saving_act2_imagej-jython.py | 41 +++++++++++++++ .../output_saving_act2_imagej-macro.ijm | 27 ++++++++++ _modules/output_saving.md | 17 ++----- 11 files changed, 84 insertions(+), 108 deletions(-) delete mode 100644 _includes/output_saving/exercises/output_saving_imagej-jython.md delete mode 100644 _includes/output_saving/exercises/output_saving_imagej-macro.md rename _includes/output_saving/{activities/output_saving.md => output_saving_act1.md} (61%) rename _includes/output_saving/{activities/output_saving_imagej-jython.md => output_saving_act1_imagej-jython.md} (84%) rename _includes/output_saving/{activities/output_saving_imagej-jython_template.py.py => output_saving_act1_imagej-jython_template.py} (100%) rename _includes/output_saving/{activities/output_saving_imagej-macro.md => output_saving_act1_imagej-macro.md} (82%) rename _includes/output_saving/{activities/output_saving_imagej-macro_template.ijm.ijm => output_saving_act1_imagej-macro_template.ijm} (100%) create mode 100644 _includes/output_saving/output_saving_act2.md create mode 100644 _includes/output_saving/output_saving_act2_imagej-jython.py create mode 100644 _includes/output_saving/output_saving_act2_imagej-macro.ijm diff --git a/_includes/output_saving/exercises/output_saving_imagej-jython.md b/_includes/output_saving/exercises/output_saving_imagej-jython.md deleted file mode 100644 index b5845b4b..00000000 --- a/_includes/output_saving/exercises/output_saving_imagej-jython.md +++ /dev/null @@ -1,50 +0,0 @@ -Adapt the code from the activity such that you: -1. Specify an output directory in the beginning -2. Save the results table as comma-separated data table instead of text-delimited data. -3. Save the output label image in a different image format (e.g. PNG, JPEG). Is this a good format for label images? - -> ## Solution -> ```python -># import classes ->from ij import IJ, ImagePlus, WindowManager ->from ij.io import FileSaver ->from ij.plugin.filter import ParticleAnalyzer ->from ij.plugin.frame import RoiManager ->from ij.measure import ResultsTable, Measurements ->from ij.process import ImageProcessor ->import os -> -># make sure the background is set to black in Process>Binary>Options ->IJ.run("Options...", "iterations=1 count=1 black") -> -># Specify an output directory ->outputDir = FIXME # (e.g. r'C:\Users\username\Desktop', 'C:\\Users\\username\\Desktop' or 'C:/Users/username/Desktop' on Windows or '/Users/username/Desktop/' on MacOS) -> -># Specify size parameters for object selection ->min_size = 0 ->max_size = 1000 -> -># Initialize Roi Manager and empty results table, close other open images ->rm = RoiManager().getInstance() ->rm.reset() ->IJ.run("Close All") -> -># Open binary shapes image ->shapes = IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif") -> -># Configure and run particle analyzer ->results = ResultsTable() # construct empty resultstable ->pa = ParticleAnalyzer((ParticleAnalyzer.ADD_TO_MANAGER + ParticleAnalyzer.SHOW_ROI_MASKS),(Measurements.AREA + Measurements.CENTROID + Measurements.CENTER_OF_MASS + Measurements.PERIMETER + Measurements.RECT), results, min_size, max_size, 0, 1) ->pa.analyze(shapes) # run the particle analyzer on the image ->results.show("Results") -> -># Save results, label mask, and ROIs ->labelMask = WindowManager.getImage("Count Masks of xy_8bit_binary_randomshapes.tif") ->IJ.run(labelMask, "Glasbey", "") # set glasbey LUT ->FileSaver(labelMask).saveAsPng(os.path.join(outputDir, "shapes_labels_jython.png")) # save the label mask -> ->results.save(os.path.join(outputDir, "shapes_results_jython.csv")) # save results table -> ->rm.runCommand("Save", os.path.join(outputDir, "shapes_ROIset_jython.zip")) # save the ROIs -> ``` -{: .solution} diff --git a/_includes/output_saving/exercises/output_saving_imagej-macro.md b/_includes/output_saving/exercises/output_saving_imagej-macro.md deleted file mode 100644 index bf1c2b3d..00000000 --- a/_includes/output_saving/exercises/output_saving_imagej-macro.md +++ /dev/null @@ -1,36 +0,0 @@ -Adapt the code from the activity such that you: -1. Specify an output directory in the beginning -2. Save the results table as comma-separated data table instead of text-delimited data. -3. Save the output label image in a different image format (e.g. PNG, JPEG). Is this a good format for label images? - -> ## Solution -> ```java ->// This macro uses the particle analyzer to measure features of shapes. ->// Different outputs are saved: ROIs, results table, and label mask. -> ->// make sure the background is set to black in Process>Binary>Options ->run("Options...", "iterations=1 count=1 black do=Nothing"); -> ->// specify an output directory ->outputDir = FIXME // (e.g. 'C:\\Users\\username\\Desktop' or 'C:/Users/username/Desktop' on Windows, or '/Users/username/Desktop/' on MacOS) -> ->// specify size parameters for object selection ->minSize = 0 ->maxSize = 1000 -> ->// close any pre-existing output you do not want in your saving results ->roiManager("reset"); // clear any pre-existing ROIs ->run("Clear Results"); // clear any pre-existing results ->run("Close All"); // close any open images -> ->// Set measurements and run particle analyzer on binary shapes image ->run("Set Measurements...", "area centroid center perimeter bounding redirect=None decimal=3") // set desired measurements ->open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif"); // open binary image with random shapes ->run("Analyze Particles...", "size=&minSize-&maxSize show=[Count Masks] display add") // run the particle analyzer>run("glasbey") -> ->// Save the results ->saveAs("Png", outputDir + File.separator + "/shapes_labels_macro.png"); // save label mask to output directory ->saveAs("Results", outputDir + File.separator + "/shapes_results_macro.csv"); // save results file to output directory ->roiManager("Save", outputDir + File.separator + "/shapes_ROIset_macro.zip"); // save ROIs to output directory -> ``` -{: .solution} diff --git a/_includes/output_saving/activities/output_saving.md b/_includes/output_saving/output_saving_act1.md similarity index 61% rename from _includes/output_saving/activities/output_saving.md rename to _includes/output_saving/output_saving_act1.md index 9342c866..a14a4572 100644 --- a/_includes/output_saving/activities/output_saving.md +++ b/_includes/output_saving/output_saving_act1.md @@ -1,6 +1,6 @@ ### Save file outputs - - Use a template script and run it. + - Open and run the template script available in the dropdown menu below. - Understand what the commands do. - - Record yourself saving the output. - - Edit the script using the recorded commands. - - Optional: define an output directory as a parameter and use string concatenation to specify where to save the file and with what name. An example output directory could be 'C:\\\Users\\\username\\\Desktop' on Windows (note double "\\" because a single "\\" is interpreted as an escape character) or '/Users/username/Desktop/' on MacOS. \ No newline at end of file + - Save the output manually and use the ImageJ recorder function to record the steps. + - Edit the template script using the recorded commands. + - Optional: define an output directory as a parameter and use string concatenation to specify where to save the file and with what name. An example output directory could be 'C:\\\Users\\\username\\\Desktop' on Windows (note double "\\" because a single "\\" is interpreted as an escape character) or '/Users/username/Desktop/' on MacOS. diff --git a/_includes/output_saving/activities/output_saving_imagej-jython.md b/_includes/output_saving/output_saving_act1_imagej-jython.md similarity index 84% rename from _includes/output_saving/activities/output_saving_imagej-jython.md rename to _includes/output_saving/output_saving_act1_imagej-jython.md index 3f229cbc..18749dbd 100644 --- a/_includes/output_saving/activities/output_saving_imagej-jython.md +++ b/_includes/output_saving/output_saving_act1_imagej-jython.md @@ -1,6 +1,6 @@ -1. Download the template script: [output_saving_imagej-jython_template.py](https://github.com/NEUBIAS/training-resources/tree/master/_includes/output_saving/activities/output_saving_imagej-jython_template.py). The aim of the script is to generate different kinds of output (labels, results, ROIs) from this image: [xy_8bit_binary_randomshapes.tif](https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif). +1. Download the template script: [output_saving_imagej-jython_template.py](https://github.com/NEUBIAS/training-resources/tree/master/_includes/output_saving/output_saving_act1_imagej-jython_template.py). The aim of the script is to generate different kinds of output (labels, results, ROIs) from this image: [xy_8bit_binary_randomshapes.tif](https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif). 2. Run and understand the script. -2. Add commands that save the different output: label image, results table, ROIs. +3. Add commands that save the different output: label image, results table, ROIs. > ## Solution > ```python diff --git a/_includes/output_saving/activities/output_saving_imagej-jython_template.py.py b/_includes/output_saving/output_saving_act1_imagej-jython_template.py similarity index 100% rename from _includes/output_saving/activities/output_saving_imagej-jython_template.py.py rename to _includes/output_saving/output_saving_act1_imagej-jython_template.py diff --git a/_includes/output_saving/activities/output_saving_imagej-macro.md b/_includes/output_saving/output_saving_act1_imagej-macro.md similarity index 82% rename from _includes/output_saving/activities/output_saving_imagej-macro.md rename to _includes/output_saving/output_saving_act1_imagej-macro.md index dcaad076..bac4ad8e 100644 --- a/_includes/output_saving/activities/output_saving_imagej-macro.md +++ b/_includes/output_saving/output_saving_act1_imagej-macro.md @@ -1,6 +1,6 @@ -1. Download the template script: [output_saving_imagej-macro_template.ijm](https://github.com/NEUBIAS/training-resources/tree/master/_includes/output_saving/activities/output_saving_imagej-macro_template.ijm). The aim of the script is to generate different kinds of output (labels, results, ROIs) from this image: [xy_8bit_binary_randomshapes.tif](https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif). +1. Download the template script: [output_saving_imagej-macro_template.ijm](https://github.com/NEUBIAS/training-resources/tree/master/_includes/output_saving//output_saving_act1_imagej-macro.ijm). The aim of the script is to generate different kinds of output (labels, results, ROIs) from this image: [xy_8bit_binary_randomshapes.tif](https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif). 2. Run and understand the script. -2. Add commands that save the different output: label image, results table, ROIs. +3. Add commands that save the different output: label image, results table, ROIs. > ## Solution > ```java diff --git a/_includes/output_saving/activities/output_saving_imagej-macro_template.ijm.ijm b/_includes/output_saving/output_saving_act1_imagej-macro_template.ijm similarity index 100% rename from _includes/output_saving/activities/output_saving_imagej-macro_template.ijm.ijm rename to _includes/output_saving/output_saving_act1_imagej-macro_template.ijm diff --git a/_includes/output_saving/output_saving_act2.md b/_includes/output_saving/output_saving_act2.md new file mode 100644 index 00000000..5a09ff93 --- /dev/null +++ b/_includes/output_saving/output_saving_act2.md @@ -0,0 +1,5 @@ +### Customize output saving +Adapt the code from activity 1 such that you: +1. Specify an output directory in the beginning +2. Save the results table as comma-separated data table instead of text-delimited data. +3. Save the output label image in a different image format (e.g. PNG, JPEG). Is this a good format for label images? diff --git a/_includes/output_saving/output_saving_act2_imagej-jython.py b/_includes/output_saving/output_saving_act2_imagej-jython.py new file mode 100644 index 00000000..d75ff3de --- /dev/null +++ b/_includes/output_saving/output_saving_act2_imagej-jython.py @@ -0,0 +1,41 @@ +# import classes +from ij import IJ, ImagePlus, WindowManager +from ij.io import FileSaver +from ij.plugin.filter import ParticleAnalyzer +from ij.plugin.frame import RoiManager +from ij.measure import ResultsTable, Measurements +from ij.process import ImageProcessor +import os + +# make sure the background is set to black in ProcessBinaryOptions +IJ.run("Options...", "iterations=1 count=1 black") + +# Specify an output directory +outputDir = FIXME # (e.g. r'C:\Users\username\Desktop', 'C:\\Users\\username\\Desktop' or 'C:/Users/username/Desktop' on Windows or '/Users/username/Desktop/' on MacOS) + +# Specify size parameters for object selection +min_size = 0 +max_size = 1000 + +# Initialize Roi Manager and empty results table, close other open images +rm = RoiManager().getInstance() +rm.reset() +IJ.run("Close All") + +# Open binary shapes image +shapes = IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif") + +# Configure and run particle analyzer +results = ResultsTable() # construct empty resultstable +pa = ParticleAnalyzer((ParticleAnalyzer.ADD_TO_MANAGER + ParticleAnalyzer.SHOW_ROI_MASKS),(Measurements.AREA + Measurements.CENTROID + Measurements.CENTER_OF_MASS + Measurements.PERIMETER + Measurements.RECT), results, min_size, max_size, 0, 1) +pa.analyze(shapes) # run the particle analyzer on the image +results.show("Results") + +# Save results, label mask, and ROIs +labelMask = WindowManager.getImage("Count Masks of xy_8bit_binary_randomshapes.tif") +IJ.run(labelMask, "Glasbey", "") # set glasbey LUT +FileSaver(labelMask).saveAsPng(os.path.join(outputDir, "shapes_labels_jython.png")) # save the label mask + +results.save(os.path.join(outputDir, "shapes_results_jython.csv")) # save results table + +rm.runCommand("Save", os.path.join(outputDir, "shapes_ROIset_jython.zip")) # save the ROIs diff --git a/_includes/output_saving/output_saving_act2_imagej-macro.ijm b/_includes/output_saving/output_saving_act2_imagej-macro.ijm new file mode 100644 index 00000000..f21c520a --- /dev/null +++ b/_includes/output_saving/output_saving_act2_imagej-macro.ijm @@ -0,0 +1,27 @@ +// This macro uses the particle analyzer to measure features of shapes. +// Different outputs are saved: ROIs, results table, and label mask. + +// make sure the background is set to black in ProcessBinaryOptions +run("Options...", "iterations=1 count=1 black do=Nothing"); + +// specify an output directory +outputDir = FIXME // (e.g. 'C:\\Users\\username\\Desktop' or 'C:/Users/username/Desktop' on Windows, or '/Users/username/Desktop/' on MacOS) + +// specify size parameters for object selection +minSize = 0 +maxSize = 1000 + +// close any pre-existing output you do not want in your saving results +roiManager("reset"); // clear any pre-existing ROIs +run("Clear Results"); // clear any pre-existing results +run("Close All"); // close any open images + +// Set measurements and run particle analyzer on binary shapes image +run("Set Measurements...", "area centroid center perimeter bounding redirect=None decimal=3") // set desired measurements +open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif"); // open binary image with random shapes +run("Analyze Particles...", "size=&minSize-&maxSize show=[Count Masks] display add") // run the particle analyzerrun("glasbey") + +// Save the results +saveAs("Png", outputDir + File.separator + "/shapes_labels_macro.png"); // save label mask to output directory +saveAs("Results", outputDir + File.separator + "/shapes_results_macro.csv"); // save results file to output directory +roiManager("Save", outputDir + File.separator + "/shapes_ROIset_macro.zip"); // save ROIs to output directory diff --git a/_modules/output_saving.md b/_modules/output_saving.md index a314b07b..9ec0a885 100644 --- a/_modules/output_saving.md +++ b/_modules/output_saving.md @@ -27,20 +27,9 @@ concept_map: > figure: /figures/output_saving.png figure_legend: Image analysis processes can yield several outputs, such as a) label masks, b) measurement tables, or c) regions of interest (ROIs), which may include different types, such as polygon outlines, lines or points. -activity_preface: | - - Use a template script and run it. - - Understand what the commands do. - - Record yourself saving the output. - - Edit the script using the recorded commands. - - Optional: define an output directory as a parameter and use string concatenation to specify where to save the file and with what name. An example output directory could be 'C:\\\Users\\\username\\\Desktop' on Windows (note double "\\" because a single "\\" is interpreted as an escape character) or '/Users/username/Desktop/' on MacOS. - -activities: - - ["ImageJ Macro", "output_saving/activities/output_saving_imagej-macro.md"] - - ["ImageJ Jython", "output_saving/activities/output_saving_imagej-jython.md"] - -exercises: - - ["ImageJ Macro", "output_saving/exercises/output_saving_imagej-macro.md"] - - ["ImageJ Jython", "output_saving/exercises/output_saving_imagej-jython.md"] +multiactivities: + - ["output_saving/output_saving_act1.md", [["ImageJ Macro", "output_saving/output_saving_act1_imagej-macro.md", "markdown"], ['ImageJ Jython', output_saving/output_saving_act1_imagej-jython.md]]] + - ["output_saving/output_saving_act2.md", [["ImageJ Macro", "output_saving/output_saving_act2_imagej-macro.ijm"], ['ImageJ Jython', output_saving/output_saving_act2_imagej-jython.py]]] assessment: >