You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. import os
  5. import sys
  6. import pandas as pd
  7. PROJ_DIR = os.path.dirname(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..')))
  8. sys.path.insert(0, PROJ_DIR)
  9. from drug.models import GCN
  10. from drug.datasets import DDInteractionDataset
  11. from model.utils import get_FP_by_negative_index
  12. from const import Drug2FP_FILE
  13. class Connector(nn.Module):
  14. def __init__(self, gpu_id=None):
  15. self.gpu_id = gpu_id
  16. super(Connector, self).__init__()
  17. # self.ddiDataset = DDInteractionDataset(gpu_id = gpu_id)
  18. self.gcn = None
  19. self.drug2FP_df = pd.read_csv(Drug2FP_FILE)
  20. #Cell line features
  21. # np.load('cell_feat.npy')
  22. def forward(self, drug1_idx, drug2_idx, cell_feat, subgraph):
  23. if self.gcn == None:
  24. # print("here is for initializing the GCN. num_features: ", subgraph.num_features)
  25. self.gcn = GCN(subgraph.num_features, subgraph.num_features // 2)
  26. # print("this is subgraph in connector model forward: --------------")
  27. # print(subgraph)
  28. # graph.get().x --> DDInteractionDataset
  29. # subgraph = graph.get() --> Data
  30. x = subgraph.x
  31. edge_index = subgraph.edge_index
  32. x = self.gcn(x, edge_index)
  33. # print("node local indices:")
  34. node_indices = edge_index.flatten().unique()
  35. # print(node_indices)
  36. # print("-----------------------")
  37. # print("node global indices:")
  38. node_indices = subgraph.n_id
  39. if self.gpu_id is not None:
  40. node_indices = node_indices.cuda(self.gpu_id)
  41. # print(node_indices)
  42. drug1_idx = torch.flatten(drug1_idx)
  43. drug2_idx = torch.flatten(drug2_idx)
  44. #drug1_feat = x[drug1_idx]
  45. #drug2_feat = x[drug2_idx]
  46. drug1_feat = torch.empty((len(drug1_idx), len(x[0])))
  47. drug2_feat = torch.empty((len(drug2_idx), len(x[0])))
  48. for index, element in enumerate(drug1_idx):
  49. x_element = element
  50. if element >= 0:
  51. x_element = (node_indices == element).nonzero().squeeze()
  52. drug1_feat[index] = (x[x_element])
  53. for index, element in enumerate(drug2_idx):
  54. x_element = element
  55. if element >= 0:
  56. x_element = (node_indices == element).nonzero().squeeze()
  57. drug2_feat[index] = (x[x_element])
  58. if self.gpu_id is not None:
  59. drug1_feat = drug1_feat.cuda(self.gpu_id)
  60. drug2_feat = drug2_feat.cuda(self.gpu_id)
  61. for i, x in enumerate(drug1_idx):
  62. if x < 0:
  63. drug1_feat[i] = get_FP_by_negative_index(x,self.drug2FP_df)
  64. for i, x in enumerate(drug2_idx):
  65. if x < 0:
  66. drug2_feat[i] = get_FP_by_negative_index(x,self.drug2FP_df)
  67. feat = torch.cat([drug1_feat, drug2_feat, cell_feat], 1)
  68. return feat
  69. class MLP(nn.Module):
  70. def __init__(self, input_size: int, hidden_size: int, gpu_id=None):
  71. super(MLP, self).__init__()
  72. self.layers = nn.Sequential(
  73. nn.Linear(input_size, hidden_size),
  74. nn.ReLU(),
  75. nn.BatchNorm1d(hidden_size),
  76. nn.Linear(hidden_size, hidden_size // 2),
  77. nn.ReLU(),
  78. nn.BatchNorm1d(hidden_size // 2),
  79. nn.Linear(hidden_size // 2, 1)
  80. )
  81. self.connector = Connector(gpu_id)
  82. # prev input: self, drug1_feat: torch.Tensor, drug2_feat: torch.Tensor, cell_feat: torch.Tensor, subgraph: related subgraph for the batch
  83. def forward(self, drug1_idx, drug2_idx, cell_feat, subgraph):
  84. feat = self.connector(drug1_idx, drug2_idx, cell_feat, subgraph)
  85. out = self.layers(feat)
  86. return out
  87. # other PRODeepSyn models have been deleted for now