ralate2 commited on
Commit
4b8004d
·
verified ·
1 Parent(s): d6169d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -74
app.py CHANGED
@@ -95,13 +95,16 @@ if viz_type == "Complaints Over Time":
95
 
96
  # Dropdown for Housing Block (only show when Complaints by Housing Block and Type or the updated version is selected)
97
  if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"]:
98
- block_options = ['All Blocks'] + sorted(data['Housing Block'].unique().tolist())
99
- selected_block = st.sidebar.selectbox(
100
- "Select Housing Block",
101
- options=block_options,
102
- key=f"block_select_{viz_type}" # Unique key for each visualization
103
- )
104
-
 
 
 
105
  # Ensuring selected_block is only used if defined
106
  if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"] and 'selected_block' not in locals():
107
  selected_block = 'All Blocks' # Default to 'All Blocks' if no selection made
@@ -435,86 +438,86 @@ elif viz_type == "Complaints by Housing Block and Type":
435
  The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
436
  """)
437
 
438
- elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
439
- st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
440
 
441
- # Filtering the data based on the selected year and housing block
442
- filtered_data_time = data # Use filtered_data if date range is not needed
443
- if selected_year != 'All Time':
444
- filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
445
 
446
- # Further filtering by Housing Block (if applicable)
447
- if selected_block != 'All Blocks':
448
- filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
449
 
450
- # Pivoting the data based on the filtered data
451
- complaint_pivot = filtered_data_time.pivot_table(
452
- index='Housing Block',
453
- columns='Type of Complaint',
454
- values='Disposition',
455
- aggfunc='count',
456
- fill_value=0
457
- )
458
 
459
- # Ensuring the pivoted data is numeric for plotting
460
- complaint_pivot = complaint_pivot.astype(float)
461
 
462
- # Desired order for the housing blocks
463
- desired_order = [
464
- '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
465
- '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
466
- '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
467
- '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
468
- '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
469
- '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
470
- '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
471
- '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
472
- '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
473
- '4700 block', '4800 block', '4900 block', '5000 block'
474
- ]
475
 
476
- # Reordering the index of the pivot table according to the desired order
477
- complaint_pivot = complaint_pivot.reindex(desired_order)
478
 
479
- # Calculating percentages for each complaint type per housing block
480
- percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
481
 
482
- # Plotting the data
483
- fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
484
 
485
- # Adding percentage labels to the plot
486
- ax = fig.gca()
487
- for idx, block in enumerate(complaint_pivot.index):
488
- cumulative_height = 0
489
- for i, complaint_type in enumerate(complaint_pivot.columns):
490
- count = complaint_pivot.iloc[idx, i]
491
- percent = percentages.iloc[idx, i]
492
- if count > 0:
493
- # Compute the position for the percentage label
494
- x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
495
- y_pos = cumulative_height + count / 2
496
- ax.text(
497
- x_pos, y_pos, f"{percent:.1f}%",
498
- ha='center', va='center',
499
- fontsize=10, color='black',
500
- bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
501
- )
502
- cumulative_height += count
503
 
504
- # Display the plot in Streamlit
505
- st.pyplot(fig)
506
 
507
- # writeup
508
- st.write("""
509
- **What this visualization shows:**
510
- This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
511
 
512
- **Why it's interesting:**
513
- By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
514
 
515
- **Color Scheme:**
516
- The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
517
- """)
518
  # In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
519
 
520
  # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
@@ -617,7 +620,78 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
617
  # The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
618
  # """)
619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620
 
 
 
 
 
621
  # Footer
622
  st.markdown("---")
623
  st.markdown("Dataset provided by the City of Urbana Open Data Portal - https://data.urbanaillinois.us/Environment/Nuisance-Complaints/tsn9-95m3/about_data ")
 
95
 
96
  # Dropdown for Housing Block (only show when Complaints by Housing Block and Type or the updated version is selected)
97
  if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"]:
98
+ # block_options = ['All Blocks'] + sorted(data['Housing Block'].unique().tolist())
99
+ # selected_block = st.sidebar.selectbox(
100
+ # "Select Housing Block",
101
+ # options=block_options,
102
+ # key=f"block_select_{viz_type}" # Unique key for each visualization
103
+ # )
104
+ valid_blocks = [block for block in data['Housing Block'].unique() if int(block.split()[0]) < 3400]
105
+ block_options = ['All Blocks'] + sorted(valid_blocks)
106
+ selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options, key="block_select")
107
+
108
  # Ensuring selected_block is only used if defined
109
  if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)"] and 'selected_block' not in locals():
110
  selected_block = 'All Blocks' # Default to 'All Blocks' if no selection made
 
438
  The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
