Skip to content

Commit

Permalink
107 move plotting functions to plotspy (#124)
Browse files Browse the repository at this point in the history
* Aggregating RWI by region and updating existing data if it exists

* Fixed RWI aggregation and updating

* Minor changes

* GADM+CHIRPS data being unpacked and processed for different levels and timeframes

* Changed get chirps_rainfall_data() input to be a partial date

* Completed process_chirps_rainfall_data()

* Completed and testing geospatial/chirps-rainfall and meteorological/chirps-rainfall

* Updated README

* Fixing failing tests

* Fixing failing tests

* Fixing failing tests

* Fixing failing tests

* Created plot_heatmap()

* Plotting chirps-rainfall and terraclimate

* Added tests

* Fixing failing tests

* Fixing failing tests

* Fixing failing tests

* Refactoring RWI

* Added plotting to RWI

* Minor change

* WIP

* Changed the plotting functions to produce either macro level or micro level borders

* Added tests

* Fixing failing tests

* Fixing failing tests
  • Loading branch information
rowannicholls authored Nov 21, 2024
1 parent 5d98224 commit 725e1c5
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 137 deletions.
35 changes: 18 additions & 17 deletions README.dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ Development Status
Done: ✅, in progress: ⏳, not working: ❌

`uv run dart-pipeline get`
├── ✅ `uv run dart-pipeline get economic/relative-wealth-index iso3=VNM` 6.309s
├── `uv run dart-pipeline get epidemiological/dengue/peru` 21m31.33s
├── ✅ `uv run dart-pipeline get economic/relative-wealth-index 3=VNM` 6.309s
├── `uv run dart-pipeline get epidemiological/dengue/peru` 21m31.33s
├── `uv run dart-pipeline get geospatial/gadm`
│ ├── ✅ `uv run dart-pipeline get geospatial/gadm iso3=PER`
│ └── ✅ `uv run dart-pipeline get geospatial/gadm iso3=VNM` 17.964s
├── ✅ `uv run dart-pipeline get geospatial/gadm 3=PER`
└── ✅ `uv run dart-pipeline get geospatial/gadm 3=VNM` 17.964s
├── ✅ `uv run dart-pipeline get meteorological/aphrodite-daily-mean-temp` 12m56.12s
├── ❌ `uv run dart-pipeline get meteorological/aphrodite-daily-precip` 4.551s
├── `uv run dart-pipeline get meteorological/chirps-rainfall`
│ ├── ✅ `uv run dart-pipeline get meteorological/chirps-rainfall d=2023` 7.24s
│ └── ✅ `uv run dart-pipeline get meteorological/chirps-rainfall d=2023-05` 1m32.80s
├── ✅ `uv run dart-pipeline get meteorological/chirps-rainfall d=2023` 19.750s
└── ✅ `uv run dart-pipeline get meteorological/chirps-rainfall d=2023-05` 1m32.80s
├── ✅ `uv run dart-pipeline get meteorological/terraclimate year=2023` 9m30.29s
├── ✅ `uv run dart-pipeline get sociodemographic/meta-pop-density iso3=VNM` 10m25.46s
├── ❌ `uv run dart-pipeline get sociodemographic/worldpop-count iso3=VNM`
└── ❌ `uv run dart-pipeline get sociodemographic/worldpop-density iso3=VNM`
├── ✅ `uv run dart-pipeline get sociodemographic/meta-pop-density 3=VNM` 10m25.46s
├── ❌ `uv run dart-pipeline get sociodemographic/worldpop-count 3=VNM`
└── ❌ `uv run dart-pipeline get sociodemographic/worldpop-density 3=VNM`

`uv run dart-pipeline process`
├── `uv run dart-pipeline process economic/relative-wealth-index`
Expand All @@ -29,22 +29,23 @@ Done: ✅, in progress: ⏳, not working: ❌
│ └── ✅ `uv run dart-pipeline process economic/relative-wealth-index 3=VNM a=3` 7m18.860s
├── `uv run dart-pipeline process epidemiological/dengue/peru`
├── `uv run dart-pipeline process geospatial/chirps-rainfall`
│ ├── ✅ `uv run dart-pipeline process geospatial/chirps-rainfall 3=VNM d=2023 a=0 l=INFO plots`
│ ├── ✅ `uv run dart-pipeline process geospatial/chirps-rainfall 3=VNM d=2023-05 a=0 l=INFO plots`
│ └── ✅ `uv run dart-pipeline process geospatial/chirps-rainfall 3=VNM d=2023-05-11 a=0 l=INFO plots`
│ ├── ✅ `uv run dart-pipeline process geospatial/chirps-rainfall 3=VNM d=2023 a=0 l=INFO plots` 1.503s
│ ├── ✅ `uv run dart-pipeline process geospatial/chirps-rainfall 3=VNM d=2023-05 a=0 l=INFO plots` 1.500s
│ └── ✅ `uv run dart-pipeline process geospatial/chirps-rainfall 3=VNM d=2023-05-11 a=0 l=INFO plots` 1.474s
├── ❌ `uv run dart-pipeline process geospatial/worldpop-count`
├── ✅ `uv run dart-pipeline process meteorological/aphrodite-daily-mean-temp` 0.691s
├── ❌ `uv run dart-pipeline process meteorological/aphrodite-daily-precip`
├── `uv run dart-pipeline process meteorological/chirps-rainfall`
│ ├── ✅ `uv run dart-pipeline process meteorological/chirps-rainfall d=2023 l=INFO`
│ ├── ✅ `uv run dart-pipeline process meteorological/chirps-rainfall d=2023 l=INFO` 0.825s
│ ├── ✅ `uv run dart-pipeline process meteorological/chirps-rainfall d=2023 l=INFO plots` 1.367s
│ ├── ✅ `uv run dart-pipeline process meteorological/chirps-rainfall d=2023-05 l=INFO`
│ └── ✅ `uv run dart-pipeline process meteorological/chirps-rainfall d=2023-05-11 l=INFO`
├── ❌ `uv run dart-pipeline process meteorological/era5-reanalysis`
├── `uv run dart-pipeline process meteorological/terraclimate`
│ ├── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=PER a=0 l=INFO`
│ ├── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=PER a=1 l=INFO`
│ ├── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=VNM a=0 l=INFO plots`
│ └── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=VNM a=1 l=INFO plots`
│ ├── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=PER a=0 l=INFO` 1m46.532s
│ ├── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=PER a=1 l=INFO` 2m52.306s
│ ├── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=VNM a=0 l=INFO plots` 2m22.568s
│ └── ✅ `uv run dart-pipeline process meteorological/terraclimate d=2023-01 3=VNM a=1 l=INFO plots` 4m58.389s
├── ❌ `uv run dart-pipeline process sociodemographic/worldpop-count iso3=VNM`
└── ❌ `uv run dart-pipeline process sociodemographic/worldpop-density iso3=VNM year=2023`

Expand Down
83 changes: 83 additions & 0 deletions src/dart_pipeline/plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Plot data."""
from pathlib import Path
import logging
import re

from matplotlib import pyplot as plt
import geopandas as gpd
import numpy as np

from .util import output_path


def plot_heatmap(data, title, colourbar_label, path):
"""Create a heat map."""
data[data == 0] = np.nan
plt.imshow(data, cmap='coolwarm', origin='upper')
plt.colorbar(label=colourbar_label)
plt.title(title)
# Make the plot title file-system safe
title = re.sub(r'[<>:"/\\|?*]', '_', title)
title = title.strip()
# Export
path.parent.mkdir(parents=True, exist_ok=True)
logging.info('exporting:%s', path)
plt.savefig(path)
plt.close()


def plot_gadm_micro_heatmap(
source, data, gdf, pdate, title, colourbar_label, region, extent
):
"""Create a heat map with GADM region overlaid."""
geometry = region.geometry
min_lon, min_lat, max_lon, max_lat = geometry.bounds
_, ax = plt.subplots()
im = ax.imshow(data, cmap='coolwarm', origin='upper', extent=extent)
# Add the geographical borders
gdf.plot(ax=ax, color='none', edgecolor='gray')
gpd.GeoDataFrame([region]).plot(ax=ax, color='none', edgecolor='k')
# Add colour bar
plt.colorbar(im, ax=ax, label=colourbar_label)
# Titles and axes
ax.set_title(title)
ax.set_xlim(min_lon, max_lon)
ax.set_ylim(min_lat, max_lat)
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
# Make the plot title file-system safe
title = re.sub(r'[<>:"/\\|?*]', '_', title)
title = title.strip()
# Export
path = Path(
output_path(source), str(pdate).replace('-', '/'), title + '.png'
)
path.parent.mkdir(parents=True, exist_ok=True)
logging.info('exporting:%s', path)
plt.savefig(path)
plt.close()


def plot_gadm_macro_heatmap(
data, origin, extent, limits, gdf, zorder, title, colourbar_label, path
):
"""Create a heat map with a macro GADM region overlaid."""
_, ax = plt.subplots()
gdf.boundary.plot(ax=ax, edgecolor='k', linewidth=0.5, zorder=zorder)
im = ax.imshow(data, cmap='coolwarm', origin=origin, extent=extent)
# Add colour bar
plt.colorbar(im, ax=ax, label=colourbar_label)
# Titles and axes
ax.set_title(title)
ax.set_xlim(limits[0], limits[2])
ax.set_ylim(limits[1], limits[3])
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
# Make the plot title file-system safe
title = re.sub(r'[<>:"/\\|?*]', '_', title)
title = title.strip()
# Export
path.parent.mkdir(parents=True, exist_ok=True)
logging.info('exporting:%s', path)
plt.savefig(path)
plt.close()
Loading

0 comments on commit 725e1c5

Please sign in to comment.