Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SNT Process/
.vscode/
venv/
uv.lock
__pycache__/

# Jupyter stuff -------------------------

Expand Down
367 changes: 156 additions & 211 deletions pipelines/snt_map_extracts/reporting/snt_map_extracts_report.ipynb
Original file line number Diff line number Diff line change
@@ -1,216 +1,161 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5777b72c-d87e-47c5-87b1-2698a6510b2f",
"metadata": {},
"source": [
"# **Cartes extraites du Malaria Atlas Project (MAP)**"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6397ab91-1ae4-4db7-b6c3-061c453a7b03",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# Set SNT Paths\n",
"SNT_ROOT_PATH <- \"~/workspace\"\n",
"CODE_PATH <- file.path(SNT_ROOT_PATH, \"code\")\n",
"CONFIG_PATH <- file.path(SNT_ROOT_PATH, \"configuration\")\n",
"PIPELINE_PATH <- file.path(SNT_ROOT_PATH, \"pipelines\", \"snt_map_extracts\")\n",
"\n",
"# load util functions\n",
"source(file.path(CODE_PATH, \"snt_utils.r\"))\n",
"source(file.path(PIPELINE_PATH, \"utils\", \"snt_map_extracts_report.r\"))\n",
"\n",
"# List required packages\n",
"required_packages <- c(\"dplyr\", \"tidyr\", \"terra\", \"ggplot2\", \"stringr\", \"lubridate\", \"viridis\", \"patchwork\", \"zoo\", \"purrr\", \"arrow\", \"sf\", \"reticulate\")\n",
"\n",
"# Execute function\n",
"install_and_load(required_packages)\n",
"\n",
"# Set environment to load openhexa.sdk from the right environment\n",
"Sys.setenv(RETICULATE_PYTHON = \"/opt/conda/bin/python\")\n",
"reticulate::py_config()$python\n",
"openhexa <- import(\"openhexa.sdk\")\n",
"\n",
"# Load SNT config\n",
"config_json <- tryCatch({ jsonlite::fromJSON(file.path(CONFIG_PATH, \"SNT_config.json\"))},\n",
" error = function(e) {\n",
" msg <- paste0(\"Error while loading configuration\", conditionMessage(e))\n",
" cat(msg)\n",
" stop(msg)\n",
" })\n",
"\n",
"# Required environment for the sf packages\n",
"Sys.setenv(PROJ_LIB = \"/opt/conda/share/proj\")\n",
"Sys.setenv(GDAL_DATA = \"/opt/conda/share/gdal\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93e04996-13ba-4855-a1d1-46e70ba4640e",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# Configuration variables\n",
"DATASET_NAME <- config_json$SNT_DATASET_IDENTIFIERS$SNT_MAP_EXTRACT\n",
"COUNTRY_CODE <- config_json$SNT_CONFIG$COUNTRY_CODE\n",
"ADM_2 <- toupper(config_json$SNT_CONFIG$DHIS2_ADMINISTRATION_2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1ee21d1-c7d1-4893-ac56-91abb92926ea",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# printdim() loaded from utils/snt_map_extracts_report.r"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7de799e-896c-4237-a9f6-9dafc0f30bde",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# import seasonality data\n",
"map_data <- load_map_report_input(\n",
" dataset_name = DATASET_NAME,\n",
" filename = paste0(COUNTRY_CODE, \"_map_data.parquet\"),\n",
" label = \"MAP extracted data\"\n",
")\n",
"\n",
"# import DHIS2 shapes data\n",
"DATASET_DHIS2 <- config_json$SNT_DATASET_IDENTIFIERS$DHIS2_DATASET_FORMATTED\n",
"shapes_data <- load_map_report_input(\n",
" dataset_name = DATASET_DHIS2,\n",
" filename = paste0(COUNTRY_CODE, \"_shapes.geojson\"),\n",
" label = \"DHIS2 shapes data\"\n",
")\n",
"\n",
"printdim(map_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84031c7e-e9c6-4496-896f-7f7f3403d951",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"names(map_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66b05b53-3f65-424a-af22-0686238a06c9",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"unique(map_data$METRIC_NAME)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d0515ab-4dc2-4671-8c3c-236578a840d8",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# Merge geometry with map data\n",
"map_data_joined <- dplyr::left_join(shapes_data, map_data, by = c(\"ADM2_ID\" = \"ADM2_ID\"))\n",
"\n",
"# Get list of metrics\n",
"metrics <- unique(map_data$METRIC_NAME)\n",
"\n",
"# Create one map per metric\n",
"plots <- build_metric_plots(map_data_joined = map_data_joined, metrics = metrics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f1dc1df-211d-4174-83ae-e8ae974fa790",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# Set plot size for individual display\n",
"options(repr.plot.width = 10, repr.plot.height = 8)\n",
"\n",
"# Loop through plots and print one by one\n",
"for (p in plots) {\n",
" print(p)\n",
" Sys.sleep(1) # Optional: short pause between plots\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50c843e4-9157-480d-acde-80887410d156",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": []
"cells": [
{
"cell_type": "markdown",
"id": "5777b72c-d87e-47c5-87b1-2698a6510b2f",
"metadata": {},
"source": [
"# **Cartes extraites du Malaria Atlas Project (MAP)**"
]
},
{
"cell_type": "markdown",
"id": "f0860675-9819-4a0a-b0ce-23f1b16d40c3",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dfb0dee9-0da8-4afd-9f3a-7e8f0a830f85",
"metadata": {},
"outputs": [],
"source": [
"source(file.path(\"~/workspace/pipelines/snt_map_extracts/utils/snt_map_extracts_report.r\"))\n",
"setup_var <- get_setup_variables(packages= c(\"arrow\", \"dplyr\", \"tidyr\", \"stringr\", \"stringi\", \"jsonlite\", \"httr\", \"reticulate\", \"glue\"))\n",
"config_json <- load_snt_config(file.path(setup_var$CONFIG_PATH, \"SNT_config.json\"))\n",
"\n",
"# Save config variables\n",
"DATASET_MAP <- config_json$SNT_DATASET_IDENTIFIERS$SNT_MAP_EXTRACTS\n",
"DATASET_FORMATTED <- config_json$SNT_DATASET_IDENTIFIERS$DHIS2_DATASET_FORMATTED\n",
"COUNTRY_CODE <- config_json$SNT_CONFIG$COUNTRY_CODE"
Comment thread
EstebanMontandon marked this conversation as resolved.
]
},
{
"cell_type": "markdown",
"id": "6b3456da-ddfd-4b9b-9c9b-506f583aab3e",
"metadata": {},
"source": [
"## Load Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7de799e-896c-4237-a9f6-9dafc0f30bde",
"metadata": {
"vscode": {
"languageId": "r"
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.4.3"
},
"outputs": [],
"source": [
"# Load parameters\n",
"map_parameters <- load_dataset_file(dataset_id = DATASET_MAP, filename = paste0(COUNTRY_CODE, \"_parameters.json\"))\n",
"cat(jsonlite::toJSON(map_parameters, pretty = TRUE, auto_unbox = TRUE), \"\\n\")\n",
"\n",
"# Load latest year file available\n",
"map_data <- load_dataset_file(dataset_id = DATASET_MAP, filename = glue(\"{COUNTRY_CODE}_map_data_{map_parameters$YEAR_END}.parquet\"))\n",
"\n",
"# import DHIS2 shapes data\n",
"shapes_data <- load_dataset_file(dataset_id = DATASET_FORMATTED, filename = paste0(COUNTRY_CODE, \"_shapes.geojson\"))\n",
"\n",
"printdim(map_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66b05b53-3f65-424a-af22-0686238a06c9",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"print(glue(\"Year file selection: {map_parameters$YEAR_END}\"))\n",
"print(glue(\"Indicators: {unique(map_data$METRIC_NAME)}\"))"
]
},
{
"cell_type": "markdown",
"id": "0ea5587e-b7d3-47c1-a4b3-c69061d24bca",
"metadata": {},
"source": [
"## Create plots \n",
"\n",
"We select the latest year of the selected years based on the parameters run."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d0515ab-4dc2-4671-8c3c-236578a840d8",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# Merge geometry with map data\n",
"map_data_joined <- dplyr::left_join(shapes_data, map_data, by = c(\"ADM2_ID\" = \"ADM2_ID\"))\n",
"\n",
"# Get list of metrics\n",
"metrics <- unique(map_data$METRIC_NAME)\n",
"\n",
"# Create one map per metric\n",
"plots <- build_metric_plots(map_data_joined = map_data_joined, metrics = metrics, year=map_parameters$YEAR_END)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f1dc1df-211d-4174-83ae-e8ae974fa790",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"# Set plot size for individual display\n",
"options(repr.plot.width = 10, repr.plot.height = 8)\n",
"\n",
"# Loop through plots and print one by one\n",
"for (p in plots) {\n",
" print(p)\n",
" Sys.sleep(1) # Optional: short pause between plots\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8eb9cc86-f3ed-45f2-84f3-9e395ef85106",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"nbformat": 4,
"nbformat_minor": 5
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading