Skip to content
Merged
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
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ version = "0.0.0"
# - or: python = ">=3.10"

[tool.poetry.dependencies]
imcf-fiji-mocks = ">=0.6.0"
imcf-fiji-mocks = ">=0.7.0"
python = ">=2.7"
python-micrometa = "^15.2.2"
sjlogging = ">=0.5.2"
Expand Down
89 changes: 84 additions & 5 deletions src/imcflibs/imagej/projections.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""Functions for creating Z projections."""
"""Functions for creating projections."""

from ij.plugin import ZProjector # pylint: disable-msg=E0401

from .bioformats import export_using_orig_name # pylint: disable-msg=E0401
from ..log import LOG as log

from net.imagej.axis import Axes
from net.imagej.ops import Ops
from ij import ImagePlus, IJ
from net.imagej import Dataset


def average(imp):
"""Create an average intensity projection.
"""Create an average intensity Z projection.

Parameters
----------
Expand All @@ -23,13 +28,13 @@
log.warn("ImagePlus is not a z-stack, not creating a projection!")
return imp

log.debug("Creating average projection...")
log.debug("Creating average Z projection...")

Check warning on line 31 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L31

Added line #L31 was not covered by tests
proj = ZProjector.run(imp, "avg")
return proj


def maximum(imp):
"""Create a maximum intensity projection.
"""Create a maximum intensity Z projection.

Parameters
----------
Expand All @@ -45,7 +50,7 @@
log.warn("ImagePlus is not a z-stack, not creating a projection!")
return imp

log.debug("Creating maximum intensity projection...")
log.debug("Creating maximum intensity Z projection...")

Check warning on line 53 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L53

Added line #L53 was not covered by tests
proj = ZProjector.run(imp, "max")
return proj

Expand Down Expand Up @@ -100,3 +105,77 @@
proj.close()

return True


def project_stack(imp, projected_dimension, projection_type, ops, ds, cs):
"""Project along a defined axis using the given projection type.

Parameters
----------
imp : ImagePlus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure docstrings do not exceed the 88 chars line length, you may want to consider the Rewrap Revived extension for VS Code for doing this.

The input image to be projected.
projected_dimension : str
The dimension along which to project the data. Must be one of {"X", "Y", "Z",
"TIME", "CHANNEL"}.
projection_type : str
The type of projection to perform. Must be one of {"Max", "Mean", "Median",
"Min", "StdDev", "Sum"}.
ops : OpService
The service used to access image processing operations. Use e.g. from script
parameter: `#@ OpService ops`
ds : DatasetService
The service used to create new datasets. Use e.g. from script parameter:
`#@ DatasetService ds`
cs : ConvertService
The service used to convert between formats. Use e.g. from script parameter:
`#@ ConvertService cs`

Returns
-------
ImagePlus
The resulting projected image as an ImagePlus object.

Raises
------
Exception
If the specified dimension is not found or if the dimension has only one frame.
"""
bit_depth = imp.getBitDepth()
data = cs.convert(imp, Dataset)

Check warning on line 144 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L143-L144

Added lines #L143 - L144 were not covered by tests
# Select which dimension to project
dim = data.dimensionIndex(getattr(Axes, projected_dimension))
if dim == -1:
raise Exception("%s dimension not found." % projected_dimension)
if data.dimension(dim) < 2:
raise Exception("%s dimension has only one frame." % projected_dimension)

Check warning on line 150 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L146-L150

Added lines #L146 - L150 were not covered by tests

# Write the output dimensions
new_dimensions = [

Check warning on line 153 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L153

Added line #L153 was not covered by tests
data.dimension(d) for d in range(0, data.numDimensions()) if d != dim
]

# Create the output image
projected = ops.create().img(new_dimensions)

Check warning on line 158 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L158

Added line #L158 was not covered by tests

# Create the op and run it
proj_op = ops.op(getattr(Ops.Stats, projection_type), data)
ops.transform().project(projected, data, proj_op, dim)

Check warning on line 162 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L161-L162

Added lines #L161 - L162 were not covered by tests

# Create the output Dataset and convert to ImagePlus
output = ds.create(projected)
output_imp = cs.convert(output, ImagePlus)
output_imp = output_imp.duplicate()
output_imp.setTitle("%s %s projection" % (projected_dimension, projection_type))
IJ.run(output_imp, "Enhance Contrast", "saturated=0.35")

Check warning on line 169 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L165-L169

Added lines #L165 - L169 were not covered by tests

# Rescale bit depth if possible
if projection_type in ["Max", "Min", "Median"]:
IJ.run("Conversions...", " ")
if bit_depth in [8, 16]:
IJ.run(output_imp, str(bit_depth) + "-bit", "")
if bit_depth == 12:
IJ.run(output_imp, "16-bit", "")

Check warning on line 177 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L172-L177

Added lines #L172 - L177 were not covered by tests

IJ.run("Conversions...", "scale")

Check warning on line 179 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L179

Added line #L179 was not covered by tests

return output_imp

Check warning on line 181 in src/imcflibs/imagej/projections.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/projections.py#L181

Added line #L181 was not covered by tests