Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| import subprocess | |
| required_libraries = ["streamlit", "pandas", "plotly", "altair"] | |
| for lib in required_libraries: | |
| try: | |
| __import__(lib) | |
| except ImportError: | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", lib]) | |
| import streamlit as st | |
| import pandas as pd | |
| import plotly.express as px | |
| import altair as alt | |
| child_mortality_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/child_mortality_0_5_year_olds_dying_per_1000_born.csv" | |
| life_expectancy_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/life_expectancy.csv" | |
| population_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/pop.csv" | |
| income_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/mincpcap_cppp.csv" | |
| child_mortality = pd.read_csv(child_mortality_url) | |
| life_expectancy = pd.read_csv(life_expectancy_url) | |
| population = pd.read_csv(population_url) | |
| income = pd.read_csv(income_url) | |
| def convert_population(value): | |
| if isinstance(value, str): | |
| if 'B' in value: | |
| return float(value.replace('B', '')) * 1_000_000_000 | |
| elif 'M' in value: | |
| return float(value.replace('M', '')) * 1_000_000 | |
| elif 'k' in value: | |
| return float(value.replace('k', '')) * 1_000 | |
| else: | |
| return float(value) | |
| return value | |
| population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population) | |
| # 1. Choropleth Map | |
| st.title("Analyzing Global Child Mortality") | |
| st.write("By Jiya Chachan, Smeet Patel, Ji Eun Kim, Miloni Shah, Chenzhao Wang") | |
| data = pd.read_csv("https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/child_mortality_0_5_year_olds_dying_per_1000_born.csv") | |
| st.write(" ") | |
| st.write("Dataset: Child Mortality") | |
| st.dataframe(data) | |
| st.write("""Credits: https://www.gapminder.org/data/""") | |
| data_melted = child_mortality.melt(id_vars=["country"], var_name="year", value_name="mortality_rate") | |
| data_melted["year"] = pd.to_numeric(data_melted["year"]) | |
| min_year = int(data_melted["year"].min()) | |
| max_year = int(data_melted["year"].max()) | |
| st.write(""" | |
| ### Chart 1: Child Mortality Map - per 1000 children born | |
| This Chart reveals an important trend of how the child mortality rate have been changing across the years. | |
| This gives us a very important insight on how the present developed countries have successfully reduced the rate, and underdeveloped countries still faces challenges to curb child mortality successfully. | |
| We can utilise the trends in the graph to understand the factors which might be the responsible for high mortality or low mortality. | |
| This will help the policymakers in developing/under-developed countries to develop data-driven policy to reduce child mortality. | |
| """) | |
| selected_year = st.slider("Select Year", min_value=min_year, max_value=max_year, value=2024, step=5) | |
| filtered_data = data_melted[data_melted["year"] == selected_year] | |
| fig1 = px.choropleth( | |
| filtered_data, | |
| locations="country", | |
| locationmode="country names", | |
| color="mortality_rate", | |
| title=f"Child Mortality Rate in {selected_year}", | |
| color_continuous_scale=px.colors.sequential.OrRd, | |
| ) | |
| st.plotly_chart(fig1) | |
| # 2. Visual 2: Child Mortality Trends (Altair Line Chart) | |
| child_mortality = pd.read_csv("https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/child_mortality_0_5_year_olds_dying_per_1000_born.csv") | |
| life_expectancy = pd.read_csv("https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/life_expectancy.csv") | |
| countries = list(child_mortality['country'].unique()) | |
| selected_countries = st.sidebar.multiselect("Select Countries", countries, default=["Argentina", "Australia", "China", "India", "South Africa", "UK", "USA"]) | |
| year_range = st.sidebar.slider("Select Year Range", 1900, 2024, (1900, 2024)) | |
| filtered_mortality = child_mortality[child_mortality['country'].isin(selected_countries)] | |
| filtered_expectancy = life_expectancy[life_expectancy['country'].isin(selected_countries)] | |
| years = [str(year) for year in range(year_range[0], year_range[1] + 1)] | |
| filtered_mortality = filtered_mortality[['country'] + years] | |
| filtered_expectancy = filtered_expectancy[['country'] + years] | |
| def melt_dataframe(df, value_name): | |
| df_melted = df.melt(id_vars=["country"], var_name="year", value_name=value_name) | |
| df_melted["year"] = df_melted["year"].astype(int) | |
| return df_melted | |
| mortality_melted = melt_dataframe(filtered_mortality, "Child Mortality") | |
| expectancy_melted = melt_dataframe(filtered_expectancy, "Life Expectancy") | |
| # countries = list(child_mortality['country'].unique()) | |
| # selected_countries = st.sidebar.multiselect("Select Countries", countries, default=["Argentina", "Australia", "India", "USA"]) | |
| # years = [str(year) for year in range(1900, 2025)] | |
| # filtered_mortality = child_mortality[child_mortality['country'].isin(selected_countries)][['country'] + years] | |
| # mortality_melted = filtered_mortality.melt(id_vars="country", var_name="year", value_name="mortality_rate") | |
| st.write(""" | |
| ### Chart 2.1: Child Mortality Trends | |
| (Note: You can select a year range and countries from the sidebar to see specific trends in detail for Viz 2.1, 2.2, 2.3). | |
| This line chart shows the trends in child mortality rates over the years for selected countries. For instance, India and South Africa initially exhibit much higher mortality rates compared to developed nations like the USA and the UK, reflecting disparities in healthcare access. Notably, the chart captures periods of global crises such as pandemics or wars, where temporary spikes in child mortality rates can be observed, such as in South Africa during the mid-20th century. Over time, all countries demonstrate a marked decline, indicating progress in global health and development. | |
| """) | |
| # mortality_chart = alt.Chart(mortality_melted).mark_line().encode( | |
| # x="year:O", | |
| # y="mortality_rate:Q", | |
| # color="country:N", | |
| # tooltip=["country", "year", "mortality_rate"] | |
| # ).properties(width=700, height=400) | |
| # st.altair_chart(mortality_chart) | |
| mortality_chart = alt.Chart(mortality_melted).mark_line().encode( | |
| x=alt.X("year:O", title="Year"), | |
| y=alt.Y("Child Mortality:Q", title="Child Mortality (0–5 years per 1000 births)"), | |
| color="country:N" | |
| ).properties(width=700, height=400) | |
| st.altair_chart(mortality_chart) | |
| # 5. Visual 5: Life Expectancy Trends | |
| # filtered_expectancy = life_expectancy[life_expectancy['country'].isin(selected_countries)] | |
| # expectancy_melted = filtered_expectancy.melt(id_vars="country", var_name="year", value_name="life_expectancy") | |
| st.write(""" | |
| ### Chart 2.2: Life Expectancy Trends | |
| This visualization shows how life expectancy has improved across all regions, with countries like the UK and the USA maintaining consistently higher life expectancy, while developing nations such as India and South Africa only recently catching up. However, fluctuations are visible, notably during the 1918 influenza pandemic and the global conflicts of the 20th century, where life expectancy briefly plummeted. Interestingly, the sharp rise in life expectancy in countries like China during the mid-20th century reflects public health reforms and economic growth.""") | |
| # expectancy_chart = alt.Chart(expectancy_melted).mark_line().encode( | |
| # x="year:O", | |
| # y="life_expectancy:Q", | |
| # color="country:N", | |
| # tooltip=["country", "year", "life_expectancy"] | |
| # ).properties(width=700, height=400) | |
| # st.altair_chart(expectancy_chart) | |
| expectancy_chart = alt.Chart(expectancy_melted).mark_line().encode( | |
| x=alt.X("year:O", title="Year"), | |
| y=alt.Y("Life Expectancy:Q", title="Life Expectancy at Birth"), | |
| color="country:N" | |
| ).properties(width=700, height=400) | |
| st.altair_chart(expectancy_chart) | |
| st.subheader("Chart 2.3: Child Mortality vs. Life Expectancy") | |
| st.write(""" | |
| This scatter plot illustrates the strong inverse relationship between child mortality | |
| rates and life expectancy at birth, underscoring how advancements in healthcare, | |
| nutrition, and living conditions improve both indicators simultaneously. A particularly | |
| striking observation is how the data for countries like India and South Africa forms a | |
| steep curve, signifying rapid improvements in life expectancy as child mortality declines. | |
| Developed nations such as Australia and the USA show a plateau in the later stages, where | |
| child mortality rates are already low, and life expectancy improvements are incremental. | |
| The trajectory of China’s data during the mid-20th century highlights a rapid transition, | |
| likely due to systemic public health efforts. | |
| """) | |
| # mortality_long = pd.melt(child_mortality, id_vars=["country"], var_name="year", value_name="child_mortality") | |
| # expectancy_long = pd.melt(life_expectancy, id_vars=["country"], var_name="year", value_name="life_expectancy") | |
| # mortality_long["year"] = mortality_long["year"].astype(int) | |
| # expectancy_long["year"] = expectancy_long["year"].astype(int) | |
| # merged_data = pd.merge(mortality_long, expectancy_long, on=["country", "year"]) | |
| # merged_data = merged_data.dropna(subset=["child_mortality", "life_expectancy"]) | |
| # default_countries = ["Argentina", "Australia", "China", "India", "South Africa", "UK", "USA"] | |
| # countries = merged_data["country"].unique() | |
| # selected_countries = st.multiselect("Select Countries", countries, default=default_countries) | |
| # filtered_data = merged_data[merged_data["country"].isin(selected_countries)] | |
| # scatter_chart = alt.Chart(filtered_data).mark_circle(size=60).encode( | |
| # x=alt.X("life_expectancy:Q", title="Life Expectancy at Birth"), | |
| # y=alt.Y("child_mortality:Q", title="Child Mortality (0–5 years per 1000 births)"), | |
| # color="country:N", | |
| # tooltip=["country", "year", "child_mortality", "life_expectancy"] | |
| # ).properties(width=700, height=400) | |
| # st.altair_chart(scatter_chart) | |
| merged_data = pd.merge(mortality_melted, expectancy_melted, on=["country", "year"]) | |
| scatter_chart = alt.Chart(merged_data).mark_circle(size=60).encode( | |
| x=alt.X("Life Expectancy:Q", title="Life Expectancy at Birth"), | |
| y=alt.Y("Child Mortality:Q", title="Child Mortality (0–5 years per 1000 births)"), | |
| color="country:N", | |
| tooltip=["country", "year", "Child Mortality", "Life Expectancy"] | |
| ).properties(width=700, height=400) | |
| st.altair_chart(scatter_chart) | |
| # 3. Visual 3: Child Mortality vs. Population | |
| st.write(""" | |
| ### Chart 3: Child Mortality vs Population | |
| This visualization shows the relationship between child mortality rates (per 1,000 live births) and population size over time for a selected country. | |
| Displayed on a dual-y-axis chart, it allows users to explore how both metrics have evolved across decades. | |
| 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. | |
| """) | |
| selected_country = st.selectbox("Country for Population vs. Mortality", countries, index=0) | |
| mortality_country = child_mortality[child_mortality['country'] == selected_country].melt( | |
| id_vars='country', var_name='year', value_name='child_mortality' | |
| ) | |
| population_country = population[population['country'] == selected_country].melt( | |
| id_vars='country', var_name='year', value_name='population' | |
| ) | |
| merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner') | |
| merged_country_data['year'] = merged_country_data['year'].astype(int) | |
| dual_axis_chart = alt.layer( | |
| alt.Chart(merged_country_data).mark_line().encode( | |
| x="year:O", | |
| y="child_mortality:Q", | |
| tooltip=["year", "child_mortality"] | |
| ), | |
| alt.Chart(merged_country_data).mark_line(color='orange').encode( | |
| x="year:O", | |
| y="population:Q", | |
| tooltip=["year", "population"] | |
| ) | |
| ).resolve_scale(y='independent').properties(width=700, height=400) | |
| st.altair_chart(dual_axis_chart) | |
| # 4. Visual 4: Child Mortality vs. Income | |
| income_long = pd.melt(income, id_vars=['country'], var_name='year', value_name='income') | |
| mortality_long = pd.melt(child_mortality, id_vars=['country'], var_name='year', value_name='mortality') | |
| income_long['year'] = income_long['year'].astype(int) | |
| mortality_long['year'] = mortality_long['year'].astype(int) | |
| yay = pd.merge(income_long, mortality_long, on=['country', 'year']) | |
| st.write(""" | |
| ### Chart 4: Child Mortality vs Daily Income | |
| The interactive scatterplot compares child mortality to daily income for 24 countries, with a slider to select a year between 1800 and 2024. | |
| The x-axis represents daily income (USD) on a logarithmic scale, and the y-axis shows child mortality (per 1,000). | |
| Each country is color-coded, and hover tooltips display details for each data point. | |
| Our analysis reveals that in earlier years (e.g., 1800), countries have tightly clustered mortality and income. | |
| By 2024, a negative correlation emerges, showing that as daily income increases, child mortality decreases. | |
| """) | |
| yeyear = st.slider("Year for Income vs. Mortality", min_value=1800, max_value=2024, value=2024) | |
| filtered_yay = yay[yay["year"] == yeyear] | |
| scatter_plot = alt.Chart(filtered_yay).mark_circle(size=60).encode( | |
| x=alt.X('income', title='Daily Income (USD)', scale=alt.Scale(type='log')), | |
| y=alt.Y('mortality', title='Child Mortality (per 1,000)'), | |
| color='country', | |
| tooltip=['country', 'year', 'income', 'mortality'] | |
| ).properties(width=700, height=400) | |
| st.altair_chart(scatter_plot) | |