File size: 14,989 Bytes
7ea279c
 
b85f76a
 
7ea279c
b85f76a
7ea279c
 
 
 
 
 
 
 
 
 
 
 
 
b85f76a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea279c
 
 
 
 
b85f76a
7ea279c
b85f76a
7ea279c
 
 
 
 
 
 
 
 
 
b85f76a
 
 
 
 
 
7ea279c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b85f76a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea279c
 
 
 
 
b85f76a
 
7ea279c
 
 
 
 
 
b85f76a
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea279c
 
 
 
 
 
b85f76a
 
 
 
 
 
 
 
 
7ea279c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b85f76a
7ea279c
 
 
b85f76a
 
7ea279c
 
 
b85f76a
7ea279c
b85f76a
7ea279c
 
b85f76a
 
 
 
 
 
7ea279c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b85f76a
 
7ea279c
 
 
 
 
 
 
b85f76a
 
 
 
 
 
7ea279c
 
 
 
 
 
 
 
 
 
 
 
 
b85f76a
 
7ea279c
 
 
 
b85f76a
7ea279c
b85f76a
7ea279c
 
 
b85f76a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea279c
b85f76a
7ea279c
 
 
 
 
 
b85f76a
7ea279c
 
b85f76a
7ea279c
 
 
b85f76a
7ea279c
 
 
b85f76a
 
 
 
 
 
 
 
 
 
 
 
 
7ea279c
 
 
 
 
 
 
 
 
 
b85f76a
 
7ea279c
 
b85f76a
 
