File size: 11,201 Bytes
5663a2a
 
 
6e41341
5663a2a
 
 
6e41341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5663a2a
 
6e41341
5663a2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e41341
 
 
5663a2a
 
6e41341
5663a2a
6e41341
5663a2a
6e41341
 
5663a2a
 
 
 
 
 
 
6e41341
5663a2a
 
6e41341
5663a2a
 
6e41341
5663a2a
 
6e41341
5663a2a
6e41341
5663a2a
6e41341
5663a2a
 
 
 
6e41341
 
 
 
 
080b398
6e41341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55b451b
6e41341
 
 
 
 
 
 
080b398
6e41341
 
55b451b
6e41341
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import streamlit as st
import numpy as np
import pandas as pd
from AutoVisualizer.processing import check_dataset_cleanliness, task_type, is_probably_categorical, is_discrete, is_continuous, parse_datetime_columns
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
from AutoVisualizer.numerical_viz import generate_box_plots, generate_numeric_correlation_heatmap, generate_scatter_plots, generate_histograms, generate_line_plots

st.set_page_config(page_title="Auto-Visualizer", page_icon="πŸ“Š", layout="wide")

# Initialize session state for storing plots
if 'plots_generated' not in st.session_state:
    st.session_state.plots_generated = False
    st.session_state.all_plots = {
        'count_plots': [],
        'bar_plots': [],
        'grp_bar_plots': [],
        'pie_plots': [],
        'box_plots': [],
        'heat_maps': [],
        'scatter_plots': [],
        'histograms': [],
        'line_plots': []
    }

with st.sidebar:
    # Upload the dataset file
    uploaded_file = st.file_uploader("Upload your dataset file:", ["csv", "xlsx", "json", "xml"])

