Sajid030 commited on
Commit
6e41341
·
verified ·
1 Parent(s): 080b398

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -140
app.py CHANGED
@@ -1,15 +1,29 @@
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
- from AutoVisualizer.processing import check_dataset_cleanliness, task_type,is_probably_categorical, is_discrete, is_continuous, parse_datetime_columns
5
  from AutoVisualizer.categorical_viz import combine_figures_as_subplots, generate_count_plots, generate_bar_plots, generate_grouped_bar_plots, generate_pie_plots, generate_categorical_correlation_heatmap
6
  from AutoVisualizer.numerical_viz import generate_box_plots, generate_numeric_correlation_heatmap, generate_scatter_plots, generate_histograms, generate_line_plots
7
 
8
- st.set_page_config(page_title= "Auto-Visualizer",page_icon= "📊",layout="wide")
9
- # col1, col2 = st.columns([0.3, 0.7])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  with st.sidebar:
12
- # Upload the dataset file (can upload only CSV, XLSX, JSON, XML)
13
  uploaded_file = st.file_uploader("Upload your dataset file:", ["csv", "xlsx", "json", "xml"])
14
 
15
  if uploaded_file is not None:
@@ -29,7 +43,6 @@ if uploaded_file is not None:
29
  st.write("Error:", e)
30
 
31
  with st.sidebar:
32
- # Show a a Disclaimer to user to upload as clean data as possible
33
  st.info("""
34
  ⚠️ **Heads up!** For the best experience, please upload a clean dataset.
35
 
@@ -39,185 +52,155 @@ if uploaded_file is not None:
39
  """)
40
 
41
  if st.button("Run Cleanliness Check"):
42
- # Use session state to track button click
43
  st.session_state.run_clean_check = True
44
  st.divider()
45
 
46
- # Display the cleanliness checker result on the main page (not sidebar) if button was clicked
47
  if st.session_state.get("run_clean_check", False):
48
  with st.expander("➡️ See Cleanliness Checker Result"):
49
  check_dataset_cleanliness(df)
50
 
51
- # Display the user DataFrame
52
  st.markdown("Your Dataset:")
53
  st.dataframe(df, height=210)
54
  st.divider()
55
 
56
- # Extract column names from the dataset + add "No Target" element if there's no target in the user dataset
57
  feature_list = list(df.columns)
58
  target_selector = ["No Target"] + feature_list
59
 
60
  with st.sidebar:
61
- # Ask the user to select target column from their dataset
62
  target_col = st.selectbox("Specify the target column in your dataset:", target_selector)
63
- # Identify the task/type of dataset (i.e. classification/regression/clustering(if no target feature at all))
64
  task = task_type(df, target_col)
65
- # Display the task to user
66
  st.write(f"🔍 Task identified: **{task}**")
67
 
68
- # Identify the date-time columns (if any) and extract new time-based components from it
69
- # date_time_ls --> List that will store date-time feature names
70
- # extracted_datetime --> List that will store extracted date-time feature names
71
  df, date_time_ls, extracted_datetime = parse_datetime_columns(df)
72
-
73
- # Remove date-time feature names as we already extracted time based components from it
74
  feature_list = [x for x in feature_list if x not in date_time_ls]
75
 
76
- categorical_ls = [] # List that will store categorical feature names
77
- discrete_ls = [] # List that will store discrete feature names
78
- continuous_ls = [] # List that will store continuous feature names
79
  for feature in feature_list:
80
  if is_probably_categorical(df[feature]):
81
- categorical_ls.append(feature) # Calling Categorical Feature Identifier Function
82
  elif is_discrete(df[feature]):
83
- discrete_ls.append(feature) # Calling Discrete Feature Identifier Function
84
  elif is_continuous(df[feature]):
85
- continuous_ls.append(feature) # Calling Continuous Feature Identifier Function
86
- # Add time based components that appear to be categorical
87
  for feature in extracted_datetime:
88
  if is_probably_categorical(df[feature]):
89
  categorical_ls.append(feature)