439
  """)
440
 
441
+ # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
442
+ # st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
443
 
444
+ # # Filtering the data based on the selected year and housing block
445
+ # filtered_data_time = data # Use filtered_data if date range is not needed
446
+ # if selected_year != 'All Time':
447
+ # filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
448
 
449
+ # # Further filtering by Housing Block (if applicable)
450
+ # if selected_block != 'All Blocks':
451
+ # filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
452
 
453
+ # # Pivoting the data based on the filtered data
454
+ # complaint_pivot = filtered_data_time.pivot_table(
455
+ # index='Housing Block',
456
+ # columns='Type of Complaint',
457
+ # values='Disposition',
458
+ # aggfunc='count',
459
+ # fill_value=0
460
+ # )
461
 
462
+ # # Ensuring the pivoted data is numeric for plotting
463
+ # complaint_pivot = complaint_pivot.astype(float)
464
 
465
+ # # Desired order for the housing blocks
466
+ # desired_order = [
467
+ # '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
468
+ # '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
469
+ # '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
470
+ # '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
471
+ # '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
472
+ # '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
473
+ # '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
474
+ # '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
475
+ # '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
476
+ # '4700 block', '4800 block', '4900 block', '5000 block'
477
+ # ]
478
 
479
+ # # Reordering the index of the pivot table according to the desired order
480
+ # complaint_pivot = complaint_pivot.reindex(desired_order)
481
 
482
+ # # Calculating percentages for each complaint type per housing block
483
+ # percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
484
 
485
+ # # Plotting the data
486
+ # fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
487
 
488
+ # # Adding percentage labels to the plot
489
+ # ax = fig.gca()
490
+ # for idx, block in enumerate(complaint_pivot.index):
491
+ # cumulative_height = 0
492
+ # for i, complaint_type in enumerate(complaint_pivot.columns):
493
+ # count = complaint_pivot.iloc[idx, i]
494
+ # percent = percentages.iloc[idx, i]
495
+ # if count > 0:
496
+ # # Compute the position for the percentage label
497
+ # x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
498
+ # y_pos = cumulative_height + count / 2
499
+ # ax.text(
500
+ # x_pos, y_pos, f"{percent:.1f}%",
501
+ # ha='center', va='center',
502
+ # fontsize=10, color='black',
503
+ # bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
504
+ # )
505
+ # cumulative_height += count
506
 
507
+ # # Display the plot in Streamlit
508
+ # st.pyplot(fig)
509
 
510
+ # # writeup
511
+ # st.write("""
512
+ # **What this visualization shows:**
513
+ # This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
514
 
515
+ # **Why it's interesting:**
516
+ # By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
517
 
518
+ # **Color Scheme:**
519
+ # The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
520
+ # """)
521
  # In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
522
 
523
  # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
 
620
  # The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
621
  # """)
622
 
623
+ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
624
+ st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
625
+
626
+ # Filtering the data based on the selected year and housing block
627
+ filtered_data_time = data # Use filtered_data if date range is not needed
628
+ if selected_year != 'All Time':
629
+ filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
630
+
631
+ # Exclude blocks from 3400 onwards
632
+ valid_blocks = [block for block in filtered_data_time['Housing Block'].unique() if int(block.split()[0]) < 3400]
633
+ filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'].isin(valid_blocks)]
634
+
635
+ # Further filtering by Housing Block (if applicable)
636
+ if selected_block != 'All Blocks':
637
+ filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
638
+
639
+ # Pivoting the data based on the filtered data
640
+ complaint_pivot = filtered_data_time.pivot_table(
641
+ index='Housing Block',
642
+ columns='Type of Complaint',
643
+ values='Disposition',
644
+ aggfunc='count',
645
+ fill_value=0
646
+ )
647
+
648
+ # Ensuring the pivoted data is numeric for plotting
649
+ complaint_pivot = complaint_pivot.astype(float)
650
+
651
+ # Show only the selected block on the X-axis if a specific block is chosen
652
+ if selected_block != 'All Blocks':
653
+ complaint_pivot = complaint_pivot.loc[[selected_block]]
654
+
655
+ # Calculating percentages for each complaint type per housing block
656
+ percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
657
+
658
+ # Plotting the data
659
+ fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
660
+
661
+ # Adding percentage labels to the plot
662
+ ax = fig.gca()
663
+ for idx, block in enumerate(complaint_pivot.index):
664
+ cumulative_height = 0
665
+ for i, complaint_type in enumerate(complaint_pivot.columns):
666
+ count = complaint_pivot.iloc[idx, i]
667
+ percent = percentages.iloc[idx, i]
668
+ if count > 0:
669
+ # Compute the position for the percentage label
670
+ x_pos = idx # Adjusting the position of the label
671
+ y_pos = cumulative_height + count / 2
672
+ ax.text(
673
+ x_pos, y_pos, f"{percent:.1f}%",
674
+ ha='center', va='center',
675
+ fontsize=10, color='black',
676
+ bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
677
+ )
678
+ cumulative_height += count
679
+
680
+ # Display the plot in Streamlit
681
+ st.pyplot(fig)
682
+
683
+ # Writeup
684
+ st.write("""
685
+ **What this visualization shows:**
686
+ This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
687
+
688
+ **Why it's interesting:**
689
+ By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
690
 
691
+ **Color Scheme:**
692
+ The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
693
+ """)
694
+
695
  # Footer
696
  st.markdown("---")
697
  st.markdown("Dataset provided by the City of Urbana Open Data Portal - https://data.urbanaillinois.us/Environment/Nuisance-Complaints/tsn9-95m3/about_data ")