Skip to content
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
86a3447
Add method for associating label images using 3DImageJSuite
lguerard Mar 11, 2025
97084b6
Fix the filtering of objects to use the calibrated method
lguerard Mar 11, 2025
408131c
Use the morpholibj package to 2D dilate labels
lguerard Mar 11, 2025
c542b19
Format imports
lguerard Mar 11, 2025
886613a
Add method to write results to CSV
lguerard Mar 11, 2025
831d771
Add methods for 3D Maxima Finder and 3D Watershed
lguerard Mar 11, 2025
4dc4504
Add processing library to do basic methods
lguerard Mar 11, 2025
6fa4d69
Add methods to get different metadata using bioformats
lguerard Mar 13, 2025
0bdd98e
Update method to support NaN (discarding them) and rounding results
lguerard Mar 13, 2025
34d5880
Add method to save in different file format
lguerard Mar 13, 2025
15bc0c4
Add method to pad a string
lguerard Mar 13, 2025
6189088
Add methods to find imaris and convert an image to IMS
lguerard Mar 13, 2025
272df1f
Add missing imports and formatting
lguerard Mar 13, 2025
c1a9332
Add methods to get different metadata from OMERO
lguerard Mar 13, 2025
05e6a2e
Add method to delete annotation from OMERO
lguerard Mar 13, 2025
fae0930
Add methods to save an OMERO.table
lguerard Mar 13, 2025
de5c84a
Add method to save IJ-ROIs to OMERO
lguerard Mar 13, 2025
16dd961
Add missing imports and formatting
lguerard Mar 13, 2025
04a9f84
Formatting
lguerard Mar 13, 2025
8a041de
Use `imp` instead of `imageplus`
lguerard Mar 19, 2025
309360f
Improve method name and docstring
lguerard Mar 19, 2025
170ac0c
Improve method name and docstring
lguerard Mar 19, 2025
cca38ba
Add missing import
lguerard Mar 19, 2025
e858900
Change method to use the one added to pathtools
lguerard Mar 19, 2025
b42cec3
Formatting
lguerard Mar 19, 2025
184a943
Update the formatting
lguerard Mar 19, 2025
f072877
Merge branch 'devel' into coding-session/2025-03-11
ehrenfeu Mar 19, 2025
54ee5f9
Format docstring's Returns `dict`
ehrenfeu Mar 19, 2025
88ee3a8
Add back `create_directory` with better docstring
lguerard Mar 19, 2025
7466838
Fix linting
lguerard Mar 19, 2025
5bea0ad
Formatting
lguerard Mar 19, 2025
0e81c37
Fix linting issues
lguerard Mar 19, 2025
1b222d0
Update docstring of write_ordereddict_to_csv()
ehrenfeu Mar 19, 2025
f9375c5
Rework docstring
ehrenfeu Mar 19, 2025
e99dcdb
Update docstring of locate_latest_imaris()
ehrenfeu Mar 19, 2025
d2a4991
Improve docstring
ehrenfeu Mar 19, 2025
6eb32db
Improve docstring
ehrenfeu Mar 19, 2025
bfbe585
Shorten docstring
ehrenfeu Mar 19, 2025
18131a8
Update dependency for mocks to 0.8.0.a0
ehrenfeu Mar 20, 2025
b3110ec
Merge branch 'devel' into coding-session/2025-03-11
ehrenfeu Mar 20, 2025
b520c28
Revert "Fix linting issues"
lguerard Mar 24, 2025
2b6a8d8
Revert "Formatting"
lguerard Mar 24, 2025
82b8683
Revert "Fix linting"
lguerard Mar 24, 2025
ebb1413
Rename functions
lguerard Mar 24, 2025
b59b597
Change returns to use dictionaries instead
lguerard Mar 24, 2025
98b9ca8
Change input variable name
lguerard Mar 24, 2025
14d2efc
Use logging instead
lguerard Mar 24, 2025
0045e65
Format using Ruff
lguerard Mar 24, 2025
0ff3433
Add comments and notes to docstring, fix missing inputs
lguerard Mar 24, 2025
74ffe42
Change output to inform if command has failed
lguerard Mar 24, 2025
a094dc9
Change method to return faster in case of empty list
lguerard Mar 24, 2025
25ee6ed
Add module docstring for ImageJ processing utilities
lguerard Mar 24, 2025
00dc641
Move minor implementation detail into code comment
ehrenfeu Mar 25, 2025
51248a4
Docstring cleanups
ehrenfeu Mar 25, 2025
d81e682
Shorten docstring example
ehrenfeu Mar 25, 2025
6b641bb
Indent to show as a fixed-width block
ehrenfeu Mar 25, 2025
51f2cec
One example should be enough
ehrenfeu Mar 25, 2025
a65fe41
Add empty line to fix docstring rendering
ehrenfeu Mar 25, 2025
c35e312
Run black formatting (restore default line length of 88)
ehrenfeu Mar 25, 2025
caa1370
Notes section comes before Examples
ehrenfeu Mar 25, 2025
99ab0f0
Minor updates on Notes section
ehrenfeu Mar 25, 2025
07f3064
Update function details
ehrenfeu Mar 25, 2025
4333086
Place Notes section after Returns
ehrenfeu Mar 25, 2025
ec158ff
Docstring conventions
ehrenfeu Mar 25, 2025
047d913
Docstring conventions
ehrenfeu Mar 25, 2025
98cfaff
Reformat dict in Returns section
ehrenfeu Mar 25, 2025
f0a1c9c
Merge branch 'devel' into pr/laurent/coding-session/2025-03-11
ehrenfeu Mar 25, 2025
8cef7bf
Merge branch 'devel' into pr/laurent/coding-session/2025-03-11
ehrenfeu Mar 25, 2025
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
223 changes: 152 additions & 71 deletions src/imcflibs/imagej/bdv.py