7ea279c
b85f76a
7ea279c
 
 
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""Data loading, profiling, and preprocessing utilities."""

from __future__ import annotations

import os
import re
import tempfile

import numpy as np
import pandas as pd

# Temp workspace that persists for the app session
TEMP_DIR = os.path.join(tempfile.gettempdir(), "ml_analysis_space")
os.makedirs(TEMP_DIR, exist_ok=True)

CLEANED_PATH = os.path.join(TEMP_DIR, "cleaned_data.csv")

SUPPORTED_EXTENSIONS = [".csv", ".tsv", ".xlsx", ".xls", ".json", ".parquet", ".txt"]

# ------------------------------------------------------------------ load ----

ENCODINGS = ("utf-8", "utf-8-sig", "cp1252", "latin-1")


def _read_text_table(file_path: str, sep: str | None = None) -> pd.DataFrame:
    """Read a delimited text file, trying several encodings.

    With sep=None pandas sniffs the delimiter (handles ; | tab exports).
    Malformed rows are skipped instead of failing the whole file.
    """
    last_err: Exception | None = None
    for encoding in ENCODINGS:
        try:
            return pd.read_csv(
                file_path,
                sep=sep,
                engine="python",
                encoding=encoding,
                on_bad_lines="skip",
            )
        except UnicodeDecodeError as e:
            last_err = e
    raise ValueError(f"Could not decode file with any of {ENCODINGS}: {last_err}")


def _tidy_columns(df: pd.DataFrame) -> pd.DataFrame:
    """Fix messy headers: strip whitespace, name blank/auto columns, dedupe."""
    df = df.copy()
    names = []
    for i, col in enumerate(df.columns):
        name = re.sub(r"\s+", " ", str(col)).strip()
        if not name or name.lower().startswith("unnamed:"):
            name = f"column_{i}"
        names.append(name)
    seen: dict[str, int] = {}
    unique = []
    for name in names:
        if name in seen:
            seen[name] += 1
            name = f"{name}_{seen[name]}"
        else:
            seen[name] = 0
        unique.append(name)
    df.columns = unique
    return df


def load_data(file_path: str) -> pd.DataFrame:
    """Load a dataset from CSV, TSV, Excel, JSON, or Parquet."""
    ext = os.path.splitext(file_path)[1].lower()
    if ext in (".csv", ".txt"):
        df = _read_text_table(file_path)
    elif ext == ".tsv":
        df = _read_text_table(file_path, sep="\t")
    elif ext in (".xlsx", ".xls"):
        df = pd.read_excel(file_path)
    elif ext == ".json":
        df = pd.read_json(file_path)
    elif ext == ".parquet":
        df = pd.read_parquet(file_path)
    else:
        raise ValueError(
            f"Unsupported file type '{ext}'. Supported: {', '.join(SUPPORTED_EXTENSIONS)}"
        )
    if df.shape[0] == 0 or df.shape[1] == 0:
        raise ValueError("The file was read but contains no data.")
    return _tidy_columns(df)


# --------------------------------------------------------------- profile ----


def profile_data(df: pd.DataFrame) -> dict:
    """Return a summary profile of the dataset."""
    numeric_cols = df.select_dtypes(include=np.number).columns.tolist()
    categorical_cols = df.select_dtypes(exclude=np.number).columns.tolist()
    profile = {
        "n_rows": int(df.shape[0]),
        "n_cols": int(df.shape[1]),
        "columns": df.columns.tolist(),
        "dtypes": {c: str(t) for c, t in df.dtypes.items()},
        "numeric_columns": numeric_cols,
        "categorical_columns": categorical_cols,
        "missing_counts": df.isna().sum().to_dict(),
        "missing_total": int(df.isna().sum().sum()),
        "duplicate_rows": int(df.duplicated().sum()),
        "memory_kb": round(df.memory_usage(deep=True).sum() / 1024, 1),
    }
    return profile


def profile_text(profile: dict) -> str:
    """Human-readable summary of a profile dict."""
    lines = [
        f"**Rows:** {profile['n_rows']:,} | **Columns:** {profile['n_cols']} "
        f"| **Memory:** {profile['memory_kb']} KB",
        f"**Numeric columns ({len(profile['numeric_columns'])}):** "
        + (", ".join(profile["numeric_columns"]) or "none"),
        f"**Categorical columns ({len(profile['categorical_columns'])}):** "
        + (", ".join(profile["categorical_columns"]) or "none"),
        f"**Missing values:** {profile['missing_total']:,} | "
        f"**Duplicate rows:** {profile['duplicate_rows']:,}",
    ]
    missing = {k: v for k, v in profile["missing_counts"].items() if v > 0}
    if missing:
        lines.append(
            "**Columns with missing values:** "
            + ", ".join(f"{k} ({v})" for k, v in missing.items())
        )
    return "\n\n".join(lines)


# ------------------------------------------------------------ preprocess ----

_NUMERIC_JUNK = re.compile(r"[\s$€£,%]")
_MISSING_TOKENS = {"", "nan", "none", "null", "na", "n/a", "-", "?", "missing"}


def _normalize_missing_tokens(df: pd.DataFrame) -> tuple[pd.DataFrame, int]:
    """Turn placeholder strings like 'N/A', '?', '-' into real NaN."""
    before = int(df.isna().sum().sum())
    for col in df.select_dtypes(exclude=np.number).columns:
        mask = df[col].astype(str).str.strip().str.lower().isin(_MISSING_TOKENS)
        if mask.any():
            df.loc[mask, col] = np.nan
    return df, int(df.isna().sum().sum()) - before


def _coerce_numeric_strings(df: pd.DataFrame) -> tuple[pd.DataFrame, list[str]]:
    """Convert text columns that are really numbers ('$1,234', '45%') to numeric."""
    converted = []
    for col in df.select_dtypes(include="object").columns:
        stripped = df[col].astype(str).str.replace(_NUMERIC_JUNK, "", regex=True)
        num = pd.to_numeric(stripped, errors="coerce")
        notna = df[col].notna()
        if notna.any() and num[notna].notna().mean() >= 0.8:
            df[col] = num
            converted.append(col)
    return df, converted


def _clip_outliers(df: pd.DataFrame, target_column: str | None) -> tuple[pd.DataFrame, list[str]]:
    """Clip numeric values outside 1.5*IQR to the whisker bounds."""
    clipped = []
    for col in df.select_dtypes(include=np.number).columns:
        if col == target_column:
            continue
        q1, q3 = df[col].quantile([0.25, 0.75])
        iqr = q3 - q1
        if not iqr:
            continue
        lo, hi = q1 - 1.5 * iqr, q3 + 1.5 * iqr
        n = int(((df[col] < lo) | (df[col] > hi)).sum())
        if n:
            df[col] = df[col].clip(lo, hi)
            clipped.append(f"{col} ({n})")
    return df, clipped


def preprocess_data(
    df: pd.DataFrame,
    missing_strategy: str = "Impute (mean/mode)",
    drop_duplicates: bool = True,
    encode_categoricals: bool = True,
    scaling: str = "None",
    clip_outliers: bool = False,
    target_column: str | None = None,
) -> tuple[pd.DataFrame, list[str]]:
    """Clean the dataset and return (cleaned_df, list of steps applied)."""
    steps = []
    df = df.copy()

    # Text tidy-up: strip whitespace in string cells, normalize missing tokens
    for col in df.select_dtypes(include="object").columns:
        df[col] = df[col].str.strip()
    df, n_tokens = _normalize_missing_tokens(df)
    if n_tokens:
        steps.append(
            f"Converted {n_tokens} placeholder values ('N/A', '?', '-', 'null'...) to missing"
        )

    # Text columns that are actually numeric ('$1,234', '45%', '1 000')
    df, converted = _coerce_numeric_strings(df)
    if converted:
        steps.append(f"Converted numeric-looking text columns to numbers: {', '.join(converted)}")

    # Drop columns that are entirely empty
    empty_cols = [c for c in df.columns if df[c].isna().all()]
    if empty_cols:
        df = df.drop(columns=empty_cols)
        steps.append(f"Dropped fully-empty columns: {', '.join(empty_cols)}")

    # Drop constant columns — they carry no signal
    const_cols = [
        c for c in df.columns
        if c != target_column and df[c].nunique(dropna=False) <= 1
    ]
    if const_cols:
        df = df.drop(columns=const_cols)
        steps.append(f"Dropped constant columns: {', '.join(const_cols)}")

    # Duplicates
    if drop_duplicates:
        n = int(df.duplicated().sum())
        if n:
            df = df.drop_duplicates().reset_index(drop=True)
            steps.append(f"Removed {n} duplicate rows")

    # Missing values
    n_missing = int(df.isna().sum().sum())
    if n_missing:
        if missing_strategy.startswith("Drop"):
            before = len(df)
            df = df.dropna().reset_index(drop=True)
            steps.append(f"Dropped {before - len(df)} rows with missing values")
        else:
            use_median = "median" in missing_strategy.lower()
            for col in df.columns:
                if df[col].isna().any():
                    if pd.api.types.is_numeric_dtype(df[col]):
                        fill = df[col].median() if use_median else df[col].mean()
                        df[col] = df[col].fillna(fill)
                    else:
                        mode = df[col].mode()
                        df[col] = df[col].fillna(mode.iloc[0] if len(mode) else "unknown")
            centre = "median" if use_median else "mean"
            steps.append(
                f"Imputed {n_missing} missing values ({centre} for numeric, mode for categorical)"
            )

    # Outliers
    if clip_outliers:
        df, clipped = _clip_outliers(df, target_column)
        if clipped:
            steps.append(f"Clipped outliers beyond 1.5*IQR: {', '.join(clipped)}")

    # Encode categoricals (except the target, which models handle separately)
    if encode_categoricals:
        cat_cols = [
            c
            for c in df.select_dtypes(exclude=np.number).columns
            if c != target_column
        ]
        low_card = [c for c in cat_cols if df[c].nunique() <= 20]
        high_card = [c for c in cat_cols if df[c].nunique() > 20]
        if high_card:
            df = df.drop(columns=high_card)
            steps.append(
                f"Dropped high-cardinality text columns (>20 unique): {', '.join(high_card)}"
            )
        if low_card:
            df = pd.get_dummies(df, columns=low_card, drop_first=True, dtype=int)
            steps.append(f"One-hot encoded: {', '.join(low_card)}")

    # Scale numeric features
    if scaling and scaling != "None":
        from sklearn.preprocessing import MinMaxScaler, StandardScaler

        num_cols = [
            c
            for c in df.select_dtypes(include=np.number).columns
            if c != target_column
        ]
        if num_cols:
            if "Min-Max" in scaling:
                df[num_cols] = MinMaxScaler().fit_transform(df[num_cols])
                steps.append(f"Min-Max scaled {len(num_cols)} numeric columns to [0, 1]")
            else:
                df[num_cols] = StandardScaler().fit_transform(df[num_cols])
                steps.append(f"Standard-scaled {len(num_cols)} numeric columns (mean 0, std 1)")

    if not steps:
        steps.append("Data was already clean — no changes needed")

    df.to_csv(CLEANED_PATH, index=False)
    steps.append(f"Saved cleaned data ({len(df):,} rows x {df.shape[1]} cols) for modeling")
    return df, steps


def generate_preprocessing_code(
    missing_strategy: str,
    drop_duplicates: bool,
    encode_categoricals: bool,
    scaling: str,
    clip_outliers: bool,
    target_column: str | None,
) -> str:
    """Return equivalent standalone pandas code for the preprocessing performed."""
    code = [
        "import re",
        "import numpy as np",
        "import pandas as pd",
        "",
        "df = pd.read_csv('your_data.csv')",
        "",
        "# Tidy text cells and turn placeholder strings into real NaN",
        "MISSING = {'', 'nan', 'none', 'null', 'na', 'n/a', '-', '?', 'missing'}",
        "for col in df.select_dtypes(include='object').columns:",
        "    df[col] = df[col].str.strip()",
        "    mask = df[col].astype(str).str.strip().str.lower().isin(MISSING)",
        "    df.loc[mask, col] = np.nan",
        "",
        "# Convert numeric-looking text columns ('$1,234', '45%') to numbers",
        "for col in df.select_dtypes(include='object').columns:",
        "    num = pd.to_numeric(df[col].astype(str).str.replace(r'[\\s$€£,%]', '', regex=True),",
        "                        errors='coerce')",
        "    if df[col].notna().any() and num[df[col].notna()].notna().mean() >= 0.8:",
        "        df[col] = num",
        "",
        "# Drop fully-empty and constant columns",
        "df = df.dropna(axis=1, how='all')",
        "df = df.drop(columns=[c for c in df.columns if df[c].nunique(dropna=False) <= 1])",
    ]
    if drop_duplicates:
        code += ["", "# Remove duplicate rows", "df = df.drop_duplicates().reset_index(drop=True)"]
    if missing_strategy.startswith("Drop"):
        code += ["", "# Drop rows with missing values", "df = df.dropna().reset_index(drop=True)"]
    else:
        centre = "median" if "median" in missing_strategy.lower() else "mean"
        code += [
            "",
            f"# Impute missing values: {centre} for numeric, mode for categorical",
            "for col in df.columns:",
            "    if df[col].isna().any():",
            "        if pd.api.types.is_numeric_dtype(df[col]):",
            f"            df[col] = df[col].fillna(df[col].{centre}())",
            "        else:",
            "            df[col] = df[col].fillna(df[col].mode().iloc[0])",
        ]
    if clip_outliers:
        code += [
            "",
            "# Clip numeric outliers beyond 1.5*IQR",
            f"target = {target_column!r}",
            "for col in df.select_dtypes(include=np.number).columns:",
            "    if col == target:",
            "        continue",
            "    q1, q3 = df[col].quantile([0.25, 0.75])",
            "    iqr = q3 - q1",
            "    if iqr:",
            "        df[col] = df[col].clip(q1 - 1.5 * iqr, q3 + 1.5 * iqr)",
        ]
    if encode_categoricals:
        code += [
            "",
            "# One-hot encode low-cardinality categoricals (drop high-cardinality text)",
            f"target = {target_column!r}",
            "cat_cols = [c for c in df.select_dtypes(exclude=np.number).columns if c != target]",
            "df = df.drop(columns=[c for c in cat_cols if df[c].nunique() > 20])",
            "cat_cols = [c for c in df.select_dtypes(exclude=np.number).columns if c != target]",
            "df = pd.get_dummies(df, columns=cat_cols, drop_first=True, dtype=int)",
        ]
    if scaling and scaling != "None":
        scaler = "MinMaxScaler" if "Min-Max" in scaling else "StandardScaler"
        code += [
            "",
            f"# Scale numeric features with {scaler}",
            f"from sklearn.preprocessing import {scaler}",
            f"num_cols = [c for c in df.select_dtypes(include=np.number).columns if c != {target_column!r}]",
            f"df[num_cols] = {scaler}().fit_transform(df[num_cols])",
        ]
    code += ["", "df.to_csv('cleaned_data.csv', index=False)"]
    return "\n".join(code)