File size: 12,112 Bytes
5913dd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from io import StringIO
import time

# Set page configuration
st.set_page_config(
    page_title="Debunker - Data Quality Validator",
    page_icon="πŸ”",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for modern styling
st.markdown("""
<style>
    .main-header {
        font-size: 3rem;
        font-weight: 700;
        color: #1f77b4;
        margin-bottom: 1rem;
    }
    .card {
        background-color: #f8f9fa;
        border-radius: 10px;
        padding: 1.5rem;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        margin-bottom: 1rem;
    }
    .metric-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 10px;
        padding: 1.5rem;
        color: white;
        text-align: center;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }
    .success-text { color: #2ecc71; }
    .warning-text { color: #f1c40f; }
    .error-text { color: #e74c3c; }
</style>
""", unsafe_allow_html=True)

# Header with Built with anycoder
st.markdown("""
<div style="text-align: center; margin-bottom: 2rem;">
    <h1 class="main-header">πŸ” Debunker</h1>
    <p style="font-size: 1.2rem; color: #666;">
        Advanced Data Quality Validator & Anomaly Detector
    </p>
    <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" 
       style="color: #1f77b4; font-weight: bold; text-decoration: none; margin-top: 10px; display: inline-block;">
        Built with anycoder
    </a>
</div>
""", unsafe_allow_html=True)

# Sidebar Configuration
with st.sidebar:
    st.header("βš™οΈ Configuration")
    
    st.markdown("---")
    
    st.subheader("πŸ“Š Data Input Method")
    input_method = st.radio(
        "Choose input method:",
        ["Upload CSV File", "Paste Data", "Generate Sample Data"],
        label_visibility="collapsed"
    )
    
    st.markdown("---")
    
    st.subheader("🎯 Validation Rules")
    st.checkbox("Detect Missing Values", value=True, help="Check for NaN or empty cells")
    st.checkbox("Detect Duplicates", value=True, help="Identify duplicate rows")
    st.checkbox("Detect Outliers (IQR)", value=True, help="Flag values beyond statistical bounds")
    st.checkbox("Detect Empty Strings", value=True, help="Find rows with empty string values")
    
    st.markdown("---")
    
    st.subheader("πŸ“ˆ Visualization Options")
    plot_type = st.selectbox(
        "Chart Type:",
        ["Bar Chart", "Scatter Plot", "Distribution Plot", "Heatmap"],
        label_visibility="collapsed"
    )

# Initialize session state
if 'df' not in st.session_state:
    st.session_state.df = None
if 'analysis_results' not in st.session_state:
    st.session_state.analysis_results = None

# Main Application Logic
def load_sample_data():
    """Generate sample dataset for demonstration"""
    np.random.seed(42)
    data = {
        'Customer_ID': range(1, 101),
        'Name': np.random.choice(['Alice', 'Bob', 'Charlie', 'David', 'Eve'], 100),
        'Age': np.random.randint(18, 70, 100),
        'Purchase_Amount': np.random.uniform(10, 500, 100),
        'Rating': np.random.randint(1, 6, 100),
        'Date': pd.date_range(start='2023-01-01', periods=100).strftime('%Y-%m-%d')
    }
    return pd.DataFrame(data)

def detect_anomalies(df):
    """Perform comprehensive data quality checks"""
    results = {
        'missing_values': {},
        'duplicates': 0,
        'outliers': {},
        'empty_strings': {}
    }
    
    # Missing Values
    for col in df.columns:
        missing_count = df[col].isna().sum()
        if missing_count > 0:
            results['missing_values'][col] = missing_count
    
    # Duplicates
    results['duplicates'] = df.duplicated().sum()
    
    # Outliers using IQR method
    for col in df.select_dtypes(include=[np.number]).columns:
        Q1 = df[col].quantile(0.25)
        Q3 = df[col].quantile(0.75)
        IQR = Q3 - Q1
        lower_bound = Q1 - 1.5 * IQR
        upper_bound = Q3 + 1.5 * IQR
        outliers = df[(df[col] < lower_bound) | (df[col] > upper_bound)]
        if len(outliers) > 0:
            results['outliers'][col] = {
                'count': len(outliers),
                'percentage': round((len(outliers) / len(df)) * 100, 2),
                'values': outliers[col].tolist()
            }
    
    # Empty Strings
    for col in df.select_dtypes(include=['object']).columns:
        empty_count = (df[col] == '').sum()
        if empty_count > 0:
            results['empty_strings'][col] = empty_count
    
    return results

def main():
    # Input Handling
    if input_method == "Upload CSV File":
        uploaded_file = st.file_uploader("Upload your CSV file", type=['csv'])
        if uploaded_file is not None:
            try:
                st.session_state.df = pd.read_csv(uploaded_file)
                st.success(f"Successfully loaded: {uploaded_file.name}")
            except Exception as e:
                st.error(f"Error loading file: {str(e)}")
    
    elif input_method == "Paste Data":
        st.info("Paste your CSV data below:")
        csv_text = st.text_area("CSV Data", height=200, placeholder="column1,column2,column3\nvalue1,value2,value3")
        if st.button("Process Data", type="primary"):
            try:
                st.session_state.df = pd.read_csv(StringIO(csv_text))
                st.success("Data processed successfully!")
            except Exception as e:
                st.error(f"Error processing data: {str(e)}")
    
    elif input_method == "Generate Sample Data":
        if st.button("Generate Sample Data", type="primary"):
            st.session_state.df = load_sample_data()
            st.success("Sample data generated!")

    # Process Data if available
    if st.session_state.df is not None:
        st.markdown("---")
        st.header("πŸ“Š Data Overview")
        
        # Display data preview
        col1, col2, col3 = st.columns(3)
        with col1:
            st.metric("Total Rows", st.session_state.df.shape[0])
        with col2:
            st.metric("Total Columns", st.session_state.df.shape[1])
        with col3:
            st.metric("Memory Usage", f"{st.session_state.df.memory_usage(deep=True).sum() / 1024:.2f} KB")
        
        # Data Preview
        with st.expander("View Data Preview", expanded=True):
            st.dataframe(st.session_state.df.head(10))
        
        # Run Analysis
        with st.spinner("Analyzing data quality..."):
            time.sleep(0.5)  # Simulate processing time
            st.session_state.analysis_results = detect_anomalies(st.session_state.df)
        
        # Analysis Results
        st.markdown("---")
        st.header("πŸ” Analysis Results")
        
        # Missing Values Section
        if st.session_state.analysis_results['missing_values']:
            st.subheader("⚠️ Missing Values Detected")
            missing_df = pd.DataFrame.from_dict(
                st.session_state.analysis_results['missing_values'], 
                orient='index', 
                columns=['Count']
            )
            st.dataframe(missing_df, use_container_width=True)
            st.caption(f"Total missing values: {sum(st.session_state.analysis_results['missing_values'].values())}")
        else:
            st.success("βœ… No missing values detected in the dataset.")
        
        # Duplicates Section
        if st.session_state.analysis_results['duplicates'] > 0:
            st.warning(f"⚠️ {st.session_state.analysis_results['duplicates']} duplicate rows detected.")
        else:
            st.success("βœ… No duplicate rows detected.")
        
        # Outliers Section
        if st.session_state.analysis_results['outliers']:
            st.subheader("🚨 Outliers Detected (IQR Method)")
            outlier_df = pd.DataFrame.from_dict(
                {k: v['count'] for k, v in st.session_state.analysis_results['outliers'].items()},
                orient='index',
                columns=['Count']
            )
            st.dataframe(outlier_df, use_container_width=True)
            
            # Visualization
            if plot_type in ["Bar Chart", "Distribution Plot"]:
                fig = px.bar(
                    outlier_df, 
                    x=outlier_df.index, 
                    y='Count',
                    title="Outliers by Column",
                    color='Count',
                    color_continuous_scale='Reds'
                )
                st.plotly_chart(fig, use_container_width=True)
        else:
            st.success("βœ… No outliers detected in numerical columns.")
        
        # Empty Strings Section
        if st.session_state.analysis_results['empty_strings']:
            st.subheader("πŸ“ Empty Strings Detected")
            empty_df = pd.DataFrame.from_dict(
                st.session_state.analysis_results['empty_strings'], 
                orient='index', 
                columns=['Count']
            )
            st.dataframe(empty_df, use_container_width=True)
        else:
            st.success("βœ… No empty strings detected in text columns.")
        
        # Detailed Analysis Section
        st.markdown("---")
        st.header("πŸ“ˆ Detailed Analysis")
        
        # Summary Metrics
        col1, col2, col3, col4 = st.columns(4)
        
        total_issues = (
            sum(st.session_state.analysis_results['missing_values'].values()) +
            st.session_state.analysis_results['duplicates'] +
            sum([v['count'] for v in st.session_state.analysis_results['outliers'].values()]) +
            sum(st.session_state.analysis_results['empty_strings'].values())
        )
        
        with col1:
            st.metric("Total Issues Found", total_issues, delta_color="inverse")
        
        with col2:
            st.metric("Data Quality Score", f"{max(0, 100 - (total_issues / (st.session_state.df.shape[0] * st.session_state.df.shape[1]) * 100)):.1f}%")
        
        with col3:
            st.metric("Columns Analyzed", st.session_state.df.shape[1])
        
        with col4:
            st.metric("Rows Analyzed", st.session_state.df.shape[0])
        
        # Visualizations
        if st.session_state.analysis_results['missing_values'] or st.session_state.analysis_results['outliers']:
            st.subheader("Visual Summary")
            
            # Create a summary chart
            chart_data = {
                'Missing Values': sum(st.session_state.analysis_results['missing_values'].values()),
                'Duplicates': st.session_state.analysis_results['duplicates'],
                'Outliers': sum([v['count'] for v in st.session_state.analysis_results['outliers'].values()]),
                'Empty Strings': sum(st.session_state.analysis_results['empty_strings'].values())
            }
            
            fig = px.bar(
                x=list(chart_data.keys()),
                y=list(chart_data.values()),
                title="Data Quality Issues Summary",
                labels={'x': 'Issue Type', 'y': 'Count'},
                color=list(chart_data.keys()),
                color_discrete_sequence=px.colors.qualitative.Set2
            )
            st.plotly_chart(fig, use_container_width=True)
            
            # Distribution Plots for numerical columns
            num_cols = st.session_state.df.select_dtypes(include=[np.number]).columns
            if len(num_cols) > 0 and plot_type == "Distribution Plot":
                with st.expander("Distribution Plots"):
                    for col in num_cols[:3]:  # Show first 3 numerical columns
                        fig_dist = px.histogram(
                            st.session_state.df, 
                            x=col, 
                            title=f"Distribution of {col}",
                            nbins=30
                        )
                        st.plotly_chart(fig_dist, use_container_width=True)

if __name__ == "__main__":
    main()