Skip to content

Commit

Permalink
moved time frequency conversion to util dates_times
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGebhart committed Nov 4, 2024
1 parent 4b88cb0 commit 4d29cbf
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 45 deletions.
3 changes: 1 addition & 2 deletions climada/engine/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from scipy import sparse
from tqdm import tqdm

import climada.util.checker as u_check
import climada.util.coordinates as u_coord
import climada.util.dates_times as u_dt
import climada.util.interpolation as u_interp
Expand Down Expand Up @@ -549,7 +548,7 @@ def local_exceedance_impact(
)

# check frequency unit
return_period_unit = u_check.convert_frequency_unit_to_time_unit(
return_period_unit = u_dt.convert_frequency_unit_to_time_unit(
self.frequency_unit
)

Expand Down
4 changes: 2 additions & 2 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def local_exceedance_intensity(
if not min_intensity and min_intensity != 0:
min_intensity = self.intensity_thres
# check frequency unit
return_period_unit = u_check.convert_frequency_unit_to_time_unit(
return_period_unit = u_dt.convert_frequency_unit_to_time_unit(
self.frequency_unit
)

Expand Down Expand Up @@ -659,7 +659,7 @@ def local_return_period(
if not min_intensity and min_intensity != 0:
min_intensity = self.intensity_thres
# check frequency unit
return_period_unit = u_check.convert_frequency_unit_to_time_unit(
return_period_unit = u_dt.convert_frequency_unit_to_time_unit(
self.frequency_unit
)

Expand Down
29 changes: 0 additions & 29 deletions climada/util/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,32 +228,3 @@ def prune_csr_matrix(matrix: sparse.csr_matrix):
matrix.check_format()
matrix.eliminate_zeros()
matrix.sum_duplicates()


def convert_frequency_unit_to_time_unit(frequency_unit):
"""Converts common frequency units to corresponding time units. Unknown frequency
units are converted to "years".
Parameters
----------
frequency_unit : str
unit of frequency
Returns
-------
str
corresponding time unit.
"""
if frequency_unit in ["1/year", "annual", "1/y", "1/a"]:
time_unit = "years"
elif frequency_unit in ["1/month", "monthly", "1/m"]:
time_unit = "months"
elif frequency_unit in ["1/week", "weekly", "1/w"]:
time_unit = "weeks"
else:
LOGGER.warning(
f"Frequency unit {frequency_unit} is not known, "
"years will be used as time unit."
)
time_unit = "years"
return time_unit
29 changes: 29 additions & 0 deletions climada/util/dates_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,32 @@ def first_year(ordinal_vector):
int
"""
return dt.date.fromordinal(np.min(ordinal_vector)).year


def convert_frequency_unit_to_time_unit(frequency_unit):
"""Converts common frequency units to corresponding time units. Unknown frequency
units are converted to "years".
Parameters
----------
frequency_unit : str
unit of frequency
Returns
-------
str
corresponding time unit.
"""
if frequency_unit in ["1/year", "annual", "1/y", "1/a"]:
time_unit = "years"
elif frequency_unit in ["1/month", "monthly", "1/m"]:
time_unit = "months"
elif frequency_unit in ["1/week", "weekly", "1/w"]:
time_unit = "weeks"
else:
LOGGER.warning(

Check warning on line 140 in climada/util/dates_times.py

View check run for this annotation

Jenkins - WCR / Pylint

logging-fstring-interpolation

NORMAL: Use lazy % formatting in logging functions
Raw output
no description found
f"Frequency unit {frequency_unit} is not known, "
"years will be used as time unit."
)
time_unit = "years"
return time_unit
12 changes: 0 additions & 12 deletions climada/util/test/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,6 @@ def test_prune_csr_matrix(self):
np.testing.assert_array_equal(matrix.data, [3])
self.assertEqual(matrix.nnz, 1)

def test_convert_frequency_unit_to_time_unit(self):
# test frequency unit to time unit conversion
frequency_units = ["1/year", "1/y", "1/month", "1/week", "", "unknown"]
time_units = ["years", "years", "months", "weeks", "years", "years"]
np.testing.assert_equal(
time_units,
[
u_check.convert_frequency_unit_to_time_unit(frequency_unit)
for frequency_unit in frequency_units
],
)


# Execute Tests
if __name__ == "__main__":
Expand Down
17 changes: 17 additions & 0 deletions climada/util/test/test_dates_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,25 @@ def test_first_year_pass(self):
self.assertEqual(u_dt.first_year(np.array(ordinal_date)), 1918)


class TestUnitConversion(unittest.TestCase):
"""Test time and frequency conversion functions"""

def test_convert_frequency_unit_to_time_unit(self):
# test frequency unit to time unit conversion
frequency_units = ["1/year", "1/y", "1/month", "1/week", "", "unknown"]
time_units = ["years", "years", "months", "weeks", "years", "years"]
np.testing.assert_equal(
time_units,
[
u_dt.convert_frequency_unit_to_time_unit(frequency_unit)
for frequency_unit in frequency_units
],
)


# Execute Tests
if __name__ == "__main__":
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestDateString)
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestDateNumpy))
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestUnitConversion))
unittest.TextTestRunner(verbosity=2).run(TESTS)

0 comments on commit 4d29cbf

Please sign in to comment.