Skip to content

Commit

Permalink
Linters
Browse files Browse the repository at this point in the history
  • Loading branch information
Golnesa committed Jan 9, 2024
1 parent b9fdf4b commit 322728f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 27 deletions.
29 changes: 19 additions & 10 deletions threedigrid_builder/grid/cross_section_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class CrossSectionDefinition:
vegetation_drag_coefficients: str # space-separated list of floats



class CrossSectionDefinitions(Array[CrossSectionDefinition]):
def convert(self, ids):
"""Convert to CrossSections.
Expand All @@ -49,32 +48,34 @@ def convert(self, ids):
content_pk=ids,
code=self.code[idx],
count=0,
count_yz = 0,
count_yz=0,
)
if len(result) == 0:
return result

tables = []
tables_yz = []


# Numpy array views on width/height based on idx
width_idx = self.width[idx]
height_idx = self.height[idx]

for i, shape in enumerate(self.shape[idx]):
tabulator = tabulators[shape]
result.shape[i], result.width_1d[i], result.height_1d[i], table, yz = tabulator(
shape, width_idx[i], height_idx[i]
)
(
result.shape[i],
result.width_1d[i],
result.height_1d[i],
table,
yz,
) = tabulator(shape, width_idx[i], height_idx[i])
if table is not None:
result.count[i] = len(table)
tables.append(table)
if yz is not None:
result.count_yz[i] = len(yz)
tables_yz.append(yz)


result.offset[:] = np.roll(np.cumsum(result.count), 1)
result.offset[0] = 0
result.offset_yz[:] = np.roll(np.cumsum(result.count_yz), 1)
Expand All @@ -88,7 +89,7 @@ def convert(self, ids):
if len(tables_yz) > 0:
result.tables_yz = np.concatenate(tables_yz, axis=0)
else:
result.tables_yz = np.empty((0, 4))
result.tables_yz = np.empty((0, 4))

return result

Expand Down Expand Up @@ -252,7 +253,16 @@ def tabulate_tabulated(shape, width, height):
return shape, np.max(widths), np.max(heights), np.array([heights, widths]).T, None


def tabulate_yz(shape, width, height, friction_values, vegetation_stem_densities, vegetation_stem_diameters, vegetation_heights, vegetation_drag_coefficients):
def tabulate_yz(
shape,
width,
height,
friction_values,
vegetation_stem_densities,
vegetation_stem_diameters,
vegetation_heights,
vegetation_drag_coefficients,
):
"""Tabulate an (open or closed) YZ profile
Args:
Expand Down Expand Up @@ -308,7 +318,6 @@ def tabulate_yz(shape, width, height, friction_values, vegetation_stem_densities
yz[:-1, 2] = fric
yz[:-1, 3] = veg_stemden * veg_stemdia * veg_hght * veg_drag


# Adapt non-unique height coordinates. Why?
# Because if a segment of the profile is exactly horizontal, we need 2 widths
seen = set()
Expand Down
14 changes: 12 additions & 2 deletions threedigrid_builder/grid/cross_section_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@ def apply_to_lines(self, lines, channels, extrapolate=False):
lines.frict_type2 = self.friction_type[idx2]
lines.frict_value1 = self.friction_value[idx1]
lines.frict_value2 = self.friction_value[idx2]
lines.veg_coef1 = self.vegetation_stem_density[idx1] * self.vegetation_stem_diameter[idx1] * self.vegetation_height[idx1] * self.vegetation_drag_coefficient[idx1]
lines.veg_coef2 = self.vegetation_stem_density[idx2] * self.vegetation_stem_diameter[idx2] * self.vegetation_height[idx2] * self.vegetation_drag_coefficient[idx2]
lines.veg_coef1 = (
self.vegetation_stem_density[idx1]
* self.vegetation_stem_diameter[idx1]
* self.vegetation_height[idx1]
* self.vegetation_drag_coefficient[idx1]
)
lines.veg_coef2 = (
self.vegetation_stem_density[idx2]
* self.vegetation_stem_diameter[idx2]
* self.vegetation_height[idx2]
* self.vegetation_drag_coefficient[idx2]
)

# Compute invert levels and start and end
lines.invert_level_start_point = compute_bottom_level(
Expand Down
4 changes: 3 additions & 1 deletion threedigrid_builder/interface/gridadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,9 @@ def write_cross_sections(self, cross_sections: CrossSections):
if cross_sections.tables_yz is not None:
self.write_dataset(group, "offset_yz", cross_sections.offset_yz)
self.write_dataset(group, "count_yz", cross_sections.count_yz)
self.write_dataset(group, "tables_yz", cross_sections.tables_yz.T, insert_dummy=False)
self.write_dataset(
group, "tables_yz", cross_sections.tables_yz.T, insert_dummy=False
)

def write_obstacles(self, obstacles: Obstacles):
"""For backwards compat, the group is named 'levees'"""
Expand Down
100 changes: 88 additions & 12 deletions threedigrid_builder/tests/test_cross_section_definitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from cmath import nan
from unittest import mock

import numpy as np
Expand Down Expand Up @@ -38,8 +37,12 @@ def test_convert_multiple(cross_section_definitions):
"threedigrid_builder.grid.cross_section_definitions.tabulators",
{
SHP.CIRCLE: mock.Mock(return_value=(1, 0.1, None, None, None)),
SHP.TABULATED_TRAPEZIUM: mock.Mock(return_value=(5, 15.0, 2.0, table_1, None)),
SHP.TABULATED_RECTANGLE: mock.Mock(return_value=(6, 11.0, 2.0, table_2, None)),
SHP.TABULATED_TRAPEZIUM: mock.Mock(
return_value=(5, 15.0, 2.0, table_1, None)
),
SHP.TABULATED_RECTANGLE: mock.Mock(
return_value=(6, 11.0, 2.0, table_2, None)
),
},
):
actual = cross_section_definitions.convert([1, 3, 9])
Expand Down Expand Up @@ -134,14 +137,17 @@ def test_tabulate_egg():


def test_tabulate_tabulated():
shape, width_1d, height_1d, table, yz = tabulate_tabulated("my-shape", "1 2 3", "0 1 2")
shape, width_1d, height_1d, table, yz = tabulate_tabulated(
"my-shape", "1 2 3", "0 1 2"
)

assert shape == "my-shape"
assert width_1d == 3.0 # the max
assert height_1d == 2.0
assert_almost_equal(table, np.array([[0, 1], [1, 2], [2, 3]], dtype=float))
assert yz is None


@pytest.mark.parametrize(
"width,height,match",
[
Expand Down Expand Up @@ -195,7 +201,19 @@ def test_tabulate_inverted_egg():
@pytest.mark.parametrize(
"width,height,friction_values,vegetation_stem_densities,vegetation_stem_diameters,vegetation_heights,vegetation_drag_coefficients,exp_width,exp_height,exp_table,exp_yz",
[
("0 0.5 1 1.5", "0.5 0 0 0.5", "1 1 1", "1 1 1", "1 1 1", "1 1 1", "1 1 1", 1.5, 0.5, [[0, 0.5], [0.5, 1.5]], [[0, 0.5, 1, 1], [0.5, 0, 1, 1], [1, 0, 1, 1], [1.5, 0.5, 0, 0]]),
(
"0 0.5 1 1.5",
"0.5 0 0 0.5",
"1 1 1",
"1 1 1",
"1 1 1",
"1 1 1",
"1 1 1",
1.5,
0.5,
[[0, 0.5], [0.5, 1.5]],
[[0, 0.5, 1, 1], [0.5, 0, 1, 1], [1, 0, 1, 1], [1.5, 0.5, 0, 0]],
),
(
"0 0.5 1 1.5",
"0.5 0 0 0.25",
Expand All @@ -207,7 +225,7 @@ def test_tabulate_inverted_egg():
1.5,
0.5,
[[0, 0.5], [0.25, 1.25], [0.5, 1.5]],
[[0, 0.5, 1, 3], [0.5, 0, 1, 1], [1, 0, 1, 1], [1.5, 0.25, 0, 0]]
[[0, 0.5, 1, 3], [0.5, 0, 1, 1], [1, 0, 1, 1], [1.5, 0.25, 0, 0]],
),
(
"0 1 2 3 4 5",
Expand All @@ -220,7 +238,14 @@ def test_tabulate_inverted_egg():
5,
1,
[[0, 0], [0.5, 3], [0.5, 4], [1, 5]],
[[0, 1, 1, 1], [1, 0, 1, 1], [2, 0.5, 1, 1], [3, 0.5, 1, 1], [4, 0, 1, 1], [5, 1, 0, 0]]
[
[0, 1, 1, 1],
[1, 0, 1, 1],
[2, 0.5, 1, 1],
[3, 0.5, 1, 1],
[4, 0, 1, 1],
[5, 1, 0, 0],
],
),
(
"0 1 2 2 0 0",
Expand All @@ -233,14 +258,65 @@ def test_tabulate_inverted_egg():
2.0,
1.5,
[[0, 0], [0.5, 2.0], [1.5, 2.0], [1.5, 0.0]],
None
None,
),
(
"0 0.5 0.75 1.0 1.5",
"0.5 0 0 0 0.5",
"1 1 1 1",
"1 1 1 1",
"1 1 1 1",
"1 1 1 1",
"1 1 1 1",
1.5,
0.5,
[[0, 0.5], [0.5, 1.5]],
[
[0, 0.5, 1, 1],
[0.5, 0, 1, 1],
[0.75, 0, 1, 1],
[1, 0, 1, 1],
[1.5, 0.5, 0, 0],
],
),
(
"0 1 0 1 0",
"0 1 1 0 0",
"1 1 1 1",
"1 1 1 1",
"1 1 1 1",
"1 1 1 1",
"1 1 1 1",
1,
1,
[[0, 1], [0.5, 0], [1, 1], [1, 0]],
None,
),
("0 0.5 0.75 1.0 1.5", "0.5 0 0 0 0.5", "1 1 1 1", "1 1 1 1", "1 1 1 1", "1 1 1 1", "1 1 1 1", 1.5, 0.5, [[0, 0.5], [0.5, 1.5]], [[0, 0.5, 1, 1], [0.5, 0, 1, 1], [0.75, 0, 1, 1], [1, 0, 1, 1], [1.5, 0.5, 0, 0]]),
("0 1 0 1 0", "0 1 1 0 0", "1 1 1 1", "1 1 1 1", "1 1 1 1", "1 1 1 1", "1 1 1 1", 1, 1, [[0, 1], [0.5, 0], [1, 1], [1, 0]], None),
],
)
def test_tabulate_yz(width, height, friction_values,vegetation_stem_densities,vegetation_stem_diameters,vegetation_heights,vegetation_drag_coefficients,exp_width, exp_height, exp_table, exp_yz):
shape, width_1d, height_1d, table, yz = tabulate_yz("my-shape", width, height,friction_values,vegetation_stem_densities,vegetation_stem_diameters,vegetation_heights,vegetation_drag_coefficients)
def test_tabulate_yz(
width,
height,
friction_values,
vegetation_stem_densities,
vegetation_stem_diameters,
vegetation_heights,
vegetation_drag_coefficients,
exp_width,
exp_height,
exp_table,
exp_yz,
):
shape, width_1d, height_1d, table, yz = tabulate_yz(
"my-shape",
width,
height,
friction_values,
vegetation_stem_densities,
vegetation_stem_diameters,
vegetation_heights,
vegetation_drag_coefficients,
)

assert shape == CrossSectionShape.TABULATED_TRAPEZIUM
assert width_1d == exp_width
Expand Down
8 changes: 6 additions & 2 deletions threedigrid_builder/tests/test_cross_section_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,12 @@ def test_apply_to_lines(channels, channel_lines, locations):
assert_equal(channel_lines.frict_type2, [1, 2, 1, 1, 1, 2, 2])
assert_equal(channel_lines.frict_value1, [30, 0.02, 40, 40, 40, 0.03, 0.03])
assert_equal(channel_lines.frict_value2, [30, 0.02, 35, 35, 35, 0.03, 0.03])
assert_almost_equal(channel_lines.veg_coef1, [0.1, 0.006, 0.2, 0.2, 0.2, 0.036, 0.036])
assert_almost_equal(channel_lines.veg_coef2, [0.1, 0.006, 0.15, 0.15, 0.15, 0.036, 0.036])
assert_almost_equal(
channel_lines.veg_coef1, [0.1, 0.006, 0.2, 0.2, 0.2, 0.036, 0.036]
)
assert_almost_equal(
channel_lines.veg_coef2, [0.1, 0.006, 0.15, 0.15, 0.15, 0.036, 0.036]
)
assert_almost_equal(
channel_lines.cross_weight, [1.0, 1.0, 1.0, 0.65, 0.0, 1.0, 1.0]
)
Expand Down

0 comments on commit 322728f

Please sign in to comment.