Skip to content

Commit

Permalink
Merge pull request #61 from blowekamp/restore_clamp
Browse files Browse the repository at this point in the history
Restore clamping for the computed visual min max range.
  • Loading branch information
blowekamp authored May 30, 2023
2 parents 826f786 + 0c52d37 commit 92205d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pytools/workflow_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
def visual_min_max(
input_image: Union[Path, str],
mad_scale: float,
clamp: bool = True,
) -> Dict[str, int]:
"""Reads a path to an input_image file or a directory of zarr array to estimate minimum and maximum ranges to be
used for visualization of the data set in Neuroglancer.
Expand All @@ -43,6 +44,9 @@ def visual_min_max(
:param mad_scale: The scale factor for the robust median absolute deviation (MAD) about the median to produce the
"minimum and maximum range."
:param clamp: If True then the minimum and maximum range will be clamped to the computed floor and limit values.
:returns: The resulting dictionary will contain the following data elements with integer values as strings:
- "neuroglancerPrecomputedMin"
- "neuroglancerPrecomputedMax"
Expand Down Expand Up @@ -77,6 +81,10 @@ def visual_min_max(

floor_limit = weighted_quantile(mids, quantiles=[0.0, 1.0], sample_weight=h, values_sorted=True)

if clamp:
logger.debug(f"clamping min_max: {min_max} to floor_limit: {floor_limit}")
min_max = (max(min_max[0], floor_limit[0]), min(min_max[1], floor_limit[1]))

output = {
"neuroglancerPrecomputedMin": str(math.floor(min_max[0])),
"neuroglancerPrecomputedMax": str(math.ceil(min_max[1])),
Expand Down
18 changes: 10 additions & 8 deletions test/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,20 @@ def test_histogram_robust_stats():


@pytest.mark.parametrize(
"image_mrc,expected_min, expected_max, expected_floor, expected_limit",
"image_mrc,expected_min, expected_max, expected_floor, expected_limit, clamp",
[
(sitk.sitkUInt8, 0, 0, 0, 0),
(sitk.sitkUInt16, 0, 0, 0, 0),
("uint16_uniform", 8191, 57344, 0, 65535),
("float32_uniform", 0, 1, 0, 1),
("uint8_bimodal", -64, 319, 0, 255),
(sitk.sitkUInt8, 0, 0, 0, 0, False),
(sitk.sitkUInt16, 0, 0, 0, 0, False),
("uint16_uniform", 8191, 57344, 0, 65535, False),
("uint16_uniform", 8191, 57344, 0, 65535, True),
("float32_uniform", 0, 1, 0, 1, False),
("uint8_bimodal", -64, 319, 0, 255, False),
("uint8_bimodal", 0, 255, 0, 255, True),
],
indirect=["image_mrc"],
)
def test_build_histogram_main(image_mrc, expected_min, expected_max, expected_floor, expected_limit):
res = pytools.visual_min_max(Path(image_mrc), mad_scale=1.5)
def test_build_histogram_main(image_mrc, expected_min, expected_max, expected_floor, expected_limit, clamp):
res = pytools.visual_min_max(Path(image_mrc), mad_scale=1.5, clamp=clamp)

assert "neuroglancerPrecomputedMin" in res
assert "neuroglancerPrecomputedMax" in res
Expand Down

0 comments on commit 92205d5

Please sign in to comment.