90
 
91
- # Creating Dialog Box to show identified features
92
  @st.dialog("Identified/Extracted Features from your Dataset:-")
93
  def open_dialog():
94
  if categorical_ls:
95
- with st.popover("Categorical Features", use_container_width= True):
96
  st.code("\n".join([f"• {item}" for item in categorical_ls]))
97
  if discrete_ls:
98
- with st.popover("Discrete Features", use_container_width= True):
99
  st.code("\n".join([f"• {item}" for item in discrete_ls]))
100
  if continuous_ls:
101
- with st.popover("Continuous Features", use_container_width= True):
102
  st.code("\n".join([f"• {item}" for item in continuous_ls]))
103
  if date_time_ls:
104
- with st.popover("Date-Time Features", use_container_width= True):
105
  st.code("\n".join([f"• {item}" for item in date_time_ls]))
106
- with st.popover("Extracted features from your Date-Time like features", use_container_width= True):
107
  st.code("\n".join([f"• {item}" for item in extracted_datetime]))
 
108
  with st.sidebar:
109
- # Calling the dialog box through a button
110
  if st.button("See Your Feature Details"):
111
  open_dialog()
112
 
113
- # if st.button("Generate Plots"):
114
- # Generate the Plots
115
- with st.spinner("Generating Plots.....", show_time= True):
116
- if categorical_ls:
117
- st.header("📊 Categorical Plots")
118
- # 1. Count Plots
119
- count_plots = []
120
- for x_col in categorical_ls:
121
- if df[x_col].nunique() <= 20:
122
- count_plots.extend(generate_count_plots(df, x_col))
123
-
124
- if count_plots:
125
- st.subheader("Count Plots :-")
126
- st.plotly_chart(combine_figures_as_subplots(count_plots), use_container_width=True)
127
-
128
- # 2. Bar Plots
129
- bar_plots = []
130
- # (Categorical vs Discrete + Continuous)
131
- for x_col in categorical_ls:
132
- if df[x_col].nunique() <= 20:
133
- bar_plots.extend(generate_bar_plots(df, x_col, discrete_ls + continuous_ls))
134
-
135
- if bar_plots:
136
- st.subheader("Bar Plots :-")
137
- st.plotly_chart(combine_figures_as_subplots(bar_plots), use_container_width=True)
138
-
139
- # 3. Grouped Bar Plots
140
- grp_bar_plots = []
141
- # (Categorical vs Discrete + Continuous)
142
- grp_bar_plots.extend(generate_grouped_bar_plots(df, categorical_ls, discrete_ls + continuous_ls))
143
-
144
- if grp_bar_plots:
145
- st.subheader("Grouped Bar Plots :-")
146
- st.plotly_chart(combine_figures_as_subplots(grp_bar_plots), use_container_width=True)
147
-
148
- # 4. Pie Charts
149
- pie_plots = []
150
- for x_col in categorical_ls:
151
- if df[x_col].nunique() <= 20:
152
- pie_plots.extend(generate_pie_plots(df, x_col))
153
-
154
- if pie_plots:
155
- st.subheader("Pie Charts :-")
156
- st.plotly_chart(combine_figures_as_subplots(pie_plots), use_container_width=True)
157
-
158
- if continuous_ls:
159
- st.header("📊 Numerical Plots")
160
- # 5. Box Plots
161
- box_plots = []
162
- for x_col in categorical_ls:
163
- if df[x_col].nunique() <= 10:
164
- box_plots.extend(generate_box_plots(df, x_col, continuous_ls))
165
-
166
- if box_plots:
167
- st.subheader("Box Plots :-")
168
- st.plotly_chart(combine_figures_as_subplots(box_plots), use_container_width=True)
169
-
170
- # 6. Heat Maps
171
- heat_maps = []
172
- if task == 'Regression':
173
  if categorical_ls:
