mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Add image_has_intensities and tests
This commit is contained in:
@@ -2,7 +2,8 @@ import io
|
||||
from typing import (
|
||||
Optional,
|
||||
Union,
|
||||
List
|
||||
List,
|
||||
Tuple,
|
||||
)
|
||||
|
||||
import numpy
|
||||
@@ -32,6 +33,53 @@ def assert_image_has_metadata(
|
||||
f"Image has wrong number of channels: {actual_channels} (expected {int(channels)})"
|
||||
|
||||
|
||||
def _compute_center_of_mass(im_arr):
|
||||
while im_arr.ndim > 2:
|
||||
im_arr = im_arr.sum(axis=2)
|
||||
im_arr = numpy.abs(im_arr)
|
||||
if im_arr.sum() == 0:
|
||||
return (numpy.nan, numpy.nan)
|
||||
im_arr = im_arr / im_arr.sum()
|
||||
yy, xx = numpy.indices(im_arr.shape)
|
||||
return (im_arr * xx).sum(), (im_arr * yy).sum()
|
||||
|
||||
|
||||
def assert_image_has_intensities(
|
||||
output_bytes: bytes,
|
||||
channel: Optional[Union[int, str]] = None,
|
||||
mean_intensity: Optional[Union[float, str]] = None,
|
||||
center_of_mass: Optional[Union[Tuple[float], str]] = None,
|
||||
eps: Optional[Union[float, str]] = 1e-8,
|
||||
) -> None:
|
||||
"""
|
||||
Assert the image output has specific intensity content.
|
||||
"""
|
||||
buf = io.BytesIO(output_bytes)
|
||||
with Image.open(buf) as im:
|
||||
im_arr = numpy.array(im)
|
||||
|
||||
# Select the specified channel (if any).
|
||||
if channel is not None:
|
||||
im_arr = im_arr[:, :, channel]
|
||||
|
||||
# Perform `mean_intensity` assertion.
|
||||
if mean_intensity is not None:
|
||||
actual = im_arr.mean()
|
||||
expected = float(mean_intensity)
|
||||
assert abs(actual - expected) <= float(eps), \
|
||||
f"Wrong mean intensity: {actual} (expected {expected})"
|
||||
|
||||
# Perform `center_of_mass` assertion.
|
||||
if center_of_mass is not None:
|
||||
if isinstance(center_of_mass, str):
|
||||
center_of_mass = [float(c.strip()) for c in center_of_mass.split(",")]
|
||||
assert len(center_of_mass) == 2, "center_of_mass must have two components"
|
||||
actual = _compute_center_of_mass(im_arr)
|
||||
distance = numpy.linalg.norm(numpy.subtract(center_of_mass, expected))
|
||||
assert distance <= float(eps), \
|
||||
f"Wrong center of mass: {actual} (expected {center_of_mass})"
|
||||
|
||||
|
||||
def assert_image_has_labels(
|
||||
output_bytes: bytes,
|
||||
number_of_objects: Optional[Union[int, str]] = None,
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<param name="input" value="im1_uint8.tif" />
|
||||
<output name="output">
|
||||
<assert_contents>
|
||||
<image_has_metadata width="32" height="32" channels="1" />
|
||||
<image_has_metadata width="32" height="32" channels="1" center_of_mass="15.61, 15.48" eps="0.01" />
|
||||
</assert_contents>
|
||||
</output>
|
||||
</test>
|
||||
@@ -23,6 +23,25 @@
|
||||
<output name="output">
|
||||
<assert_contents>
|
||||
<image_has_metadata width="32" height="32" channels="3" />
|
||||
<image_has_intensities channel="0" mean_intensity="0.25" center_of_mass="7.5, 7.5" />
|
||||
<image_has_intensities channel="1" mean_intensity="0.0" />
|
||||
<image_has_intensities channel="2" mean_intensity="0" />
|
||||
</assert_contents>
|
||||
</output>
|
||||
</test>
|
||||
<test expect_test_failure="true">
|
||||
<param name="input" value="im3_a.png" />
|
||||
<output name="output">
|
||||
<assert_contents>
|
||||
<image_has_intensities channel="0" mean_intensity="0.24" />
|
||||
</assert_contents>
|
||||
</output>
|
||||
</test>
|
||||
<test>
|
||||
<param name="input" value="im3_a.png" />
|
||||
<output name="output">
|
||||
<assert_contents>
|
||||
<image_has_intensities channel="0" mean_intensity="0.24" eps="0.01" />
|
||||
</assert_contents>
|
||||
</output>
|
||||
</test>
|
||||
|
||||
Reference in New Issue
Block a user