-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
250 lines (193 loc) · 9.53 KB
/
main.py
File metadata and controls
250 lines (193 loc) · 9.53 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
import os
import torch
import torch.nn as nn
import numpy as np
# Fix for numpy compatibility issues with PyTorch model loading
import sys
try:
import numpy._core.multiarray
except ImportError:
if not hasattr(np, '_core'):
np._core = np.core
sys.modules['numpy._core.multiarray'] = sys.modules['numpy.core.multiarray']
from GNNModels.models import GINConvNet
from GNNModels.config_manager import Config
from GNNModels.datasets import load_dataset
from MorganFingerprintMLP.models import MLP as MorganFingerprintMLP
from MorganFingerprintMLP.data_loader import extract_features_from_smiles, prepare_data_loaders
from StructuralModels.models import TraditionalMLEnsemble, DeepMLP, MolecularFeatureExtractor
from Inference.gnn_predict import smiles_to_pyg_data
def smiles_to_graph(smiles):
return smiles_to_pyg_data(smiles)
class StackedModel(nn.Module):
def __init__(self, dataset):
super().__init__()
self.gnn_model = GINConvNet(
num_features=dataset.num_features,
num_classes=1 # Use 1 for binary classification with sigmoid
)
self.morgan_mlp = MorganFingerprintMLP(
input_dim=2072
)
self.traditional_ensemble = TraditionalMLEnsemble()
self.deep_mlp = DeepMLP(
input_dim=64
)
# Add feature extractor for structural models
self.feature_extractor = MolecularFeatureExtractor()
self.meta_classifier = nn.Sequential(
nn.Linear(3, 64),
nn.ReLU(),
nn.Linear(64, 1),
nn.Sigmoid()
)
def forward(self, input_data, model_type: str = "deep"):
# Handle both SMILES strings and PyG Data objects
if isinstance(input_data, str):
# SMILES string input
smiles = input_data
graph = smiles_to_graph(smiles)
morgan_features = extract_features_from_smiles(smiles)
# Create batch tensor for single graph
batch = torch.zeros(graph.x.size(0), dtype=torch.long, device=graph.x.device)
else:
# PyG Data object input (from dataset)
graph = input_data
# For training, we can't extract Morgan features without SMILES
morgan_features = torch.zeros(1, 2072, device=graph.x.device)
# Create batch tensor for single graph
batch = torch.zeros(graph.x.size(0), dtype=torch.long, device=graph.x.device)
with torch.no_grad():
if isinstance(input_data, str):
morgan_pred = self.morgan_mlp(morgan_features)
else:
morgan_pred = torch.zeros(1, 1, device=graph.x.device)
gnn_pred = self.gnn_model(graph.x, graph.edge_index, batch)
if model_type == "traditional":
structure_pred = self.traditional_ensemble(graph)
else:
# Extract features for DeepMLP
features = self.feature_extractor.extract_features(graph)
features_tensor = torch.tensor(features, dtype=torch.float32, device=graph.x.device).unsqueeze(0)
# Set model to eval mode for single sample inference
training_mode = self.deep_mlp.training
self.deep_mlp.eval()
structure_pred = self.deep_mlp(features_tensor)
self.deep_mlp.train(training_mode) # Restore original mode
# Ensure all predictions have the same shape (1, 1)
gnn_pred = gnn_pred.view(1, 1)
morgan_pred = morgan_pred.view(1, 1)
structure_pred = structure_pred.view(1, 1)
individual_preds = torch.cat([gnn_pred, morgan_pred, structure_pred], dim=1) # Shape: (1, 3)
meta_pred = self.meta_classifier(individual_preds)
return meta_pred
# train method
def train_meta(self, epochs, optimizer, train_dataset):
self.train()
for epoch in range(epochs):
print(f"Training epoch {epoch + 1}/{epochs}")
total_loss = 0
num_batches = 0
for i, data in enumerate(train_dataset):
if i >= 100: # Limit training for demo purposes
break
optimizer.zero_grad()
pred = self.forward(data)
# Get the label from the data object
target = data.y.float().view(-1, 1)
loss = nn.BCELoss()(pred, target)
loss.backward()
optimizer.step()
total_loss += loss.item()
num_batches += 1
avg_loss = total_loss / num_batches if num_batches > 0 else 0
print(f"Epoch {epoch + 1} average loss: {avg_loss:.4f}")
@torch.no_grad()
def predict(self, input_data, model_type: str = "deep"):
# Handle both SMILES strings and PyG Data objects
if isinstance(input_data, str):
# SMILES string input
smiles = input_data
graph = smiles_to_graph(smiles)
morgan_features = extract_features_from_smiles([smiles]) # Pass as list
# Create batch tensor for single graph
batch = torch.zeros(graph.x.size(0), dtype=torch.long, device=graph.x.device)
else:
# PyG Data object input
graph = input_data
# For Data objects, we can't extract Morgan features without SMILES
morgan_features = torch.zeros(1, 2072, device=graph.x.device)
# Create batch tensor for single graph
batch = torch.zeros(graph.x.size(0), dtype=torch.long, device=graph.x.device)
# Forward pass
if isinstance(input_data, str):
morgan_features_tensor = torch.tensor(morgan_features[0], dtype=torch.float32, device=graph.x.device).unsqueeze(0) # Take first sample and add batch dim
morgan_pred = self.morgan_mlp(morgan_features_tensor)
else:
# Skip Morgan prediction for graph data without SMILES
morgan_pred = torch.zeros(1, 1, device=graph.x.device)
gnn_pred = self.gnn_model(graph.x, graph.edge_index, batch)
if model_type == "traditional":
structure_pred = self.traditional_ensemble(graph)
else:
# Extract features for DeepMLP
features = self.feature_extractor.extract_features(graph)
features_tensor = torch.tensor(features, dtype=torch.float32, device=graph.x.device).unsqueeze(0)
structure_pred = self.deep_mlp(features_tensor)
# Ensure all predictions have the same shape (1, 1)
gnn_pred = gnn_pred.view(1, 1)
morgan_pred = morgan_pred.view(1, 1)
structure_pred = structure_pred.view(1, 1)
individual_preds = torch.cat([gnn_pred, morgan_pred, structure_pred], dim=1) # Shape: (1, 3)
meta_pred = self.meta_classifier(individual_preds)
return meta_pred
def load_models(self):
# Load models with compatibility for older numpy versions
try:
# Load GIN model
self.gnn_model.load_state_dict(torch.load("GNNModels/models/best_hiv_gin_model.pth", weights_only=False))
# Load Morgan MLP model (check if it's a full checkpoint or just state_dict)
morgan_checkpoint = torch.load("MorganFingerprintMLP/models/best_mlp_model_standard.pth", weights_only=False)
if 'model_state_dict' in morgan_checkpoint:
self.morgan_mlp.load_state_dict(morgan_checkpoint['model_state_dict'])
else:
self.morgan_mlp.load_state_dict(morgan_checkpoint)
# Load Deep MLP model
self.deep_mlp.load_state_dict(torch.load("StructuralModels/models/best_mlp_model.pth", weights_only=False))
except Exception as e:
print(f"Error loading models: {e}")
# Print what's actually in the checkpoint for debugging
if "MorganFingerprintMLP" in str(e):
morgan_checkpoint = torch.load("MorganFingerprintMLP/models/best_mlp_model_standard.pth", weights_only=False)
print(f"Morgan checkpoint keys: {list(morgan_checkpoint.keys())}")
if 'model_state_dict' in morgan_checkpoint:
print(f"Model state dict keys: {list(morgan_checkpoint['model_state_dict'].keys())[:5]}...")
raise e
def save_models(self):
torch.save(self.gnn_model.state_dict(), "GNNModels/gnn_model.pth")
torch.save(self.morgan_mlp.state_dict(), "MorganFingerprintMLP/morgan_mlp.pth")
torch.save(self.deep_mlp.state_dict(), "StructuralModels/structure_mlp.pth")
torch.save(self.meta_classifier.state_dict(), "Complete/meta_classifier.pth")
def main():
smiles = "C1CC1C#C[C@]2(C3=C(C=CC(=C3)Cl)NC(=O)O2)C(F)(F)F"
# Get dataloader
loader_config = Config("./GNNModels/config.yaml")
dataset, train_dataset, val_dataset, test_dataset = load_dataset(loader_config)
# Model
model = StackedModel(
dataset=dataset
)
model.load_models()
# Train Meta
model.train_meta(
epochs=10,
optimizer=torch.optim.Adam(model.parameters(), lr=0.001),
train_dataset=train_dataset
)
# Prediction
model.eval()
pred = model.predict(smiles)
print(pred)
model.save_models()
if __name__ == "__main__":
main()