Skip to content

Commit

Permalink
Add country percentage normalisation
Browse files Browse the repository at this point in the history
  • Loading branch information
leilafarsani committed Dec 3, 2024
1 parent c48e3ff commit bf3d08c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions iati_activity_details_split_by_fields/iati_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,23 @@ def get_transactions_split_as_json(self):
return [x.get_as_json() for x in self.get_transactions_split()]

def _get_recipient_countries_with_normalised_percentages(self):
# TODO
return self.recipient_countries
"""Normalise country percentages to ensure they sum to 100%"""
if not self.recipient_countries:
return []
total_percentage = sum(
country.percentage or 0
for country in self.recipient_countries
)
if total_percentage == 0:
return self.recipient_countries

normalized_countries = copy.deepcopy(self.recipient_countries)

for country in normalized_countries:
if country.percentage:
country.percentage = (country.percentage / total_percentage) * 100

return normalized_countries

def _get_sectors_grouped_by_vocab_with_normalised_percentages(self) -> dict:
output: dict = {}
Expand Down

0 comments on commit bf3d08c

Please sign in to comment.