Skip to content

Commit

Permalink
consistent naming of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGebhart committed Dec 13, 2024
1 parent 8ad36cb commit 847b906
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
6 changes: 3 additions & 3 deletions climada/util/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ def _ensure_utf8(val):
return admin1_info, admin1_shapes


def global_bounding_box():
def bounding_box_global():
"""
Return global bounds in EPSG 4326
Expand All @@ -1705,7 +1705,7 @@ def global_bounding_box():
return (-180, -90, 180, 90)


def get_country_bounding_box(country_names, buffer=1.0):
def bounding_box_from_countries(country_names, buffer=1.0):
"""
Return bounding box in EPSG 4326 containing given countries.
Expand Down Expand Up @@ -1738,7 +1738,7 @@ def get_country_bounding_box(country_names, buffer=1.0):
return latlon_bounds(np.array(latitudes), np.array(longitudes), buffer=buffer)


def bounds_from_cardinal_bounds(*, northern, eastern, western, southern):
def bounding_box_from_cardinal_bounds(*, northern, eastern, western, southern):
"""
Return and normalize bounding box in EPSG 4326 from given cardinal bounds.
Expand Down
39 changes: 20 additions & 19 deletions climada/util/test/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2297,15 +2297,15 @@ def test_mask_raster_with_geometry(self):
class TestBoundsFromUserInput(unittest.TestCase):
"""Unit tests for the bounds_from_user_input function."""

def global_bounding_box(self):
def test_bounding_box_global(self):
"""Test for 'global' area selection."""
result = u_coord.global_bounding_box()
result = u_coord.bounding_box_global()
expected = (-180, -90, 180, 90)
np.testing.assert_almost_equal(result, expected)

def test_get_country_bounding_box(self):
def test_bounding_box_from_countries(self):
"""Test for a list of ISO country codes."""
result = u_coord.get_country_bounding_box(
result = u_coord.bounding_box_from_countries(
["ITA"], buffer=1.0
) # Testing with Italy (ITA)
# Real expected bounds for Italy (calculated or manually known)
Expand All @@ -2316,56 +2316,57 @@ def test_get_country_bounding_box(self):
48.08521494500006,
] # Italy's bounding box

def test_bounds_from_cardinal_bounds(self):
# invalid input
with self.assertRaises(ValueError):
u_coord.bounding_box_from_countries(["invalid_ISO", "DEU"])

def test_bounding_box_from_cardinal_bounds(self):
"""Test for conversion from cardinal bounds to bounds."""
np.testing.assert_array_almost_equal(
u_coord.bounds_from_cardinal_bounds(
u_coord.bounding_box_from_cardinal_bounds(
northern=90, southern=-20, eastern=30, western=20
),
(20, -20, 30, 90),
)
np.testing.assert_array_almost_equal(
u_coord.bounds_from_cardinal_bounds(
u_coord.bounding_box_from_cardinal_bounds(
northern=90, southern=-20, eastern=20, western=30
),
(30, -20, 380, 90),
)
np.testing.assert_array_almost_equal(
u_coord.bounds_from_cardinal_bounds(
u_coord.bounding_box_from_cardinal_bounds(
northern=90, southern=-20, eastern=170, western=-170
),
(-170, -20, 170, 90),
)
np.testing.assert_array_almost_equal(
u_coord.bounds_from_cardinal_bounds(
u_coord.bounding_box_from_cardinal_bounds(
northern=90, southern=-20, eastern=-170, western=170
),
(170, -20, 190, 90),
)
np.testing.assert_array_almost_equal(
u_coord.bounds_from_cardinal_bounds(
u_coord.bounding_box_from_cardinal_bounds(
northern=90, southern=-20, eastern=170, western=175
),
(175, -20, 530, 90),
)

# some invalid cases
with self.assertRaises(TypeError):
u_coord.bounds_from_cardinal_bounds(southern=-20, eastern=30, western=20)
u_coord.bounding_box_from_cardinal_bounds(
southern=-20, eastern=30, western=20
)
with self.assertRaises(TypeError):
u_coord.bounds_from_cardinal_bounds([90, -20, 30, 20])
u_coord.bounding_box_from_cardinal_bounds([90, -20, 30, 20])
with self.assertRaises(TypeError):
u_coord.bounds_from_cardinal_bounds(90, -20, 30, 20)
u_coord.bounding_box_from_cardinal_bounds(90, -20, 30, 20)
with self.assertRaises(TypeError):
u_coord.bounds_from_cardinal_bounds(
u_coord.bounding_box_from_cardinal_bounds(
northern="90", southern=-20, eastern=30, western=20
)

def test_invalid_input_string(self):
"""Test for invalid string input."""
with self.assertRaises(Exception):
u_coord.get_bound("invalid_ISO")


# Execute Tests
if __name__ == "__main__":
Expand Down

0 comments on commit 847b906

Please sign in to comment.