sigima 0.0.1.dev0__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sigima/__init__.py +142 -2
- sigima/client/__init__.py +105 -0
- sigima/client/base.py +780 -0
- sigima/client/remote.py +469 -0
- sigima/client/stub.py +814 -0
- sigima/client/utils.py +90 -0
- sigima/config.py +444 -0
- sigima/data/logo/Sigima.svg +135 -0
- sigima/data/tests/annotations.json +798 -0
- sigima/data/tests/curve_fitting/exponential_fit.txt +511 -0
- sigima/data/tests/curve_fitting/gaussian_fit.txt +100 -0
- sigima/data/tests/curve_fitting/piecewiseexponential_fit.txt +1022 -0
- sigima/data/tests/curve_fitting/polynomial_fit.txt +100 -0
- sigima/data/tests/curve_fitting/twohalfgaussian_fit.txt +1000 -0
- sigima/data/tests/curve_formats/bandwidth.txt +201 -0
- sigima/data/tests/curve_formats/boxcar.npy +0 -0
- sigima/data/tests/curve_formats/datetime.txt +1001 -0
- sigima/data/tests/curve_formats/dynamic_parameters.txt +4000 -0
- sigima/data/tests/curve_formats/fw1e2.txt +301 -0
- sigima/data/tests/curve_formats/fwhm.txt +319 -0
- sigima/data/tests/curve_formats/multiple_curves.csv +29 -0
- sigima/data/tests/curve_formats/noised_saw.mat +0 -0
- sigima/data/tests/curve_formats/oscilloscope.csv +111 -0
- sigima/data/tests/curve_formats/other/other2/recursive2.txt +5 -0
- sigima/data/tests/curve_formats/other/recursive1.txt +5 -0
- sigima/data/tests/curve_formats/paracetamol.npy +0 -0
- sigima/data/tests/curve_formats/paracetamol.txt +1010 -0
- sigima/data/tests/curve_formats/paracetamol_dx_dy.csv +1000 -0
- sigima/data/tests/curve_formats/paracetamol_dy.csv +1001 -0
- sigima/data/tests/curve_formats/pulse1.npy +0 -0
- sigima/data/tests/curve_formats/pulse2.npy +0 -0
- sigima/data/tests/curve_formats/simple.txt +5 -0
- sigima/data/tests/curve_formats/spectrum.mca +2139 -0
- sigima/data/tests/curve_formats/square2.npy +0 -0
- sigima/data/tests/curve_formats/step.npy +0 -0
- sigima/data/tests/fabry-perot1.jpg +0 -0
- sigima/data/tests/fabry-perot2.jpg +0 -0
- sigima/data/tests/flower.npy +0 -0
- sigima/data/tests/image_formats/NF 180338201.scor-data +11003 -0
- sigima/data/tests/image_formats/binary_image.npy +0 -0
- sigima/data/tests/image_formats/binary_image.png +0 -0
- sigima/data/tests/image_formats/centroid_test.npy +0 -0
- sigima/data/tests/image_formats/coordinated_text/complex_image.txt +10011 -0
- sigima/data/tests/image_formats/coordinated_text/complex_ref_image.txt +10010 -0
- sigima/data/tests/image_formats/coordinated_text/image.txt +15 -0
- sigima/data/tests/image_formats/coordinated_text/image2.txt +14 -0
- sigima/data/tests/image_formats/coordinated_text/image_no_unit_no_label.txt +14 -0
- sigima/data/tests/image_formats/coordinated_text/image_with_nan.txt +15 -0
- sigima/data/tests/image_formats/coordinated_text/image_with_unit.txt +14 -0
- sigima/data/tests/image_formats/fiber.csv +480 -0
- sigima/data/tests/image_formats/fiber.jpg +0 -0
- sigima/data/tests/image_formats/fiber.png +0 -0
- sigima/data/tests/image_formats/fiber.txt +480 -0
- sigima/data/tests/image_formats/gaussian_spot_with_noise.npy +0 -0
- sigima/data/tests/image_formats/mr-brain.dcm +0 -0
- sigima/data/tests/image_formats/noised_gaussian.mat +0 -0
- sigima/data/tests/image_formats/sif_reader/nd_lum_image_no_glue.sif +0 -0
- sigima/data/tests/image_formats/sif_reader/raman1.sif +0 -0
- sigima/data/tests/image_formats/tiling.txt +10 -0
- sigima/data/tests/image_formats/uint16.tiff +0 -0
- sigima/data/tests/image_formats/uint8.tiff +0 -0
- sigima/data/tests/laser_beam/TEM00_z_13.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_18.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_23.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_30.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_35.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_40.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_45.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_50.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_55.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_60.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_65.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_70.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_75.jpg +0 -0
- sigima/data/tests/laser_beam/TEM00_z_80.jpg +0 -0
- sigima/enums.py +195 -0
- sigima/io/__init__.py +123 -0
- sigima/io/base.py +311 -0
- sigima/io/common/__init__.py +5 -0
- sigima/io/common/basename.py +164 -0
- sigima/io/common/converters.py +189 -0
- sigima/io/common/objmeta.py +181 -0
- sigima/io/common/textreader.py +58 -0
- sigima/io/convenience.py +157 -0
- sigima/io/enums.py +17 -0
- sigima/io/ftlab.py +395 -0
- sigima/io/image/__init__.py +9 -0
- sigima/io/image/base.py +177 -0
- sigima/io/image/formats.py +1016 -0
- sigima/io/image/funcs.py +414 -0
- sigima/io/signal/__init__.py +9 -0
- sigima/io/signal/base.py +129 -0
- sigima/io/signal/formats.py +290 -0
- sigima/io/signal/funcs.py +723 -0
- sigima/objects/__init__.py +260 -0
- sigima/objects/base.py +937 -0
- sigima/objects/image/__init__.py +88 -0
- sigima/objects/image/creation.py +556 -0
- sigima/objects/image/object.py +524 -0
- sigima/objects/image/roi.py +904 -0
- sigima/objects/scalar/__init__.py +57 -0
- sigima/objects/scalar/common.py +215 -0
- sigima/objects/scalar/geometry.py +502 -0
- sigima/objects/scalar/table.py +784 -0
- sigima/objects/shape.py +290 -0
- sigima/objects/signal/__init__.py +133 -0
- sigima/objects/signal/constants.py +27 -0
- sigima/objects/signal/creation.py +1428 -0
- sigima/objects/signal/object.py +444 -0
- sigima/objects/signal/roi.py +274 -0
- sigima/params.py +405 -0
- sigima/proc/__init__.py +96 -0
- sigima/proc/base.py +381 -0
- sigima/proc/decorator.py +330 -0
- sigima/proc/image/__init__.py +513 -0
- sigima/proc/image/arithmetic.py +335 -0
- sigima/proc/image/base.py +260 -0
- sigima/proc/image/detection.py +519 -0
- sigima/proc/image/edges.py +329 -0
- sigima/proc/image/exposure.py +406 -0
- sigima/proc/image/extraction.py +458 -0
- sigima/proc/image/filtering.py +219 -0
- sigima/proc/image/fourier.py +147 -0
- sigima/proc/image/geometry.py +661 -0
- sigima/proc/image/mathops.py +340 -0
- sigima/proc/image/measurement.py +195 -0
- sigima/proc/image/morphology.py +155 -0
- sigima/proc/image/noise.py +107 -0
- sigima/proc/image/preprocessing.py +182 -0
- sigima/proc/image/restoration.py +235 -0
- sigima/proc/image/threshold.py +217 -0
- sigima/proc/image/transformations.py +393 -0
- sigima/proc/signal/__init__.py +376 -0
- sigima/proc/signal/analysis.py +206 -0
- sigima/proc/signal/arithmetic.py +551 -0
- sigima/proc/signal/base.py +262 -0
- sigima/proc/signal/extraction.py +60 -0
- sigima/proc/signal/features.py +310 -0
- sigima/proc/signal/filtering.py +484 -0
- sigima/proc/signal/fitting.py +276 -0
- sigima/proc/signal/fourier.py +259 -0
- sigima/proc/signal/mathops.py +420 -0
- sigima/proc/signal/processing.py +580 -0
- sigima/proc/signal/stability.py +175 -0
- sigima/proc/title_formatting.py +227 -0
- sigima/proc/validation.py +272 -0
- sigima/tests/__init__.py +7 -0
- sigima/tests/common/__init__.py +0 -0
- sigima/tests/common/arithmeticparam_unit_test.py +26 -0
- sigima/tests/common/basename_unit_test.py +126 -0
- sigima/tests/common/client_unit_test.py +412 -0
- sigima/tests/common/converters_unit_test.py +77 -0
- sigima/tests/common/decorator_unit_test.py +176 -0
- sigima/tests/common/examples_unit_test.py +104 -0
- sigima/tests/common/kernel_normalization_unit_test.py +242 -0
- sigima/tests/common/roi_basic_unit_test.py +73 -0
- sigima/tests/common/roi_geometry_unit_test.py +171 -0
- sigima/tests/common/scalar_builder_unit_test.py +142 -0
- sigima/tests/common/scalar_unit_test.py +991 -0
- sigima/tests/common/shape_unit_test.py +183 -0
- sigima/tests/common/stat_unit_test.py +138 -0
- sigima/tests/common/title_formatting_unit_test.py +338 -0
- sigima/tests/common/tools_coordinates_unit_test.py +60 -0
- sigima/tests/common/transformations_unit_test.py +178 -0
- sigima/tests/common/validation_unit_test.py +205 -0
- sigima/tests/conftest.py +129 -0
- sigima/tests/data.py +998 -0
- sigima/tests/env.py +280 -0
- sigima/tests/guiutils.py +163 -0
- sigima/tests/helpers.py +532 -0
- sigima/tests/image/__init__.py +28 -0
- sigima/tests/image/binning_unit_test.py +128 -0
- sigima/tests/image/blob_detection_unit_test.py +312 -0
- sigima/tests/image/centroid_unit_test.py +170 -0
- sigima/tests/image/check_2d_array_unit_test.py +63 -0
- sigima/tests/image/contour_unit_test.py +172 -0
- sigima/tests/image/convolution_unit_test.py +178 -0
- sigima/tests/image/datatype_unit_test.py +67 -0
- sigima/tests/image/edges_unit_test.py +155 -0
- sigima/tests/image/enclosingcircle_unit_test.py +88 -0
- sigima/tests/image/exposure_unit_test.py +223 -0
- sigima/tests/image/fft2d_unit_test.py +189 -0
- sigima/tests/image/filtering_unit_test.py +166 -0
- sigima/tests/image/geometry_unit_test.py +654 -0
- sigima/tests/image/hough_circle_unit_test.py +147 -0
- sigima/tests/image/imageobj_unit_test.py +737 -0
- sigima/tests/image/morphology_unit_test.py +71 -0
- sigima/tests/image/noise_unit_test.py +57 -0
- sigima/tests/image/offset_correction_unit_test.py +72 -0
- sigima/tests/image/operation_unit_test.py +518 -0
- sigima/tests/image/peak2d_limits_unit_test.py +41 -0
- sigima/tests/image/peak2d_unit_test.py +133 -0
- sigima/tests/image/profile_unit_test.py +159 -0
- sigima/tests/image/projections_unit_test.py +121 -0
- sigima/tests/image/restoration_unit_test.py +141 -0
- sigima/tests/image/roi2dparam_unit_test.py +53 -0
- sigima/tests/image/roi_advanced_unit_test.py +588 -0
- sigima/tests/image/roi_grid_unit_test.py +279 -0
- sigima/tests/image/spectrum2d_unit_test.py +40 -0
- sigima/tests/image/threshold_unit_test.py +91 -0
- sigima/tests/io/__init__.py +0 -0
- sigima/tests/io/addnewformat_unit_test.py +125 -0
- sigima/tests/io/convenience_funcs_unit_test.py +470 -0
- sigima/tests/io/coordinated_text_format_unit_test.py +495 -0
- sigima/tests/io/datetime_csv_unit_test.py +198 -0
- sigima/tests/io/imageio_formats_test.py +41 -0
- sigima/tests/io/ioregistry_unit_test.py +69 -0
- sigima/tests/io/objmeta_unit_test.py +87 -0
- sigima/tests/io/readobj_unit_test.py +130 -0
- sigima/tests/io/readwriteobj_unit_test.py +67 -0
- sigima/tests/signal/__init__.py +0 -0
- sigima/tests/signal/analysis_unit_test.py +135 -0
- sigima/tests/signal/check_1d_arrays_unit_test.py +169 -0
- sigima/tests/signal/convolution_unit_test.py +404 -0
- sigima/tests/signal/datetime_unit_test.py +176 -0
- sigima/tests/signal/fft1d_unit_test.py +303 -0
- sigima/tests/signal/filters_unit_test.py +403 -0
- sigima/tests/signal/fitting_unit_test.py +929 -0
- sigima/tests/signal/fwhm_unit_test.py +111 -0
- sigima/tests/signal/noise_unit_test.py +128 -0
- sigima/tests/signal/offset_correction_unit_test.py +34 -0
- sigima/tests/signal/operation_unit_test.py +489 -0
- sigima/tests/signal/peakdetection_unit_test.py +145 -0
- sigima/tests/signal/processing_unit_test.py +657 -0
- sigima/tests/signal/pulse/__init__.py +112 -0
- sigima/tests/signal/pulse/crossing_times_unit_test.py +123 -0
- sigima/tests/signal/pulse/plateau_detection_unit_test.py +102 -0
- sigima/tests/signal/pulse/pulse_unit_test.py +1824 -0
- sigima/tests/signal/roi_advanced_unit_test.py +392 -0
- sigima/tests/signal/signalobj_unit_test.py +603 -0
- sigima/tests/signal/stability_unit_test.py +431 -0
- sigima/tests/signal/uncertainty_unit_test.py +611 -0
- sigima/tests/vistools.py +1030 -0
- sigima/tools/__init__.py +59 -0
- sigima/tools/checks.py +290 -0
- sigima/tools/coordinates.py +308 -0
- sigima/tools/datatypes.py +26 -0
- sigima/tools/image/__init__.py +97 -0
- sigima/tools/image/detection.py +451 -0
- sigima/tools/image/exposure.py +77 -0
- sigima/tools/image/extraction.py +48 -0
- sigima/tools/image/fourier.py +260 -0
- sigima/tools/image/geometry.py +190 -0
- sigima/tools/image/preprocessing.py +165 -0
- sigima/tools/signal/__init__.py +86 -0
- sigima/tools/signal/dynamic.py +254 -0
- sigima/tools/signal/features.py +135 -0
- sigima/tools/signal/filtering.py +171 -0
- sigima/tools/signal/fitting.py +1171 -0
- sigima/tools/signal/fourier.py +466 -0
- sigima/tools/signal/interpolation.py +70 -0
- sigima/tools/signal/peakdetection.py +126 -0
- sigima/tools/signal/pulse.py +1626 -0
- sigima/tools/signal/scaling.py +50 -0
- sigima/tools/signal/stability.py +258 -0
- sigima/tools/signal/windowing.py +90 -0
- sigima/worker.py +79 -0
- sigima-1.0.0.dist-info/METADATA +233 -0
- sigima-1.0.0.dist-info/RECORD +262 -0
- {sigima-0.0.1.dev0.dist-info → sigima-1.0.0.dist-info}/licenses/LICENSE +29 -29
- sigima-0.0.1.dev0.dist-info/METADATA +0 -60
- sigima-0.0.1.dev0.dist-info/RECORD +0 -6
- {sigima-0.0.1.dev0.dist-info → sigima-1.0.0.dist-info}/WHEEL +0 -0
- {sigima-0.0.1.dev0.dist-info → sigima-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
|
|
2
|
+
|
|
3
|
+
"""Image ROI advanced unit tests"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import pytest
|
|
9
|
+
from skimage import draw
|
|
10
|
+
|
|
11
|
+
import sigima.objects
|
|
12
|
+
import sigima.params
|
|
13
|
+
import sigima.proc.image
|
|
14
|
+
from sigima.tests import guiutils
|
|
15
|
+
from sigima.tests.data import create_multigaussian_image
|
|
16
|
+
from sigima.tests.helpers import print_obj_data_dimensions
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_image_roi_merge() -> None:
|
|
20
|
+
"""Test image ROI merge"""
|
|
21
|
+
# Create an image object with a single ROI, and another one with another ROI.
|
|
22
|
+
# Compute the average of the two objects, and check if the resulting object
|
|
23
|
+
# has the expected ROI (i.e. the union of the original object's ROI).
|
|
24
|
+
obj1 = create_multigaussian_image()
|
|
25
|
+
obj2 = create_multigaussian_image()
|
|
26
|
+
obj2.roi = sigima.objects.create_image_roi(
|
|
27
|
+
"rectangle", [600, 800, 1000, 1200], inverse=False
|
|
28
|
+
)
|
|
29
|
+
obj1.roi = sigima.objects.create_image_roi(
|
|
30
|
+
"rectangle", [500, 750, 1000, 1250], inverse=False
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Compute the average of the two objects
|
|
34
|
+
obj3 = sigima.proc.image.average([obj1, obj2])
|
|
35
|
+
assert obj3.roi is not None, "Merged object should have a ROI"
|
|
36
|
+
assert len(obj3.roi) == 2, "Merged object should have two single ROIs"
|
|
37
|
+
for single_roi in obj3.roi:
|
|
38
|
+
assert single_roi.get_indices_coords(obj3) in (
|
|
39
|
+
[500, 750, 1000, 1250],
|
|
40
|
+
[600, 800, 1000, 1200],
|
|
41
|
+
), "Merged object should have the union of the original object's ROIs"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_image_roi_combine() -> None:
|
|
45
|
+
"""Test `ImageROI.combine_with` method"""
|
|
46
|
+
coords1, coords2 = [600, 800, 1000, 1200], [500, 750, 1000, 1250]
|
|
47
|
+
roi1 = sigima.objects.create_image_roi(
|
|
48
|
+
"rectangle", coords1, indices=True, inverse=False
|
|
49
|
+
)
|
|
50
|
+
roi2 = sigima.objects.create_image_roi(
|
|
51
|
+
"rectangle", coords2, indices=True, inverse=False
|
|
52
|
+
)
|
|
53
|
+
exp_combined = sigima.objects.create_image_roi(
|
|
54
|
+
"rectangle",
|
|
55
|
+
[coords1, coords2],
|
|
56
|
+
indices=True,
|
|
57
|
+
inverse=False,
|
|
58
|
+
)
|
|
59
|
+
# Check that combining two ROIs results in a new ROI with both coordinates:
|
|
60
|
+
roi3 = roi1.combine_with(roi2)
|
|
61
|
+
assert roi3 == exp_combined, "Combined ROI should match expected"
|
|
62
|
+
# Check that combining again with the same ROI does not change it:
|
|
63
|
+
roi3 = roi1.combine_with(roi2)
|
|
64
|
+
assert roi3 == exp_combined, "Combining with the same ROI should not change it"
|
|
65
|
+
# Check that combining with a signal ROI raises an error:
|
|
66
|
+
with pytest.raises(
|
|
67
|
+
TypeError, match=r"Cannot combine([\S ]*)ImageROI([\S ]*)SignalROI"
|
|
68
|
+
):
|
|
69
|
+
roi1.combine_with(sigima.objects.create_signal_roi([50, 100], indices=True))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
SIZE = 200
|
|
73
|
+
|
|
74
|
+
# Image ROIs:
|
|
75
|
+
IROI1 = [100, 100, 75, 100] # Rectangle
|
|
76
|
+
IROI2 = [66, 100, 50] # Circle
|
|
77
|
+
# Polygon (triangle, that is intentionally inside the rectangle, so that this ROI
|
|
78
|
+
# has no impact on the mask calculations in the tests)
|
|
79
|
+
IROI3 = [100, 100, 100, 150, 150, 133]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def __roi_str(obj: sigima.objects.ImageObj) -> str:
|
|
83
|
+
"""Return a string representation of a ImageROI object for context."""
|
|
84
|
+
if obj.roi is None:
|
|
85
|
+
return "None"
|
|
86
|
+
if obj.roi.is_empty():
|
|
87
|
+
return "Empty"
|
|
88
|
+
return ", ".join(
|
|
89
|
+
f"{single_roi.__class__.__name__}({single_roi.get_indices_coords(obj)})"
|
|
90
|
+
for single_roi in obj.roi.single_rois
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def __create_test_roi() -> sigima.objects.ImageROI:
|
|
95
|
+
"""Create test ROI"""
|
|
96
|
+
roi = sigima.objects.create_image_roi("rectangle", IROI1, inverse=False)
|
|
97
|
+
roi.add_roi(sigima.objects.create_image_roi("circle", IROI2, inverse=False))
|
|
98
|
+
roi.add_roi(sigima.objects.create_image_roi("polygon", IROI3, inverse=False))
|
|
99
|
+
return roi
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def __create_test_image() -> sigima.objects.ImageObj:
|
|
103
|
+
"""Create test image"""
|
|
104
|
+
param = sigima.objects.NewImageParam.create(height=SIZE, width=SIZE)
|
|
105
|
+
ima = create_multigaussian_image(param)
|
|
106
|
+
ima.data += 1 # Ensure that the image has non-zero values (for ROI check tests)
|
|
107
|
+
return ima
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def __test_processing_in_roi(src: sigima.objects.ImageObj) -> None:
|
|
111
|
+
"""Run image processing in ROI
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
src: Source image object (with or without ROI)
|
|
115
|
+
"""
|
|
116
|
+
print_obj_data_dimensions(src)
|
|
117
|
+
value = 1
|
|
118
|
+
p = sigima.params.ConstantParam.create(value=value)
|
|
119
|
+
dst = sigima.proc.image.addition_constant(src, p)
|
|
120
|
+
orig = src.data
|
|
121
|
+
new = dst.data
|
|
122
|
+
context = f" [ROI: {__roi_str(src)}]"
|
|
123
|
+
if src.roi is not None and not src.roi.is_empty():
|
|
124
|
+
# A ROI has been set in the source image.
|
|
125
|
+
assert np.all(
|
|
126
|
+
new[IROI1[1] : IROI1[3] + IROI1[1], IROI1[0] : IROI1[2] + IROI1[0]]
|
|
127
|
+
== orig[IROI1[1] : IROI1[3] + IROI1[1], IROI1[0] : IROI1[2] + IROI1[0]]
|
|
128
|
+
+ value
|
|
129
|
+
), f"Image ROI 1 data mismatch{context}"
|
|
130
|
+
assert np.all(
|
|
131
|
+
new[IROI2[1] : IROI1[1] + 1, IROI2[0] : IROI2[0] + 2 * IROI2[2]]
|
|
132
|
+
== orig[IROI2[1] : IROI1[1] + 1, IROI2[0] : IROI2[0] + 2 * IROI2[2]] + value
|
|
133
|
+
), f"Image ROI 2 data mismatch{context}"
|
|
134
|
+
first_col = min(IROI1[0], IROI2[0] - IROI2[2])
|
|
135
|
+
first_row = min(IROI1[1], IROI2[1] - IROI2[2])
|
|
136
|
+
last_col = max(IROI1[0] + IROI1[2], IROI2[0] + 2 * IROI2[2])
|
|
137
|
+
last_row = max(IROI1[1] + IROI1[3], IROI2[1] + 2 * IROI2[2])
|
|
138
|
+
assert np.all(
|
|
139
|
+
new[:first_row, :first_col] == np.array(orig[:first_row, :first_col], float)
|
|
140
|
+
), f"Image before ROIs data mismatch{context}"
|
|
141
|
+
assert np.all(new[:first_row, last_col:] == orig[:first_row, last_col:]), (
|
|
142
|
+
f"Image after ROIs data mismatch{context}"
|
|
143
|
+
)
|
|
144
|
+
assert np.all(new[last_row:, :first_col] == orig[last_row:, :first_col]), (
|
|
145
|
+
f"Image before ROIs data mismatch{context}"
|
|
146
|
+
)
|
|
147
|
+
assert np.all(new[last_row:, last_col:] == orig[last_row:, last_col:]), (
|
|
148
|
+
f"Image after ROIs data mismatch{context}"
|
|
149
|
+
)
|
|
150
|
+
else:
|
|
151
|
+
# No ROI has been set in the source image.
|
|
152
|
+
assert np.all(new == orig + value), f"Image data mismatch{context}"
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def test_image_roi_processing() -> None:
|
|
156
|
+
"""Test image ROI processing"""
|
|
157
|
+
src = __create_test_image()
|
|
158
|
+
base_roi = __create_test_roi()
|
|
159
|
+
empty_roi = sigima.objects.ImageROI()
|
|
160
|
+
for roi in (empty_roi, base_roi):
|
|
161
|
+
src.roi = roi
|
|
162
|
+
__test_processing_in_roi(src)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_empty_image_roi() -> None:
|
|
166
|
+
"""Test empty image ROI"""
|
|
167
|
+
src = __create_test_image()
|
|
168
|
+
empty_roi = sigima.objects.ImageROI()
|
|
169
|
+
for roi in (None, empty_roi):
|
|
170
|
+
src.roi = roi
|
|
171
|
+
context = f" [ROI: {__roi_str(src)}]"
|
|
172
|
+
assert src.roi is None or src.roi.is_empty(), (
|
|
173
|
+
f"Source object ROI should be empty or None{context}"
|
|
174
|
+
)
|
|
175
|
+
if src.roi is not None:
|
|
176
|
+
# No ROI has been set in the source image
|
|
177
|
+
im1 = sigima.proc.image.extract_roi(src, src.roi.to_params(src))
|
|
178
|
+
assert im1.data.shape == (0, 0), f"Extracted image should be empty{context}"
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@pytest.mark.validation
|
|
182
|
+
def test_image_extract_rois() -> None:
|
|
183
|
+
"""Validation test for image ROI extraction into a single object"""
|
|
184
|
+
src = __create_test_image()
|
|
185
|
+
src.roi = __create_test_roi()
|
|
186
|
+
context = f" [ROI: {__roi_str(src)}]"
|
|
187
|
+
nzroi = f"Non-zero values expected in ROI{context}"
|
|
188
|
+
zroi = f"Zero values expected outside ROI{context}"
|
|
189
|
+
|
|
190
|
+
im1 = sigima.proc.image.extract_rois(src, src.roi.to_params(src))
|
|
191
|
+
|
|
192
|
+
mask1 = np.zeros(shape=(SIZE, SIZE), dtype=bool)
|
|
193
|
+
mask1[IROI1[1] : IROI1[1] + IROI1[3], IROI1[0] : IROI1[0] + IROI1[2]] = 1
|
|
194
|
+
xc, yc, r = IROI2
|
|
195
|
+
mask2 = np.zeros(shape=(SIZE, SIZE), dtype=bool)
|
|
196
|
+
rr, cc = draw.disk((yc, xc), r)
|
|
197
|
+
mask2[rr, cc] = 1
|
|
198
|
+
mask = mask1 | mask2
|
|
199
|
+
row_min = int(min(IROI1[1], IROI2[1] - r))
|
|
200
|
+
col_min = int(min(IROI1[0], IROI2[0] - r))
|
|
201
|
+
row_max = int(max(IROI1[1] + IROI1[3], IROI2[1] + r))
|
|
202
|
+
col_max = int(max(IROI1[0] + IROI1[2], IROI2[0] + r))
|
|
203
|
+
mask = mask[row_min:row_max, col_min:col_max]
|
|
204
|
+
|
|
205
|
+
assert np.all(im1.data[mask] != 0), nzroi
|
|
206
|
+
assert np.all(im1.data[~mask] == 0), zroi
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
@pytest.mark.validation
|
|
210
|
+
def test_image_extract_roi() -> None:
|
|
211
|
+
"""Validation test for image ROI extraction into multiple objects"""
|
|
212
|
+
src = __create_test_image()
|
|
213
|
+
src.roi = __create_test_roi()
|
|
214
|
+
context = f" [ROI: {__roi_str(src)}]"
|
|
215
|
+
nzroi = f"Non-zero values expected in ROI{context}"
|
|
216
|
+
roisham = f"ROI shape mismatch{context}"
|
|
217
|
+
|
|
218
|
+
images: list[sigima.objects.ImageObj] = []
|
|
219
|
+
for index, single_roi in enumerate(src.roi):
|
|
220
|
+
roiparam = single_roi.to_param(src, index)
|
|
221
|
+
image = sigima.proc.image.extract_roi(src, roiparam)
|
|
222
|
+
images.append(image)
|
|
223
|
+
assert len(images) == 3, f"Three images expected{context}"
|
|
224
|
+
im1, im2 = images[:2] # pylint: disable=unbalanced-tuple-unpacking
|
|
225
|
+
assert np.all(im1.data != 0), nzroi
|
|
226
|
+
assert im1.data.shape == (IROI1[3], IROI1[2]), roisham
|
|
227
|
+
assert np.all(im2.data != 0), nzroi
|
|
228
|
+
assert im2.data.shape == (IROI2[2] * 2, IROI2[2] * 2), roisham
|
|
229
|
+
mask2 = np.zeros(shape=im2.data.shape, dtype=bool)
|
|
230
|
+
xc = yc = r = IROI2[2] # Adjust for ROI origin
|
|
231
|
+
rr, cc = draw.disk((yc, xc), r, shape=im2.data.shape)
|
|
232
|
+
mask2[rr, cc] = 1
|
|
233
|
+
assert np.all(im2.maskdata == ~mask2), f"Mask data mismatch{context}"
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def test_roi_coordinates_validation() -> None:
|
|
237
|
+
"""Test ROI coordinates validation"""
|
|
238
|
+
# Create a 20x20 Gaussian image
|
|
239
|
+
param = sigima.objects.Gauss2DParam.create(a=10.0, height=20, width=20)
|
|
240
|
+
src = sigima.objects.create_image_from_param(param)
|
|
241
|
+
|
|
242
|
+
# Create ROI coordinates
|
|
243
|
+
rect_coords = np.array([4.5, 4.5, 10.0, 10.0])
|
|
244
|
+
circ_coords = np.array([9.5, 9.5, 5.0])
|
|
245
|
+
poly_coords = np.array([5.1, 15.1, 14.7, 12.0, 12.5, 7.0, 5.2, 4.9])
|
|
246
|
+
|
|
247
|
+
# Create ROIs
|
|
248
|
+
rect_roi = sigima.objects.create_image_roi(
|
|
249
|
+
"rectangle", rect_coords, title="rectangular"
|
|
250
|
+
)
|
|
251
|
+
circ_roi = sigima.objects.create_image_roi("circle", circ_coords, title="circular")
|
|
252
|
+
poly_roi = sigima.objects.create_image_roi(
|
|
253
|
+
"polygon", poly_coords, title="polygonal"
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
# Check that coordinates are correct
|
|
257
|
+
assert np.all(rect_roi.get_single_roi(0).get_physical_coords(src) == rect_coords)
|
|
258
|
+
assert np.all(circ_roi.get_single_roi(0).get_physical_coords(src) == circ_coords)
|
|
259
|
+
assert np.all(poly_roi.get_single_roi(0).get_physical_coords(src) == poly_coords)
|
|
260
|
+
|
|
261
|
+
# Check that extracted images have correct data
|
|
262
|
+
for roi in (rect_roi, circ_roi, poly_roi):
|
|
263
|
+
extracted = sigima.proc.image.extract_roi(src, roi.to_params(src)[0])
|
|
264
|
+
assert np.all(extracted.data != 0), "Extracted image should have non-zero data"
|
|
265
|
+
assert extracted.data.shape == (10, 10), "Extracted image shape mismatch"
|
|
266
|
+
|
|
267
|
+
# Display the original image and the ROIs
|
|
268
|
+
if guiutils.is_gui_enabled():
|
|
269
|
+
images = [src]
|
|
270
|
+
titles = ["Original Image"]
|
|
271
|
+
for inverse in (False, True):
|
|
272
|
+
for roi in (rect_roi, circ_roi, poly_roi):
|
|
273
|
+
src2 = src.copy()
|
|
274
|
+
roi.get_single_roi(0).inverse = inverse
|
|
275
|
+
src2.roi = roi
|
|
276
|
+
images.append(src2)
|
|
277
|
+
roi_title = roi.get_single_roi(0).title
|
|
278
|
+
mask_str = "mask inside" if inverse else "mask outside"
|
|
279
|
+
titles.append(f"Image with {roi_title} ROI ({mask_str})")
|
|
280
|
+
guiutils.view_images_side_by_side_if_gui(
|
|
281
|
+
images, titles, rows=2, title="Image ROIs"
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def test_create_image_roi_inverse_parameter() -> None:
|
|
286
|
+
"""Test create_image_roi function with inverse parameter functionality"""
|
|
287
|
+
# Test 1: Single ROI with inverse=True (mask inside)
|
|
288
|
+
roi1 = sigima.objects.create_image_roi("rectangle", [10, 20, 30, 40], inverse=True)
|
|
289
|
+
assert len(roi1) == 1, "Should create one ROI"
|
|
290
|
+
assert roi1.single_rois[0].inverse is True, "ROI should have inverse=True"
|
|
291
|
+
|
|
292
|
+
# Test 2: Single ROI with inverse=False (default)
|
|
293
|
+
roi2 = sigima.objects.create_image_roi("rectangle", [10, 20, 30, 40])
|
|
294
|
+
assert roi2.single_rois[0].inverse is False, "Default should be False"
|
|
295
|
+
|
|
296
|
+
# Test 3: Multiple ROIs with global inverse parameter
|
|
297
|
+
coords = [[10, 20, 30, 40], [50, 60, 70, 80]]
|
|
298
|
+
roi3 = sigima.objects.create_image_roi("rectangle", coords, inverse=True)
|
|
299
|
+
assert len(roi3) == 2, "Should create two ROIs"
|
|
300
|
+
assert all(single_roi.inverse is True for single_roi in roi3.single_rois), (
|
|
301
|
+
"All ROIs should have inverse=True (internal representation)"
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
# Test 4: Multiple ROIs with individual inverse parameters
|
|
305
|
+
inverse_values = [True, False] # mask inside, then mask outside
|
|
306
|
+
roi4 = sigima.objects.create_image_roi("rectangle", coords, inverse=inverse_values)
|
|
307
|
+
assert len(roi4) == 2, "Should create two ROIs"
|
|
308
|
+
assert roi4.single_rois[0].inverse is True, "First ROI should be True"
|
|
309
|
+
assert roi4.single_rois[1].inverse is False, "Second ROI should be False"
|
|
310
|
+
|
|
311
|
+
# Test 5: Circle ROIs with mixed inverse parameters
|
|
312
|
+
circle_coords = [[50, 50, 25], [150, 150, 30]]
|
|
313
|
+
roi5 = sigima.objects.create_image_roi(
|
|
314
|
+
"circle",
|
|
315
|
+
circle_coords,
|
|
316
|
+
inverse=[False, True], # mask outside, then mask inside
|
|
317
|
+
)
|
|
318
|
+
assert len(roi5) == 2, "Should create two circle ROIs"
|
|
319
|
+
assert roi5.single_rois[0].inverse is False, "First circle should be False"
|
|
320
|
+
assert roi5.single_rois[1].inverse is True, "Second circle should be True"
|
|
321
|
+
|
|
322
|
+
# Test 6: Polygon ROIs with varying vertex counts and mixed inverse parameters
|
|
323
|
+
polygon_coords = [
|
|
324
|
+
[0, 0, 10, 0, 5, 8], # Triangle (3 vertices)
|
|
325
|
+
[20, 20, 30, 20, 30, 30, 20, 30], # Rectangle (4 vertices)
|
|
326
|
+
]
|
|
327
|
+
roi6 = sigima.objects.create_image_roi(
|
|
328
|
+
"polygon",
|
|
329
|
+
polygon_coords,
|
|
330
|
+
inverse=[True, False], # mask inside, then mask outside
|
|
331
|
+
)
|
|
332
|
+
assert len(roi6) == 2, "Should create two polygon ROIs"
|
|
333
|
+
assert roi6.single_rois[0].inverse is True, "Triangle should be True"
|
|
334
|
+
assert roi6.single_rois[1].inverse is False, "Rectangle should be False"
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def test_create_image_roi_inverse_parameter_errors() -> None:
|
|
338
|
+
"""Test error handling for inverse parameter in create_image_roi"""
|
|
339
|
+
# Test error when inverse parameter count doesn't match ROI count
|
|
340
|
+
coords = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]]
|
|
341
|
+
# Only 2 values for 3 ROIs
|
|
342
|
+
inverse_params = [
|
|
343
|
+
True, # mask inside
|
|
344
|
+
False, # mask outside
|
|
345
|
+
]
|
|
346
|
+
|
|
347
|
+
with pytest.raises(
|
|
348
|
+
ValueError,
|
|
349
|
+
match=r"Number of inverse values \(2\) must match number of ROIs \(3\)",
|
|
350
|
+
):
|
|
351
|
+
sigima.objects.create_image_roi("rectangle", coords, inverse=inverse_params)
|
|
352
|
+
|
|
353
|
+
# Test with too many inverse values
|
|
354
|
+
# 4 values for 3 ROIs
|
|
355
|
+
inverse_params_too_many = [
|
|
356
|
+
True, # mask inside
|
|
357
|
+
False, # mask outside
|
|
358
|
+
True, # mask inside
|
|
359
|
+
False, # mask outside
|
|
360
|
+
]
|
|
361
|
+
with pytest.raises(
|
|
362
|
+
ValueError,
|
|
363
|
+
match=r"Number of inverse values \(4\) must match number of ROIs \(3\)",
|
|
364
|
+
):
|
|
365
|
+
sigima.objects.create_image_roi(
|
|
366
|
+
"rectangle", coords, inverse=inverse_params_too_many
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def test_roi_inverse_affects_mask_generation() -> None:
|
|
371
|
+
"""Test that inverse parameter affects mask generation correctly"""
|
|
372
|
+
# Create a test image
|
|
373
|
+
img = __create_test_image()
|
|
374
|
+
|
|
375
|
+
# Test rectangle ROI with inverse=True vs inverse=False
|
|
376
|
+
rect_coords = [75, 75, 50, 50] # Rectangle that should be inside image bounds
|
|
377
|
+
|
|
378
|
+
# ROI with inverse=True (mask is True inside the rectangle)
|
|
379
|
+
roi_inside = sigima.objects.create_image_roi("rectangle", rect_coords, inverse=True)
|
|
380
|
+
mask_inside = roi_inside.to_mask(img)
|
|
381
|
+
|
|
382
|
+
# ROI with inverse=False (mask is True outside the rectangle)
|
|
383
|
+
roi_outside = sigima.objects.create_image_roi(
|
|
384
|
+
"rectangle", rect_coords, inverse=False
|
|
385
|
+
)
|
|
386
|
+
mask_outside = roi_outside.to_mask(img)
|
|
387
|
+
|
|
388
|
+
# The two masks should be inverse of each other
|
|
389
|
+
assert np.array_equal(mask_inside, ~mask_outside), (
|
|
390
|
+
"Inside and outside masks should be inverse of each other"
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
# Check that inside mask has True values inside the rectangle region
|
|
394
|
+
# For a rectangle [x0, y0, dx, dy], the region is [x0:x0+dx, y0:y0+dy]
|
|
395
|
+
x0, y0, dx, dy = rect_coords
|
|
396
|
+
expected_inside_region = np.zeros_like(img.data, dtype=bool)
|
|
397
|
+
expected_inside_region[y0 : y0 + dy, x0 : x0 + dx] = True
|
|
398
|
+
|
|
399
|
+
assert np.array_equal(mask_inside, expected_inside_region), (
|
|
400
|
+
"Inside mask should match expected rectangular region"
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def test_roi_inverse_serialization() -> None:
|
|
405
|
+
"""Test that inverse parameter is preserved during
|
|
406
|
+
serialization/deserialization"""
|
|
407
|
+
# Create ROIs with mixed inverse parameters
|
|
408
|
+
coords = [[10, 20, 30, 40], [50, 60, 70, 80]]
|
|
409
|
+
inverse_params = [
|
|
410
|
+
True, # mask inside
|
|
411
|
+
False, # mask outside
|
|
412
|
+
]
|
|
413
|
+
original_roi = sigima.objects.create_image_roi(
|
|
414
|
+
"rectangle", coords, inverse=inverse_params
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
# Serialize to dictionary
|
|
418
|
+
roi_dict = original_roi.to_dict()
|
|
419
|
+
|
|
420
|
+
# Deserialize from dictionary
|
|
421
|
+
restored_roi = sigima.objects.ImageROI.from_dict(roi_dict)
|
|
422
|
+
|
|
423
|
+
# Check that inverse parameters are preserved
|
|
424
|
+
assert len(restored_roi) == len(original_roi), "ROI count should be preserved"
|
|
425
|
+
for i in range(len(original_roi)):
|
|
426
|
+
original_inverse = original_roi.single_rois[i].inverse
|
|
427
|
+
restored_inverse = restored_roi.single_rois[i].inverse
|
|
428
|
+
assert original_inverse == restored_inverse, (
|
|
429
|
+
f"inverse parameter for ROI {i} should be preserved "
|
|
430
|
+
f"(expected {original_inverse}, got {restored_inverse})"
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def test_roi_inverse_parameter_conversion() -> None:
|
|
435
|
+
"""Test that inverse parameter works correctly with parameter conversion"""
|
|
436
|
+
img = __create_test_image()
|
|
437
|
+
|
|
438
|
+
# Create ROI with inverse=True (mask inside)
|
|
439
|
+
roi = sigima.objects.create_image_roi("rectangle", [50, 50, 40, 40], inverse=True)
|
|
440
|
+
|
|
441
|
+
# Convert to parameters
|
|
442
|
+
params = roi.to_params(img)
|
|
443
|
+
assert len(params) == 1, "Should create one parameter"
|
|
444
|
+
|
|
445
|
+
# Check that inverse parameter is preserved in the parameter
|
|
446
|
+
param = params[0]
|
|
447
|
+
assert hasattr(param, "inverse"), "Parameter should have inverse attribute"
|
|
448
|
+
assert param.inverse is True, "Parameter should preserve inverse=True"
|
|
449
|
+
|
|
450
|
+
# Create ROI from parameter and check inverse is preserved
|
|
451
|
+
new_roi = sigima.objects.ImageROI.from_params(img, params)
|
|
452
|
+
assert len(new_roi) == 1, "Should recreate one ROI"
|
|
453
|
+
assert new_roi.single_rois[0].inverse is True, (
|
|
454
|
+
"Recreated ROI should have inverse=True (internal representation)"
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def test_multiple_rois_inverse_true() -> None:
|
|
459
|
+
"""Test multiple ROIs with inverse=True on distinct areas
|
|
460
|
+
|
|
461
|
+
This test checks that when multiple ROIs are defined with inverse=True
|
|
462
|
+
on distinct areas of an image, the resulting mask should have True values
|
|
463
|
+
in BOTH ROI areas, not just their intersection.
|
|
464
|
+
"""
|
|
465
|
+
# Create a test image
|
|
466
|
+
img = __create_test_image()
|
|
467
|
+
|
|
468
|
+
# Define two rectangular ROIs on distinct areas of the image
|
|
469
|
+
# ROI 1: top-left area
|
|
470
|
+
roi1_coords = [30, 30, 40, 40] # x, y, width, height
|
|
471
|
+
# ROI 2: bottom-right area (distinct from ROI 1)
|
|
472
|
+
roi2_coords = [130, 130, 40, 40] # x, y, width, height
|
|
473
|
+
|
|
474
|
+
# Create ROI with inverse=True for both rectangles
|
|
475
|
+
roi = sigima.objects.create_image_roi(
|
|
476
|
+
"rectangle", [roi1_coords, roi2_coords], inverse=True
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
# Generate the mask
|
|
480
|
+
mask = roi.to_mask(img)
|
|
481
|
+
|
|
482
|
+
# Expected behavior: mask should be True in BOTH rectangular areas
|
|
483
|
+
# Create expected mask manually
|
|
484
|
+
expected_mask = np.zeros_like(img.data, dtype=bool)
|
|
485
|
+
|
|
486
|
+
# ROI 1 area
|
|
487
|
+
x1, y1, w1, h1 = roi1_coords
|
|
488
|
+
expected_mask[y1 : y1 + h1, x1 : x1 + w1] = True
|
|
489
|
+
|
|
490
|
+
# ROI 2 area
|
|
491
|
+
x2, y2, w2, h2 = roi2_coords
|
|
492
|
+
expected_mask[y2 : y2 + h2, x2 : x2 + w2] = True
|
|
493
|
+
|
|
494
|
+
# Check that the mask has True values in both ROI areas
|
|
495
|
+
assert np.any(mask[y1 : y1 + h1, x1 : x1 + w1]), (
|
|
496
|
+
"Mask should have True values in first ROI area"
|
|
497
|
+
)
|
|
498
|
+
assert np.any(mask[y2 : y2 + h2, x2 : x2 + w2]), (
|
|
499
|
+
"Mask should have True values in second ROI area"
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
# Check that the mask matches our expected mask
|
|
503
|
+
assert np.array_equal(mask, expected_mask), (
|
|
504
|
+
"Mask should have True values in both ROI areas and False elsewhere"
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
# Verify that the two ROI areas don't overlap (test integrity)
|
|
508
|
+
roi1_mask = np.zeros_like(img.data, dtype=bool)
|
|
509
|
+
roi1_mask[y1 : y1 + h1, x1 : x1 + w1] = True
|
|
510
|
+
roi2_mask = np.zeros_like(img.data, dtype=bool)
|
|
511
|
+
roi2_mask[y2 : y2 + h2, x2 : x2 + w2] = True
|
|
512
|
+
assert not np.any(roi1_mask & roi2_mask), (
|
|
513
|
+
"Test integrity: ROI areas should not overlap"
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
def test_multiple_rois_mixed_inverse() -> None:
|
|
518
|
+
"""Test multiple ROIs with mixed inverse values
|
|
519
|
+
|
|
520
|
+
This test checks that when ROIs have mixed inverse values,
|
|
521
|
+
the combination logic works correctly.
|
|
522
|
+
"""
|
|
523
|
+
# Create a test image
|
|
524
|
+
img = __create_test_image()
|
|
525
|
+
|
|
526
|
+
# Define three rectangular ROIs
|
|
527
|
+
# ROI 1: top-left area (inverse=True - include this area)
|
|
528
|
+
roi1_coords = [30, 30, 40, 40] # x, y, width, height
|
|
529
|
+
# ROI 2: top-right area (inverse=False - exclude this area)
|
|
530
|
+
roi2_coords = [130, 30, 40, 40] # x, y, width, height
|
|
531
|
+
# ROI 3: bottom-left area (inverse=True - include this area)
|
|
532
|
+
roi3_coords = [30, 130, 40, 40] # x, y, width, height
|
|
533
|
+
|
|
534
|
+
# Create ROI with mixed inverse values
|
|
535
|
+
roi = sigima.objects.create_image_roi(
|
|
536
|
+
"rectangle",
|
|
537
|
+
[roi1_coords, roi2_coords, roi3_coords],
|
|
538
|
+
inverse=[True, False, True], # include, exclude, include
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
# Generate the mask
|
|
542
|
+
mask = roi.to_mask(img)
|
|
543
|
+
|
|
544
|
+
# Expected behavior:
|
|
545
|
+
# - ROI 1 area should have True values (inverse=True)
|
|
546
|
+
# - ROI 2 area should have False values (inverse=False)
|
|
547
|
+
# - ROI 3 area should have True values (inverse=True)
|
|
548
|
+
# - Areas outside all ROIs should have True values (due to ROI 2 being
|
|
549
|
+
# inverse=False)
|
|
550
|
+
|
|
551
|
+
x1, y1, w1, h1 = roi1_coords
|
|
552
|
+
x2, y2, w2, h2 = roi2_coords
|
|
553
|
+
x3, y3, w3, h3 = roi3_coords
|
|
554
|
+
|
|
555
|
+
# Check that ROI 1 and ROI 3 areas have True values (inverse=True)
|
|
556
|
+
assert np.all(mask[y1 : y1 + h1, x1 : x1 + w1]), (
|
|
557
|
+
"ROI 1 area should have True values (inverse=True)"
|
|
558
|
+
)
|
|
559
|
+
assert np.all(mask[y3 : y3 + h3, x3 : x3 + w3]), (
|
|
560
|
+
"ROI 3 area should have True values (inverse=True)"
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
# Check that ROI 2 area has False values (inverse=False)
|
|
564
|
+
assert not np.any(mask[y2 : y2 + h2, x2 : x2 + w2]), (
|
|
565
|
+
"ROI 2 area should have False values (inverse=False)"
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
# Check that areas outside all ROIs have True values (due to ROI 2)
|
|
569
|
+
# For example, the bottom-right corner should be True
|
|
570
|
+
assert np.all(mask[170:190, 170:190]), (
|
|
571
|
+
"Areas outside all ROIs should have True values"
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
if __name__ == "__main__":
|
|
576
|
+
guiutils.enable_gui()
|
|
577
|
+
test_roi_coordinates_validation()
|
|
578
|
+
# test_image_roi_merge()
|
|
579
|
+
# test_image_roi_combine()
|
|
580
|
+
# test_image_roi_processing()
|
|
581
|
+
# test_empty_image_roi()
|
|
582
|
+
# test_image_extract_rois()
|
|
583
|
+
# test_image_extract_roi()
|
|
584
|
+
# test_create_image_roi_inside_parameter()
|
|
585
|
+
# test_create_image_roi_inside_parameter_errors()
|
|
586
|
+
# test_roi_inverse()
|
|
587
|
+
# test_roi_inside_serialization()
|
|
588
|
+
# test_roi_inside_parameter_conversion()
|