cellprofiler-library-nightly 5.0.0.dev475__tar.gz → 5.0.0.dev496__tar.gz

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.
Files changed (91) hide show
  1. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/PKG-INFO +1 -1
  2. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/_version.py +3 -3
  3. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/functions/image_processing.py +135 -4
  4. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/functions/measurement.py +704 -4
  5. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/functions/object_processing.py +24 -4
  6. cellprofiler_library_nightly-5.0.0.dev496/cellprofiler_library/modules/_measurecolocalization.py +392 -0
  7. cellprofiler_library_nightly-5.0.0.dev496/cellprofiler_library/modules/_smooth.py +44 -0
  8. cellprofiler_library_nightly-5.0.0.dev496/cellprofiler_library/opts/measurecolocalization.py +32 -0
  9. cellprofiler_library_nightly-5.0.0.dev496/cellprofiler_library/opts/smooth.py +9 -0
  10. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/types.py +4 -3
  11. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library_nightly.egg-info/PKG-INFO +1 -1
  12. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library_nightly.egg-info/SOURCES.txt +4 -0
  13. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/LICENSE +0 -0
  14. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/README.md +0 -0
  15. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/__init__.py +0 -0
  16. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/functions/__init__.py +0 -0
  17. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/functions/file_processing.py +0 -0
  18. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/functions/segmentation.py +0 -0
  19. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/__init__.py +0 -0
  20. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_closing.py +0 -0
  21. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_colortogray.py +0 -0
  22. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_combineobjects.py +0 -0
  23. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_convertimagetoobjects.py +0 -0
  24. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_convertobjectstoimage.py +0 -0
  25. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_correctilluminationapply.py +0 -0
  26. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_crop.py +0 -0
  27. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_dilateimage.py +0 -0
  28. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_dilateobjects.py +0 -0
  29. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_enhanceedges.py +0 -0
  30. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_enhanceorsuppressfeatures.py +0 -0
  31. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_erodeimage.py +0 -0
  32. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_erodeobjects.py +0 -0
  33. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_expandorshrinkobjects.py +0 -0
  34. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_fillobjects.py +0 -0
  35. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_flipandrotate.py +0 -0
  36. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_gaussianfilter.py +0 -0
  37. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_graytocolor.py +0 -0
  38. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_identifyprimaryobjects.py +0 -0
  39. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_identifysecondaryobjects.py +0 -0
  40. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_identifytertiaryobjects.py +0 -0
  41. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_imagemath.py +0 -0
  42. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_measureimageoverlap.py +0 -0
  43. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_measureobjectsizeshape.py +0 -0
  44. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_medialaxis.py +0 -0
  45. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_medianfilter.py +0 -0
  46. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_morph.py +0 -0
  47. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_morphologicalskeleton.py +0 -0
  48. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_opening.py +0 -0
  49. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_overlayobjects.py +0 -0
  50. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_overlayoutlines.py +0 -0
  51. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_reducenoise.py +0 -0
  52. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_removeholes.py +0 -0
  53. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_resize.py +0 -0
  54. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_savecroppedobjects.py +0 -0
  55. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_shrinktoobjectcenters.py +0 -0
  56. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_threshold.py +0 -0
  57. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/modules/_watershed.py +0 -0
  58. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/__init__.py +0 -0
  59. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/colortogray.py +0 -0
  60. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/convertimagetoobjects.py +0 -0
  61. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/convertobjectstoimage.py +0 -0
  62. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/correctilluminationapply.py +0 -0
  63. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/crop.py +0 -0
  64. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/dilateimage.py +0 -0
  65. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/dilateobjects.py +0 -0
  66. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/enhanceedges.py +0 -0
  67. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/enhanceorsuppressfeatures.py +0 -0
  68. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/erodeimage.py +0 -0
  69. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/erodeobjects.py +0 -0
  70. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/flipandrotate.py +0 -0
  71. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/graytocolor.py +0 -0
  72. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/identifyprimaryobjects.py +0 -0
  73. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/identifysecondaryobjects.py +0 -0
  74. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/identifytertiaryobjects.py +0 -0
  75. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/imagemath.py +0 -0
  76. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/measureimageoverlap.py +0 -0
  77. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/morph.py +0 -0
  78. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/objectsizeshapefeatures.py +0 -0
  79. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/overlayoutlines.py +0 -0
  80. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/removeholes.py +0 -0
  81. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/resize.py +0 -0
  82. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/shrinktoobjectcenters.py +0 -0
  83. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/structuring_elements.py +0 -0
  84. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/opts/threshold.py +0 -0
  85. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library/py.typed +0 -0
  86. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library_nightly.egg-info/dependency_links.txt +0 -0
  87. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library_nightly.egg-info/requires.txt +0 -0
  88. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/cellprofiler_library_nightly.egg-info/top_level.txt +0 -0
  89. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/environment.yml +0 -0
  90. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/pyproject.toml +0 -0
  91. {cellprofiler_library_nightly-5.0.0.dev475 → cellprofiler_library_nightly-5.0.0.dev496}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cellprofiler-library-nightly
