Repository navigation
Expand file tree
/
Copy pathprediction.py
More file actions
429 lines (349 loc) · 15.7 KB
/
Copy pathprediction.py
File metadata and controls
429 lines (349 loc) · 15.7 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import time
import json
import argparse
import datetime
import torch
from model.utils import parser
from parse_feature_dict import process_feature_dict
import traceback
import pandas as pd
import numpy as np
# =======================================================================
# 1. Configuration and Constants
# =======================================================================
# Prediction probability threshold for binary classification
PREDICTION_THRESHOLD = 0.5
# =======================================================================
# 2. Utility Functions for Model and Data
# =======================================================================
def load_checkpoint(filepath: str, device: str):
"""
Loads a PyTorch model checkpoint and extracts the model.8
Args:
filepath (str): Path to the model checkpoint file.
device (str): Computation device (e.g., 'cpu', 'cuda:0').
Returns:
torch.nn.Module: The loaded model in evaluation mode.
"""
model_path = os.path.join(filepath, "VITAL.pt")
# Load model
model = torch.jit.load(model_path, map_location=device)
model.eval()
return model
def move_data_to_device(data, target_device, dtype=None):
"""
Converts data to a PyTorch tensor and moves it to the specified device.
Args:
data: The input data (e.g., list, numpy array).
target_device (str): The device to move the tensor to.
dtype (torch.dtype, optional): Target data type. Defaults to None (uses torch.Tensor default).
Returns:
torch.Tensor: The tensor on the target device.
"""
tensor = torch.tensor(data, dtype=dtype) if dtype else torch.Tensor(data)
return tensor.to(target_device)
# =======================================================================
# 3. Prediction Logic
# =======================================================================
def get_prediction(feature_dict_path: str, model_ckpt: torch.nn.Module, device: str):
"""
The main model inference logic: loads features, converts to tensors, and executes prediction.
Args:
feature_dict_path (str): Path to the feature dictionary JSON file.
model_ckpt (torch.nn.Module): The loaded model instance.
device (str): Computation device.
Returns:
tuple[int, float]: A tuple containing the predicted class (0 or 1) and the probability score.
"""
if model_ckpt is None:
raise RuntimeError("Model is not loaded. Cannot perform prediction.")
# 1. Feature Preprocessing
with open(feature_dict_path, 'r') as f:
feature_dict = json.load(f)
# Unpack features from the custom preprocessing function
(x_features, y_features, pep_segment_num, prot_segment_num,
segment_label_num, x_segment_seq, y_segment_seq,
full_seq_features_x, full_seq_features_y) = process_feature_dict(feature_dict)
# 2. Data Preparation and Device Movement
x_features = move_data_to_device(x_features, device).unsqueeze(0)
y_features = move_data_to_device(y_features, device).unsqueeze(0)
full_seq_features_x = move_data_to_device(full_seq_features_x, device).unsqueeze(0)
full_seq_features_y = move_data_to_device(full_seq_features_y, device).unsqueeze(0)
x_segment_seq = move_data_to_device(x_segment_seq, device, torch.long).unsqueeze(0)
y_segment_seq = move_data_to_device(y_segment_seq, device, torch.long).unsqueeze(0)
pep_segment_num = torch.tensor(pep_segment_num, dtype=torch.long, device=device)
prot_segment_num = torch.tensor(prot_segment_num, dtype=torch.long, device=device)
segment_label_num = torch.tensor(segment_label_num, dtype=torch.long, device=device)
# 3. Model Inference
with torch.no_grad():
probability, ASM = model_ckpt(
x_features, y_features, pep_segment_num, prot_segment_num,
segment_label_num, x_segment_seq, y_segment_seq,
full_seq_features_x, full_seq_features_y
)
# Extract scalar probability value from the output tensor
scalar_value = probability.cpu().item()
predicted_class = 1 if scalar_value > PREDICTION_THRESHOLD else 0
return predicted_class, scalar_value, ASM.cpu().numpy()
# =======================================================================
# 4. Command Line Interface
# =======================================================================
def save_asm_data(asm_array: np.ndarray, output_path: str, pair_id: str = None) -> str:
"""
Save ASM numpy array to file with unique filename.
Args:
asm_array: ASM numpy array to save
output_path: Directory or file path for saving
pair_id: Optional pair ID for filename generation
Returns:
str: Path where ASM was saved
"""
try:
output_dir = os.path.dirname(output_path) if output_path.endswith('.npy') else output_path
if output_dir:
os.makedirs(output_dir, exist_ok=True)
# Generate unique filename
if output_path.endswith('.npy'):
asm_save_path = output_path
else:
if pair_id:
filename = f"ASM_{pair_id}.npy"
else:
filename = f"ASM_output.npy"
asm_save_path = os.path.join(output_path, filename)
np.save(asm_save_path, asm_array)
return asm_save_path
except Exception as e:
print(f"Failed to save ASM data: {e}")
raise
def predict_single(args, model_ckpt) -> int:
"""
Predict interaction for a single feature dictionary.
Args:
args: Command line arguments
model_ckpt: Loaded model checkpoint
Returns:
Exit code (0 for success, 1 for error)
"""
try:
start_time = time.time()
label, probability, ASM = get_prediction(args.feature_dict_path, model_ckpt, args.device)
duration = time.time() - start_time
# Save ASM data
asm_save_path = save_asm_data(ASM, args.ASM_output_path)
result = {
"status": "success",
"prediction": {
"is_interaction": bool(label == 1),
"probability": round(probability, 4),
"predicted_label": int(label),
"ASM_path": asm_save_path,
"duration_seconds": round(duration, 2)
},
"metadata": {
"input_file": os.path.basename(args.feature_dict_path),
"model_checkpoint": os.path.basename(args.ckpt_path),
"computation_device": args.device,
"timestamp": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 8 * 3600))
}
}
if args.verbose:
print(f"\n Prediction completed in {duration:.2f} seconds")
print(f"Result: Interaction={'YES' if label==1 else 'NO'}, Probability={probability:.4f}")
output_dir = os.path.dirname(args.output)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
# Save to file
with open(args.output, 'w', encoding='utf-8') as f:
json.dump(result, f, indent=4, ensure_ascii=False)
print(f"Prediction result saved to: {args.output}")
return 0
except Exception as e:
print(f"Single prediction failed: {str(e)}")
if args.verbose:
traceback.print_exc()
# Save error result
error_result = {
"status": "error",
"error_message": str(e),
"input_file": args.feature_dict_path,
"timestamp": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 8 * 3600))
}
try:
output_dir = os.path.dirname(args.output)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
with open(args.output, 'w', encoding='utf-8') as f:
json.dump(error_result, f, indent=4, ensure_ascii=False)
print(f"Error information saved to: {args.output}")
except Exception as save_error:
print(f"Failed to save error results: {save_error}")
return 1
def predict_batch(args, model_ckpt) -> int:
"""
Batch prediction for multiple feature dictionaries.
Args:
args: Command line arguments
model_ckpt: Loaded model checkpoint
Returns:
Exit code (0 for success, 1 for error)
"""
try:
# Read input CSV
if not os.path.exists(args.batch_input_csv):
print(f"Batch input CSV not found: {args.batch_input_csv}")
return 1
df = pd.read_csv(args.batch_input_csv)
# Validate required columns
required_columns = ['pair_id', 'feature_dict_path']
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
print(f"Missing required columns in CSV: {missing_columns}")
print(f"Required columns: {required_columns}")
return 1
if args.verbose:
print(f"\nBatch prediction started")
print(f"Input CSV: {args.batch_input_csv}")
print(f"Total items: {len(df)}")
print(f"Output file: {args.output}")
results = []
successful_predictions = 0
failed_predictions = 0
# Process each row
for index, row in df.iterrows():
pair_id = row['pair_id']
feature_dict_path = row['feature_dict_path']
if args.verbose:
print(f"Processing: {pair_id} -> {feature_dict_path}")
# Check if feature dictionary exists
if not os.path.exists(feature_dict_path):
print(f"Feature dictionary not found for {pair_id}: {feature_dict_path}")
result = {
"pair_id": pair_id,
"status": "error",
"error_message": f"Feature dictionary not found: {feature_dict_path}",
"feature_dict_path": feature_dict_path
}
failed_predictions += 1
results.append(result)
continue
try:
# Perform prediction
start_time = time.time()
label, probability, ASM = get_prediction(feature_dict_path, model_ckpt, args.device)
duration = time.time() - start_time
# Save ASM data
asm_save_path = save_asm_data(ASM, args.ASM_output_path, pair_id=pair_id)
result = {
"pair_id": pair_id,
"prediction": {
"is_interaction": bool(label == 1),
"probability": round(probability, 4),
"predicted_label": int(label),
"ASM_path": asm_save_path,
"duration_seconds": round(duration, 2)
},
"feature_dict_path": feature_dict_path,
"timestamp": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 8 * 3600))
}
successful_predictions += 1
if args.verbose:
print(f"{pair_id}: Interaction={'YES' if label==1 else 'NO'}, Prob={probability:.4f}")
except Exception as e:
print(f"Prediction failed for {pair_id}: {str(e)}")
result = {
"pair_id": pair_id,
"status": "error",
"error_message": str(e),
"feature_dict_path": feature_dict_path,
"timestamp": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 8 * 3600))
}
failed_predictions += 1
results.append(result)
# Prepare final output
batch_result = {
"status": "completed",
"summary": {
"total_items": len(df),
"successful_predictions": successful_predictions,
"failed_predictions": failed_predictions,
"success_rate": round(successful_predictions / len(df) * 100, 2) if len(df) > 0 else 0,
"timestamp": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 8 * 3600))
},
"results": results
}
# Ensure output directory exists
output_dir = os.path.dirname(args.output)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
# Save batch results
with open(args.output, 'w', encoding='utf-8') as f:
json.dump(batch_result, f, indent=4, ensure_ascii=False)
# Print summary
print(f"\nBatch prediction completed:")
print(f"Successful: {successful_predictions}/{len(df)}")
print(f"Failed: {failed_predictions}/{len(df)}")
print(f"Success rate: {batch_result['summary']['success_rate']}%")
print(f"Results saved to: {args.output}")
return 0 if failed_predictions == 0 else 1
except Exception as e:
print(f"Batch prediction failed: {str(e)}")
if args.verbose:
traceback.print_exc()
return 1
def main():
"""
Main function for running the prediction script via command line.
"""
args = parser()
if args.verbose:
print(f"--- PePPI Prediction Initiated ---")
print(f"Using device: {args.device}")
if hasattr(args, 'feature_dict_path') and args.feature_dict_path:
print(f"Single prediction input: {args.feature_dict_path}")
if hasattr(args, 'batch_input_csv') and args.batch_input_csv:
print(f"Batch prediction input: {args.batch_input_csv}")
print(f"Output file: {args.output}")
# 1. Model Loading
model_ckpt = None
try:
model_ckpt = load_checkpoint(args.ckpt_path, args.device).to(args.device)
print(f"Model loaded successfully on {args.device}")
except Exception as e:
print(f"Fatal Error: Failed to load model from {args.ckpt_path}. Details: {e}")
return 1
# 2. Prediction Execution
try:
if hasattr(args, 'batch_input_csv') and args.batch_input_csv:
# Batch prediction mode
return predict_batch(args, model_ckpt)
else:
# Single prediction mode
return predict_single(args, model_ckpt)
except Exception as e:
error_msg = f"❌ Prediction execution failed. Details: {str(e)}"
print(f"\n--- Error ---")
print(error_msg)
if args.verbose:
traceback.print_exc()
# Prepare error result structure
error_result = {
"status": "error",
"error_message": str(e),
"input_file": getattr(args, 'feature_dict_path', getattr(args, 'batch_input_csv', 'unknown')),
"timestamp": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 8 * 3600))
}
try:
output_dir = os.path.dirname(args.output)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
with open(args.output, 'w', encoding='utf-8') as f:
json.dump(error_result, f, indent=4, ensure_ascii=False)
print(f"💾 Error information saved to: {args.output}")
except Exception as save_error:
print(f"❌ Failed to save error results: {save_error}")
return 1
if __name__ == "__main__":
exit_code = main()
exit(exit_code)