def test_relative_scaling(self): img = self.imt[0] lower = 10 upper = 99 b_min = 100 b_max = 300 scaler = ScaleIntensityRangePercentiles(lower=lower, upper=upper, b_min=b_min, b_max=b_max, relative=True) expected_a_min = np.percentile(img, lower) expected_a_max = np.percentile(img, upper) expected_b_min = ((b_max - b_min) * (lower / 100.0)) + b_min expected_b_max = ((b_max - b_min) * (upper / 100.0)) + b_min expected_img = (img - expected_a_min) / (expected_a_max - expected_a_min) expected_img = (expected_img * (expected_b_max - expected_b_min)) + expected_b_min for p in TEST_NDARRAYS: result = scaler(p(img)) assert_allclose(result, p(expected_img), rtol=1e-3) scaler = ScaleIntensityRangePercentiles( lower=lower, upper=upper, b_min=b_min, b_max=b_max, relative=True, clip=True ) for p in TEST_NDARRAYS: result = scaler(p(img)) assert_allclose(result, p(np.clip(expected_img, expected_b_min, expected_b_max)), rtol=1e-4)
def __init__( self, keys: KeysCollection, lower: float, upper: float, b_min: float, b_max: float, clip: bool = False, relative: bool = False, ) -> None: super().__init__(keys) self.scaler = ScaleIntensityRangePercentiles(lower, upper, b_min, b_max, clip, relative)
def test_scaling(self): img = self.imt[0] lower = 10 upper = 99 b_min = 0 b_max = 255 a_min = np.percentile(img, lower) a_max = np.percentile(img, upper) expected = (((img - a_min) / (a_max - a_min)) * (b_max - b_min) + b_min).astype(np.uint8) scaler = ScaleIntensityRangePercentiles(lower=lower, upper=upper, b_min=b_min, b_max=b_max, dtype=np.uint8) for p in TEST_NDARRAYS: result = scaler(p(img)) assert_allclose(result, p(expected), rtol=1e-4)
def test_scaling(self): img = self.imt lower = 10 upper = 99 b_min = 0 b_max = 255 a_min = np.percentile(img, lower) a_max = np.percentile(img, upper) expected = (img - a_min) / (a_max - a_min) expected = (expected * (b_max - b_min)) + b_min scaler = ScaleIntensityRangePercentiles(lower=lower, upper=upper, b_min=b_min, b_max=b_max) self.assertTrue(np.allclose(expected, scaler(img)))
def test_channel_wise(self): img = np.tile(self.imt, (3, 1, 1, 1)) lower = 10 upper = 99 b_min = 0 b_max = 255 scaler = ScaleIntensityRangePercentiles( lower=lower, upper=upper, b_min=b_min, b_max=b_max, channel_wise=True, dtype=np.uint8 ) expected = [] for c in img: a_min = np.percentile(c, lower) a_max = np.percentile(c, upper) expected.append((((c - a_min) / (a_max - a_min)) * (b_max - b_min) + b_min).astype(np.uint8)) expected = np.stack(expected) for p in TEST_NDARRAYS: result = scaler(p(img)) assert_allclose(result, p(expected), rtol=1e-4)
def test_relative_scaling(self): img = self.imt lower = 10 upper = 99 b_min = 100 b_max = 300 scaler = ScaleIntensityRangePercentiles(lower=lower, upper=upper, b_min=b_min, b_max=b_max, relative=True) expected_a_min = np.percentile(img, lower) expected_a_max = np.percentile(img, upper) expected_b_min = ((b_max - b_min) * (lower / 100.0)) + b_min expected_b_max = ((b_max - b_min) * (upper / 100.0)) + b_min expected_img = (img - expected_a_min) / (expected_a_max - expected_a_min) expected_img = (expected_img * (expected_b_max - expected_b_min)) + expected_b_min self.assertTrue(np.allclose(expected_img, scaler(img)))