Large diffs are not rendered by default.

242 changes: 242 additions & 0 deletions src/imcflibs/imagej/bioformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,245 @@
reader = Memoizer(ImageReader())
reader.setId(path_to_file)
reader.close()


def get_metadata_from_image(path_to_image):
Comment thread
ehrenfeu marked this conversation as resolved.
Outdated
"""Extract metadata from an image file using Bio-Formats.

This function reads an image file using the Bio-Formats library and extracts
various metadata properties including physical dimensions, pixel dimensions,
and other image characteristics.

Parameters
----------
path_to_image : str or pathlib.Path
Path to the image file from which metadata should be extracted.

Returns
-------
dict
A dictionary containing the following metadata:

{
unit_width : float, # physical width of a pixel
unit_height : float, # physical height of a pixel
unit_depth : float, # physical depth of a voxel
pixel_width : int, # width of the image in pixels
pixel_height : int, # height of the image in pixels
slice_count : int, # number of Z-slices
channel_count : int, # number of channels
timepoints_count : int, # number of timepoints
dimension_order : str, # order of dimensions, e.g. "XYZCT"
pixel_type : str, # data type of the pixel values
}
"""
reader = ImageReader()
ome_meta = MetadataTools.createOMEXMLMetadata()
reader.setMetadataStore(ome_meta)
reader.setId(str(path_to_image))

phys_size_x = ome_meta.getPixelsPhysicalSizeX(0)
phys_size_y = ome_meta.getPixelsPhysicalSizeY(0)
phys_size_z = ome_meta.getPixelsPhysicalSizeZ(0)
pixel_size_x = ome_meta.getPixelsSizeX(0)
pixel_size_y = ome_meta.getPixelsSizeY(0)
pixel_size_z = ome_meta.getPixelsSizeZ(0)
channel_count = ome_meta.getPixelsSizeC(0)
timepoints_count = ome_meta.getPixelsSizeT(0)
dimension_order = ome_meta.getPixelsDimensionOrder(0)
pixel_type = ome_meta.getPixelsType(0)

image_calibration = {
"unit_width": phys_size_x.value(),
"unit_height": phys_size_y.value(),
"unit_depth": phys_size_z.value(),
"pixel_width": pixel_size_x.getNumberValue(),
"pixel_height": pixel_size_y.getNumberValue(),
"slice_count": pixel_size_z.getNumberValue(),
"channel_count": channel_count.getNumberValue(),
"timepoints_count": timepoints_count.getNumberValue(),
"dimension_order": dimension_order,
"pixel_type": pixel_type,
}

reader.close()

return image_calibration


