From acc73116f9378ad46886ba8bdc9a8c422f5c0ebc Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Tue, 30 May 2023 14:50:52 -0400 Subject: [PATCH 1/2] Restore clamping for the computed visual min max range. --- pytools/workflow_functions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pytools/workflow_functions.py b/pytools/workflow_functions.py index 8e57093..746f532 100644 --- a/pytools/workflow_functions.py +++ b/pytools/workflow_functions.py @@ -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. @@ -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" @@ -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])), From 0c52d37d5b6b05c07541a9766aa276f17d2995f5 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Tue, 30 May 2023 15:11:49 -0400 Subject: [PATCH 2/2] Add testing of clamp option to visual_min_max --- test/test_histogram.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/test_histogram.py b/test/test_histogram.py index 0844905..137b063 100644 --- a/test/test_histogram.py +++ b/test/test_histogram.py @@ -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