-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathICL_test_reverse_cls.py
170 lines (116 loc) · 7.77 KB
/
ICL_test_reverse_cls.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
158
159
160
161
162
163
164
165
166
167
168
169
from rdkit import Chem
from rdkit import Chem
import numpy as np
import tqdm
import datetime
import pyarrow as pa
import pandas as pd
from fastparquet import write
import os
import pyarrow.parquet as pq
from dataset import FingerprintedDataset, shape_maccs
from pathlib import Path
def create_prompt(results_smiles, results_label):
prompt=''
for i in range(len(results_smiles)-1,-1,-1):
# classifaction tasks
if results_label[i] == str(1.0):
prompt += f"SMILES: {results_smiles[i]}\nlabel: Yes\n"
elif results_label[i] == str(0.0):
prompt += f"SMILES: {results_smiles[i]}\nlabel: No\n"
else:
print(f'label is not 1.0 or 0.0')
# regression tasks
# prompt += f"SMILES: {results_smiles[i]}\nlabel: {results_label[i]}\n"
return prompt
datasets = [
'bace',
'bbbp',
'cyp450',
'hiv',
'muv',
'tox21',
'toxcast',
]
#regression tasks:
# datasets = ['esol','freesolv','lipo']
for dataset in datasets:
path = './test_process'
path = os.path.join(path, dataset)
for file in os.listdir(path):
if file.startswith("test") and not file.endswith("_add.parquet"):
num = file.split('_')[-1].split('.')[0]
table_path = os.path.join(path, file)
data_path = os.path.join(path, num)
data = FingerprintedDataset.open(data_path, shape=shape_maccs)
table = pq.read_table(table_path)
print(f'table: {len(table)}')
maccs = table['maccs']
smiles = table['smiles']
label = table['label']
ins = pd.read_json(os.path.join('./test_dataset/0-shot/', dataset, num+'.json'), lines=True)
text = ins['instruction']
data_1shot = []
data_2shot = []
data_3shot = []
data_4shot = []
data_5shot = []
data_6shot = []
data_7shot = []
data_8shot = []
i=0
for macc in maccs:
macc = list(macc.as_py())
results =data.search(macc, 8)
index = [r[0] for r in results]
results_keys = [r[0] for r in results]
results_smiles = [r[1] for r in results]
results_scores = [r[2] for r in results]
results_label = [r[3] for r in results]
sentences = str(text[i]).split(". ")
front_sentences = ". ".join(sentences[:-1])
last_sentence = sentences[-1]
# for property dataset, its instruction will have one sentence
if len(front_sentences)==0:
# for test dataset
data_2shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles[:2], results_label[:2]) + last_sentence, str(smiles[i]), str(label[i])])
data_3shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles[:3], results_label[:3]) + last_sentence, str(smiles[i]), str(label[i])])
data_4shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles[:4], results_label[:4]) + last_sentence, str(smiles[i]), str(label[i])])
# data_5shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles[:5], results_label[:5]) + last_sentence, smiles[i], label[i]])
data_6shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles[:6], results_label[:6]) + last_sentence, str(smiles[i]), str(label[i])])
# data_7shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles[:7], results_label[:7]) + last_sentence, smiles[i], label[i]])
data_8shot.append([f'Here are some examples about molecular property.\n' + create_prompt(results_smiles, results_label) + last_sentence, str(smiles[i]), str(label[i])])
else:
# for test dataset
data_2shot.append([front_sentences +'. Here are some examples.\n' + create_prompt(results_smiles[:2], results_label[:2]) + last_sentence, str(smiles[i]), str(label[i])])
data_3shot.append([front_sentences +'. Here are some examples.\n' + create_prompt(results_smiles[:3], results_label[:3]) + last_sentence, str(smiles[i]), str(label[i])])
data_4shot.append([front_sentences +'. Here are some examples.\n' + create_prompt(results_smiles[:4], results_label[:4]) + last_sentence, str(smiles[i]), str(label[i])])
data_6shot.append([front_sentences +'. Here are some examples.\n' + create_prompt(results_smiles[:6], results_label[:6]) + last_sentence, str(smiles[i]), str(label[i])])
data_8shot.append([front_sentences +'. Here are some examples.\n' + create_prompt(results_smiles, results_label) + last_sentence, str(smiles[i]), str(label[i])]) #float
i = i+1
new_columns_2shot = pd.DataFrame(data_2shot, columns=['instruction', 'input', 'output'])
new_columns_3shot = pd.DataFrame(data_3shot, columns=['instruction', 'input', 'output'])
new_columns_4shot = pd.DataFrame(data_4shot, columns=['instruction', 'input', 'output'])
new_columns_6shot = pd.DataFrame(data_6shot, columns=['instruction', 'input', 'output'])
new_columns_8shot = pd.DataFrame(data_8shot, columns=['instruction', 'input', 'output'])
new_path = './test_dataset/2-shot-reverse'
os.makedirs(os.path.join(new_path, dataset), exist_ok=True)
new_columns_2shot.to_json(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'), orient='records', lines=True)
print(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'))
new_path = './test_dataset/3-shot-reverse'
os.makedirs(os.path.join(new_path, dataset), exist_ok=True)
new_columns_3shot.to_json(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'), orient='records', lines=True)
print(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'))
new_path = './test_dataset/4-shot-reverse'
os.makedirs(os.path.join(new_path, dataset), exist_ok=True)
new_columns_4shot.to_json(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'), orient='records', lines=True)
print(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'))
new_path = './test_dataset/6-shot/-reverse'
os.makedirs(os.path.join(new_path, dataset), exist_ok=True)
new_columns_6shot.to_json(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'), orient='records', lines=True)
print(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'))
new_path = './test_dataset/8-shot/-reverse'
os.makedirs(os.path.join(new_path, dataset), exist_ok=True)
new_columns_8shot.to_json(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'), orient='records', lines=True)
print(os.path.join(new_path, dataset, file.split('_')[1] + '_' + num + '.json'))
print('saved !')