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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
required_libraries = ["streamlit", "pandas", "plotly", "altair"]
|
| 6 |
+
|
| 7 |
+
for lib in required_libraries:
|
| 8 |
+
try:
|
| 9 |
+
__import__(lib)
|
| 10 |
+
except ImportError:
|
| 11 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
|
| 12 |
+
|
| 13 |
+
import streamlit as st
|
| 14 |
+
import pandas as pd
|
| 15 |
+
import plotly.express as px
|
| 16 |
+
import altair as alt
|
| 17 |
+
|
| 18 |
+
child_mortality_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/child_mortality_0_5_year_olds_dying_per_1000_born.csv"
|
| 19 |
+
life_expectancy_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/life_expectancy.csv"
|
| 20 |
+
population_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/pop.csv"
|
| 21 |
+
income_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/mincpcap_cppp.csv"
|
| 22 |
+
|
| 23 |
+
child_mortality = pd.read_csv(child_mortality_url)
|
| 24 |
+
life_expectancy = pd.read_csv(life_expectancy_url)
|
| 25 |
+
population = pd.read_csv(population_url)
|
| 26 |
+
income = pd.read_csv(income_url)
|
| 27 |
+
|
| 28 |
+
def convert_population(value):
|
| 29 |
+
if isinstance(value, str):
|
| 30 |
+
if 'B' in value:
|
| 31 |
+
return float(value.replace('B', '')) * 1_000_000_000
|
| 32 |
+
elif 'M' in value:
|
| 33 |
+
return float(value.replace('M', '')) * 1_000_000
|
| 34 |
+
elif 'k' in value:
|
| 35 |
+
return float(value.replace('k', '')) * 1_000
|
| 36 |
+
else:
|
| 37 |
+
return float(value)
|
| 38 |
+
return value
|
| 39 |
+
|
| 40 |
+
population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
|
| 41 |
+
|
| 42 |
+
# 1. Choropleth Map
|
| 43 |
+
st.title("Analyzing Global Child Mortality")
|
| 44 |
+
st.write("By Jiya Chachan, Smeet Patel, Ji Eun Kim, Miloni Shah, Chenzhao Wang")
|
| 45 |
+
data_melted = child_mortality.melt(id_vars=["country"], var_name="year", value_name="mortality_rate")
|
| 46 |
+
data_melted["year"] = pd.to_numeric(data_melted["year"])
|
| 47 |
+
min_year = int(data_melted["year"].min())
|
| 48 |
+
max_year = int(data_melted["year"].max())
|
| 49 |
+
selected_year = st.slider("Select Year", min_value=min_year, max_value=max_year, value=2024, step=5)
|
| 50 |
+
filtered_data = data_melted[data_melted["year"] == selected_year]
|
| 51 |
+
|
| 52 |
+
st.write("""
|
| 53 |
+
### Child Mortality Map - per 1000 children born
|
| 54 |
+
This chart reveals an important trend of how the child mortality rate has been changing across the years.
|
| 55 |
+
It provides insights into how developed countries have successfully reduced the rate, while underdeveloped countries still face challenges in curbing child mortality.
|
| 56 |
+
We can utilize the trends in the graph to understand the factors responsible for high or low mortality.
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
fig1 = px.choropleth(
|
| 60 |
+
filtered_data,
|
| 61 |
+
locations="country",
|
| 62 |
+
locationmode="country names",
|
| 63 |
+
color="mortality_rate",
|
| 64 |
+
title=f"Child Mortality Rate in {selected_year}",
|
| 65 |
+
color_continuous_scale=px.colors.sequential.OrRd,
|
| 66 |
+
)
|
| 67 |
+
st.plotly_chart(fig1)
|
| 68 |
+
|
| 69 |
+
# 2. Visual 2: Child Mortality Trends (Altair Line Chart)
|
| 70 |
+
|
| 71 |
+
countries = list(child_mortality['country'].unique())
|
| 72 |
+
selected_countries = st.sidebar.multiselect("Select Countries", countries, default=["Argentina", "Australia", "India", "USA"])
|
| 73 |
+
years = [str(year) for year in range(1900, 2025)]
|
| 74 |
+
filtered_mortality = child_mortality[child_mortality['country'].isin(selected_countries)][['country'] + years]
|
| 75 |
+
mortality_melted = filtered_mortality.melt(id_vars="country", var_name="year", value_name="mortality_rate")
|
| 76 |
+
|
| 77 |
+
st.write("""
|
| 78 |
+
### Child Mortality Trends
|
| 79 |
+
This line chart shows the trends in child mortality rates over the years for selected countries.
|
| 80 |
+
""")
|
| 81 |
+
|
| 82 |
+
mortality_chart = alt.Chart(mortality_melted).mark_line().encode(
|
| 83 |
+
x="year:O",
|
| 84 |
+
y="mortality_rate:Q",
|
| 85 |
+
color="country:N",
|
| 86 |
+
tooltip=["country", "year", "mortality_rate"]
|
| 87 |
+
).properties(width=700, height=400)
|
| 88 |
+
st.altair_chart(mortality_chart)
|
| 89 |
+
|
| 90 |
+
# 3. Visual 3: Child Mortality vs. Population
|
| 91 |
+
|
| 92 |
+
selected_country = st.selectbox("Country for Population vs. Mortality", countries, index=0)
|
| 93 |
+
mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
|
| 94 |
+
id_vars='country', var_name='year', value_name='child_mortality'
|
| 95 |
+
)
|
| 96 |
+
population_country = population[population['country'] == selected_country].melt(
|
| 97 |
+
id_vars='country', var_name='year', value_name='population'
|
| 98 |
+
)
|
| 99 |
+
merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
|
| 100 |
+
merged_country_data['year'] = merged_country_data['year'].astype(int)
|
| 101 |
+
|
| 102 |
+
st.write("""
|
| 103 |
+
### Child Mortality vs Population
|
| 104 |
+
This visualization shows the relationship between child mortality rates (per 1,000 live births) and population size over time for a selected country.
|
| 105 |
+
Displayed on a dual-y-axis chart, it allows users to explore how both metrics have evolved across decades.
|
| 106 |
+
The interactive feature lets users choose a country, and hovering over the chart reveals detailed tooltips with year, population size, and child mortality rate information.
|
| 107 |
+
""")
|
| 108 |
+
dual_axis_chart = alt.layer(
|
| 109 |
+
alt.Chart(merged_country_data).mark_line().encode(
|
| 110 |
+
x="year:O",
|
| 111 |
+
y="child_mortality:Q",
|
| 112 |
+
tooltip=["year", "child_mortality"]
|
| 113 |
+
),
|
| 114 |
+
alt.Chart(merged_country_data).mark_line(color='orange').encode(
|
| 115 |
+
x="year:O",
|
| 116 |
+
y="population:Q",
|
| 117 |
+
tooltip=["year", "population"]
|
| 118 |
+
)
|
| 119 |
+
).resolve_scale(y='independent').properties(width=700, height=400)
|
| 120 |
+
st.altair_chart(dual_axis_chart)
|
| 121 |
+
|
| 122 |
+
# 4. Visual 4: Child Mortality vs. Income
|
| 123 |
+
income_long = pd.melt(income, id_vars=['country'], var_name='year', value_name='income')
|
| 124 |
+
mortality_long = pd.melt(child_mortality, id_vars=['country'], var_name='year', value_name='mortality')
|
| 125 |
+
income_long['year'] = income_long['year'].astype(int)
|
| 126 |
+
mortality_long['year'] = mortality_long['year'].astype(int)
|
| 127 |
+
yay = pd.merge(income_long, mortality_long, on=['country', 'year'])
|
| 128 |
+
yeyear = st.slider("Year for Income vs. Mortality", min_value=1800, max_value=2024, value=2024)
|
| 129 |
+
filtered_yay = yay[yay["year"] == yeyear]
|
| 130 |
+
|
| 131 |
+
st.write("""
|
| 132 |
+
### Child Mortality vs Daily Income
|
| 133 |
+
The interactive scatterplot compares child mortality to daily income for 24 countries, with a slider to select a year between 1800 and 2024.
|
| 134 |
+
The x-axis represents daily income (USD) on a logarithmic scale, and the y-axis shows child mortality (per 1,000).
|
| 135 |
+
Each country is color-coded, and hover tooltips display details for each data point.
|
| 136 |
+
Our analysis reveals that in earlier years (e.g., 1800), countries have tightly clustered mortality and income.
|
| 137 |
+
By 2024, a negative correlation emerges, showing that as child mortality decreases, daily income increases.
|
| 138 |
+
""")
|
| 139 |
+
scatter_plot = alt.Chart(filtered_yay).mark_circle(size=60).encode(
|
| 140 |
+
x=alt.X('income', title='Daily Income (USD)', scale=alt.Scale(type='log')),
|
| 141 |
+
y=alt.Y('mortality', title='Child Mortality (per 1,000)'),
|
| 142 |
+
color='country',
|
| 143 |
+
tooltip=['country', 'year', 'income', 'mortality']
|
| 144 |
+
).properties(width=700, height=400)
|
| 145 |
+
st.altair_chart(scatter_plot)
|
| 146 |
+
|
| 147 |
+
# 5. Visual 5: Life Expectancy Trends
|
| 148 |
+
filtered_expectancy = life_expectancy[life_expectancy['country'].isin(selected_countries)]
|
| 149 |
+
expectancy_melted = filtered_expectancy.melt(id_vars="country", var_name="year", value_name="life_expectancy")
|
| 150 |
+
st.write("""
|
| 151 |
+
### Life Expectancy Trends
|
| 152 |
+
The scatter plot reveals a strong inverse relationship between child mortality and life expectancy, emphasizing how improvements in healthcare, nutrition, and living conditions enhance both indicators.
|
| 153 |
+
Notably, countries like India and South Africa show steep curves, reflecting rapid progress in life expectancy as child mortality declines.
|
| 154 |
+
Developed nations such as Australia and the USA exhibit a plateau, with incremental life expectancy gains as child mortality remains low.
|
| 155 |
+
""")
|
| 156 |
+
expectancy_chart = alt.Chart(expectancy_melted).mark_line().encode(
|
| 157 |
+
x="year:O",
|
| 158 |
+
y="life_expectancy:Q",
|
| 159 |
+
color="country:N",
|
| 160 |
+
tooltip=["country", "year", "life_expectancy"]
|
| 161 |
+
).properties(width=700, height=400)
|
| 162 |
+
st.altair_chart(expectancy_chart)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
st.subheader("Child Mortality vs. Life Expectancy")
|
| 166 |
+
st.write("""
|
| 167 |
+
This scatter plot illustrates the strong inverse relationship between child mortality
|
| 168 |
+
rates and life expectancy at birth, underscoring how advancements in healthcare,
|
| 169 |
+
nutrition, and living conditions improve both indicators simultaneously. A particularly
|
| 170 |
+
striking observation is how the data for countries like India and South Africa forms a
|
| 171 |
+
steep curve, signifying rapid improvements in life expectancy as child mortality declines.
|
| 172 |
+
Developed nations such as Australia and the USA show a plateau in the later stages, where
|
| 173 |
+
child mortality rates are already low, and life expectancy improvements are incremental.
|
| 174 |
+
The trajectory of China’s data during the mid-20th century highlights a rapid transition,
|
| 175 |
+
likely due to systemic public health efforts.
|
| 176 |
+
""")
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
mortality_long = pd.melt(child_mortality, id_vars=["country"], var_name="year", value_name="child_mortality")
|
| 180 |
+
expectancy_long = pd.melt(life_expectancy, id_vars=["country"], var_name="year", value_name="life_expectancy")
|
| 181 |
+
|
| 182 |
+
mortality_long["year"] = mortality_long["year"].astype(int)
|
| 183 |
+
expectancy_long["year"] = expectancy_long["year"].astype(int)
|
| 184 |
+
|
| 185 |
+
merged_data = pd.merge(mortality_long, expectancy_long, on=["country", "year"])
|
| 186 |
+
merged_data = merged_data.dropna(subset=["child_mortality", "life_expectancy"])
|
| 187 |
+
|
| 188 |
+
default_countries = ["Argentina", "Australia", "China", "India", "South Africa", "UK", "USA"]
|
| 189 |
+
|
| 190 |
+
countries = merged_data["country"].unique()
|
| 191 |
+
selected_countries = st.multiselect("Select Countries", countries, default=default_countries)
|
| 192 |
+
filtered_data = merged_data[merged_data["country"].isin(selected_countries)]
|
| 193 |
+
|
| 194 |
+
scatter_chart = alt.Chart(filtered_data).mark_circle(size=60).encode(
|
| 195 |
+
x=alt.X("life_expectancy:Q", title="Life Expectancy at Birth"),
|
| 196 |
+
y=alt.Y("child_mortality:Q", title="Child Mortality (0–5 years per 1000 births)"),
|
| 197 |
+
color="country:N",
|
| 198 |
+
tooltip=["country", "year", "child_mortality", "life_expectancy"]
|
| 199 |
+
).properties(width=700, height=400)
|
| 200 |
+
|
| 201 |
+
st.altair_chart(scatter_chart)
|