-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpreprocessing.py
More file actions
179 lines (107 loc) · 4.4 KB
/
Copy pathpreprocessing.py
File metadata and controls
179 lines (107 loc) · 4.4 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
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
import pandas as pd
import numpy as np
def preprocess_dataset(df):
print("\n==============================")
print("Starting Dataset Preprocessing")
print("==============================")
print(f"Initial dataset shape: {df.shape}")
df = df.copy()
# ------------------------------------------------
# Remove duplicate rows
# ------------------------------------------------
before = len(df)
df = df.drop_duplicates()
after = len(df)
print(f"Removed {before-after} duplicate rows")
# ------------------------------------------------
# Remove fully empty columns
# ------------------------------------------------
before_cols = df.shape[1]
df = df.dropna(axis=1, how="all")
after_cols = df.shape[1]
print(f"Removed {before_cols-after_cols} empty columns")
# ------------------------------------------------
# Convert numeric columns
# ------------------------------------------------
print("Converting numeric columns where possible...")
for col in df.columns:
if df[col].dtype == "object":
try:
df[col] = pd.to_numeric(df[col])
except:
pass
print("Numeric conversion completed")
# ------------------------------------------------
# Replace infinite values
# ------------------------------------------------
inf_count = np.isinf(df.select_dtypes(include=np.number)).sum().sum()
if inf_count > 0:
print(f"Replacing {inf_count} infinite values")
df.replace([np.inf, -np.inf], np.nan, inplace=True)
# ------------------------------------------------
# Timestamp handling
# ------------------------------------------------
if "timestamp" in df.columns:
print("Processing timestamps...")
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
df = df.sort_values("timestamp")
df = df.reset_index(drop=True)
print("Timestamp sorting completed")
# ------------------------------------------------
# Missing value analysis
# ------------------------------------------------
total_missing = df.isna().sum().sum()
print(f"Total missing values detected: {total_missing}")
numeric_cols = df.select_dtypes(include=[np.number]).columns
# Forward fill
print("Applying forward fill to numeric telemetry data...")
df[numeric_cols] = df[numeric_cols].ffill()
# Backward fill
print("Applying backward fill...")
df[numeric_cols] = df[numeric_cols].bfill()
# Remaining NaN
remaining_nan = df[numeric_cols].isna().sum().sum()
if remaining_nan > 0:
print(f"Filling remaining {remaining_nan} NaN values with 0")
df[numeric_cols] = df[numeric_cols].fillna(0)
# ------------------------------------------------
# Sensor spike removal
# ------------------------------------------------
print("Detecting and clipping sensor outliers using IQR method...")
for col in numeric_cols:
q1 = df[col].quantile(0.25)
q3 = df[col].quantile(0.75)
iqr = q3 - q1
lower = q1 - 3 * iqr
upper = q3 + 3 * iqr
df[col] = df[col].clip(lower, upper)
print("Outlier clipping completed")
# ------------------------------------------------
# Sensor sanity limits
# ------------------------------------------------
print("Applying sensor sanity limits...")
if "power" in df.columns:
df["power"] = df["power"].clip(lower=0)
if "temp" in df.columns:
df["temp"] = df["temp"].clip(-40, 120)
if "freq" in df.columns:
df["freq"] = df["freq"].clip(45, 65)
print("Sensor sanity checks completed")
# ------------------------------------------------
# Timestamp gap detection
# ------------------------------------------------
if "timestamp" in df.columns:
print("Checking for timestamp gaps...")
time_diff = df["timestamp"].diff()
expected = pd.Timedelta(minutes=5)
gap_mask = time_diff > expected
gaps = gap_mask.sum()
if gaps > 0:
print(f"WARNING: Detected {gaps} timestamp gaps")
else:
print("No timestamp gaps detected")
print("----------------------------------")
print(f"Final cleaned dataset shape: {df.shape}")
print("Dataset preprocessing completed")
print("----------------------------------\n")
return df