@@ -63,35 +63,12 @@ def model_pretrained(path_dir = None, model = None):
6363 model .load_pretrained (path_dir + '/model.pt' )
6464 return model
6565
66- # def mpnn_feature_collate_func(x):
67- # ## first version
68- # return [torch.cat([x[j][i] for j in range(len(x))], 0) for i in range(len(x[0]))]
69-
70- # def mpnn_feature_collate_func(x):
71- # assert len(x[0]) == 5
72- # N_atoms_N_bonds = [i[-1] for i in x]
73- # N_atoms_scope = []
74- # f_a = torch.cat([x[j][0] for j in range(len(x))], 0)
75- # f_b = torch.cat([x[j][1] for j in range(len(x))], 0)
76- # agraph_lst, bgraph_lst = [], []
77- # Na, Nb = 0, 0
78- # for j in range(len(x)):
79- # agraph_lst.append(x[j][2] + Na)
80- # bgraph_lst.append(x[j][3] + Nb)
81- # N_atoms_scope.append([Na, x[j][2].shape[0]])
82- # Na += x[j][2].shape[0]
83- # Nb += x[j][3].shape[0]
84- # agraph = torch.cat(agraph_lst, 0)
85- # bgraph = torch.cat(bgraph_lst, 0)
86- # return [f_a, f_b, agraph, bgraph, N_atoms_scope]
87-
88- # def mpnn_collate_func(x):
89- # mpnn_feature = [i[0] for i in x]
90- # mpnn_feature = mpnn_feature_collate_func(mpnn_feature)
91- # from torch.utils.data.dataloader import default_collate
92- # x_remain = [[i[1], i[2]] for i in x]
93- # x_remain_collated = default_collate(x_remain)
94- # return [mpnn_feature] + x_remain_collated
66+ def dgl_collate_func (x ):
67+ d1 , d2 , y = zip (* x )
68+ import dgl
69+ d1 = dgl .batch (d1 )
70+ d2 = dgl .batch (d2 )
71+ return d1 , d2 , torch .tensor (y )
9572
9673class DDI_Model :
9774
@@ -110,6 +87,31 @@ def __init__(self, **config):
11087 self .model_drug = transformer ('drug' , ** config )
11188 elif drug_encoding == 'MPNN' :
11289 self .model_drug = MPNN (config ['hidden_dim_drug' ], config ['mpnn_depth' ])
90+ elif drug_encoding == 'DGL_GCN' :
91+ self .model_drug = DGL_GCN (in_feats = 74 ,
92+ hidden_feats = [config ['gnn_hid_dim_drug' ]] * config ['gnn_num_layers' ],
93+ activation = [config ['gnn_activation' ]] * config ['gnn_num_layers' ],
94+ predictor_dim = config ['hidden_dim_drug' ])
95+ elif drug_encoding == 'DGL_NeuralFP' :
96+ self .model_drug = DGL_NeuralFP (in_feats = 74 ,
97+ hidden_feats = [config ['gnn_hid_dim_drug' ]] * config ['gnn_num_layers' ],
98+ max_degree = config ['neuralfp_max_degree' ],
99+ activation = [config ['gnn_activation' ]] * config ['gnn_num_layers' ],
100+ predictor_hidden_size = config ['neuralfp_predictor_hid_dim' ],
101+ predictor_dim = config ['hidden_dim_drug' ],
102+ predictor_activation = config ['neuralfp_predictor_activation' ])
103+ elif drug_encoding == 'DGL_GIN_AttrMasking' :
104+ self .model_drug = DGL_GIN_AttrMasking (predictor_dim = config ['hidden_dim_drug' ])
105+ elif drug_encoding == 'DGL_GIN_ContextPred' :
106+ self .model_drug = DGL_GIN_ContextPred (predictor_dim = config ['hidden_dim_drug' ])
107+ elif drug_encoding == 'DGL_AttentiveFP' :
108+ self .model_drug = DGL_AttentiveFP (node_feat_size = 39 ,
109+ edge_feat_size = 11 ,
110+ num_layers = config ['gnn_num_layers' ],
111+ num_timesteps = config ['attentivefp_num_timesteps' ],
112+ graph_feat_size = config ['gnn_hid_dim_drug' ],
113+ predictor_dim = config ['hidden_dim_drug' ])
114+
113115 else :
114116 raise AttributeError ('Please use one of the available encoding method.' )
115117
@@ -132,7 +134,7 @@ def test_(self, data_generator, model, repurposing_mode = False, test = False):
132134 y_label = []
133135 model .eval ()
134136 for i , (v_d , v_p , label ) in enumerate (data_generator ):
135- if self .drug_encoding == "MPNN" or self . drug_encoding == 'Transformer' :
137+ if self .drug_encoding in [ "MPNN" , 'Transformer' , 'DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'DGL_AttentiveFP' ] :
136138 v_d = v_d
137139 v_p = v_p
138140 else :
@@ -206,6 +208,8 @@ def train(self, train, val, test = None, verbose = True):
206208 'drop_last' : False }
207209 if (self .drug_encoding == "MPNN" ):
208210 params ['collate_fn' ] = mpnn_collate_func
211+ elif self .drug_encoding in ['DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'DGL_AttentiveFP' ]:
212+ params ['collate_fn' ] = dgl_collate_func
209213
210214 training_generator = data .DataLoader (data_process_DDI_loader (train .index .values , train .Label .values , train , ** self .config ), ** params )
211215 validation_generator = data .DataLoader (data_process_DDI_loader (val .index .values , val .Label .values , val , ** self .config ), ** params )
@@ -220,6 +224,8 @@ def train(self, train, val, test = None, verbose = True):
220224
221225 if (self .drug_encoding == "MPNN" ):
222226 params_test ['collate_fn' ] = mpnn_collate_func
227+ elif self .drug_encoding in ['DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'DGL_AttentiveFP' ]:
228+ params_test ['collate_fn' ] = dgl_collate_func
223229 testing_generator = data .DataLoader (data_process_DDI_loader (test .index .values , test .Label .values , test , ** self .config ), ** params_test )
224230
225231 # early stopping
@@ -242,7 +248,7 @@ def train(self, train, val, test = None, verbose = True):
242248 t_start = time ()
243249 for epo in range (train_epoch ):
244250 for i , (v_d , v_p , label ) in enumerate (training_generator ):
245- if self .drug_encoding == "MPNN" or self . drug_encoding == 'Transformer' :
251+ if self .drug_encoding in [ "MPNN" , 'Transformer' , 'DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'DGL_AttentiveFP' ] :
246252 v_d = v_d
247253 v_p = v_p
248254 else :
@@ -372,7 +378,8 @@ def predict(self, df_data):
372378
373379 if (self .drug_encoding == "MPNN" ):
374380 params ['collate_fn' ] = mpnn_collate_func
375-
381+ elif self .drug_encoding in ['DGL_GCN' , 'DGL_GAT' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'DGL_AttentiveFP' ]:
382+ params_test ['collate_fn' ] = dgl_collate_func
376383
377384 generator = data .DataLoader (info , ** params )
378385
0 commit comments