Skip to content

Commit

Permalink
🚧 Fill empty l1 records with l2 aggregates
Browse files Browse the repository at this point in the history
  • Loading branch information
i-be-snek committed Nov 15, 2024
1 parent 5b06404 commit d566161
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Database/fill_data_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,21 @@
)
except BaseException as err:
logger.error(f"Could not fill area data gap for {impact} at l3. Error: {err}")

logger.info("Filling data gap upwards (l2 -> l1) for NULL impacts")
for impact in l2.keys():
empty_l1_events = l1[
[dg_util.event_id, f"Total_{impact}_Min", f"Total_{impact}_Max", f"Total_{impact}_Approx"]
][l1[f"Total_{impact}_Min"].isna()][dg_util.event_id].unique()
for e_id in empty_l1_events:
impact_per_event_id = l2[impact][[dg_util.num_min, dg_util.num_max]][
(l2[impact][dg_util.event_id] == e_id) & (~l2[impact][dg_util.num_min].isna())
]
if not impact_per_event_id.empty:
agg_min, agg_max = impact_per_event_id.sum()
l1[l1[dg_util.event_id] == e_id] = l1[l1[dg_util.event_id] == e_id].apply(
lambda row: dg_util.l2_to_l1(row, agg_min, agg_max, impact), axis=1
)
logger.info(
f"Aggregated values found for {e_id} in impact category {impact}: agg_min = {agg_min}, agg_max = {agg_max}"
)
7 changes: 7 additions & 0 deletions Database/scr/normalize_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def fill_area(self, row: dict, missing_areas: dict[str, list], area_col: str) ->
row[f"{self.admin_areas}_{c}"] = row[f"{self.admin_areas}_{c}"].extend(missing_areas[f"{area_col}_{c}"])
return row

@staticmethod
def l2_to_l1(row: dict, agg_min: float, agg_max: float, impact: str) -> dict:
row[f"Total_{impact}_Min"] = agg_min
row[f"Total_{impact}_Max"] = agg_max
row[f"Total_{impact}_Approx"] = 1
return row

def l3_to_l2(self, l3_row: dict) -> dict:
l2_row = {}
for k in l3_row.keys():
Expand Down

0 comments on commit d566161

Please sign in to comment.