174
- heat_maps.extend(generate_categorical_correlation_heatmap(df, target_col, categorical_ls))
175
- heat_maps.extend(generate_numeric_correlation_heatmap(df[continuous_ls]))
176
- if heat_maps:
177
- st.subheader("Heat Maps :-")
178
- st.plotly_chart(combine_figures_as_subplots(heat_maps), use_container_width=True)
179
-
180
- # 7. Scatter Plots
181
- if len(continuous_ls) >= 2:
182
- st.subheader("Scatter Plots")
183
- scatter_plots = []
184
- # Creation of unique feature pairs (no repetition like (B, A) if (A, B) is already used)
185
- feature_pairs = []
186
- for i in range(len(continuous_ls)):
187
- for j in range(i + 1, len(continuous_ls)):
188
- feature_pairs.append((continuous_ls[i], continuous_ls[j]))
189
- selection = st.pills("Highlight using a categorical feature :- ", categorical_ls)
190
- scatter_plots.extend(generate_scatter_plots(df, feature_pairs, selection))
191
- if scatter_plots:
192
- st.plotly_chart(combine_figures_as_subplots(scatter_plots), use_container_width=True)
193
-
194
- # 8. Histograms
195
- histograms = []
196
- histograms.extend(generate_histograms(df, continuous_ls))
197
-
198
- if histograms:
199
- st.subheader("Histograms")
200
- st.plotly_chart(combine_figures_as_subplots(histograms), use_container_width=True)
201
-
202
- # 9. Line Plots
203
- line_plots = []
204
- if date_time_ls:
205
- # Extract only date-related components
206
- date_related_keywords = ['_year', '_month', '_day', '_weekday']
207
- date_component_cols = [col for col in extracted_datetime if any(key in col for key in date_related_keywords)]
208
- if date_component_cols:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  st.subheader("Line Plots :-")
210
- # Mapping of labels to values
211
- time_grouping_options = {
212
- "Daily": "D",
213
- "Weekly": "W",
214
- "Monthly": "ME",
215
- "Yearly": "YE"
216
- }
217
- time_choice = st.pills("Choose time interval for grouping :- ", list(time_grouping_options.keys()))
218
- # Extract the actual value for resampling
219
- selected_freq = time_grouping_options[time_choice] if time_choice else "ME" # Use default "ME" if no selection
220
- line_plots.extend(generate_line_plots(df, date_component_cols, continuous_ls, selected_freq))
221
-
222
- if line_plots:
223
- st.plotly_chart(combine_figures_as_subplots(line_plots), use_container_width= True)
 
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
+ from AutoVisualizer.processing import check_dataset_cleanliness, task_type, is_probably_categorical, is_discrete, is_continuous, parse_datetime_columns
5
  from AutoVisualizer.categorical_viz import combine_figures_as_subplots, generate_count_plots, generate_bar_plots, generate_grouped_bar_plots, generate_pie_plots, generate_categorical_correlation_heatmap
6
  from AutoVisualizer.numerical_viz import generate_box_plots, generate_numeric_correlation_heatmap, generate_scatter_plots, generate_histograms, generate_line_plots
7
 
8
+ st.set_page_config(page_title="Auto-Visualizer", page_icon="📊", layout="wide")
9
+
10
+ # Initialize session state for storing plots
11
+ if 'plots_generated' not in st.session_state:
12
+ st.session_state.plots_generated = False
13
+ st.session_state.all_plots = {
14
+ 'count_plots': [],
15
+ 'bar_plots': [],
16
+ 'grp_bar_plots': [],
17
+ 'pie_plots': [],
18
+ 'box_plots': [],
19
+ 'heat_maps': [],
20
+ 'scatter_plots': [],
21
+ 'histograms': [],
22
+ 'line_plots': []
23
+ }
24
 
25
  with st.sidebar:
26
+ # Upload the dataset file
27
  uploaded_file = st.file_uploader("Upload your dataset file:", ["csv", "xlsx", "json", "xml"])
28
 
29
  if uploaded_file is not None:
 
43
  st.write("Error:", e)
44
 
45
  with st.sidebar:
 
46
  st.info("""
47
  ⚠️ **Heads up!** For the best experience, please upload a clean dataset.
48
 
 
52
  """)
