Download app.py from kchun9/Final: direct link, hf CLI and curl.
- Browser
- Download file 7.95 kB
-
https://huggingface.co/spaces/kchun9/Final/resolve/main/app.py
- Command line
-
hf download hf://spaces/kchun9/Final/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kchun9/Final/resolve/main/app.py
7.95 kB
| import pandas as pd | |
| import streamlit as st | |
| import altair as alt | |
| # Load the dataset | |
| def load_data(file_path): | |
| data = pd.read_csv("Air_Quality.csv") | |
| return data | |
| # Preprocess the dataset | |
| def preprocess_data(data): | |
| # Ensure Date is in datetime format | |
| data['Start_Date'] = pd.to_datetime(data['Start_Date'], errors='coerce') | |
| return data | |
| def main(): | |
| st.title("Living in the Clouds: New York’s Air Quality Through Time") | |
| st.subheader("By Kaylee Youngseo Chun") | |
| # Load and preprocess data | |
| file_path = st.text_input("Enter the file path of your dataset:", "Air_Quality.csv") | |
| data = load_data(file_path) | |
| data = preprocess_data(data) | |
| # Sidebar for pollutant selection | |
| st.sidebar.title("Filter Options") | |
| pollutants = st.sidebar.multiselect( | |
| "Select pollutants to display:", | |
| options=data["Name"].unique(), | |
| default=data["Name"].unique()[:5] | |
| ) | |
| st.markdown("""Today, I will examine the air quality trends in New York using an air quality dataset provided by Data.gov. | |
| You can also access and explore the dataset through the following link: New York City Air Quality Data. | |
| This dataset is publicly available for anyone to use. | |
| To analyze the data, you can apply filters to explore how various components affecting air quality change over time. | |
| Note that some components are available only for specific periods, which may be due to incomplete data collection or recent monitoring initiatives. | |
| Key components to focus on include: | |
| 1. Ozone (O3): While ozone depletion doesn’t cause global warming, | |
| both are linked to human activities that release pollutants into the atmosphere. | |
| 2. Nitrogen Dioxide (N2O): This gas has a significant warming impact, | |
| with 1 pound of N2O being 265 times more potent than 1 pound of carbon dioxide. | |
| 3. Fine Particles (PM2.5): These are tiny particles or droplets in the air measuring 2.5 microns | |
| or smaller—about 30 times smaller than a human hair. | |
| Their size allows them to penetrate deeply into the lungs and even enter the bloodstream.""") | |
| # Filter data based on pollutants | |
| filtered_data = data[data["Name"].isin(pollutants)] | |
| # Select numeric columns only | |
| numeric_cols = filtered_data.select_dtypes(include=["number"]).columns | |
| filtered_data = filtered_data[["Start_Date", "Name"] + list(numeric_cols)] | |
| # Resample data to monthly averages for cleaner visualization | |
| filtered_data = ( | |
| filtered_data | |
| .groupby([pd.Grouper(key="Start_Date", freq="M"), "Name"])[numeric_cols] | |
| .mean() | |
| .reset_index() | |
| ) | |
| # Create an interactive Altair line chart | |
| chart = alt.Chart(filtered_data).mark_line().encode( | |
| x=alt.X("Start_Date:T", title="Date"), | |
| y=alt.Y("Data Value:Q", title="Value"), | |
| color="Name:N", | |
| tooltip=["Start_Date:T", "Name:N", "Data Value:Q"] | |
| ).properties( | |
| width=800, | |
| height=400, | |
| title="Pollutant Levels Over Time (Monthly Average)" | |
| ).interactive() | |
| # Display the chart | |
| st.altair_chart(chart, use_container_width=True) | |
| if __name__ == "__main__": | |
| main() | |
| import matplotlib.pyplot as plt | |
| def main(): | |
| st.subheader("Fine Particle (PM2.5) Trend Analysis") | |
| st.markdown("""How Did Fine Particles Change During COVID-19? To explore this, we compared the dataset before and during the COVID-19 lockdowns. | |
| Below is a graph showing trends in PM2.5 levels, which have been steadily declining since 2010. The map below highlights further reductions during the lockdown. | |
| While annual declines in PM2.5 were already apparent, the shutdown contributed significantly to this trend. | |
| During the COVID-19 health lockdown, PM2.5 levels in New York decreased by 23%. | |
| A five-year improvement plan based on this data predicts benefits including reductions in pre-term births, low birth weight, infant mortality, childhood asthma, emergency room visits, and adult mortality. | |
| These findings are supported by Perera, F. et al. (2021).""") | |
| # Load and preprocess data | |
| file_path = st.text_input( | |
| "Enter the file path of your dataset:", | |
| "Air_Quality.csv", | |
| key="file_path_input" | |
| ) | |
| data = load_data(file_path) | |
| data = preprocess_data(data) | |
| # Filter for Fine Particle (PM2.5) data only | |
| fine_particle_data = data[data["Name"] == "Fine particles (PM 2.5)"] | |
| # Ensure "Data Value" column is numeric | |
| fine_particle_data["Data Value"] = pd.to_numeric(fine_particle_data["Data Value"], errors="coerce") | |
| fine_particle_data = fine_particle_data.dropna(subset=["Data Value"]) | |
| # Resample data to monthly averages for cleaner visualization | |
| fine_particle_data = ( | |
| fine_particle_data | |
| .groupby(pd.Grouper(key="Start_Date", freq="M"))["Data Value"] | |
| .mean() | |
| .reset_index() | |
| ) | |
| # Check if data is empty | |
| if fine_particle_data.empty: | |
| st.warning("No data available for Fine Particle (PM2.5). Please check your dataset.") | |
| return | |
| # Plotting with Matplotlib | |
| plt.figure(figsize=(10, 6)) | |
| plt.plot(fine_particle_data["Start_Date"], fine_particle_data["Data Value"], marker='o', linestyle='-') | |
| plt.title("Monthly Average of Fine Particle (PM2.5) Levels", fontsize=16) | |
| plt.xlabel("Date", fontsize=12) | |
| plt.ylabel("PM2.5 Level", fontsize=12) | |
| plt.grid(True) | |
| plt.tight_layout() | |
| # Display the Matplotlib chart in Streamlit | |
| st.pyplot(plt) | |
| if __name__ == "__main__": | |
| main() | |
| def main(): | |
| st.title("Fine Particles (PM 2.5) during shutdown") | |
| # Ensure the image path is correct | |
| st.image("PM.jpg", caption="Fine Particles (PM 2.5) during Covid-19 shutdown", use_container_width=True) | |
| st.markdown("source: https://kunakair.com/air-pollution-in-new-york/") | |
| # Use st.image within the Streamlit app context | |
| try: | |
| st.image(image_path, caption="Fine Particles (PM 2.5) during Covid-19 shutdown", use_column_width=True) | |
| except Exception as e: | |
| st.error(f"Error displaying image: {e}") | |
| st.subheader("Summary") | |
| st.markdown("""This analysis explores air quality trends in New York using publicly available data, with a focus on ozone (O3), nitrogen dioxide (N2O), and fine particulate matter (PM2.5). | |
| PM2.5 levels have been decreasing since 2010, and the COVID-19 lockdown accelerated this trend, showing a 23% reduction. | |
| These improvements highlight the benefits of clean air, including better health outcomes and lower mortality rates. | |
| The findings emphasize the importance of long-term air quality management for public health.""") | |
| st.subheader("Citation") | |
| st.markdown(""" | |
| 1. Is there a connection between the ozone hole and global warming?. Union of Concerned Scientists. (n.d.). | |
| https://www.ucsusa.org/resources/ozone-hole-and-global-warming. | |
| 2. Environmental Protection Agency. (n.d.). EPA. https://www.epa.gov/ghgemissions/overview-greenhouse-gases | |
| 3. Particle pollution and health. (n.d.). https://www.health.ny.gov/environmental/indoors/air/pmq_a.htm | |
| 4. Frederica Perera, Alique Berberian, David Cooley, Elizabeth Shenaut, Hollie Olmstead, Zev Ross, Thomas Matte. | |
| Potential health benefits of sustained air quality improvements in New York City: A simulation based on air pollution levels during the COVID-19 shutdown. | |
| Environmental Research, Volume 193, 2021, 110555, ISSN 0013-9351, https://doi.org/10.1016/j.envres.2020.110555 | |
| """) | |
| if __name__ == "__main__": | |
| main() |