Comment on page
WHI Create indicator
Tags: #wsr #whi #indicators #opendata #worldsituationroom #analytics #dataframe #image
Last update: 2023-04-12 (Created: 2022-03-10)
Description: This notebook creates an indicator to measure the performance of the WSR-WHI portfolio.
import pandas as pd
from PIL import Image, ImageDraw, ImageFont
from datetime import date
# Input extracted from your open source data
data = [
{
"DATE_PROCESSED": "2021-05-28",
"INDICATOR": "COVID-19 Active Cases",
"VALUE": 0.21,
"WEIGHT": 4,
},
{
"DATE_PROCESSED": "2021-05-28",
"INDICATOR": "Sea Level",
"VALUE": 4.951165245651996,
"WEIGHT": 2,
},
{
"DATE_PROCESSED": "2021-06-10",
"INDICATOR": "Delta global temperature",
"VALUE": 4.9,
"WEIGHT": 4,
},
{
"DATE_PROCESSED": "2021-06-10",
"INDICATOR": "Arctic Sea Ice level (million square km)",
"VALUE": 4.9,
"WEIGHT": 2,
},
]
# Input image
input_image = "layout.png"
# Input font
input_font = "ArchivoBlack-Regular.ttf"
# Output image
output_image = "WHI.png"
df = pd.DataFrame(data)
df
def whi(df):
return round((df["VALUE"] * df["WEIGHT"]).sum() / df["WEIGHT"].sum(), 2)
whi(df)
def create_image(value, datetime):
img = Image.open(input_image)
d = ImageDraw.Draw(img)
font = ImageFont.truetype(input_font, 90)
fill = (255, 255, 255)
d.text(
(50, 900),
"{indicator}/10, {date}".format(
date=datetime.strftime("%d/%m/%Y"), indicator=value
),
font=font,
fill=fill,
)
return img
img = create_image(f"{whi(df)}", date.today())
display(img)
img.save(output_image)
naas.asset.add(output_image)
# -> Uncomment the line below to remove your asset
# naas.asset.delete(output_image)
Last modified 3mo ago