53
 
54
  if st.button("Run Cleanliness Check"):
 
55
  st.session_state.run_clean_check = True
56
  st.divider()
57
 
 
58
  if st.session_state.get("run_clean_check", False):
59
  with st.expander("➡️ See Cleanliness Checker Result"):
60
  check_dataset_cleanliness(df)
61
 
 
62
  st.markdown("Your Dataset:")
63
  st.dataframe(df, height=210)
64
  st.divider()
65
 
 
66
  feature_list = list(df.columns)
67
  target_selector = ["No Target"] + feature_list
68
 
69
  with st.sidebar:
 
70
  target_col = st.selectbox("Specify the target column in your dataset:", target_selector)
 
71
  task = task_type(df, target_col)
 
72
  st.write(f"🔍 Task identified: **{task}**")
73
 
 
 
 
74
  df, date_time_ls, extracted_datetime = parse_datetime_columns(df)
 
 
75
  feature_list = [x for x in feature_list if x not in date_time_ls]
76
 
77
+ categorical_ls = []
78
+ discrete_ls = []
79
+ continuous_ls = []
80
  for feature in feature_list:
81
  if is_probably_categorical(df[feature]):
82
+ categorical_ls.append(feature)
83
  elif is_discrete(df[feature]):
84
+ discrete_ls.append(feature)
85
  elif is_continuous(df[feature]):
86
+ continuous_ls.append(feature)
87
+
88
  for feature in extracted_datetime:
89
  if is_probably_categorical(df[feature]):
90
  categorical_ls.append(feature)
91
 
 
92
  @st.dialog("Identified/Extracted Features from your Dataset:-")
93
  def open_dialog():
94
  if categorical_ls:
95
+ with st.popover("Categorical Features", use_container_width=True):
96
  st.code("\n".join([f"• {item}" for item in categorical_ls]))
97
  if discrete_ls:
98
+ with st.popover("Discrete Features", use_container_width=True):
99
  st.code("\n".join([f"• {item}" for item in discrete_ls]))
100
  if continuous_ls:
101
+ with st.popover("Continuous Features", use_container_width=True):
102
  st.code("\n".join([f"• {item}" for item in continuous_ls]))
103
  if date_time_ls:
104
+ with st.popover("Date-Time Features", use_container_width=True):
105
  st.code("\n".join([f"• {item}" for item in date_time_ls]))
106
+ with st.popover("Extracted features from your Date-Time like features", use_container_width=True):
107
  st.code("\n".join([f"• {item}" for item in extracted_datetime]))
108
+
109
  with st.sidebar:
 
110
  if st.button("See Your Feature Details"):
111
  open_dialog()
112
 
