def test___init__(self): g1 = RasterGrid([10, 10], [2.3, 2.1], self.bs, [RasterNode(0, 5, NodeStatus.FIXED_VALUE_BOUNDARY)]) assert g1.size == 100 npt.assert_almost_equal(g1.spacing, [2.3, 2.1]) npt.assert_almost_equal(g1.length, [20.7, 18.9]) with pytest.raises(IndexError): RasterGrid([5, 10], [2.2, 2.4], self.bs, [RasterNode(20, 255, NodeStatus.FIXED_VALUE_BOUNDARY)])
def setup_class(cls): profile_grid = ProfileGrid(8, 2.2, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, []) cls.profile_flow_graph = FlowGraph(profile_grid, SingleFlowRouter()) cls.profile_elevation = np.r_[0.82, 0.16, 0.14, 0.20, 0.71, 0.97, 0.41, 0.09] cls.result_profile_elevation = cls.profile_flow_graph.update_routes( cls.profile_elevation ) raster_grid = RasterGrid( [4, 4], [1.1, 1.2], RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY), [], ) cls.raster_flow_graph = FlowGraph(raster_grid, SingleFlowRouter()) cls.raster_elevation = np.array( [ [0.82, 0.16, 0.14, 0.20], [0.71, 0.97, 0.41, 0.09], [0.49, 0.01, 0.19, 0.38], [0.29, 0.82, 0.09, 0.88], ] ) cls.result_raster_elevation = cls.raster_flow_graph.update_routes( cls.raster_elevation )
def test_raster_grid(self, func, k): # Test on a tiny (2x2) 2-d square grid with a planar surface # tilted in y (rows) and with all outlets on the 1st row. spacing = 300. grid = RasterGrid([2, 2], [spacing, spacing], RasterBoundaryStatus( NodeStatus.FIXED_VALUE_BOUNDARY), []) flow_graph = FlowGraph(grid, SingleFlowRouter()) h = 1. elevation = np.array([[0., 0.], [h, h]], dtype='d') graph_elevation = flow_graph.update_routes(elevation) drainage_area = flow_graph.accumulate(1.) erosion = np.zeros_like(elevation) m_exp = 0.5 n_exp = 1. dt = 1 # use small time step (compare with explicit scheme) tolerance = 1e-3 n_corr = func(erosion, elevation, drainage_area, flow_graph, k, m_exp, n_exp, dt, tolerance) slope = h / spacing a = spacing**2 k_coef = 1e-3 err = dt * k_coef * a**m_exp * slope**n_exp expected_erosion = np.array([[0., 0.], [err, err]], dtype='d') np.testing.assert_allclose(erosion, expected_erosion, atol=1e-5) assert n_corr == 0
def test_from_length(self): g = RasterGrid.from_length( [11, 11], np.r_[23, 21], self.bs, [RasterNode(0, 5, NodeStatus.FIXED_VALUE_BOUNDARY)]) assert g.size == 121 npt.assert_almost_equal(g.spacing, [2.3, 2.1]) npt.assert_almost_equal(g.length, [23., 21.])
def grid(grid_type): if grid_type == "profile": bs = ProfileBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY) return ProfileGrid(4, 1.0, bs, []) elif grid_type == "raster": bs = RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY) return RasterGrid([3, 3], np.array([1.0, 1.0]), bs, [])
def test___init__(self): profile_grid = ProfileGrid(8, 2.2, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, []) raster_grid = RasterGrid([5, 10], [2.2, 2.4], RasterBoundaryStatus( NodeStatus.FIXED_VALUE_BOUNDARY), []) FlowGraph(profile_grid, DummyFlowRouter()) FlowGraph(profile_grid, MultipleFlowRouter(1., 1.1)) FlowGraph(profile_grid, SingleFlowRouter())
def test_accumulate(self): grid = ProfileGrid(8, 2.2, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, []) flow_graph = FlowGraph(grid, SingleFlowRouter()) elevation = np.r_[0.82, 0.16, 0.14, 0.20, 0.71, 0.97, 0.41, 0.09] graph_elevation = flow_graph.update_routes(elevation) npt.assert_almost_equal( flow_graph.accumulate(np.ones(elevation.shape)), np.r_[2.2, 4.4, 11.0, 4.4, 2.2, 2.2, 4.4, 6.6], ) grid = RasterGrid( [4, 4], [1.1, 1.2], RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY), [], ) flow_graph = FlowGraph(grid, SingleFlowRouter()) elevation = np.array([ [0.82, 0.16, 0.14, 0.20], [0.71, 0.97, 0.41, 0.09], [0.49, 0.01, 0.19, 0.38], [0.29, 0.82, 0.09, 0.88], ]) graph_elevation = flow_graph.update_routes(elevation) expected = np.array([ [1.32, 2.64, 3.96, 1.32], [1.32, 1.32, 1.32, 9.24], [1.32, 11.88, 1.32, 1.32], [1.32, 1.32, 2.64, 1.32], ]) npt.assert_almost_equal( flow_graph.accumulate(np.ones(elevation.shape)), expected) npt.assert_almost_equal(flow_graph.accumulate(1.0), expected) npt.assert_almost_equal(flow_graph.accumulate(5.0), 5 * expected) data = np.array([ [1.1, 1.0, 1.1, 1.0], [1.1, 1.0, 1.1, 1.0], [1.1, 1.0, 1.1, 1.0], [1.1, 1.0, 1.1, 1.0], ]) expected = np.array([ [1.452, 2.772, 4.224, 1.32], [1.452, 1.32, 1.452, 9.636], [1.452, 12.54, 1.452, 1.32], [1.452, 1.32, 2.772, 1.32], ]) npt.assert_almost_equal(flow_graph.accumulate(data), expected)
def setup_method(self, method): self.bs = bs = RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY) self.g = RasterGrid( [5, 10], [2.2, 2.4], bs, [RasterNode(0, 5, NodeStatus.FIXED_VALUE_BOUNDARY)])
class TestRasterGrid: def setup_method(self, method): self.bs = bs = RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY) self.g = RasterGrid( [5, 10], [2.2, 2.4], bs, [RasterNode(0, 5, NodeStatus.FIXED_VALUE_BOUNDARY)] ) def test_static_props(self): assert RasterGrid.is_structured is True assert RasterGrid.is_uniform is True assert RasterGrid.max_neighbors == 8 def test___init__(self): g1 = RasterGrid( [10, 10], [2.3, 2.1], self.bs, [RasterNode(0, 5, NodeStatus.FIXED_VALUE_BOUNDARY)], ) assert g1.size == 100 npt.assert_almost_equal(g1.spacing, [2.3, 2.1]) npt.assert_almost_equal(g1.length, [20.7, 18.9]) with pytest.raises(IndexError): RasterGrid( [5, 10], [2.2, 2.4], self.bs, [RasterNode(20, 255, NodeStatus.FIXED_VALUE_BOUNDARY)], ) def test_from_length(self): g = RasterGrid.from_length( [11, 11], np.r_[23, 21], self.bs, [RasterNode(0, 5, NodeStatus.FIXED_VALUE_BOUNDARY)], ) assert g.size == 121 npt.assert_almost_equal(g.spacing, [2.3, 2.1]) npt.assert_almost_equal(g.length, [23.0, 21.0]) def test_size(self): assert self.g.size == 50 def test_shape(self): npt.assert_equal(self.g.shape, [5, 10]) def test_spacing(self): npt.assert_equal(self.g.spacing, [2.2, 2.4]) def test_neighbors_count(self): assert self.g.neighbors_count(0) == 3 assert self.g.neighbors_count(15) == 8 def test_neighbors_indices(self): npt.assert_equal(self.g.neighbors_indices(0), np.array([1, 10, 11])) npt.assert_equal( self.g.neighbors_indices(15), np.array([4, 5, 6, 14, 16, 24, 25, 26]) ) with pytest.raises(IndexError, match="grid index out of range"): self.g.neighbors(51) def test_neighbors_raster_indices(self): assert self.g.neighbors_indices(0, 0) == [(0, 1), (1, 0), (1, 1)] assert self.g.neighbors_indices(1, 5) == [ (0, 4), (0, 5), (0, 6), (1, 4), (1, 6), (2, 4), (2, 5), (2, 6), ] with pytest.raises(IndexError, match="grid index out of range"): self.g.neighbors(10, 10) def test_neighbors_distances(self): dist_diag = np.sqrt(2.2**2 + 2.4**2) npt.assert_equal(self.g.neighbors_distances(0), np.array([2.4, 2.2, dist_diag])) npt.assert_equal( self.g.neighbors_distances(15), np.array([dist_diag, 2.2, dist_diag, 2.4, 2.4, dist_diag, 2.2, dist_diag]), ) with pytest.raises(IndexError, match="grid index out of range"): self.g.neighbors(51) def test_neighbors(self): dist_diag = np.sqrt(2.2**2 + 2.4**2) assert self.g.neighbors(0) == [ Neighbor(1, 2.4, NodeStatus.FIXED_VALUE_BOUNDARY), Neighbor(10, 2.2, NodeStatus.FIXED_VALUE_BOUNDARY), Neighbor(11, dist_diag, NodeStatus.CORE), ] with pytest.raises(IndexError, match="grid index out of range"): self.g.neighbors(51) def test_raster_neighbors(self): dist_diag = np.sqrt(2.2**2 + 2.4**2) assert self.g.neighbors(0, 0) == [ RasterNeighbor(1, 0, 1, 2.4, NodeStatus.FIXED_VALUE_BOUNDARY), RasterNeighbor(10, 1, 0, 2.2, NodeStatus.FIXED_VALUE_BOUNDARY), RasterNeighbor(11, 1, 1, dist_diag, NodeStatus.CORE), ] with pytest.raises(IndexError, match="grid index out of range"): self.g.neighbors(10, 10)