-
Notifications
You must be signed in to change notification settings - Fork 1
/
pretrain_multiprocessing.py
157 lines (130 loc) · 5.29 KB
/
pretrain_multiprocessing.py
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
import multiprocessing
from pickle import dump
from math import ceil
from dataset_load import *
from utils.compound_tools import *
len_dict = {
"bace": 194,
"bbbp": 234,
"hiv": 495,
"clintox": 254,
"tox21": 267,
"sider": 982,
"toxcast": 240,
"muv": 84,
"iron": 211,
}
def run_geo_for_smiles(start, end, mol_list):
cur_mol_list = mol_list[start: end]
raw_data_list = []
for mol in cur_mol_list:
raw_data = mol_to_geognn_graph_data_MMFF3d(mol)
raw_data_list.append(raw_data)
return raw_data_list
def run_seq_for_smiles(start, end, smiles_list, max_len):
cur_smiles_list = smiles_list[start: end]
return CompoundSeqKit.encode_multi_smiles(cur_smiles_list, max_len)
def preprocess_graph_feat(input_path,
seed=42,
process_num=20,
dataset="bace",
task="graph"):
cur_path = os.getcwd()
if dataset == "bace":
trn_df, val_df, test_df = load_bace_dataset(
input_path, seed, random=False)
elif dataset == "bbbp":
trn_df, val_df, test_df = load_bbbp_dataset(
input_path, seed, random=False)
elif dataset == "hiv":
trn_df, val_df, test_df = load_hiv_dataset(
input_path, seed, random=False)
elif dataset == "clintox":
trn_df, val_df, test_df = load_clintox_dataset(input_path, seed)
elif dataset == "tox21":
trn_df, val_df, test_df = load_tox21_dataset(input_path, seed)
elif dataset == "sider":
trn_df, val_df, test_df = load_sider_dataset(input_path, seed)
elif dataset == "toxcast":
trn_df, val_df, test_df = load_toxcast_dataset(input_path, seed)
elif dataset == "muv":
trn_df, val_df, test_df = load_muv_dataset(input_path, seed)
elif dataset == "iron":
trn_df, val_df, test_df = load_iron_dataset(input_path, seed)
else:
raise ValueError("Unsupported Dataset!")
print("dataset loaded!")
path_prefix_name = ["train", "valid", "test"]
cur_path = os.getcwd()
saved_graph_feat_path = os.path.join(
cur_path, "molattrs/saved_graph_feat/{}".format(dataset))
saved_seq_feat_path = os.path.join(
cur_path, "molattrs/saved_seq_feat/{}".format(dataset))
check_save_pre_path = [saved_graph_feat_path, saved_seq_feat_path]
for path in check_save_pre_path:
if not os.path.exists(path):
os.mkdir(path)
for name in path_prefix_name:
cur_graph_feat_path = os.path.join(
saved_graph_feat_path, "{}".format(name))
cur_seq_feat_path = os.path.join(
saved_seq_feat_path, "{}".format(name))
cur_path_check = [cur_graph_feat_path, cur_seq_feat_path]
for path in cur_path_check:
if not os.path.exists(path):
os.mkdir(path)
for i, dataframe in enumerate([trn_df, val_df, test_df]):
smiles_list = list(dataframe["smiles"])
smiles_len = len(smiles_list)
print("{} :{}".format(path_prefix_name[i], smiles_len))
rdkit_mol_list = [AllChem.MolFromSmiles(
smiles) for smiles in smiles_list]
block_num = ceil(smiles_len / process_num)
res = []
pool = multiprocessing.Pool(processes=process_num)
for j in tqdm(range(block_num)):
start_index = j * process_num
if j != block_num - 1:
end_index = (j + 1) * process_num
else:
end_index = smiles_len
print(start_index, end_index)
# Here is for different tasks
if task == "graph":
res.append(pool.apply_async(
run_geo_for_smiles, args=(start_index, end_index, rdkit_mol_list)))
if task == "seq":
max_len = len_dict.get(dataset, 100)
res.append(pool.apply_async(
run_seq_for_smiles, args=(start_index, end_index, smiles_list, max_len)))
pool.close()
pool.join()
print("All Subprocesses Have Been Done!")
processed_data_list = []
for r in res:
processed_data_list += r.get()
cur_prefix_name = os.path.join(dataset, path_prefix_name[i])
save_path = None
if task == "graph":
save_path = os.path.join(
cur_path,
"molattrs/saved_graph_feat", cur_prefix_name, "graph_feat_{}.pt".format(seed))
if task == "seq":
save_path = os.path.join(
cur_path,
"molattrs/saved_seq_feat", cur_prefix_name, "seq_feat_{}.pt".format(seed))
with open(save_path, "wb") as f:
dump(processed_data_list, f)
print(save_path, "Have been saved!")
def main_process(input_path: str, process_number: int, seed_num: int, dataset: str):
preprocess_graph_feat(input_path,
task="graph",
dataset=dataset,
process_num=process_number,
seed=seed_num)
preprocess_graph_feat(input_path,
task="seq",
dataset=dataset,
process_num=process_number,
seed=seed_num)
print("#" * 100, "multiprocess preprocessing has done!", "#" * 100, sep="\n")