Spaces:
Sleeping
Sleeping
Update AutoVisualizer/processing.py
Browse files- AutoVisualizer/processing.py +255 -250
AutoVisualizer/processing.py
CHANGED
|
@@ -1,250 +1,255 @@
|
|
| 1 |
-
# Module that will handle processing tasks of the user dataset
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
# A quick cleanliness checker function
|
| 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 |
-
f"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
if
|
| 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 |
-
return
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
elif pd.api.types.
|
| 128 |
-
return True
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
if pd.api.types.
|
| 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 |
-
# Check if all
|
| 220 |
-
if
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
]
|
| 231 |
-
|
| 232 |
-
df[f"{col}
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
df[f"{col}
|
| 239 |
-
df[f"{col}
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
df[f"{col}
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Module that will handle processing tasks of the user dataset
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# A quick cleanliness checker function
|
| 7 |
+
@st.cache_data
|
| 8 |
+
def check_dataset_cleanliness(df):
|
| 9 |
+
issues_found = False
|
| 10 |
+
|
| 11 |
+
# 1. Columns with null values
|
| 12 |
+
null_cols = df.columns[df.isnull().any()].tolist()
|
| 13 |
+
if null_cols:
|
| 14 |
+
issues_found = True
|
| 15 |
+
st.warning(f"⚠️ These columns contain missing (NaN) values: {null_cols}")
|
| 16 |
+
|
| 17 |
+
# 2. Object columns that appear to be numeric (but aren't due to dirty values)
|
| 18 |
+
misclassified_numeric = []
|
| 19 |
+
for col in df.select_dtypes(include="object").columns:
|
| 20 |
+
non_null = df[col].dropna().astype(str)
|
| 21 |
+
sample_size = min(100, len(non_null))
|
| 22 |
+
if sample_size == 0:
|
| 23 |
+
continue # skip if column has no non-null values
|
| 24 |
+
sample = non_null.sample(sample_size, random_state=1)
|
| 25 |
+
numeric_like_ratio = sample.str.replace(",", "").str.replace(".", "", regex=False).str.isdigit().mean()
|
| 26 |
+
if numeric_like_ratio > 0.6:
|
| 27 |
+
misclassified_numeric.append(col)
|
| 28 |
+
|
| 29 |
+
if misclassified_numeric:
|
| 30 |
+
issues_found = True
|
| 31 |
+
st.warning(
|
| 32 |
+
f"⚠️ These columns are stored as `object` but mostly contain numeric values.\n"
|
| 33 |
+
f"This may be due to the presence of invalid or non-numeric entries in a few rows: {misclassified_numeric}"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# 3. Checking duplicate records
|
| 37 |
+
num_duplicates = df.duplicated().sum()
|
| 38 |
+
if num_duplicates > 0:
|
| 39 |
+
issues_found = True
|
| 40 |
+
st.warning(f"⚠️ Your dataset contains {num_duplicates} duplicated rows.")
|
| 41 |
+
|
| 42 |
+
# 4. Checking Constant Columns (No Variation)
|
| 43 |
+
constant_cols = [col for col in df.columns if df[col].nunique(dropna=False) <= 1]
|
| 44 |
+
if constant_cols:
|
| 45 |
+
issues_found = True
|
| 46 |
+
st.warning(f"⚠️ These columns contain only a single unique value and may be useless for analysis: {constant_cols}")
|
| 47 |
+
|
| 48 |
+
# 5. Suspiciously High Cardinality in Categorical Columns
|
| 49 |
+
high_card_cols = [col for col in df.select_dtypes(include='object') if df[col].nunique() > 100]
|
| 50 |
+
if high_card_cols:
|
| 51 |
+
issues_found = True
|
| 52 |
+
st.warning(f"⚠️ These object-type columns have unusually high unique values (possibly IDs or noisy data): {high_card_cols}")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Final message
|
| 56 |
+
if not issues_found:
|
| 57 |
+
st.success("✅ No major issues detected. Dataset looks clean!")
|
| 58 |
+
|
| 59 |
+
@st.cache_data
|
| 60 |
+
# Function that will identify the task of the dataset
|
| 61 |
+
def task_type(df: pd.DataFrame, target_col: str) -> str:
|
| 62 |
+
"""
|
| 63 |
+
Determine the machine learning task type based on the target column.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
df (pd.DataFrame): The input dataset.
|
| 67 |
+
target_col (str or None): The target column name, or None for unsupervised tasks.
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
str: One of the following task types:
|
| 71 |
+
- "Classification" if the target is categorical.
|
| 72 |
+
- "Clustering" if the target is not provided.
|
| 73 |
+
- "Regression" if the target is numerical.
|
| 74 |
+
- "Unknown" if the type cannot be recognized.
|
| 75 |
+
"""
|
| 76 |
+
# Handle unsupervised case (no target)
|
| 77 |
+
if target_col == "No Target":
|
| 78 |
+
return "Clustering"
|
| 79 |
+
|
| 80 |
+
target_series = df[target_col]
|
| 81 |
+
dtype = target_series.dtype
|
| 82 |
+
n_unique = target_series.nunique()
|
| 83 |
+
|
| 84 |
+
# Check for classification
|
| 85 |
+
if dtype == 'object' or dtype == 'bool' or (dtype == 'category'):
|
| 86 |
+
return "Classification"
|
| 87 |
+
|
| 88 |
+
# Check for binary/multi-class classification represented as integers or floats
|
| 89 |
+
if dtype.kind in ['i', 'u', 'f']: # Integer types
|
| 90 |
+
if n_unique <= 10: # Arbitrary threshold for classification
|
| 91 |
+
return "Classification"
|
| 92 |
+
else:
|
| 93 |
+
return "Regression"
|
| 94 |
+
|
| 95 |
+
# Numeric types default to regression
|
| 96 |
+
if dtype.kind in ['i', 'u', 'f']:
|
| 97 |
+
return "Regression"
|
| 98 |
+
|
| 99 |
+
return "Unknown"
|
| 100 |
+
|
| 101 |
+
@st.cache_data
|
| 102 |
+
# Function that will identify if an object feature is truly categorical or not
|
| 103 |
+
def is_probably_categorical(series: pd.Series, threshold_unique: int = 50, threshold_ratio: float = 0.1) -> bool:
|
| 104 |
+
"""
|
| 105 |
+
Determines whether a given pandas Series is likely to be a categorical feature.
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
series (pd.Series): The input data column to analyze.
|
| 109 |
+
threshold_unique (int, optional (default=50)) : Maximum number of unique values for an object-type column to be considered categorical.
|
| 110 |
+
threshold_ratio (float, optional (default=0.1)) : Maximum ratio of unique values to total entries for object-type column to be treated as categorical.
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
bool: True if the series is likely categorical, False otherwise.
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
# Heuristic for object types (e.g., strings): avoid classifying high-cardinality fields as categorical
|
| 117 |
+
if series.dtype == 'object':
|
| 118 |
+
num_unique = series.nunique()
|
| 119 |
+
unique_ratio = num_unique / len(series)
|
| 120 |
+
|
| 121 |
+
if num_unique <= threshold_unique and unique_ratio <= threshold_ratio:
|
| 122 |
+
return True # categorical
|
| 123 |
+
else:
|
| 124 |
+
return False # high-cardinality non-categorical (like names)
|
| 125 |
+
|
| 126 |
+
# Explicit categorical or boolean data types are considered categorical
|
| 127 |
+
elif pd.api.types.is_categorical_dtype(series):
|
| 128 |
+
return True
|
| 129 |
+
elif pd.api.types.is_bool_dtype(series):
|
| 130 |
+
return True
|
| 131 |
+
|
| 132 |
+
return False
|
| 133 |
+
|
| 134 |
+
@st.cache_data
|
| 135 |
+
# Function that will identify if an numerical feature is discrete or not
|
| 136 |
+
def is_discrete(series: pd.Series, max_unique: int = 20) -> bool:
|
| 137 |
+
"""
|
| 138 |
+
Determine whether a numeric series should be considered discrete.
|
| 139 |
+
|
| 140 |
+
Args:
|
| 141 |
+
series (pd.Series): The input numeric data column to analyze.
|
| 142 |
+
max_unique (int, optional): Maximum number of unique values allowed
|
| 143 |
+
to treat a column as discrete. Default is 20.
|
| 144 |
+
|
| 145 |
+
Returns:
|
| 146 |
+
bool: True if the series is likely discrete, False otherwise.
|
| 147 |
+
"""
|
| 148 |
+
# Check if the series is of integer type
|
| 149 |
+
if pd.api.types.is_integer_dtype(series):
|
| 150 |
+
return series.nunique() <= max_unique
|
| 151 |
+
|
| 152 |
+
if pd.api.types.is_float_dtype(series):
|
| 153 |
+
# If all values are whole numbers AND unique count is low → treat as discrete
|
| 154 |
+
if series.dropna().apply(float.is_integer).all():
|
| 155 |
+
return series.nunique() <= max_unique
|
| 156 |
+
|
| 157 |
+
return False
|
| 158 |
+
|
| 159 |
+
@st.cache_data
|
| 160 |
+
# Function that will identify if an numerical feature is continuous or not
|
| 161 |
+
def is_continuous(series: pd.Series, max_unique: int = 20) -> bool:
|
| 162 |
+
"""
|
| 163 |
+
Determine whether a numeric series is continuous.
|
| 164 |
+
|
| 165 |
+
Args:
|
| 166 |
+
series (pd.Series): The input numeric data column to analyze.
|
| 167 |
+
max_unique (int, optional): Threshold for unique values. If a float-type column
|
| 168 |
+
contains only whole numbers and has fewer than this count, it is not considered continuous.
|
| 169 |
+
Default is 20.
|
| 170 |
+
|
| 171 |
+
Returns:
|
| 172 |
+
bool: True if the series is likely continuous, False otherwise.
|
| 173 |
+
"""
|
| 174 |
+
# Only float types are considered potentially continuous
|
| 175 |
+
if pd.api.types.is_float_dtype(series):
|
| 176 |
+
# If it's float but looks like discrete, then not continuous
|
| 177 |
+
all_whole_numbers = series.dropna().apply(float.is_integer).all()
|
| 178 |
+
if all_whole_numbers and series.nunique() <= max_unique:
|
| 179 |
+
return False
|
| 180 |
+
return True
|
| 181 |
+
return False
|
| 182 |
+
|
| 183 |
+
@st.cache_data
|
| 184 |
+
# Function that will identify if an feature is date-time format and then extract the time-based components
|
| 185 |
+
def parse_datetime_columns(df: pd.DataFrame) -> tuple[pd.DataFrame, list, list]:
|
| 186 |
+
"""
|
| 187 |
+
Detects and parses datetime columns in a DataFrame, and extracts useful
|
| 188 |
+
date and/or time components into new columns.
|
| 189 |
+
|
| 190 |
+
Args:
|
| 191 |
+
df (pd.DataFrame): Input dataset.
|
| 192 |
+
|
| 193 |
+
Returns:
|
| 194 |
+
tuple:
|
| 195 |
+
- pd.DataFrame: Updated DataFrame with extracted datetime components.
|
| 196 |
+
- list: List of original columns identified as datetime.
|
| 197 |
+
- list: List of newly extracted datetime-related feature names.
|
| 198 |
+
"""
|
| 199 |
+
datetime_cols = []
|
| 200 |
+
extracted_datetime = []
|
| 201 |
+
today = pd.Timestamp.today() # Just the date, no time
|
| 202 |
+
|
| 203 |
+
for col in df.columns:
|
| 204 |
+
if pd.api.types.is_datetime64_any_dtype(df[col]):
|
| 205 |
+
datetime_cols.append(col)
|
| 206 |
+
elif df[col].dtype == "object":
|
| 207 |
+
try:
|
| 208 |
+
converted = pd.to_datetime(df[col], errors="raise")
|
| 209 |
+
df[col] = converted
|
| 210 |
+
datetime_cols.append(col)
|
| 211 |
+
except Exception:
|
| 212 |
+
continue
|
| 213 |
+
|
| 214 |
+
for col in datetime_cols:
|
| 215 |
+
# Flags for what actually exists
|
| 216 |
+
has_date = True
|
| 217 |
+
has_time = True
|
| 218 |
+
|
| 219 |
+
# Check if all dates are "today" → probably not originally present
|
| 220 |
+
# if df[col].dt.normalize().nunique() == 1 and df[col].dt.normalize().iloc[0] == today:
|
| 221 |
+
if (df[col].dt.year == today.year).all() or (df[col].dt.month == today.month).all() or (df[col].dt.day == today.day).all():
|
| 222 |
+
has_date = False
|
| 223 |
+
|
| 224 |
+
# Check if all times are 00:00:00 → probably not originally present
|
| 225 |
+
if (df[col].dt.hour == 0).all() and (df[col].dt.minute == 0).all() and (df[col].dt.second == 0).all():
|
| 226 |
+
has_time = False
|
| 227 |
+
|
| 228 |
+
if has_date:
|
| 229 |
+
df[f"{col}_year"] = df[col].dt.year
|
| 230 |
+
df[f"{col}_month"] = df[col].dt.month
|
| 231 |
+
df[f"{col}_day"] = df[col].dt.day
|
| 232 |
+
df[f"{col}_weekday"] = df[col].dt.day_name()
|
| 233 |
+
extracted_datetime.extend([
|
| 234 |
+
f"{col}_year", f"{col}_month", f"{col}_day", f"{col}_weekday"
|
| 235 |
+
])
|
| 236 |
+
else:
|
| 237 |
+
df[f"{col}_year"] = np.nan
|
| 238 |
+
df[f"{col}_month"] = np.nan
|
| 239 |
+
df[f"{col}_day"] = np.nan
|
| 240 |
+
df[f"{col}_weekday"] = np.nan
|
| 241 |
+
|
| 242 |
+
if has_time:
|
| 243 |
+
df[f"{col}_hour"] = df[col].dt.hour
|
| 244 |
+
df[f"{col}_minute"] = df[col].dt.minute
|
| 245 |
+
extracted_datetime.extend([
|
| 246 |
+
f"{col}_hour", f"{col}_minute"
|
| 247 |
+
])
|
| 248 |
+
else:
|
| 249 |
+
df[f"{col}_hour"] = np.nan
|
| 250 |
+
df[f"{col}_minute"] = np.nan
|
| 251 |
+
|
| 252 |
+
# Remove any columns that are now entirely NaN
|
| 253 |
+
df = df.dropna(axis=1, how='all')
|
| 254 |
+
|
| 255 |
+
return df, datetime_cols, extracted_datetime
|