3
- Version: 5.0.0.dev475
3
+ Version: 5.0.0.dev496
4
4
  Summary: cellprofiler-library implements CellProfiler's image processing and mathematical code, and is usable as a standalone library
5
5
  Author: Anne Carpenter, Thouis (Ray) Jones, Lee Kamentsky, Vebjorn Ljosa, David Logan, Mark Bray, Madison Swain-Bowden, Allen Goodman, Claire McQuinn, Alice Lucas, Callum Tromans-Coia
6
6
  Author-email: Beth Cimini <bcimini@broadinstitute.org>, David Stirling <dstirling@glencoesoftware.com>, Nodar Gogoberidze <ngogober@broadinstitute.org>
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '5.0.0.dev475'
32
- __version_tuple__ = version_tuple = (5, 0, 0, 'dev475')
31
+ __version__ = version = '5.0.0.dev496'
32
+ __version_tuple__ = version_tuple = (5, 0, 0, 'dev496')
33
33
 
34
- __commit_id__ = commit_id = 'gc4686c345'
34
+ __commit_id__ = commit_id = 'g8d098d2ce'
@@ -15,9 +15,19 @@ import scipy
15
15
  import scipy.interpolate
16
16
  import matplotlib
17
17
  import math
18
+ from centrosome.cpmorphology import fixup_scipy_ndimage_result as fix
18
19
  from typing import Any, Optional, Tuple, Callable, Union, List, cast, Dict, TypeVar
19
20
  from numpy.typing import NDArray
20
- from cellprofiler_library.types import ImageGrayscale, ImageGrayscaleMask, Image2DColor, Image2DGrayscale, Image2DGrayscaleMask, ImageAny, ImageAnyMask, ObjectSegmentation, Image2D, Image2DMask, StructuringElement, ObjectLabelSet, ImageColor, ImageBinaryMask
21
+ from skimage.restoration import denoise_bilateral
22
+ from cellprofiler_library.types import (
23
+ ImageGrayscale, ImageGrayscaleMask,
24
+ Image2DColor,
25
+ Image2DGrayscale, Image2DGrayscaleMask,
26
+ ImageAny, ImageAnyMask,
27
+ ObjectSegmentation,
28
+ Image2D, Image2DMask,
29
+ StructuringElement, ObjectLabelSet, ImageColor, ImageBinaryMask,
30
+ )
21
31
  from cellprofiler_library.opts import threshold as Threshold
22
32
  from cellprofiler_library.opts.enhanceorsuppressfeatures import SpeckleAccuracy, NeuriteMethod
23
33
  from cellprofiler_library.opts.overlayoutlines import BrightnessMode
@@ -27,6 +37,7 @@ from cellprofiler_library.opts.resize import ResizingMethod, DimensionMethod, In
27
37
  from cellprofiler_library.opts.imagemath import Operator
28
38
  from cellprofiler_library.opts.flipandrotate import RotationCoordinateAlignmnet
29
39
  from cellprofiler_library.opts.enhanceedges import EdgeDirection