def get_stage_coordinates_from_ome_metadata(source, imagenames):
Comment thread
ehrenfeu marked this conversation as resolved.
Outdated
"""Get the stage coordinates and calibration from the ome-xml for a given list of images

Parameters
----------
source : str
Path to the images
imagenames : list of str
List of images filenames

Returns
-------
tuple
Contains
dimensions : int
Number of dimensions (2D or 3D)
stage_coordinates_x : list
The absolute stage x-coordinated from ome-xml metadata
stage_coordinates_y : list
The absolute stage y-coordinated from ome-xml metadata
stage_coordinates_z : list
The absolute stage z-coordinated from ome-xml metadata
relative_coordinates_x : list
The relative stage x-coordinates in px
relative_coordinates_y : list
The relative stage y-coordinates in px
relative_coordinates_z : list
The relative stage z-coordinates in px
image_calibration : list
x,y,z image calibration in unit/px
calibration_unit : str
Image calibration unit
image_dimensions_czt : list
Number of images in dimensions c,z,t
series_names : list of str
Names of all series contained in the files
max_size : list of int
Maximum size across all files in dimensions x,y,z
"""

Check failure on line 396 in src/imcflibs/imagej/bioformats.py

View workflow job for this annotation

GitHub Actions / Ruff ⚡🕵

src/imcflibs/imagej/bioformats.py:359:5: D415 First line should end with a period, question mark, or exclamation point

Check failure on line 396 in src/imcflibs/imagej/bioformats.py

View workflow job for this annotation

GitHub Actions / Ruff ⚡🕵

src/imcflibs/imagej/bioformats.py:359:5: D400 First line should end with a period
Comment thread
ehrenfeu marked this conversation as resolved.

# open an array to store the abosolute stage coordinates from metadata
stage_coordinates_x = []
stage_coordinates_y = []
stage_coordinates_z = []
series_names = []

for counter, image in enumerate(imagenames):
# parse metadata
reader = ImageReader()
reader.setFlattenedResolutions(False)
omeMeta = MetadataTools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(source + str(image))
series_count = reader.getSeriesCount()

# get hyperstack dimensions from the first image
if counter == 0:
frame_size_x = reader.getSizeX()
frame_size_y = reader.getSizeY()
frame_size_z = reader.getSizeZ()
frame_size_c = reader.getSizeC()
frame_size_t = reader.getSizeT()

# note the dimensions
if frame_size_z == 1:
dimensions = 2
if frame_size_z > 1:
dimensions = 3

# get the physical calibration for the first image series
physSizeX = omeMeta.getPixelsPhysicalSizeX(0)
physSizeY = omeMeta.getPixelsPhysicalSizeY(0)
physSizeZ = omeMeta.getPixelsPhysicalSizeZ(0)

# workaround to get the z-interval if physSizeZ.value() returns None.
z_interval = 1
if physSizeZ is not None:
z_interval = physSizeZ.value()

if frame_size_z > 1 and physSizeZ is None:
print("no z calibration found, trying to recover")
first_plane = omeMeta.getPlanePositionZ(0, 0)
next_plane_imagenumber = frame_size_c + frame_size_t - 1
second_plane = omeMeta.getPlanePositionZ(0, next_plane_imagenumber)
z_interval = abs(abs(first_plane.value()) - abs(second_plane.value()))
print("z-interval seems to be: " + str(z_interval))

# create an image calibration
image_calibration = [physSizeX.value(), physSizeY.value(), z_interval]
calibration_unit = physSizeX.unit().getSymbol()
image_dimensions_czt = [frame_size_c, frame_size_z, frame_size_t]

reader.close()

for series in range(series_count):
if omeMeta.getImageName(series) == "macro image":
continue

if series_count > 1 and not str(image).endswith(".vsi"):
series_names.append(omeMeta.getImageName(series))
else:
series_names.append(str(image))
# get the plane position in calibrated units
current_position_x = omeMeta.getPlanePositionX(series, 0)
current_position_y = omeMeta.getPlanePositionY(series, 0)
current_position_z = omeMeta.getPlanePositionZ(series, 0)

physSizeX_max = (
physSizeX.value()
if physSizeX.value() >= omeMeta.getPixelsPhysicalSizeX(series).value()
else omeMeta.getPixelsPhysicalSizeX(series).value()
)
physSizeY_max = (
physSizeY.value()
if physSizeY.value() >= omeMeta.getPixelsPhysicalSizeY(series).value()
else omeMeta.getPixelsPhysicalSizeY(series).value()
)
if omeMeta.getPixelsPhysicalSizeZ(series):
physSizeZ_max = (
physSizeZ.value()
if physSizeZ.value()
>= omeMeta.getPixelsPhysicalSizeZ(series).value()
else omeMeta.getPixelsPhysicalSizeZ(series).value()
)

else:
physSizeZ_max = 1.0

# get the absolute stage positions and store them
pos_x = current_position_x.value()
pos_y = current_position_y.value()

