def test_validate_true_all_values_within_value_range(self): arr = np.array([-10, -1, 0, 1, 10, 126, 127], dtype=np.int16) arr_clipped = iadt.clip_to_dtype_value_range_( np.copy(arr), np.int8, validate=True) assert np.array_equal(arr_clipped, arr) assert arr_clipped.dtype.name == "int16"
def test_clip_to_same_dtype(self): arr = np.array([-10, -1, 0, 1, 10, 255, 256], dtype=np.int16) arr_clipped = iadt.clip_to_dtype_value_range_( np.copy(arr), np.int16, validate=False) assert np.array_equal(arr_clipped, arr) assert arr_clipped.dtype.name == "int16"
def test_clip_to_narrower_dtype(self): arr = np.array([-10, -1, 0, 1, 10, 255, 256], dtype=np.int16) arr_clipped = iadt.clip_to_dtype_value_range_( np.copy(arr), np.int8, validate=False) expected = np.array([-10, -1, 0, 1, 10, 127, 127], dtype=np.int16) assert np.array_equal(arr_clipped, expected) assert arr_clipped.dtype.name == "int16"
def test_validate_too_many_values(self): arr = np.array([-10, 0, 200], dtype=np.int16) with self.assertRaises(AssertionError) as context: _ = iadt.clip_to_dtype_value_range_( np.copy(arr), np.int8, validate=100) assert ( "Maximum value of array is outside of allowed value range " "(200.0000 vs -128.0000 to 127.0000)." in str(context.exception))
def test_validate_true_min_value_outside_value_range(self): arr = np.array([-200, -1, 0, 1, 10, 126, 127], dtype=np.int16) with self.assertRaises(AssertionError) as context: _ = iadt.clip_to_dtype_value_range_( np.copy(arr), np.int8, validate=True) assert ( "Minimum value of array is outside of allowed value range " "(-200.0000 vs -128.0000 to 127.0000)." in str(context.exception))
def test_validate_too_few_values(self): arr = np.array([-10, 0, 200], dtype=np.int16) _ = iadt.clip_to_dtype_value_range_(np.copy(arr), np.int8, validate=2)