Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated output_saving scripting module to fit with multiactivity layout #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions _includes/output_saving/exercises/output_saving_imagej-jython.md

This file was deleted.

36 changes: 0 additions & 36 deletions _includes/output_saving/exercises/output_saving_imagej-macro.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
- 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.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions _includes/output_saving/output_saving_act2.md
Original file line number Diff line number Diff line change
@@ -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?
41 changes: 41 additions & 0 deletions _includes/output_saving/output_saving_act2_imagej-jython.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions _includes/output_saving/output_saving_act2_imagej-macro.ijm
Original file line number Diff line number Diff line change
@@ -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
17 changes: 3 additions & 14 deletions _modules/output_saving.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: >

Expand Down
Loading