Skip to content

Commit

Permalink
mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasrodes committed Dec 18, 2024
1 parent 4caa167 commit 54bfaee
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
11 changes: 9 additions & 2 deletions dag/demography.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,22 @@ steps:
- data://garden/hmd/2024-12-01/hmd
data://grapher/hmd/2024-12-03/hmd_country:
- data://garden/hmd/2024-12-03/hmd_country

# Patternal ages (Kai & Klüssner)
data://meadow/demography/2024-12-05/paternal_ages:
- snapshot://demography/2024-12-04/paternal_ages.rdata
data://garden/demography/2024-12-05/paternal_ages:
- data://meadow/demography/2024-12-05/paternal_ages
data://grapher/demography/2024-12-05/paternal_ages:
- data://garden/demography/2024-12-05/paternal_ages


# Mean Age at childbirth (HFD + UN WPP)
data://garden/demography/2024-12-18/mean_age_childbearing:
- data://garden/un/2024-07-12/un_wpp
- data://garden/hmd/2024-11-19/hfd
data://grapher/demography/2024-12-18/mean_age_childbearing:
- data://garden/demography/2024-12-18/mean_age_childbearing

########################################################################
# OTHERS
########################################################################
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# NOTE: To learn more about the fields, hover over their names.
definitions:
common:
presentation:
topic_tags:
- Fertility Rate

# Learn more about the available fields:
# http://docs.owid.io/projects/etl/architecture/metadata/reference/
dataset:
update_period_days: 365
title: Mean age at childbearing (HFD; UN WPP)

tables:
mean_age_childbearing:
variables:
mean_age_childbearing:
title: Mean age at childbearing
unit: years
description_short: &cb_description_short |-
Mean age of mothers at the birth of their children if women were subject throughout their lives to the age-specific fertility rates observed in a given year. UN Medium projections for 2024-2100.
description_processing: |-
This indicator is constructed by combining data from multiple sources:
- Before 1949: Historical estimates by Human Fertility Database (2024).
- 1950-2023: Population records by the UN World Population Prospects (2024 revision).
- 2024-2100: Projections based on Medium variant by the UN World Population Prospects (2024 revision).
presentation:
title_public: |-
Mean age at childbearing
grapher_config:
subtitle: *cb_description_short

mean_age_childbearing_hist:
title: Mean age at childbearing, historical
unit: years
description_short: &cb_description_short_hist |-
Mean age of mothers at the birth of their children if women were subject throughout their lives to the age-specific fertility rates observed in a given year.
description_processing: |-
This indicator is constructed by combining data from multiple sources:
- Before 1949: Historical estimates by Human Fertility Database (2024).
- 1950-2023: Population records by the UN World Population Prospects (2024 revision).
presentation:
title_public: |-
Mean age at childbearing
grapher_config:
subtitle: *cb_description_short_hist
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Load a meadow dataset and create a garden dataset."""

import pandas as pd
from owid.catalog import processing as pr

from etl.helpers import PathFinder, create_dataset

# Get paths and naming conventions for current step.
paths = PathFinder(__file__)


# Year constants
YEAR_WPP_START = 1950
YEAR_WPP_PROJ_START = 2023
# Table names
TABLE_NAME_WPP = "mean_age_childbearing"
TABLE_NAME_HFD = "period"
TABLE_NAME_NEW = "mean_age_childbearing"
# Metric names
COLUMN_NAME_WPP = "mean_age_childbearing"
COLUMN_NAME_HFD = "mab"
COLUMN_NEW_NAME = "mean_age_childbearing"


def run(dest_dir: str) -> None:
#
# Load inputs.
#
# Load meadow dataset.
ds_hfd = paths.load_dataset("hfd")
ds_un = paths.load_dataset("un_wpp")

# Read table from meadow dataset.
tb_hfd = ds_hfd.read(TABLE_NAME_HFD)
tb_un = ds_un.read(TABLE_NAME_WPP)

# UN: estimates + medium,
tb_un = tb_un.loc[
(tb_un["sex"] == "all") & (tb_un["variant"].isin(["medium", "estimates"]) & (tb_un["age"] == "all")),
["country", "year", COLUMN_NAME_WPP],
].rename(columns={COLUMN_NAME_WPP: COLUMN_NEW_NAME})

# HFD: tfr, birth_order=total,
tb_hfd = tb_hfd.loc[
((tb_hfd["birth_order"] == "total") & (tb_hfd["year"] < YEAR_WPP_START)), ["country", "year", COLUMN_NAME_HFD]
].rename(columns={COLUMN_NAME_HFD: COLUMN_NEW_NAME})

# Concatenate
tb = pr.concat([tb_hfd, tb_un], ignore_index=True, short_name=TABLE_NAME_NEW)

# Add historical variant
tb[f"{COLUMN_NEW_NAME}_hist"] = tb[COLUMN_NEW_NAME].copy()
tb.loc[tb["year"] > YEAR_WPP_PROJ_START, f"{COLUMN_NEW_NAME}_hist"] = pd.NA

# Format
tb = tb.format(["country", "year"])

#
# Save outputs.
#
# Create a new garden dataset with the same metadata as the meadow dataset.
ds_garden = create_dataset(
dest_dir,
tables=[tb],
check_variables_metadata=True,
)

# Save changes in the new garden dataset.
ds_garden.save()
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Load a garden dataset and create a grapher dataset."""

from etl.helpers import PathFinder, create_dataset

# Get paths and naming conventions for current step.
paths = PathFinder(__file__)


def run(dest_dir: str) -> None:
#
# Load inputs.
#
# Load garden dataset.
ds_garden = paths.load_dataset("mean_age_childbearing")

# Read table from garden dataset.
tb = ds_garden.read("mean_age_childbearing", reset_index=False)

#
# Save outputs.
#
# Create a new grapher dataset with the same metadata as the garden dataset.
ds_grapher = create_dataset(
dest_dir, tables=[tb], check_variables_metadata=True, default_metadata=ds_garden.metadata
)

# Save changes in the new grapher dataset.
ds_grapher.save()

0 comments on commit 54bfaee

Please sign in to comment.