Spaces:
Sleeping
Sleeping
ElvarThorS
commited on
Commit
·
037aeab
1
Parent(s):
a37ec6c
Update age_distribution and aggregate_data
Browse files
data_processing/age_distribution_by_id.py
CHANGED
@@ -13,8 +13,8 @@ def open_file(filename):
|
|
13 |
print(f"An error occurred: {e}")
|
14 |
return []
|
15 |
|
16 |
-
def get_age_distribution(
|
17 |
-
age_distributions = {smsv_id: {} for smsv_id in smsv_ids} # Initialize output dict
|
18 |
csv_data = open_file(filename)
|
19 |
|
20 |
if csv_data: # Check if data is not empty
|
@@ -27,7 +27,7 @@ def get_age_distribution(year, smsv_ids, filename):
|
|
27 |
for row in csv_data[1:]: # Skip header
|
28 |
row_smsv_id = row[smsv_id_index]
|
29 |
row_year = int(row[year_index])
|
30 |
-
if row_smsv_id in smsv_ids and row_year
|
31 |
age_group = row[agegroup_index]
|
32 |
population = int(row[population_index])
|
33 |
|
@@ -46,7 +46,7 @@ if __name__ == '__main__':
|
|
46 |
# Example usage:
|
47 |
filename = './given_data/ibuafjoldi.csv'
|
48 |
smsv_ids_to_find = ['0103', '2903', '4002'] # List of desired smsv_ids
|
49 |
-
age_dist = get_age_distribution(2017,smsv_ids_to_find, filename)
|
50 |
|
51 |
for smsv_id, distributions in age_dist.items():
|
52 |
print(f"Age Distribution for SMSV ID: {smsv_id}")
|
|
|
13 |
print(f"An error occurred: {e}")
|
14 |
return []
|
15 |
|
16 |
+
def get_age_distribution(years, smsv_ids, filename):
|
17 |
+
age_distributions = {smsv_id: {year: {} for year in years} for smsv_id in smsv_ids} # Initialize output dict
|
18 |
csv_data = open_file(filename)
|
19 |
|
20 |
if csv_data: # Check if data is not empty
|
|
|
27 |
for row in csv_data[1:]: # Skip header
|
28 |
row_smsv_id = row[smsv_id_index]
|
29 |
row_year = int(row[year_index])
|
30 |
+
if row_smsv_id in smsv_ids and row_year in years:
|
31 |
age_group = row[agegroup_index]
|
32 |
population = int(row[population_index])
|
33 |
|
|
|
46 |
# Example usage:
|
47 |
filename = './given_data/ibuafjoldi.csv'
|
48 |
smsv_ids_to_find = ['0103', '2903', '4002'] # List of desired smsv_ids
|
49 |
+
age_dist = get_age_distribution([2017, 2024],smsv_ids_to_find, filename)
|
50 |
|
51 |
for smsv_id, distributions in age_dist.items():
|
52 |
print(f"Age Distribution for SMSV ID: {smsv_id}")
|
data_processing/aggregate_data.py
CHANGED
@@ -1,7 +1,25 @@
|
|
|
|
|
|
|
|
1 |
# Small area id: id of the small area
|
2 |
# Density: current density of the small area
|
3 |
# Income distribution: the distribution of income in the small area per year (dictionary, keys: years, values: income distribution [buckets])
|
4 |
# Age distribution: distibution of age in the small area (age buckets of 5 years starting at 0-4)
|
5 |
# Geometry: the lat and long coordinates for the small area polygon
|
6 |
# Projected dwellings:
|
7 |
-
columns = ["smallAreaId", "density", "income_distribution_per_year", "age_distribution", "geometry", "projected_dwellings"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
from age_distribution_by_id import get_age_distribution
|
3 |
+
|
4 |
# Small area id: id of the small area
|
5 |
# Density: current density of the small area
|
6 |
# Income distribution: the distribution of income in the small area per year (dictionary, keys: years, values: income distribution [buckets])
|
7 |
# Age distribution: distibution of age in the small area (age buckets of 5 years starting at 0-4)
|
8 |
# Geometry: the lat and long coordinates for the small area polygon
|
9 |
# Projected dwellings:
|
10 |
+
columns = ["smallAreaId", "density", "income_distribution_per_year", "age_distribution", "geometry", "projected_dwellings"]
|
11 |
+
|
12 |
+
def open_file(filename):
|
13 |
+
try:
|
14 |
+
with open(filename, 'r', newline='', encoding='utf-8') as file:
|
15 |
+
csv_reader = csv.reader(file)
|
16 |
+
data = list(csv_reader)
|
17 |
+
return data
|
18 |
+
except FileNotFoundError:
|
19 |
+
print(f"File {filename} not found.")
|
20 |
+
return []
|
21 |
+
except Exception as e:
|
22 |
+
print(f"An error occurred: {e}")
|
23 |
+
return []
|
24 |
+
|
25 |
+
|