113
+ # Generate all plots in background when button is clicked
114
+ if st.button("Generate All Plots") or st.session_state.plots_generated:
115
+ if not st.session_state.plots_generated:
116
+ with st.spinner("Generating all plots (please wait)..."):
117
+ # Generate and store all plots
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  if categorical_ls:
119
+ st.session_state.all_plots['count_plots'] = [p for x_col in categorical_ls
120
+ if df[x_col].nunique() <= 20
121
+ for p in generate_count_plots(df, x_col)]
122
+ st.session_state.all_plots['bar_plots'] = [p for x_col in categorical_ls
123
+ if df[x_col].nunique() <= 20
124
+ for p in generate_bar_plots(df, x_col, discrete_ls + continuous_ls)]
125
+ st.session_state.all_plots['grp_bar_plots'] = generate_grouped_bar_plots(df, categorical_ls, discrete_ls + continuous_ls)
126
+ st.session_state.all_plots['pie_plots'] = [p for x_col in categorical_ls
127
+ if df[x_col].nunique() <= 20
128
+ for p in generate_pie_plots(df, x_col)]
129
+
130
+ if continuous_ls:
131
+ st.session_state.all_plots['box_plots'] = [p for x_col in categorical_ls
132
+ if df[x_col].nunique() <= 10
133
+ for p in generate_box_plots(df, x_col, continuous_ls)]
134
+
135
+ st.session_state.all_plots['heat_maps'] = []
136
+ if task == 'Regression' and categorical_ls:
137
+ st.session_state.all_plots['heat_maps'].extend(generate_categorical_correlation_heatmap(df, target_col, categorical_ls))
138
+ st.session_state.all_plots['heat_maps'].extend(generate_numeric_correlation_heatmap(df[continuous_ls]))
139
+
140
+ if len(continuous_ls) >= 2:
141
+ feature_pairs = [(continuous_ls[i], continuous_ls[j])
142
+ for i in range(len(continuous_ls))
143
+ for j in range(i + 1, len(continuous_ls))]
144
+ selection = st.session_state.get('selection', categorical_ls[0] if categorical_ls else None)
145
+ st.session_state.all_plots['scatter_plots'] = generate_scatter_plots(df, feature_pairs, selection)
146
+
147
+ st.session_state.all_plots['histograms'] = generate_histograms(df, continuous_ls)
148
+
149
+ if date_time_ls:
150
+ date_related_keywords = ['_year', '_month', '_day', '_weekday']
151
+ date_component_cols = [col for col in extracted_datetime if any(key in col for key in date_related_keywords)]
152
+ if date_component_cols:
153
+ time_choice = st.session_state.get('time_choice', 'Monthly')
154
+ time_grouping_options = {"Daily": "D", "Weekly": "W", "Monthly": "ME", "Yearly": "YE"}
155
+ selected_freq = time_grouping_options.get(time_choice, "ME")
156
+ st.session_state.all_plots['line_plots'] = generate_line_plots(df, date_component_cols, continuous_ls, selected_freq)
157
+
158
+ st.session_state.plots_generated = True
159
+ st.rerun() # Refresh to display all plots
160
+
161
+ # Display all plots after generation is complete
162
+ if st.session_state.plots_generated:
163
+ if categorical_ls:
164
+ st.header("📊 Categorical Plots")
165
+ if st.session_state.all_plots['count_plots']:
166
+ st.subheader("Count Plots :-")
167
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['count_plots']), use_container_width=True)
168
+
169
+ if st.session_state.all_plots['bar_plots']:
170
+ st.subheader("Bar Plots :-")
171
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['bar_plots']), use_container_width=True)
172
+
173
+ if st.session_state.all_plots['grp_bar_plots']:
174
+ st.subheader("Grouped Bar Plots :-")
175
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['grp_bar_plots']), use_container_width=True)
176
+
177
+ if st.session_state.all_plots['pie_plots']:
178
+ st.subheader("Pie Charts :-")
179
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['pie_plots']), use_container_width=True)
180
+
181
+ if continuous_ls:
182
+ st.header("📊 Numerical Plots")
183
+ if st.session_state.all_plots['box_plots']:
184
+ st.subheader("Box Plots :-")
185
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['box_plots']), use_container_width=True)
186
+
187
+ if st.session_state.all_plots['heat_maps']:
188
+ st.subheader("Heat Maps :-")
189
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['heat_maps']), use_container_width=True)
190
+
191
+ if len(continuous_ls) >= 2 and st.session_state.all_plots['scatter_plots']:
192
+ st.subheader("Scatter Plots")
193
+ selection = st.pills("Highlight using a categorical feature :- ", categorical_ls,
194
+ key='selection', index=0 if categorical_ls else None)
195
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['scatter_plots']), use_container_width=True)
196
+
197
+ if st.session_state.all_plots['histograms']:
198
+ st.subheader("Histograms")
199
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['histograms']), use_container_width=True)
200
+
201
+ if date_time_ls and st.session_state.all_plots['line_plots']:
202
  st.subheader("Line Plots :-")
203
+ time_choice = st.pills("Choose time interval for grouping :- ",
204
+ ["Daily", "Weekly", "Monthly", "Yearly"],
205
+ key='time_choice', index=2)
206
+ st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['line_plots']), use_container_width=True)