test_unit_utils.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. from datetime import timedelta
  2. import pint.errors
  3. import pytest
  4. import pandas as pd
  5. import timely_beliefs as tb
  6. from flexmeasures.utils.unit_utils import (
  7. convert_units,
  8. determine_flow_unit,
  9. determine_stock_unit,
  10. determine_unit_conversion_multiplier,
  11. units_are_convertible,
  12. is_energy_unit,
  13. is_power_unit,
  14. ur,
  15. )
  16. @pytest.mark.parametrize(
  17. "from_unit, to_unit, expected_multiplier, expected_values",
  18. [
  19. ("%", "‰", 10, None),
  20. ("m/s", "km/h", 3.6, None),
  21. ("m³/h", "l/h", 1000, None),
  22. ("m³", "m³/h", 4, None),
  23. ("MW", "kW", 1000, None),
  24. ("%", "kWh", 0.5, None), # i.e. 1% of 50 kWh (the capacity used in the test)
  25. ("kWh", "%", 2, None), # i.e. 1 kWh = 2% of 50 kWh
  26. ("kWh", "kW", 4, None),
  27. ("kW", "kWh", 1 / 4, None),
  28. ("-W", "W", -1, None),
  29. ("l/(100km)", "l/km", 0.01, None),
  30. ("°C", "K", None, [273.15, 283.15, 284.15]),
  31. # no support for combining an offset unit with a scaling factor, but this is also overly specific
  32. # ("-°C", "K", None, [273.15, 263.15, 262.15]),
  33. # ("l/(10°C)", "l/(°C)", 0.1, None),
  34. ],
  35. )
  36. def test_convert_unit(
  37. from_unit,
  38. to_unit,
  39. expected_multiplier,
  40. expected_values,
  41. ):
  42. """Check some common unit conversions.
  43. Note that for the above expectations:
  44. - conversion from kWh to kW, and from m³ to m³/h, both depend on the event resolution set below
  45. - conversion from °C to K depends on the data values set below
  46. """
  47. data = pd.Series([0, 10.0, 11.0])
  48. converted_data: pd.Series = convert_units(
  49. data=data,
  50. from_unit=from_unit,
  51. to_unit=to_unit,
  52. event_resolution=timedelta(minutes=15),
  53. capacity="50 kWh",
  54. )
  55. if expected_multiplier is not None:
  56. expected_data = data * expected_multiplier
  57. else:
  58. expected_data = pd.Series(expected_values)
  59. pd.testing.assert_series_equal(converted_data, expected_data)
  60. @pytest.mark.parametrize(
  61. "from_unit, to_unit, timezone, input_values, expected_values",
  62. [
  63. # datetimes are converted to seconds since UNIX epoch
  64. (
  65. "datetime",
  66. "s",
  67. None,
  68. ["1970-01-01", "1970-01-02", "1970-01-03"],
  69. [0, 60 * 60 * 24, 60 * 60 * 48],
  70. ),
  71. # nothing overflows for the next 100 years
  72. (
  73. "datetime",
  74. "s",
  75. None,
  76. ["2123-05-02", "2123-05-03", "2123-05-04"],
  77. [4838659200, 4838659200 + 60 * 60 * 24, 4838659200 + 60 * 60 * 48],
  78. ),
  79. # Same as above, but day precedes month in input
  80. (
  81. "dayfirst datetime",
  82. "s",
  83. None,
  84. ["02-05-2123", "03-05-2123", "04-05-2123"],
  85. [4838659200, 4838659200 + 60 * 60 * 24, 4838659200 + 60 * 60 * 48],
  86. ),
  87. # Localize timezone-naive datetimes to UTC in case there is no sensor information available
  88. (
  89. "datetime",
  90. "s",
  91. None,
  92. ["2023-05-02 00:00:01", "2023-05-02 00:00:02", "2023-05-02 00:00:03"],
  93. [1682985601, 1682985602, 1682985603],
  94. ),
  95. # Localize timezone-naive datetimes to sensor's timezone in case that is available
  96. (
  97. "datetime",
  98. "s",
  99. "Europe/Amsterdam",
  100. ["2023-05-02 00:00:01", "2023-05-02 00:00:02", "2023-05-02 00:00:03"],
  101. [
  102. 1682985601 - 60 * 60 * 2,
  103. 1682985602 - 60 * 60 * 2,
  104. 1682985603 - 60 * 60 * 2,
  105. ],
  106. ),
  107. # Timezone-aware datetimes work don't require localization
  108. (
  109. "datetime",
  110. "s",
  111. None,
  112. [
  113. "2023-05-02 00:00:01 +02:00",
  114. "2023-05-02 00:00:02 +02:00",
  115. "2023-05-02 00:00:03 +02:00",
  116. ],
  117. [
  118. 1682985601 - 60 * 60 * 2,
  119. 1682985602 - 60 * 60 * 2,
  120. 1682985603 - 60 * 60 * 2,
  121. ],
  122. ),
  123. # Timezone-aware datetimes also means that the sensor timezone is irrelevant
  124. (
  125. "datetime",
  126. "s",
  127. "Asia/Seoul",
  128. [
  129. "2023-05-02 00:00:01 +02:00",
  130. "2023-05-02 00:00:02 +02:00",
  131. "2023-05-02 00:00:03 +02:00",
  132. ],
  133. [
  134. 1682985601 - 60 * 60 * 2,
  135. 1682985602 - 60 * 60 * 2,
  136. 1682985603 - 60 * 60 * 2,
  137. ],
  138. ),
  139. # Timedeltas can be converted to units of time
  140. ("timedelta", "s", None, ["1 minute", "1 minute 2 seconds"], [60.0, 62.0]),
  141. # Convertible timedeltas include absolute days of 24 hours
  142. ("timedelta", "d", None, ["1 day", "1 day 12 hours"], [1.0, 1.5]),
  143. # Convertible timedeltas exclude nominal durations like month or year, which cannot be represented as a datetime.timedelta object
  144. # ("timedelta", "d", None, ["1 month", "1 year"], [30., 365.]), # fails
  145. ],
  146. )
  147. def test_convert_special_unit(
  148. from_unit,
  149. to_unit,
  150. timezone,
  151. input_values,
  152. expected_values,
  153. ):
  154. """Check some special unit conversions."""
  155. data = pd.Series(input_values)
  156. if timezone:
  157. data.sensor = tb.Sensor("test", timezone=timezone)
  158. converted_data: pd.Series = convert_units(
  159. data=data,
  160. from_unit=from_unit,
  161. to_unit=to_unit,
  162. )
  163. print(converted_data)
  164. expected_data = pd.Series(expected_values)
  165. print(expected_data)
  166. pd.testing.assert_series_equal(converted_data, expected_data)
  167. @pytest.mark.parametrize(
  168. "unit, time_unit, expected_unit",
  169. [
  170. ("m³", None, "m³/h"),
  171. ("kWh", None, "kW"),
  172. ("km", "h", "km/h"),
  173. ("m", "s", "m/s"),
  174. ],
  175. )
  176. def test_determine_flow_unit(
  177. unit,
  178. time_unit,
  179. expected_unit,
  180. ):
  181. if time_unit is None:
  182. assert determine_flow_unit(unit) == expected_unit
  183. else:
  184. assert determine_flow_unit(unit, time_unit) == expected_unit
  185. @pytest.mark.parametrize(
  186. "unit, time_unit, expected_unit",
  187. [
  188. ("m³/h", None, "m³"),
  189. ("km³/h", None, "km³"),
  190. # ("hm³/h", None, "hm³"), # todo: uncomment after switching to decimal unit registry
  191. ("kW", None, "kWh"),
  192. ("m/s", "s", "m"),
  193. ("m/s", "h", "km"),
  194. ("t/h", None, "t"),
  195. ],
  196. )
  197. def test_determine_stock_unit(
  198. unit,
  199. time_unit,
  200. expected_unit,
  201. ):
  202. if time_unit is None:
  203. assert determine_stock_unit(unit) == expected_unit
  204. else:
  205. assert determine_stock_unit(unit, time_unit) == expected_unit
  206. def test_determine_unit_conversion_multiplier():
  207. assert determine_unit_conversion_multiplier("kW", "W") == 1000
  208. assert determine_unit_conversion_multiplier("J/s", "W") == 1
  209. assert determine_unit_conversion_multiplier("Wh", "W", timedelta(minutes=10)) == 6
  210. assert determine_unit_conversion_multiplier("kWh", "MJ") == 3.6
  211. with pytest.raises(pint.errors.OffsetUnitCalculusError):
  212. # Not a conversion that can be specified as a multiplication
  213. determine_unit_conversion_multiplier("°C", "K")
  214. def test_h_denotes_hour_and_not_planck_constant():
  215. assert ur.Quantity("h").dimensionality == ur.Quantity("hour").dimensionality
  216. assert (
  217. ur.Quantity("hbar").dimensionality
  218. == ur.Quantity("planck_constant").dimensionality
  219. )
  220. def test_units_are_convertible():
  221. assert units_are_convertible("kW", "W") # units just have different prefixes
  222. assert units_are_convertible(
  223. "J/s", "W"
  224. ) # units can be converted using some multiplier
  225. assert units_are_convertible(
  226. "Wh", "W"
  227. ) # units that represent a stock delta can, knowing the duration, be converted to a flow
  228. assert units_are_convertible("toe", "W") # tonne of oil equivalent
  229. assert units_are_convertible("°C", "K") # offset unit to absolute unit
  230. assert not units_are_convertible("°C", "W")
  231. assert not units_are_convertible("EUR/MWh", "W")
  232. assert not units_are_convertible("not-a-unit", "W")
  233. @pytest.mark.parametrize(
  234. "unit, power_unit",
  235. [
  236. ("EUR/MWh", False),
  237. ("KRW/kWh", False),
  238. ("kWh", False),
  239. ("kW", True),
  240. ("watt", True),
  241. ("°C", False),
  242. ("", False),
  243. ("not-a-unit", False),
  244. ("#", False),
  245. ],
  246. )
  247. def test_is_power_unit(unit: str, power_unit: bool):
  248. assert is_power_unit(unit) is power_unit
  249. @pytest.mark.parametrize(
  250. "unit, energy_unit",
  251. [
  252. ("EUR/MWh", False),
  253. ("KRW/kWh", False),
  254. ("kWh", True),
  255. ("kW", False),
  256. ("watthour", True),
  257. ("°C", False),
  258. ("", False),
  259. ("not-a-unit", False),
  260. ],
  261. )
  262. def test_is_energy_unit(unit: str, energy_unit: bool):
  263. assert is_energy_unit(unit) is energy_unit