From 322728fa5439ca2575cc3b2299612880052ebd5f Mon Sep 17 00:00:00 2001 From: Golnesa Date: Tue, 9 Jan 2024 10:27:51 +0100 Subject: [PATCH] Linters --- .../grid/cross_section_definitions.py | 29 +++-- .../grid/cross_section_locations.py | 14 ++- threedigrid_builder/interface/gridadmin.py | 4 +- .../tests/test_cross_section_definitions.py | 100 +++++++++++++++--- .../tests/test_cross_section_locations.py | 8 +- 5 files changed, 128 insertions(+), 27 deletions(-) diff --git a/threedigrid_builder/grid/cross_section_definitions.py b/threedigrid_builder/grid/cross_section_definitions.py index bf276c0e..79e1db94 100644 --- a/threedigrid_builder/grid/cross_section_definitions.py +++ b/threedigrid_builder/grid/cross_section_definitions.py @@ -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. @@ -49,7 +48,7 @@ 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 @@ -57,16 +56,19 @@ def convert(self, ids): 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) @@ -74,7 +76,6 @@ def convert(self, ids): 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) @@ -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 @@ -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: @@ -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() diff --git a/threedigrid_builder/grid/cross_section_locations.py b/threedigrid_builder/grid/cross_section_locations.py index ba9c895a..8313ad34 100644 --- a/threedigrid_builder/grid/cross_section_locations.py +++ b/threedigrid_builder/grid/cross_section_locations.py @@ -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( diff --git a/threedigrid_builder/interface/gridadmin.py b/threedigrid_builder/interface/gridadmin.py index 250a849b..c4a43286 100644 --- a/threedigrid_builder/interface/gridadmin.py +++ b/threedigrid_builder/interface/gridadmin.py @@ -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'""" diff --git a/threedigrid_builder/tests/test_cross_section_definitions.py b/threedigrid_builder/tests/test_cross_section_definitions.py index d2a9be79..99efe7fa 100644 --- a/threedigrid_builder/tests/test_cross_section_definitions.py +++ b/threedigrid_builder/tests/test_cross_section_definitions.py @@ -1,4 +1,3 @@ -from cmath import nan from unittest import mock import numpy as np @@ -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]) @@ -134,7 +137,9 @@ 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 @@ -142,6 +147,7 @@ def test_tabulate_tabulated(): assert_almost_equal(table, np.array([[0, 1], [1, 2], [2, 3]], dtype=float)) assert yz is None + @pytest.mark.parametrize( "width,height,match", [ @@ -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", @@ -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", @@ -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", @@ -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 diff --git a/threedigrid_builder/tests/test_cross_section_locations.py b/threedigrid_builder/tests/test_cross_section_locations.py index 30a583b7..ba29c562 100644 --- a/threedigrid_builder/tests/test_cross_section_locations.py +++ b/threedigrid_builder/tests/test_cross_section_locations.py @@ -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] )