-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
152 lines (121 loc) · 4.36 KB
/
Copy pathvisualization.py
File metadata and controls
152 lines (121 loc) · 4.36 KB
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
"""Plotly-based visualization helpers for the Streamlit app."""
from __future__ import annotations
from typing import Optional, Sequence
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
def quality_score_gauge(score: int) -> go.Figure:
"""Gauge-style indicator for a 0–100 quality score."""
score = int(max(0, min(100, score)))
fig = go.Figure(
go.Indicator(
mode="gauge+number",
value=score,
number={"suffix": "/100"},
gauge={
"axis": {"range": [0, 100]},
"bar": {"thickness": 0.25},
},
)
)
fig.update_layout(height=220, margin=dict(l=10, r=10, t=30, b=10))
return fig
def histogram(df: pd.DataFrame, column: str, nbins: int = 30) -> go.Figure:
s = pd.to_numeric(df[column], errors="coerce")
fig = px.histogram(s, nbins=nbins, title=f"Histogram — {column}")
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10))
return fig
def box_plot(df: pd.DataFrame, column: str) -> go.Figure:
s = pd.to_numeric(df[column], errors="coerce")
fig = px.box(s, points="outliers", title=f"Box Plot — {column}")
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10))
return fig
def correlation_heatmap(df: pd.DataFrame, numeric_cols: Sequence[str]) -> go.Figure:
if len(numeric_cols) < 2:
return go.Figure()
corr = df[list(numeric_cols)].corr(numeric_only=True)
fig = px.imshow(
corr,
text_auto=".2f",
aspect="auto",
title="Correlation Heatmap",
color_continuous_scale="RdBu",
zmin=-1,
zmax=1,
)
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10))
return fig
def missing_value_heatmap(df: pd.DataFrame, max_rows: int = 300) -> go.Figure:
if df.empty:
return go.Figure()
view = df
if len(df) > max_rows:
view = df.sample(n=max_rows, random_state=42)
mat = view.isna().astype(int)
fig = px.imshow(
mat,
aspect="auto",
title=f"Missing Value Heatmap (sampled {len(view)} rows)",
color_continuous_scale="Reds",
zmin=0,
zmax=1,
)
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10))
return fig
def null_percentage_bar(df: pd.DataFrame, top_n: int = 40) -> go.Figure:
if df.empty:
return go.Figure()
missing_pct = (df.isna().mean() * 100).sort_values(ascending=False)
missing_pct = missing_pct.head(top_n)
fig = px.bar(
missing_pct,
title=f"Null Percentage by Column (Top {min(top_n, len(missing_pct))})",
labels={"value": "Missing %", "index": "Column"},
)
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10), xaxis_tickangle=-35)
return fig
def pie_chart_categorical(df: pd.DataFrame, column: str, top_n: int = 10) -> go.Figure:
s = df[column].astype(str).fillna("<missing>")
counts = s.value_counts(dropna=False)
if len(counts) > top_n:
top = counts.head(top_n)
other = counts.iloc[top_n:].sum()
counts = pd.concat([top, pd.Series({"Other": other})])
fig = px.pie(
values=counts.values,
names=counts.index,
title=f"Category Share — {column}",
)
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10))
return fig
def value_distribution(df: pd.DataFrame, column: str, top_n: int = 25) -> go.Figure:
if pd.api.types.is_numeric_dtype(df[column]):
return histogram(df, column)
s = df[column].astype(str).fillna("<missing>")
counts = s.value_counts().head(top_n)
fig = px.bar(
counts,
title=f"Value Distribution — {column} (Top {min(top_n, len(counts))})",
labels={"value": "Count", "index": "Value"},
)
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10), xaxis_tickangle=-35)
return fig
def scatter_plot(df: pd.DataFrame, x: str, y: str, color: Optional[str] = None) -> go.Figure:
working = df[[x, y] + ([color] if color else [])].copy()
fig = px.scatter(
working,
x=x,
y=y,
color=color,
title=f"Scatter Plot — {x} vs {y}",
opacity=0.8,
)
fig.update_layout(margin=dict(l=10, r=10, t=50, b=10))
return fig
def empty_figure(title: str = "") -> go.Figure:
fig = go.Figure()
if title:
fig.update_layout(title=title)
fig.update_layout(height=200)
return fig