if uploaded_file is not None:
    file_type = uploaded_file.name

    try:
        # Read the dataset through pandas
        if file_type.endswith(".csv"):
            df = pd.read_csv(uploaded_file)
        elif file_type.endswith(".xlsx"):
            df = pd.read_excel(uploaded_file)
        elif file_type.endswith(".json"):
            df = pd.read_json(uploaded_file)
        else:
            df = pd.read_xml(uploaded_file)
    except Exception as e:
        st.write("Error:", e)
    
    with st.sidebar:
        st.info("""
            ⚠️ **Heads up!** For the best experience, please upload a clean dataset.
            
            This app is designed for *visualizing data*, not cleaning it.
            
            πŸ“Œ *Tip:* Use the quick checker below to spot potential issues.
            """)

        if st.button("Run Cleanliness Check"):
            st.session_state.run_clean_check = True
        st.divider()

    if st.session_state.get("run_clean_check", False):
        with st.expander("➑️ See Cleanliness Checker Result"):
            check_dataset_cleanliness(df)

    st.markdown("Your Dataset:")
    st.dataframe(df, height=210)
    st.divider()

    feature_list = list(df.columns)
    target_selector = ["No Target"] + feature_list

    with st.sidebar:
        target_col = st.selectbox("Specify the target column in your dataset:", target_selector)
        task = task_type(df, target_col)
        st.write(f"πŸ” Task identified: **{task}**")
        
    df, date_time_ls, extracted_datetime = parse_datetime_columns(df)
    feature_list = [x for x in feature_list if x not in date_time_ls]
    
    categorical_ls = []
    discrete_ls = []
    continuous_ls = []
    for feature in feature_list:
        if is_probably_categorical(df[feature]):
            categorical_ls.append(feature)
        elif is_discrete(df[feature]):
            discrete_ls.append(feature)
        elif is_continuous(df[feature]):
            continuous_ls.append(feature)
    
    for feature in extracted_datetime:
        if is_probably_categorical(df[feature]):
            categorical_ls.append(feature)
    
    @st.dialog("Identified/Extracted Features from your Dataset:-")
    def open_dialog():
        if categorical_ls:
            with st.popover("Categorical Features", use_container_width=True):
                st.code("\n".join([f"β€’ {item}" for item in categorical_ls]))
        if discrete_ls:
            with st.popover("Discrete Features", use_container_width=True):
                st.code("\n".join([f"β€’ {item}" for item in discrete_ls]))
        if continuous_ls:
            with st.popover("Continuous Features", use_container_width=True):
                st.code("\n".join([f"β€’ {item}" for item in continuous_ls]))
        if date_time_ls:
            with st.popover("Date-Time Features", use_container_width=True):
                st.code("\n".join([f"β€’ {item}" for item in date_time_ls]))
            with st.popover("Extracted features from your Date-Time like features", use_container_width=True):
                st.code("\n".join([f"β€’ {item}" for item in extracted_datetime]))
    
    with st.sidebar:
        if st.button("See Your Feature Details"):
            open_dialog()

    # Generate all plots in background when button is clicked
    if st.button("Generate All Plots") or st.session_state.plots_generated:
        if not st.session_state.plots_generated:
            with st.spinner("Generating all plots (please wait)..."):
                # Generate and store all plots
                if categorical_ls:
                    st.session_state.all_plots['count_plots'] = [p for x_col in categorical_ls 
                                                               if df[x_col].nunique() <= 20 
                                                               for p in generate_count_plots(df, x_col)]
                    st.session_state.all_plots['bar_plots'] = [p for x_col in categorical_ls 
                                                             if df[x_col].nunique() <= 20 
                                                             for p in generate_bar_plots(df, x_col, discrete_ls + continuous_ls)]
                    st.session_state.all_plots['grp_bar_plots'] = generate_grouped_bar_plots(df, categorical_ls, discrete_ls + continuous_ls)
                    st.session_state.all_plots['pie_plots'] = [p for x_col in categorical_ls 
                                                              if df[x_col].nunique() <= 20 
                                                              for p in generate_pie_plots(df, x_col)]

                if continuous_ls:
                    st.session_state.all_plots['box_plots'] = [p for x_col in categorical_ls 
                                                             if df[x_col].nunique() <= 10 
                                                             for p in generate_box_plots(df, x_col, continuous_ls)]
                    
                    st.session_state.all_plots['heat_maps'] = []
                    if task == 'Regression' and categorical_ls:
                        st.session_state.all_plots['heat_maps'].extend(generate_categorical_correlation_heatmap(df, target_col, categorical_ls))
                    st.session_state.all_plots['heat_maps'].extend(generate_numeric_correlation_heatmap(df[continuous_ls]))
                    
                    if len(continuous_ls) >= 2:
                        feature_pairs = [(continuous_ls[i], continuous_ls[j]) 
                                       for i in range(len(continuous_ls)) 
                                       for j in range(i + 1, len(continuous_ls))]
                        selection = st.session_state.get('selection', categorical_ls[0] if categorical_ls else None)
                        st.session_state.all_plots['scatter_plots'] = generate_scatter_plots(df, feature_pairs, selection)
                    
                    st.session_state.all_plots['histograms'] = generate_histograms(df, continuous_ls)
                    
                    if date_time_ls:
                        date_related_keywords = ['_year', '_month', '_day', '_weekday']
                        date_component_cols = [col for col in extracted_datetime if any(key in col for key in date_related_keywords)]
                        if date_component_cols:
                            time_choice = st.session_state.get('time_choice', 'Monthly')
                            time_grouping_options = {"Daily": "D", "Weekly": "W", "Monthly": "ME", "Yearly": "YE"}
                            selected_freq = time_grouping_options.get(time_choice, "ME")
                            st.session_state.all_plots['line_plots'] = generate_line_plots(df, date_component_cols, continuous_ls, selected_freq)
                
                st.session_state.plots_generated = True
                st.rerun()  # Refresh to display all plots

        # Display all plots after generation is complete
        if st.session_state.plots_generated:
            if categorical_ls:
                st.header("πŸ“Š Categorical Plots")
                if st.session_state.all_plots['count_plots']:
                    st.subheader("Count Plots :-")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['count_plots']), use_container_width=True)
                
                if st.session_state.all_plots['bar_plots']:
                    st.subheader("Bar Plots :-")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['bar_plots']), use_container_width=True)
                
                if st.session_state.all_plots['grp_bar_plots']:
                    st.subheader("Grouped Bar Plots :-")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['grp_bar_plots']), use_container_width=True)
                
                if st.session_state.all_plots['pie_plots']:
                    st.subheader("Pie Charts :-")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['pie_plots']), use_container_width=True)

            if continuous_ls:
                st.header("πŸ“Š Numerical Plots")
                if st.session_state.all_plots['box_plots']:
                    st.subheader("Box Plots :-")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['box_plots']), use_container_width=True)
                
                if st.session_state.all_plots['heat_maps']:
                    st.subheader("Heat Maps :-")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['heat_maps']), use_container_width=True)
                
                if len(continuous_ls) >= 2 and st.session_state.all_plots['scatter_plots']:
                    st.subheader("Scatter Plots")
                    selection = st.pills("Highlight using a categorical feature :- ", categorical_ls, 
                                       key='selection')
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['scatter_plots']), use_container_width=True)
                
                if st.session_state.all_plots['histograms']:
                    st.subheader("Histograms")
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['histograms']), use_container_width=True)
                
                if date_time_ls and st.session_state.all_plots['line_plots']:
                    st.subheader("Line Plots :-")
                    time_choice = st.pills("Choose time interval for grouping :- ", 
                                          ["Daily", "Weekly", "Monthly", "Yearly"], 
                                          key='time_choice')
                    st.plotly_chart(combine_figures_as_subplots(st.session_state.all_plots['line_plots']), use_container_width=True)