mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-10 20:53:36 +08:00
Convert name to _safe_as_int, output np.int64, expand Examples
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import numpy as np
|
||||
|
||||
__all__ == ['_safe_int']
|
||||
__all__ = ['_safe_as_int']
|
||||
|
||||
|
||||
def _safe_int_cast(val, atol=1e-7):
|
||||
def _safe_as_int(val, atol=1e-7):
|
||||
"""
|
||||
Attempt to safely cast values to integer format.
|
||||
|
||||
@@ -19,8 +19,8 @@ def _safe_int_cast(val, atol=1e-7):
|
||||
|
||||
Returns
|
||||
-------
|
||||
val_int : NumPy scalar or ndarray of dtype `np.int32`
|
||||
Returns the input value(s) coerced to dtype `np.int32` assuming all
|
||||
val_int : NumPy scalar or ndarray of dtype `np.int64`
|
||||
Returns the input value(s) coerced to dtype `np.int64` assuming all
|
||||
were within ``atol`` of the nearest integer.
|
||||
|
||||
Notes
|
||||
@@ -37,13 +37,22 @@ def _safe_int_cast(val, atol=1e-7):
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> _safe_int_cast(7.0)
|
||||
>>> _safe_as_int(7.0)
|
||||
7
|
||||
>>> _safe_int_cast([9, 4, 2.9999999999])
|
||||
|
||||
>>> _safe_as_int([9, 4, 2.9999999999])
|
||||
array([9, 4, 3], dtype=int32)
|
||||
|
||||
>>> _safe_as_int(53.01)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Integer argument required but received 53.1, check inputs.
|
||||
|
||||
>>> _safe_as_int(53.01, atol=0.01)
|
||||
53
|
||||
|
||||
"""
|
||||
mod = np.asarray(val) % 1 # Modulo 1
|
||||
mod = np.asarray(val) % 1 # Extract mantissa
|
||||
|
||||
# Check for and subtract any mod values > 0.5 from 1
|
||||
if mod.ndim == 0: # Scalar input, cannot be indexed
|
||||
@@ -58,4 +67,4 @@ def _safe_int_cast(val, atol=1e-7):
|
||||
raise ValueError("Integer argument required but received "
|
||||
"{0}, check inputs.".format(val))
|
||||
|
||||
return np.round(val).astype(np.int32)
|
||||
return np.round(val).astype(np.int64)
|
||||
@@ -0,0 +1,36 @@
|
||||
import numpy as np
|
||||
from skimage._shared.safe_int_cast import _safe_as_int
|
||||
|
||||
|
||||
def test_int_cast_not_possible():
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, 7.1)
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, [7.1, 0.9])
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, np.r_[7.1, 0.9])
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, (7.1, 0.9))
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, ((3, 4, 1),
|
||||
(2, 7.6, 289)))
|
||||
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, 7.1, 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, [7.1, 0.9], 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, np.r_[7.1, 0.9], 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, (7.1, 0.9), 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_as_int, ((3, 4, 1),
|
||||
(2, 7.6, 289)), 0.25)
|
||||
|
||||
|
||||
def test_int_cast_possible():
|
||||
np.testing.assert_equal(_safe_as_int(7.1, atol=0.11), 7)
|
||||
np.testing.assert_equal(_safe_as_int(-7.1, atol=0.11), -7)
|
||||
np.testing.assert_equal(_safe_as_int(41.9, atol=0.11), 42)
|
||||
np.testing.assert_array_equal(_safe_as_int([2, 42, 5789234.0, 87, 4]),
|
||||
np.r_[2, 42, 5789234, 87, 4])
|
||||
np.testing.assert_array_equal(_safe_as_int(np.r_[[[3, 4, 1.000000001],
|
||||
[7, 2, -8.999999999],
|
||||
[6, 9, -4234918347.]]]),
|
||||
np.r_[[[3, 4, 1],
|
||||
[7, 2, -9],
|
||||
[6, 9, -4234918347]]])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
np.testing.run_module_suite()
|
||||
@@ -1,36 +0,0 @@
|
||||
import numpy as np
|
||||
from skimage._shared.safe_int_cast import _safe_int_cast
|
||||
|
||||
|
||||
def test_int_cast_not_possible():
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, 7.1)
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, [7.1, 0.9])
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, np.r_[7.1, 0.9])
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, (7.1, 0.9))
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, ((3, 4, 1),
|
||||
(2, 7.6, 289)))
|
||||
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, 7.1, 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, [7.1, 0.9], 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, np.r_[7.1, 0.9], 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, (7.1, 0.9), 0.09)
|
||||
np.testing.assert_raises(ValueError, _safe_int_cast, ((3, 4, 1),
|
||||
(2, 7.6, 289)), 0.25)
|
||||
|
||||
|
||||
def test_int_cast_possible():
|
||||
np.testing.assert_equal(_safe_int_cast(7.1, atol=0.11), 7)
|
||||
np.testing.assert_equal(_safe_int_cast(-7.1, atol=0.11), -7)
|
||||
np.testing.assert_equal(_safe_int_cast(41.9, atol=0.11), 42)
|
||||
np.testing.assert_array_equal(_safe_int_cast([2, 42, 5789234.0, 87, 4]),
|
||||
np.r_[2, 42, 5789234, 87, 4])
|
||||
np.testing.assert_array_equal(_safe_int_cast(np.r_[[[3, 4, 1.000000001],
|
||||
[7, 2, -8.999999999],
|
||||
[6, 9, -4234918347.]]]),
|
||||
np.r_[[[3, 4, 1],
|
||||
[7, 2, -9],
|
||||
[6, 9, -4234918347]]])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
np.testing.run_module_suite()
|
||||
Reference in New Issue
Block a user