Skip to content

Commit

Permalink
add is_uniform_spaced function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Clark Fitzgerald committed Jul 9, 2015
1 parent 573723f commit 3f13523
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
16 changes: 13 additions & 3 deletions xray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,17 @@ def close_on_error(f):
def is_remote_uri(path):
return bool(re.search('^https?\://', path))

def is_uniform_spaced(arr):
"""Return True if values of an array are uniformly spaced and sorted

def is_uniform_spaced(arr, **kwargs):
"""Return True if values of an array are uniformly spaced and sorted.
>>> is_uniform_spaced(range(5))
True
>>> is_uniform_spaced([-4, 0, 100])
False
kwargs are additional arguments to ``np.isclose``
"""
pass
arr = np.array(arr)
diffs = np.diff(arr)
return np.isclose(diffs.min(), diffs.max(), **kwargs)
18 changes: 15 additions & 3 deletions xray/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,20 @@ def test_chain_map(self):

class Test_is_uniform_and_sorted(TestCase):

def test_range_sorted(self):
def test_sorted_uniform(self):
self.assertTrue(utils.is_uniform_spaced(np.arange(5)))

def test_not_sorted(self):
self.assertFalse(utils.is_uniform_spaced([4, 1, 89]))
def test_sorted_not_uniform(self):
self.assertEqual(False, utils.is_uniform_spaced([-2, 1, 89]))

def test_not_sorted_uniform(self):
self.assertEqual(False, utils.is_uniform_spaced([1, -1, 3]))

def test_not_sorted_not_uniform(self):
self.assertEqual(False, utils.is_uniform_spaced([4, 1, 89]))

def test_two_numbers(self):
self.assertTrue(utils.is_uniform_spaced([0, 1.7]))

def test_relative_tolerance(self):
self.assertTrue(utils.is_uniform_spaced([0, 0.97, 2], rtol=0.1))

0 comments on commit 3f13523

Please sign in to comment.