40
+
30
41
  invert = cast(Callable[[ImageAny], ImageAny], _invert)
31
42
  isscalar = cast(Callable[[Optional[ImageAny]], bool], _isscalar)
32
43
 
@@ -716,6 +727,7 @@ def get_global_threshold(
716
727
  threshold_correction_factor: float = 1,
717
728
  assign_middle_to_foreground: Threshold.Assignment = Threshold.Assignment.FOREGROUND,
718
729
  log_transform: bool = False,
730
+ max_intensity_percentage: float = 100,
719
731
  **kwargs: Any,
720
732
  ) -> float:
721
733
  conversion_dict = None
@@ -745,6 +757,8 @@ def get_global_threshold(
745
757
  kwargs["nbins"] = kwargs.get("nbins", 128)
746
758
  threshold = skimage.filters.threshold_multiotsu(image, **kwargs)
747
759
  threshold = threshold[bin_wanted]
760
+ elif threshold_method.casefold() == Threshold.Method.MAX_INTENSITY_PERCENTAGE:
761
+ threshold = max_intensity_percentage * numpy.max(image) / 100
748
762
  else:
749
763
  raise NotImplementedError(f"Threshold method {threshold_method} not supported.")
750
764
 
@@ -976,10 +990,10 @@ def get_rectangle_cropping(
976
990
 
977
991
 
978
992
  def crop_image(
979
- image: Union[Image2D, Image2DMask],
980
- crop_mask: Image2DMask,
993
+ image: Union[ImageAny, ImageAnyMask],
994
+ crop_mask: ImageAnyMask,
981
995
  crop_internal: Optional[bool]=False
982
- ) -> Union[Image2D, Image2DMask]:
996
+ ) -> Union[ImageAny, ImageAnyMask]:
983
997
  """Crop an image to the size of the nonzero portion of a crop mask"""
984
998
  i_histogram = crop_mask.sum(axis=1)
985
999
  i_cumsum = numpy.cumsum(i_histogram != 0)
@@ -2068,3 +2082,120 @@ def rotate_image_coordinates(pixel_data: Image2D, mask: Image2DMask, rotate_poin
2068
2082
  "Unknown axis: %s" % rotate_coordinate_alignment.value
2069
2083
  )
2070
2084
  return angle
2085
+
2086
+
2087
+ ###############################################################################
2088
+ # Smoothing
2089
+ ###############################################################################
2090
+
2091
+ def smoothing_gaussian(pixel_data: Image2D, mask: Optional[Image2DMask], sigma: float) -> Image2D:
2092
+ def fn(image: Image2D) -> Image2D:
2093
+ return scipy.ndimage.gaussian_filter(
2094
+ image, sigma, mode="constant", cval=0
2095
+ )
2096
+ return centrosome.smooth.smooth_with_function_and_mask(pixel_data, fn, mask)
2097
+
2098
+
2099
+ def smoothing_median(pixel_data: Image2D, mask: Optional[Image2DMask], obj_size: float) -> Image2D:
2100
+ return centrosome.filter.median_filter(pixel_data, mask, obj_size / 2 + 1)
2101
+
2102
+
2103
+ def smoothing_keeping_edges(pixel_data: Image2D, multichannel: bool, sigma_range: float, sigma: float) -> Image2D:
2104
+ assert sigma_range is not None, "sigma_range must be provided for smooth_keeping_edges"
2105
+ return denoise_bilateral(
2106
+ image=pixel_data.astype(float),
2107
+ channel_axis=2 if multichannel else None,
2108
+ sigma_color=sigma_range,
2109
+ sigma_spatial=sigma,
2110
+ )
2111
+
2112
+
2113
+ def smoothing_fit_polynomial(pixel_data: Image2D, mask: Optional[Image2DMask], clip: bool) -> Image2D:
2114
+ return centrosome.smooth.fit_polynomial(pixel_data, mask, clip)
2115
+
2116
+
2117
+ def smoothing_circular_average(pixel_data: Image2D, mask: Optional[Image2DMask], obj_size: float) -> Image2D:
2118
+ return centrosome.filter.circular_average_filter(pixel_data, obj_size / 2 + 1, mask)
2119
+
2120
+ def smoothing_smooth_to_average(pixel_data: Image2D, mask: Optional[Image2DMask]) -> Image2D:
2121
+ if mask is not None:
2122
+ mean = numpy.mean(pixel_data[mask])
2123
+ else:
2124
+ mean = numpy.mean(pixel_data)
2125
+ return numpy.ones(pixel_data.shape, pixel_data.dtype) * mean
2126
+
2127
+
2128
+ ################################################################################
2129
+ # MeasureColocalization
2130
+ ################################################################################
2131
+
2132
+ def crop_image_similarly(
2133
+ this_image: ImageAny,
2134
+ other_image: ImageAny,
2135
+ this_crop_mask: Optional[ImageAny] = None,
2136
+ ):
2137
+ """Crop a 2-d or 3-d image (other_image) using this image's crop mask
2138
+ crop mask is the binary image used to crop the parent image to the
2139
+ dimensions of the child (this) image. The crop_mask is the same size as
2140
+ the parent image.
2141
+ image - a np.ndarray to be cropped (of any type)
2142
+ """
2143
+ if other_image.shape[:2] == this_image.shape[:2]:
2144
+ # Same size - no cropping needed
2145
+ return other_image
2146
+ if any(
2147
+ [
2148
+ my_size > other_size
2149
+ for my_size, other_size in zip(this_image.shape, other_image.shape)
2150
+ ]
2151
+ ):
2152
+ raise ValueError(
2153
+ "Image to be cropped is smaller: %s vs %s"
2154
+ % (repr(other_image.shape), repr(this_image.shape))
2155
+ )
2156
+ if this_crop_mask is None:
2157
+ raise RuntimeError(
2158
+ "Images are of different size and no crop mask available.\n"
2159
+ "Use the Crop and Align modules to match images of different sizes."
2160
+ )
2161
+ cropped_image = crop_image(other_image, this_crop_mask)
2162
+ if cropped_image.shape[0:2] != this_image.shape[0:2]:
2163
+ raise ValueError(
2164
+ "Cropped image is not the same size as the reference image: %s vs %s"
2165
+ % (repr(cropped_image.shape), repr(this_image.shape))
2166
+ )
2167
+ return cropped_image
2168
+
2169
+ def apply_threshold_to_objects(
2170
+ image: ImageGrayscale,
2171
+ segmented: ObjectSegmentation,
2172
+ threshold_value: float,
2173
+ mask: Optional[ImageGrayscaleMask] = None,
2174
+ ) -> ImageGrayscaleMask:
2175
+ output_image_arr = numpy.zeros_like(image)
2176
+ if mask is None:
2177
+ # Create a fake mask if one isn't provided
2178
+ mask = numpy.full(segmented.shape, True)
2179
+ assert (image.shape == segmented.shape)
2180
+ mask = (segmented > 0) & mask & (~numpy.isnan(image))
2181
+ segmented = segmented.copy()
2182
+ segmented = segmented[mask]
2183
+ n_objects = len(numpy.unique(segmented))
2184
+ if (not (n_objects == 0)) and (not (numpy.where(mask)[0].__len__() == 0)):
2185
+ #
2186
+ # First get the maximum intensity of each object and create
2187
+ # a 1d array of floats representing the threshold for each object
2188
+ #
2189
+ lrange = numpy.arange(n_objects, dtype=numpy.int32) + 1
2190
+ # Threshold as percentage of maximum intensity of objects in each channel
2191
+ scaled_image = (threshold_value / 100) * fix(
2192
+ scipy.ndimage.maximum(image, segmented, lrange)
2193
+ )
2194
+
2195
+ #
2196
+ # Apply the threshold to the image
2197
+ # Use the mask to apply to specific pixels
2198
+ #
2199
+ output_image_arr[mask] = (image >= scaled_image[segmented - 1])
2200
+
2201
+ return output_image_arr