WHI Create indicator
Tags: #whi #indicators #opendata #worldsituationroom #analytics
Author: Peter Turner

Input

Import libraries

1
import pandas as pd
2
from PIL import Image, ImageDraw, ImageFont
3
from datetime import date
Copied!

Variables

1
# Input extracted from your open source data
2
data = [
3
{'DATE_PROCESSED': '2021-05-28', 'INDICATOR': 'COVID-19 Active Cases', 'VALUE': 0.21, 'WEIGHT': 4},
4
{'DATE_PROCESSED': '2021-05-28', 'INDICATOR': 'Sea Level', 'VALUE': 4.951165245651996, 'WEIGHT': 2},
5
{'DATE_PROCESSED': '2021-06-10', 'INDICATOR': 'Delta global temperature', 'VALUE': 4.9, 'WEIGHT': 4},
6
{'DATE_PROCESSED': '2021-06-10', 'INDICATOR': 'Arctic Sea Ice level (million square km)', 'VALUE': 4.9, 'WEIGHT': 2}
7
]
8
9
# Input image
10
input_image = "layout.png"
11
12
# Input font
13
input_font = "ArchivoBlack-Regular.ttf"
14
15
# Output image
16
output_image = "WHI.png"
Copied!

Model

Get the name of the files present in output

1
df = pd.DataFrame(data)
2
df
Copied!

Get weighted WHI

1
def whi(df):
2
return round((df['VALUE']*df['WEIGHT']).sum() / df['WEIGHT'].sum(), 2)
3
4
whi(df)
Copied!

Create output image

1
def create_image(value, datetime):
2
img = Image.open(input_image)
3
d = ImageDraw.Draw(img)
4
5
font = ImageFont.truetype(input_font, 90)
6
fill = (255,255,255)
7
8
d.text((50,900), "{indicator}/10, {date}".format(date=datetime.strftime("%d/%m/%Y"), indicator=value), font=font, fill=fill)
9
return img
Copied!

Output

Display image

1
img = create_image(f'{whi(df)}' , date.today())
2
display(img)
Copied!

Save and share image

1
img.save(output_image)
2
3
naas.asset.add(output_image)
4
5
#-> Uncomment the line below to remove your asset
6
# naas.asset.delete(output_image)
Copied!
Copy link
Edit on GitHub