File size: 13,937 Bytes
6aa09c0 | 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from advanced_validator import AdvancedDataValidator
from typing import Dict, Any
import json
def render_advanced_validation_tab(df: pd.DataFrame):
"""
Render the advanced validation tab in Streamlit
Args:
df: DataFrame to validate
"""
st.header("π Advanced Data Quality Validation")
st.markdown("""
This advanced validation focuses on **logical inconsistencies** and **business rule violations**
rather than basic technical data issues. It performs deep analysis to detect:
- **Duplicate Identity Detection**: Email and phone number duplicates with normalization
- **Data Pattern Anomalies**: Suspicious clustering and artificial standardization
- **Business Logic Violations**: Chronological inconsistencies and employment logic errors
- **Contextual Integrity Issues**: Bulk import patterns and unrealistic data ranges
""")
# Configuration section
with st.expander("βοΈ Validation Configuration", expanded=False):
col1, col2 = st.columns(2)
with col1:
company_founding_year = st.number_input(
"Company Founding Year",
min_value=1800,
max_value=2024,
value=1990,
help="Used to validate join dates aren't before company founding"
)
with col2:
validation_scope = st.multiselect(
"Validation Scope",
["Duplicate Detection", "Pattern Analysis", "Business Logic", "Contextual Integrity"],
default=["Duplicate Detection", "Pattern Analysis", "Business Logic", "Contextual Integrity"],
help="Select which validation categories to run"
)
# Run validation button
if st.button("π Run Advanced Validation", type="primary"):
with st.spinner("Performing advanced data quality validation..."):
# Initialize validator
validator = AdvancedDataValidator(company_founding_year=company_founding_year)
# Run validation
validation_results = validator.validate_dataset(df)
# Store results in session state
st.session_state['validation_results'] = validation_results
st.session_state['validator'] = validator
# Display results if available
if 'validation_results' in st.session_state:
display_validation_results(st.session_state['validation_results'], df)
def display_validation_results(results: Dict[str, Any], df: pd.DataFrame):
"""Display comprehensive validation results"""
# Summary metrics
st.subheader("π Validation Summary")
summary = results['summary']
# Create metrics columns
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.metric("Data Quality Score", f"{summary['data_quality_score']}%")
with col2:
st.metric("Total Issues", summary['total_issues_found'])
with col3:
st.metric("High Severity", summary['high_severity_issues'],
delta=f"-{summary['high_severity_issues']}" if summary['high_severity_issues'] > 0 else None)
with col4:
st.metric("Medium Severity", summary['medium_severity_issues'])
with col5:
st.metric("Affected Records", f"{summary['total_affected_records']} ({summary['total_affected_records']/summary['total_records']*100:.1f}%)")
# Overall status
if results['validation_passed']:
st.success("β
**Validation Passed**: No critical issues found!")
else:
st.error("β **Validation Failed**: Critical issues require immediate attention!")
# Severity distribution chart
if summary['total_issues_found'] > 0:
st.subheader("π Issues by Severity")
severity_data = {
'Severity': ['High', 'Medium', 'Low'],
'Count': [summary['high_severity_issues'], summary['medium_severity_issues'], summary['low_severity_issues']],
'Color': ['#FF4B4B', '#FFA500', '#32CD32']
}
fig = px.bar(
severity_data,
x='Severity',
y='Count',
color='Color',
color_discrete_map={color: color for color in severity_data['Color']},
title="Distribution of Issues by Severity Level"
)
fig.update_layout(showlegend=False, height=400)
st.plotly_chart(fig, use_container_width=True)
# Detailed issues
st.subheader("π Detailed Issue Analysis")
if results['detailed_issues']:
# Create tabs for each severity level
severity_tabs = []
if summary['high_severity_issues'] > 0:
severity_tabs.append("π΄ High Severity")
if summary['medium_severity_issues'] > 0:
severity_tabs.append("π‘ Medium Severity")
if summary['low_severity_issues'] > 0:
severity_tabs.append("π’ Low Severity")
if severity_tabs:
tabs = st.tabs(severity_tabs)
tab_index = 0
for severity, color in [("HIGH", "π΄"), ("MEDIUM", "π‘"), ("LOW", "π’")]:
severity_issues = [issue for issue in results['detailed_issues'] if issue['severity'] == severity]
if severity_issues and tab_index < len(tabs):
with tabs[tab_index]:
display_severity_issues(severity_issues, severity, df)
tab_index += 1
else:
st.info("π No data quality issues detected! Your dataset appears to be logically consistent.")
# Recommendations section
if results['recommendations']:
st.subheader("π‘ Recommendations")
for rec_group in results['recommendations']:
priority_color = {
'HIGH': 'π΄',
'MEDIUM': 'π‘',
'LOW': 'π’'
}
with st.expander(f"{priority_color[rec_group['priority']]} {rec_group['title']}", expanded=rec_group['priority'] == 'HIGH'):
for i, item in enumerate(rec_group['items'], 1):
st.markdown(f"{i}. {item}")
# Export options
st.subheader("π₯ Export Validation Results")
col1, col2 = st.columns(2)
with col1:
if st.button("π Download Detailed Report"):
report_json = json.dumps(results, indent=2, default=str)
st.download_button(
label="Download JSON Report",
data=report_json,
file_name=f"advanced_validation_report_{pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json"
)
with col2:
if st.button("π Download Issue Summary"):
# Create summary DataFrame
summary_data = []
for issue in results['detailed_issues']:
summary_data.append({
'Category': issue['category'],
'Severity': issue['severity'],
'Description': issue['description'],
'Affected Records': issue['count'],
'Percentage': f"{issue['affected_percentage']}%",
'Recommendation': issue['recommendation']
})
summary_df = pd.DataFrame(summary_data)
csv = summary_df.to_csv(index=False)
st.download_button(
label="Download CSV Summary",
data=csv,
file_name=f"validation_summary_{pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')}.csv",
mime="text/csv"
)
def display_severity_issues(issues: list, severity: str, df: pd.DataFrame):
"""Display issues for a specific severity level"""
for i, issue in enumerate(issues):
with st.container():
# Issue header
st.markdown(f"### {i+1}. {issue['category']}")
# Issue details
col1, col2 = st.columns([2, 1])
with col1:
st.markdown(f"**Description:** {issue['description']}")
st.markdown(f"**Recommendation:** {issue['recommendation']}")
with col2:
st.metric("Affected Records", issue['count'])
st.metric("Percentage", f"{issue['affected_percentage']}%")
# Examples section
if issue['examples']:
with st.expander(f"π View Examples ({len(issue['examples'])} shown)", expanded=False):
# Handle different example formats
if isinstance(issue['examples'][0], dict):
if 'email' in issue['examples'][0]:
# Email duplicate examples
for example in issue['examples']:
st.markdown(f"**Email:** `{example['email']}` (appears {example['count']} times)")
if 'records' in example:
example_df = pd.DataFrame(example['records'])
st.dataframe(example_df, use_container_width=True)
elif 'normalized_phone' in issue['examples'][0]:
# Phone duplicate examples
for example in issue['examples']:
st.markdown(f"**Phone:** `{example['normalized_phone']}` (appears {example['count']} times)")
if 'records' in example:
example_df = pd.DataFrame(example['records'])
st.dataframe(example_df, use_container_width=True)
elif 'period' in issue['examples'][0]:
# Bulk import pattern examples
for example in issue['examples']:
st.markdown(f"**Period:** {example['period']} ({example['count']} records, {example['percentage_of_total']}% of total)")
if 'sample_records' in example:
example_df = pd.DataFrame(example['sample_records'])
st.dataframe(example_df, use_container_width=True)
else:
# Generic examples
for j, example in enumerate(issue['examples']):
st.markdown(f"**Example {j+1}:**")
st.json(example)
else:
# Simple list examples
example_df = pd.DataFrame(issue['examples'])
st.dataframe(example_df, use_container_width=True)
st.markdown("---")
def create_validation_visualization(results: Dict[str, Any]) -> go.Figure:
"""Create comprehensive validation visualization"""
# Create subplots
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('Issues by Severity', 'Affected Records Distribution',
'Data Quality Score', 'Issue Categories'),
specs=[[{"type": "bar"}, {"type": "pie"}],
[{"type": "indicator"}, {"type": "bar"}]]
)
summary = results['summary']
# Severity distribution
fig.add_trace(
go.Bar(
x=['High', 'Medium', 'Low'],
y=[summary['high_severity_issues'], summary['medium_severity_issues'], summary['low_severity_issues']],
marker_color=['#FF4B4B', '#FFA500', '#32CD32'],
name='Issues by Severity'
),
row=1, col=1
)
# Affected vs Clean records
affected = summary['total_affected_records']
clean = summary['total_records'] - affected
fig.add_trace(
go.Pie(
labels=['Clean Records', 'Affected Records'],
values=[clean, affected],
marker_colors=['#32CD32', '#FF4B4B'],
name='Record Distribution'
),
row=1, col=2
)
# Data Quality Score
fig.add_trace(
go.Indicator(
mode="gauge+number",
value=summary['data_quality_score'],
domain={'x': [0, 1], 'y': [0, 1]},
title={'text': "Data Quality Score"},
gauge={
'axis': {'range': [None, 100]},
'bar': {'color': "darkblue"},
'steps': [
{'range': [0, 50], 'color': "lightgray"},
{'range': [50, 80], 'color': "yellow"},
{'range': [80, 100], 'color': "lightgreen"}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 90
}
}
),
row=2, col=1
)
# Issue categories
if results['detailed_issues']:
categories = [issue['category'] for issue in results['detailed_issues']]
counts = [issue['count'] for issue in results['detailed_issues']]
fig.add_trace(
go.Bar(
x=categories,
y=counts,
marker_color='#1f77b4',
name='Issues by Category'
),
row=2, col=2
)
fig.update_layout(height=800, showlegend=False, title_text="Advanced Data Quality Validation Dashboard")
return fig
|