-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_script.py
More file actions
351 lines (303 loc) · 13.5 KB
/
Copy pathmodel_script.py
File metadata and controls
351 lines (303 loc) · 13.5 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
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
import argparse, os, json, warnings, sys, joblib
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.utils.class_weight import compute_class_weight
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from tqdm.auto import tqdm
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
SEED = 42
np.random.seed(SEED)
torch.manual_seed(SEED)
# Canonical Kepler feature list (order matters!)
KEPLER_FEATURES = [
"koi_period", "koi_duration", "koi_depth", "koi_model_snr", "koi_impact",
"koi_prad", "koi_teq", "koi_insol",
"koi_steff", "koi_slogg", "koi_srad",
"koi_fpflag_nt", "koi_fpflag_ss", "koi_fpflag_co", "koi_fpflag_ec",
]
LABEL_COL = "koi_disposition"
MULTI_CLASSES = ["CONFIRMED", "CANDIDATE", "FALSE POSITIVE"]
def load_csv_smart(path: str) -> pd.DataFrame:
for sep in [",", "\t"]:
try:
df = pd.read_csv(path, sep=sep, comment="#", engine="python")
if df.shape[1] > 1:
return df
except Exception:
pass
raw = [ln for ln in open(path, "r", encoding="utf-8", errors="ignore").read().splitlines()
if not ln.startswith("#") and ln.strip()]
header = raw[0]
delim = "," if header.count(",") >= header.count("\t") else "\t"
data = [r.split(delim) for r in raw]
return pd.DataFrame(data[1:], columns=data[0])
def prepare_kepler(df: pd.DataFrame, binary: bool):
feats = [c for c in KEPLER_FEATURES if c in df.columns]
need = [LABEL_COL] + feats
df = df.dropna(subset=[LABEL_COL])
df = df[[c for c in need if c in df.columns]].copy()
# numeric coercion + median impute
for c in feats:
df[c] = pd.to_numeric(df[c], errors="coerce")
if df[c].isna().all():
df[c] = 0.0
else:
df[c] = df[c].fillna(df[c].median())
df = df[df[LABEL_COL].isin(MULTI_CLASSES)].copy()
if binary:
y = np.where(df[LABEL_COL].isin(["CONFIRMED", "CANDIDATE"]), 1, 0).astype(int)
class_names = ["NOT", "PLANET"]
else:
class_to_idx = {cls: i for i, cls in enumerate(MULTI_CLASSES)}
y = df[LABEL_COL].map(class_to_idx).astype(int).values
class_names = MULTI_CLASSES
X = df[[c for c in KEPLER_FEATURES if c in df.columns]].astype(np.float32).values
used_feats = [c for c in KEPLER_FEATURES if c in df.columns]
return X, y, used_feats, class_names
# ---------- MLP path ----------
class TabularDS(Dataset):
def __init__(self, X, y):
self.X = torch.tensor(X, dtype=torch.float32)
self.y = torch.tensor(y, dtype=torch.long)
def __len__(self): return len(self.y)
def __getitem__(self, i): return self.X[i], self.y[i]
class MLP(nn.Module):
def __init__(self, in_dim, out_dim):
super().__init__()
self.net = nn.Sequential(
nn.Linear(in_dim, 256), nn.ReLU(),
nn.Linear(256, 128), nn.ReLU(),
nn.Dropout(0.10),
nn.Linear(128, 64), nn.ReLU(),
nn.Linear(64, out_dim)
)
def forward(self, x): return self.net(x)
def train_mlp(X_tr, y_tr, X_te, y_te, class_names, epochs=60, batch=256):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
classes = np.unique(y_tr)
weights_np = compute_class_weight(class_weight="balanced", classes=classes, y=y_tr).astype(np.float32)
class_weights = torch.from_numpy(weights_np).to(device=device, dtype=torch.float32)
train_loader = DataLoader(TabularDS(X_tr, y_tr), batch_size=batch, shuffle=True)
test_loader = DataLoader(TabularDS(X_te, y_te), batch_size=max(256, batch), shuffle=False)
model = MLP(in_dim=X_tr.shape[1], out_dim=len(class_names)).to(device)
criterion = nn.CrossEntropyLoss(weight=class_weights, label_smoothing=0.03)
optimizer = torch.optim.AdamW(model.parameters(), lr=8e-4, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="max", patience=4, factor=0.5)
best_acc, best_state = 0.0, None
for epoch in range(1, epochs + 1):
model.train()
running = 0.0
pbar = tqdm(train_loader, desc=f"Epoch {epoch}/{epochs}", leave=False)
for xb, yb in pbar:
xb, yb = xb.to(device), yb.to(device)
optimizer.zero_grad()
logits = model(xb)
loss = criterion(logits, yb)
loss.backward()
optimizer.step()
running += loss.item() * yb.size(0)
pbar.set_postfix(avg_loss=f"{running/len(y_tr):.4f}", lr=f"{optimizer.param_groups[0]['lr']:.1e}")
# quick val
model.eval()
preds = []
with torch.no_grad():
for xb, _ in test_loader:
logits = model(xb.to(device))
preds.append(torch.softmax(logits, dim=1).cpu().numpy())
y_prob = np.vstack(preds)
y_pred = y_prob.argmax(1)
acc = accuracy_score(y_te, y_pred)
scheduler.step(acc)
tqdm.write(f"Epoch {epoch:02d} | val_acc={acc:.4f}")
if acc > best_acc + 1e-4:
best_acc = acc
best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}
if best_state is not None:
model.load_state_dict({k: v for k, v in best_state.items()})
return model
def eval_and_print(y_te, y_pred, y_prob, class_names, tag="Model"):
print(f"\n[{tag}] Test accuracy:", accuracy_score(y_te, y_pred))
print(f"\n[{tag}] Classification report:")
print(classification_report(y_te, y_pred, target_names=class_names))
print(f"\n[{tag}] Confusion matrix:\n", confusion_matrix(y_te, y_pred))
# ---------- Train command ----------
def cmd_train(args):
print("numpy:", np.__version__)
df = load_csv_smart(args.csv)
X, y, features, class_names = prepare_kepler(df, binary=args.binary)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.21, random_state=SEED, stratify=y)
scaler = StandardScaler()
X_tr = scaler.fit_transform(X_tr)
X_te = scaler.transform(X_te)
os.makedirs(args.out, exist_ok=True)
meta = {
"binary": args.binary,
"model": args.model,
"features": features,
"classes": class_names,
"scaler_mean": scaler.mean_.tolist(),
"scaler_scale": scaler.scale_.tolist(),
}
if args.model == "tree":
model = HistGradientBoostingClassifier(
learning_rate=0.06, max_depth=8, max_iter=800,
l2_regularization=0.0, min_samples_leaf=20, random_state=SEED
)
model.fit(X_tr, y_tr)
y_pred = model.predict(X_te)
y_prob = model.predict_proba(X_te)
eval_and_print(y_te, y_pred, y_prob, class_names, tag="Tree")
joblib.dump(model, os.path.join(args.out, "model.joblib"))
np.savez(os.path.join(args.out, "scaler.npz"), mean=scaler.mean_, scale=scaler.scale_)
with open(os.path.join(args.out, "meta.json"), "w") as f:
json.dump(meta, f, indent=2)
print(f"\nSaved artifacts to: {args.out}")
else:
# MLP path
model = train_mlp(X_tr, y_tr, X_te, y_te, class_names, epochs=args.epochs, batch=args.batch_size)
# Final eval
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.eval()
with torch.no_grad():
y_prob = torch.softmax(torch.tensor(model(np.asarray(X_te, dtype=np.float32))).to("cpu"), dim=1).numpy()
y_pred = y_prob.argmax(1)
eval_and_print(y_te, y_pred, y_prob, class_names, tag="MLP")
# Save artifacts
torch.save({"state_dict": model.state_dict(),
"in_dim": X_tr.shape[1],
"out_dim": len(class_names)}, os.path.join(args.out, "model.pt"))
np.savez(os.path.join(args.out, "scaler.npz"), mean=scaler.mean_, scale=scaler.scale_)
with open(os.path.join(args.out, "meta.json"), "w") as f:
json.dump(meta, f, indent=2)
print(f"\nSaved artifacts to: {args.out}")
# ---------- Predict utilities ----------
def load_artifacts(art_dir):
with open(os.path.join(art_dir, "meta.json"), "r") as f:
meta = json.load(f)
sc = np.load(os.path.join(art_dir, "scaler.npz"))
scaler_mean, scaler_scale = sc["mean"], sc["scale"]
scaler = StandardScaler()
# Manually set trained params
scaler.mean_ = scaler_mean
scaler.scale_ = scaler_scale
if meta["model"] == "tree":
model = joblib.load(os.path.join(art_dir, "model.joblib"))
model_type = "tree"
else:
# Rebuild and load MLP
in_dim = len(meta["features"])
out_dim = len(meta["classes"])
model = MLP(in_dim, out_dim)
state = torch.load(os.path.join(art_dir, "model.pt"), map_location="cpu")
model.load_state_dict(state["state_dict"])
model.eval()
model_type = "mlp"
return meta, scaler, model, model_type
def vector_from_kwargs(meta, **kwargs):
feats = meta["features"]
x = []
for f in feats:
if f not in kwargs or kwargs[f] is None:
raise ValueError(f"Missing required feature: {f}")
x.append(float(kwargs[f]))
return np.array(x, dtype=np.float32).reshape(1, -1)
def predict_single(art_dir, **feature_kwargs):
meta, scaler, model, model_type = load_artifacts(art_dir)
x = vector_from_kwargs(meta, **feature_kwargs)
xs = (x - scaler.mean_) / scaler.scale_
if model_type == "tree":
probs = model.predict_proba(xs)[0]
else:
with torch.no_grad():
probs = torch.softmax(torch.tensor(model(xs.astype(np.float32))), dim=1).numpy()[0]
idx = int(np.argmax(probs))
label = meta["classes"][idx]
return label, {meta["classes"][i]: float(probs[i]) for i in range(len(probs))}
def predict_csv(art_dir, csv_path, out_path=None):
meta, scaler, model, model_type = load_artifacts(art_dir)
df = load_csv_smart(csv_path)
# Prepare features in correct order; coerce/median-impute
feats = meta["features"]
for c in feats:
df[c] = pd.to_numeric(df.get(c, np.nan), errors="coerce")
if df[c].isna().all():
df[c] = 0.0
else:
df[c] = df[c].fillna(df[c].median())
X = df[feats].astype(np.float32).values
Xs = (X - scaler.mean_) / scaler.scale_
if model_type == "tree":
y_prob = model.predict_proba(Xs)
else:
with torch.no_grad():
y_prob = torch.softmax(torch.tensor(model(Xs.astype(np.float32))), dim=1).numpy()
y_pred = y_prob.argmax(1)
out = pd.DataFrame({"pred_label": [meta["classes"][i] for i in y_pred]})
for i, cname in enumerate(meta["classes"]):
out[f"conf_{cname}"] = y_prob[:, i]
if out_path:
out.to_csv(out_path, index=False)
print(f"Wrote predictions to {out_path}")
return out
# ---------- Predict command ----------
def cmd_predict(args):
if args.from_csv:
predict_csv(args.artifacts, args.from_csv, out_path=args.out)
else:
# Build kwargs from provided CLI flags matching feature names
with open(os.path.join(args.artifacts, "meta.json"), "r") as f:
meta = json.load(f)
feats = meta["features"]
kwargs = {}
for f in feats:
# argparse stores dashes as underscore; our features have underscores already
val = getattr(args, f, None)
kwargs[f] = val
label, probs = predict_single(args.artifacts, **kwargs)
print("\nPREDICTION:", label)
print("PROBS:", json.dumps(probs, indent=2))
# ---------- Print-schema command ----------
def cmd_print_schema(args):
with open(os.path.join(args.artifacts, "meta.json"), "r") as f:
meta = json.load(f)
print("Required features, in order:")
for f in meta["features"]:
print("-", f)
print("\nClasses:", meta["classes"])
print("Binary mode:", meta["binary"])
print("Model:", meta["model"])
# ---------- CLI ----------
def build_parser():
ap = argparse.ArgumentParser(description="Kepler ML CLI: train/export, load, and predict from CLI")
sp = ap.add_subparsers(dest="cmd", required=True)
tr = sp.add_parser("train", help="Train and export artifacts")
tr.add_argument("--csv", required=True, help="Path to Kepler CSV")
tr.add_argument("--binary", action="store_true", help="Planet vs Not (CONFIRMED/CANDIDATE -> PLANET)")
tr.add_argument("--model", choices=["tree","mlp"], default="tree")
tr.add_argument("--epochs", type=int, default=60)
tr.add_argument("--batch-size", type=int, default=256)
tr.add_argument("--out", default="./artifacts")
tr.set_defaults(func=cmd_train)
pr = sp.add_parser("predict", help="Predict from CLI flags or CSV")
pr.add_argument("--artifacts", required=True)
pr.add_argument("--from-csv", help="Batch predict a CSV")
pr.add_argument("--out", help="Where to write CSV predictions")
# Dynamically add feature flags so you can do --koi_period 9.49 etc.
for feat in KEPLER_FEATURES:
pr.add_argument(f"--{feat}", type=float, required=False, help=f"Feature: {feat}")
pr.set_defaults(func=cmd_predict)
sc = sp.add_parser("print-schema", help="Print required features and classes for this artifact")
sc.add_argument("--artifacts", required=True)
sc.set_defaults(func=cmd_print_schema)
return ap
if __name__ == "__main__":
parser = build_parser()
args = parser.parse_args()
args.func(args)