def test_box_size(): """Test calculation of box sizes.""" # No points. Box size should be 1,1. assert_allclose(_box_size([]), (1.0, 1.0)) # Create one point. Box size should be 1,1. point = [(0, 0)] assert_allclose(_box_size(point), (1.0, 1.0)) # Create two points. Box size should be 0.5,1. points = [(0.25, 0.5), (0.75, 0.5)] assert_allclose(_box_size(points), (0.5, 1.0)) # Create three points. Box size should be (0.5, 0.5). points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points), (0.5, 0.5)) # Create a grid of points. Box size should be (0.1, 0.1). x, y = np.meshgrid(np.linspace(-0.5, 0.5, 11), np.linspace(-0.5, 0.5, 11)) x, y = x.ravel(), y.ravel() assert_allclose(_box_size(np.c_[x, y]), (0.1, 0.1)) # Create a random set of points. This should never break the function. rng = np.random.RandomState(42) points = rng.rand(100, 2) width, height = _box_size(points) assert width is not None assert height is not None # Test specifying an existing width. points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, width=0.4), (0.4, 0.5)) # Test specifying an existing width that has influence on the calculated # height. points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, width=0.2), (0.2, 1.0)) # Test specifying an existing height. points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, height=0.4), (0.5, 0.4)) # Test specifying an existing height that has influence on the calculated # width. points = [(0.25, 0.25), (0.75, 0.45), (0.5, 0.75)] assert_allclose(_box_size(points, height=0.1), (1.0, 0.1)) # Test specifying both width and height. The function should simply return # these. points = [(0.25, 0.25), (0.75, 0.45), (0.5, 0.75)] assert_array_equal(_box_size(points, width=0.1, height=0.1), (0.1, 0.1)) # Test specifying a width that will cause unfixable horizontal overlap and # essentially breaks the function (height will be 0). points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_array_equal(_box_size(points, width=1), (1, 0)) # Test adding some padding. # Create three points. Box size should be a little less than (0.5, 0.5). points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, padding=0.1), (0.9 * 0.5, 0.9 * 0.5))
def test_box_size(): """Test calculation of box sizes.""" # No points. Box size should be 1,1. assert_allclose(_box_size([]), (1.0, 1.0)) # Create one point. Box size should be 1,1. point = [(0, 0)] assert_allclose(_box_size(point), (1.0, 1.0)) # Create two points. Box size should be 0.5,1. points = [(0.25, 0.5), (0.75, 0.5)] assert_allclose(_box_size(points), (0.5, 1.0)) # Create three points. Box size should be (0.5, 0.5). points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points), (0.5, 0.5)) # Create a grid of points. Box size should be (0.1, 0.1). x, y = np.meshgrid(np.linspace(-0.5, 0.5, 11), np.linspace(-0.5, 0.5, 11)) x, y = x.ravel(), y.ravel() assert_allclose(_box_size(np.c_[x, y]), (0.1, 0.1)) # Create a random set of points. This should never break the function. rng = np.random.RandomState(42) points = rng.rand(100, 2) width, height = _box_size(points) assert_true(width is not None) assert_true(height is not None) # Test specifying an existing width. points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, width=0.4), (0.4, 0.5)) # Test specifying an existing width that has influence on the calculated # height. points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, width=0.2), (0.2, 1.0)) # Test specifying an existing height. points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, height=0.4), (0.5, 0.4)) # Test specifying an existing height that has influence on the calculated # width. points = [(0.25, 0.25), (0.75, 0.45), (0.5, 0.75)] assert_allclose(_box_size(points, height=0.1), (1.0, 0.1)) # Test specifying both width and height. The function should simply return # these. points = [(0.25, 0.25), (0.75, 0.45), (0.5, 0.75)] assert_array_equal(_box_size(points, width=0.1, height=0.1), (0.1, 0.1)) # Test specifying a width that will cause unfixable horizontal overlap and # essentially breaks the function (height will be 0). points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_array_equal(_box_size(points, width=1), (1, 0)) # Test adding some padding. # Create three points. Box size should be a little less than (0.5, 0.5). points = [(0.25, 0.25), (0.75, 0.25), (0.5, 0.75)] assert_allclose(_box_size(points, padding=0.1), (0.9 * 0.5, 0.9 * 0.5))