domainiac 9.2.0__py3-none-any.whl → 9.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ from functools import lru_cache
2
+
1
3
  import datamazing.pandas as pdz
2
4
  import numpy as np
3
5
  import pandas as pd
@@ -9,26 +11,36 @@ from ..modeling.nwp import Coordinate, Neighborhood, NWPParameter, NWPProvider
9
11
 
10
12
 
11
13
  class NWPManager:
12
- """
13
- Contains all logic concerning NWP data.
14
- This includes:
15
- - Getting NWP data
16
- - Getting average NWP data
17
- - Finding closest NWP coordinates
18
- """
19
-
20
14
  def __init__(
21
15
  self,
22
16
  db: pdz.Database,
23
17
  time_interval: pdz.TimeInterval,
24
18
  resolution: pd.Timedelta,
19
+ cache_nwp: bool = True,
25
20
  ):
21
+ """
22
+ Contains all logic concerning NWP data.
23
+ This includes:
24
+ - Getting NWP data
25
+ - Getting average NWP data
26
+ - Finding closest NWP coordinates
27
+
28
+ Args:
29
+ db (pdz.Database): Database backend
30
+ time_interval (pdz.TimeInterval): Time interval
31
+ resolution (pd.Timedelta): Time resolution
32
+ cache_nwp (bool, optional): Current implementation of this managers
33
+ is not very efficient when querying multiple coordinates. To mitigate
34
+ this, database queries can be cached. This is especially useful when
35
+ querying multiple coordinates. Defaults to True.
36
+ """
26
37
  self.db = db
27
38
  self.time_interval = time_interval
28
39
  self.resolution = resolution
29
40
 
30
41
  self._nwp_coordinates_kd_tree = dict()
31
42
  self._nwp_table_name_prefix = "forecastNwp"
43
+ self.cache_nwp = cache_nwp
32
44
 
33
45
  @staticmethod
34
46
  def calculate_wind_speed_from_vectors(u: pd.Series, v: pd.Series) -> pd.Series:
@@ -111,15 +123,24 @@ class NWPManager:
111
123
 
112
124
  return coordinates
113
125
 
126
+ @lru_cache()
127
+ def _query_cached(
128
+ self,
129
+ table: str,
130
+ time_interval: pdz.TimeInterval,
131
+ ) -> pd.DataFrame:
132
+ """
133
+ Query the database with caching.
134
+ """
135
+ df = self.db.query(table, time_interval)
136
+ return df
137
+
114
138
  def _get_nwp_parameter(
115
139
  self,
116
140
  provider: NWPProvider,
117
141
  parameter: NWPParameter,
118
142
  coordinate: Coordinate,
119
143
  ) -> pd.DataFrame:
120
- """
121
- Get NWP data for a given parameter, coordinate and altitude.
122
- """
123
144
  table = self._get_table_name(provider, parameter)
124
145
 
125
146
  filters = {
@@ -138,7 +159,13 @@ class NWPManager:
138
159
  self.time_interval.right + query_margin,
139
160
  )
140
161
 
141
- df = self.db.query(table, padded_time_interval, filters=filters)
162
+ # Cache query if specified
163
+ # This is useful when querying multiple coordinates.
164
+ if self.cache_nwp:
165
+ df = self._query_cached(table, padded_time_interval)
166
+ df = df.loc[(df[list(filters)] == pd.Series(filters)).all(axis=1)]
167
+ else:
168
+ df = self.db.query(table, padded_time_interval, filters=filters)
142
169
 
143
170
  df = df.drop(
144
171
  columns=[
@@ -160,10 +187,6 @@ class NWPManager:
160
187
  parameter: NWPParameter,
161
188
  coordinate: Coordinate,
162
189
  ):
163
- """
164
- Get NWP data for a given parameter, coordinate and altitude,
165
- using the proper interpolation technique.
166
- """
167
190
  df = self._get_nwp_parameter(provider, parameter, coordinate)
168
191
 
169
192
  df = df.sort_values(by="time_utc")
@@ -214,9 +237,12 @@ class NWPManager:
214
237
  parameter: NWPParameter,
215
238
  coordinate: Coordinate,
216
239
  ):
217
- df_nwp = self._get_interpolated_nwp_parameter(provider, parameter, coordinate)
240
+ df_nwp = self._get_interpolated_nwp_parameter(
241
+ provider,
242
+ parameter,
243
+ coordinate,
244
+ )
218
245
 
219
- # Ensure data is within time_interval defined by user
220
246
  df_nwp = df_nwp.query(
221
247
  f"time_utc >= '{self.time_interval.left}' "
222
248
  f"and time_utc <= '{self.time_interval.right}'",
@@ -230,14 +256,15 @@ class NWPManager:
230
256
  parameter: NWPParameter,
231
257
  neighborhood: Neighborhood,
232
258
  ) -> pd.DataFrame:
233
- """
234
- Get average NWP data for a given parameter, coordinates and altitude.
235
- """
236
259
  dfs = []
237
260
 
238
261
  neighbors = self.get_nwp_neighbors(provider, parameter, neighborhood)
239
262
  for coordinate in neighbors:
240
- df_coordinate = self.get_nwp_parameter(provider, parameter, coordinate)
263
+ df_coordinate = self.get_nwp_parameter(
264
+ provider,
265
+ parameter,
266
+ coordinate,
267
+ )
241
268
  dfs.append(df_coordinate)
242
269
 
243
270
  df = pd.concat(dfs)
@@ -252,7 +279,19 @@ class NWPManager:
252
279
  neighborhood: Neighborhood,
253
280
  ) -> pd.DataFrame:
254
281
  """
255
- Get weather features for a given coordinate.
282
+ Get NWP data for multiple parameters over a neighborhood.
283
+
284
+ For each parameter, retrieves the average NWP data in the neighborhood, then
285
+ merges all results on the time_utc column.
286
+
287
+ Args:
288
+ provider (NWPProvider): The NWP data provider to use.
289
+ parameters (list[NWPParameter]): List of NWP parameters to retrieve.
290
+ neighborhood (Neighborhood): The neighborhood specifying the central
291
+ coordinate and number of neighbors.
292
+
293
+ Returns:
294
+ pd.DataFrame: DataFrame with merged parameter values, indexed by time_utc.
256
295
  """
257
296
  dfs = []
258
297
  for parameter in parameters:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: domainiac
3
- Version: 9.2.0
3
+ Version: 9.3.0
4
4
  Summary: Package for working with Energinet data, but with specialized functions used for Enigma.
5
5
  Author: Team Enigma
6
6
  Author-email: enigma@energinet.dk
@@ -9,7 +9,7 @@ domainiac/functions/wind.py,sha256=ZOxSMxjWV7LZsAQVW5Y-GPMhZzfvIsh3NOtSXB5lUAY,9
9
9
  domainiac/managers/__init__.py,sha256=7N-iEsFidupPi1u3EURfeWAoQ-NdrzAwR13faA_VWUs,298
10
10
  domainiac/managers/masterdata_manager.py,sha256=_xIHEWVYssdvC2VMujvR0ANXCI_Er3qOlfAX427yimk,2298
11
11
  domainiac/managers/metering_manager.py,sha256=MauMncY_yaVPx8Y9CCD8nJiE1BGHFKTVC1bOtiyl2pI,1701
12
- domainiac/managers/nwp_manager.py,sha256=rEN9HmDe01N5QzePGsUorWgmOuY7t9vMwBF1Kuuu_38,8603
12
+ domainiac/managers/nwp_manager.py,sha256=h2J6ryXqplB_W6jC7aSenOa9MOxsYrd2HenU3WZnyu0,10040
13
13
  domainiac/managers/outage_manager.py,sha256=rH4NYY0hiD51V6SadVgtIOX0qCjG2cSZ60F9lDjkJ8o,1416
14
14
  domainiac/managers/plant_manager.py,sha256=aqJ04QB-QI83zIllQiyTrlh_SKR6CYI4bY4z4RlBL3g,5083
15
15
  domainiac/managers/resource_manager.py,sha256=QMkNcrgyXLLlkCR9kg4-nFjTaYHtlsldex-b_8lAmO0,3730
@@ -19,6 +19,6 @@ domainiac/modeling/nwp.py,sha256=PolrBdQn8W-e1M0_pefvmLn2mr4HT0NqlYlkyCV0dds,438
19
19
  domainiac/modeling/plant.py,sha256=Y0_Q6V6Lj3irJh5z-49r5gFJdD4Xl_pfHIMUhdaJVUI,2226
20
20
  domainiac/wrappers/__init__.py,sha256=vZOw9maXgVoAvudZqioD-GTPkgPI6fm7_CUELQcR_-g,43
21
21
  domainiac/wrappers/cache_wrapper.py,sha256=jDg-Lt_y-YzItyP-74tGULOxb07s_CcutmOHP1Ie00Q,355
22
- domainiac-9.2.0.dist-info/METADATA,sha256=_07orTSIon0JOGS6raqGn476L0GGh65-uNd8cksbx7g,737
23
- domainiac-9.2.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
24
- domainiac-9.2.0.dist-info/RECORD,,
22
+ domainiac-9.3.0.dist-info/METADATA,sha256=tFNeV_xnaOi5u0GcSRaCyZkdhL0oHH87X3JgbW2W8Nw,737
23
+ domainiac-9.3.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
24
+ domainiac-9.3.0.dist-info/RECORD,,