Team Ai
Apppublic

Ayush6501/DataVisualization_FinalProject

sourceHugging Facemitupdated 2y agoView on Hugging Face
1likes
app.py104 linesDownload Raw Back to root
1import streamlit as st2import altair as alt3import pandas as pd4import ssl5ssl._create_default_https_context = ssl._create_stdlib_context6alt.data_transformers.disable_max_rows()7 8# Load and preprocess data9@st.cache_data10def load_data():11    return pd.read_csv('https://huggingface.co/datasets/Ayush6501/ChicagoCrashesDataset_July_to_Nov/resolve/main/Traffic_Crashes_-_Crashes_20241121.csv')12 13df = load_data()14day_map = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: "Friday", 6: 'Saturday', 7: 'Sunday'}15 16df['CRASH_DAY_OF_WEEK'] = df['CRASH_DAY_OF_WEEK'].map(day_map)17df['CRASH_DATE'] = pd.to_datetime(df['CRASH_DATE'], errors='coerce')18 19# Remove null values20df = df.dropna(subset=['CRASH_DATE'])21 22# Create a selection parameter23heatmap_select = alt.selection_point(24    fields=["CRASH_DAY_OF_WEEK", "CRASH_HOUR"],  # Enable selection on both fields25    name="Heatmap Selection",26)27 28# Define the heatmap with the selection parameter29heatmap_data = df.groupby(['CRASH_DAY_OF_WEEK', 'CRASH_HOUR']).size().reset_index(name='Count')30heat_map_chart = alt.Chart(heatmap_data).mark_rect().encode(31        x=alt.X('CRASH_HOUR:O', title='Hour'),32        y=alt.Y('CRASH_DAY_OF_WEEK:O', title='Day'),33        tooltip=['CRASH_DAY_OF_WEEK', 'CRASH_HOUR'],34        color=alt.Color('Count:Q', scale=alt.Scale(scheme='viridis'), title='Crash Count'),35        opacity=alt.when(heatmap_select).then(alt.value(1.0)).otherwise(alt.value(0.5))36    ).properties(37        title="Crash Intensity (Hours vs Days)",38        width=700,39        height=40040    ).add_params(41        heatmap_select  # Add the selection parameter to the heatmap42    )43 44weather_bar = alt.Chart(df).mark_rect().encode(45        alt.X('WEATHER_CONDITION:N'),46        alt.Y('count()'),47        tooltip=['WEATHER_CONDITION', 'count()'],48        color=alt.ColorValue("#482676")49    ).properties(50        width=500,51        height=20052    ).transform_filter(53        heatmap_select54    )55 56crash_bar = alt.Chart(df).mark_bar().encode(57        x='FIRST_CRASH_TYPE:N',58        y='count()',59        tooltip=['FIRST_CRASH_TYPE', 'count()'],60        color=alt.ColorValue('#b9df29')61    ).properties(62        width=500,63        height=20064    ).transform_filter(65        heatmap_select66    )67 68 69concats = alt.vconcat(70        heat_map_chart,71        weather_bar,72        crash_bar,73    ).resolve_legend(74        color="independent",75        size="independent"76    )77 78st.title('Streamlit App for IS445 Final Project')79st.text('Authors: Ayush Majumdar, Saloni Ekal, Samarth Jain, Satyam Shah')80st.divider()81st.text("Instructions: ")82st.text("This dashboard's driver plot is the heatmap, the first chart on the page. To filter the heatmap by \83day of the week and hour of the day, click once. If you wish to make numerous selections, you can use Shift + \84click. To reset the dashboard, click on your selections twice. \85The driver plot is a heatmap which shows the count of crashes taking place on a particular day of the week and on a \86given hour. The next plot is a bar chart which showcases the weather conditions when the crash occurred and the final \87plot is a bar chart which showcases the type of crash.")88st.divider()89st.altair_chart(concats, use_container_width=True)90st.divider()91st.text(92    "1. The team believes that the present method of uploading this and employing this dataset using Huggingface is appropriate \93    because its size of 24.3 Mb is below the Github limit.\n \94    2. A contextual data which we identified to tell a better more rounded story is the Chicago Vehicle Crashes dataset. \95    Our current dataset only tells us about the type of crashes and more details about when, where and how the crashes occurred \96    However, with the Vehicles dataset we would have additional insights on what type of vehicles were involved in the crashes \97    which can provide us and the stakeholder with much required additional information. \98    We again faced similar issues with this dataset as the complete dataset was huge and was getting updated daily. As a \99    result we again extracted the data for the last 6 months starting July'24 and we have also uploaded this dataset to\100    out huggingface dataset repository.\n \101    3. Link: https://huggingface.co/datasets/Ayush6501/ChicagoCrashesDataset_July_to_Nov/tree/main \n \102    4. Size: 29.4 Mb"103)104st.divider()