if current_position_z is None:
print("the z-position is missing in the ome-xml metadata.")
pos_z = 1.0
else:
pos_z = current_position_z.value()

stage_coordinates_x.append(pos_x)
stage_coordinates_y.append(pos_y)
stage_coordinates_z.append(pos_z)

max_size = [physSizeX_max, physSizeY_max, physSizeZ_max]

# calculate the store the relative stage movements in px (for the grid/collection stitcher)
relative_coordinates_x_px = []
relative_coordinates_y_px = []
relative_coordinates_z_px = []

for i in range(len(stage_coordinates_x)):
rel_pos_x = (
stage_coordinates_x[i] - stage_coordinates_x[0]
) / physSizeX.value()
rel_pos_y = (
stage_coordinates_y[i] - stage_coordinates_y[0]
) / physSizeY.value()
rel_pos_z = (stage_coordinates_z[i] - stage_coordinates_z[0]) / z_interval

relative_coordinates_x_px.append(rel_pos_x)
relative_coordinates_y_px.append(rel_pos_y)
relative_coordinates_z_px.append(rel_pos_z)

return (
dimensions,
stage_coordinates_x,
stage_coordinates_y,
stage_coordinates_z,
relative_coordinates_x_px,
relative_coordinates_y_px,
relative_coordinates_z_px,
image_calibration,
calibration_unit,
image_dimensions_czt,
series_names,
max_size,
)
63 changes: 49 additions & 14 deletions src/imcflibs/imagej/labelimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""Functions to work with ImageJ label images."""

from ij import IJ, ImagePlus, Prefs, ImageStack
from ij import IJ, ImagePlus, ImageStack, Prefs
from ij.plugin import Duplicator, ImageCalculator
from ij.plugin.filter import ThresholdToSelection
from ij.process import FloatProcessor, ImageProcessor
Expand Down Expand Up @@ -97,6 +97,51 @@
return ImageCalculator.run(label_image_ref, imp_dup, "Multiply create")


def associate_label_images_3d(outer_label_imp, inner_label_imp):
"""
Associate two label images.

Uses the 3D Association plugin from the 3DImageJSuite.
Comment thread
ehrenfeu marked this conversation as resolved.
Outdated

Parameters
----------
outer_label_imp : ij.ImagePlus
The outer label image
inner_label_imp : ij.ImagePlus
The inner label image

Returns
-------
related_inner_imp : ij.ImagePlus
The related inner label image
"""

Check failure on line 117 in src/imcflibs/imagej/labelimage.py

View workflow job for this annotation

GitHub Actions / Ruff ⚡🕵

src/imcflibs/imagej/labelimage.py:101:5: D212 Multi-line docstring summary should start at the first line

outer_label_imp.show()
inner_label_imp.show()

outer_title = outer_label_imp.getTitle()
inner_title = inner_label_imp.getTitle()

IJ.run(
"3D Association",
"image_a="
+ outer_title
+ " "
+ "image_b="
+ inner_title
+ " "
+ "method=Colocalisation min=1 max=0.000",
)

related_inner_imp = IJ.getImage()

outer_label_imp.hide()
inner_label_imp.hide()
related_inner_imp.hide()

return related_inner_imp


def filter_objects(label_image, table, string, min_val, max_val):
"""Filter labels based on specific min and max values.

Expand Down Expand Up @@ -182,11 +227,11 @@

# Set the minimum size for labeling if provided
if min_vol:
labeler.setMinSize(min_vol)
labeler.setMinSizeCalibrated(min_vol)

# Set the maximum size for labeling if provided
if max_vol:
labeler.setMaxSize(max_vol)
labeler.setMinSizeCalibrated(max_vol)

# Get the labeled image
seg = labeler.getLabels(img)
Expand Down Expand Up @@ -229,17 +274,7 @@
current_imp = Duplicator().run(imp, 1, 1, i, imp.getNSlices(), 1, 1)

# Perform a dilation of the labels in the current slice
IJ.run(
current_imp,
"Label Morphological Filters",
"operation=Dilation radius=" + str(dilation_radius) + " from_any_label",
)

# Get the dilated labels
dilated_labels_imp = IJ.getImage()

# Hide the dilated labels to avoid visual clutter
dilated_labels_imp.hide()
dilated_labels_imp = li.dilateLabels(current_imp, dilation_radius)

# Append the dilated labels to the list
dilated_labels_list.append(dilated_labels_imp)
Expand Down
Loading
Loading