-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterate_fragmentation.py
More file actions
350 lines (309 loc) · 19.7 KB
/
iterate_fragmentation.py
File metadata and controls
350 lines (309 loc) · 19.7 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import os
import json
import pickle
import multiprocessing as mp
from metrics import selfies_to_smiles, is_substructure
from query import query_model, query_model_feedback, mp_query
def iterative_feedback_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name, temperature):
queries = []
feedback_queries = []
for data in dataset:
if os.path.isfile(
save_path + 'raw/json/' + previous_task + '/' + data[
'identifier'] + '_' + previous_task + '_model_response.json'):
with open(save_path + 'raw/json/' + previous_task + '/' + data[
'identifier'] + '_' + previous_task + '_model_response.json',
'r') as file:
response = json.load(file)
# process response
gen_frags = []
if 'fragments' in response.keys():
for frag in response['fragments']:
gen_frags.append(frag['frag'])
true_substructure = []
for frag in gen_frags:
if is_substructure(frag, data['smiles']):
true_substructure.append(frag)
not_true_substructure = len(gen_frags) - len(true_substructure)
new_frags_str = []
for frag in true_substructure:
new_frags_str.append({"frag": frag})
##########################
# feedback query
temp = {
"system": "You are a chemistry model specialized in mass spectrometry. In this task, give feedback on your previous prediction of the most likely mass spectrometry fragments based on molecular structure and fragmentation patterns.",
"user": "<<TASK=IterativeFragmentListPrediction>> You predicted all major fragments (ordered by descending intensity) in SELFIES format based on this information:\n<<MOL>> [[SELFIES]]\n<<EXP_SETTINGS>> [[EXP_SETTINGS]]\n\nYour response was: [[GENERATED_FRAGMENTS]]\n\nPlease give feedback on this response.",
# "assistant": "[[FRAGMENTS]]"
}
user_prompt = temp["user"].replace("[[GENERATED_FRAGMENTS]]", str(response)).replace("[[SELFIES]]", data["selfies"]).replace(
"[[EXP_SETTINGS]]",
json.dumps(data["exp_settings"],
indent=None).replace(
'"', ''))
data_point = {"messages": [{"role": "system", "content": temp["system"]},
{"role": "user", "content": user_prompt}],
"identifier": data['identifier'],
"frags": data['sorted_frags']}
feedback_queries.append(data_point)
if not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_feedback_model_response.txt') and not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + type_of_task + '_feedback_model_response.txt'):
# submit and save query
feedback_response = query_model_feedback(client, model_name, data_point, temperature)
if feedback_response == {}: # if nothing was returned
response_dict = {}
else:
response_dict = feedback_response.model_dump()
with open(save_path + 'raw/pkl/' + new_task + '/' + data[
'identifier'] + '_' + new_task + 'feedback_model_response.pkl',
'wb') as file:
pickle.dump(response_dict, file)
with open(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_feedback_model_response.txt', "w", encoding="utf-8") as file:
file.write(feedback_response.choices[0].message.content)
feedback_response_str = feedback_response.choices[0].message.content
else:
with open(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_feedback_model_response.txt', "r", encoding="utf-8") as file:
feedback_response_str = file.read()
##########################
# create new query
temp = {
"system": "You are a chemistry model specialized in mass spectrometry. In this task, predict the most likely mass spectrometry fragments based on molecular structure and fragmentation patterns.",
"user": "<<TASK=IterativeFragmentListPrediction>> You predicted all major fragments (ordered by descending intensity) in SELFIES format based on this information:\n<<MOL>> [[SELFIES]]\n<<EXP_SETTINGS>> [[EXP_SETTINGS]]\n\nYour response was: [[GENERATED_FRAGMENTS]]\n\nYour feedback was: [[FEEDBACK]]\n\n[[INVALID_SUBSTRUCTURES]] of the predicted fragments were invalid substructures, and the remaining are possible: <<FRAGMENTS>> [[PREVIOUS FRAGMENTS]]. Respond with the final list of fragments of the molecule (ordered by descending intensity) in SELFIES format produced by these experiment settings.",
"assistant": "[[FRAGMENTS]]"
}
user_prompt = temp["user"].replace("[[GENERATED_FRAGMENTS]]", str(response)).replace("[[FEEDBACK]]",str(feedback_response_str)).replace("[[INVALID_SUBSTRUCTURES]]", str(not_true_substructure)).replace(
"[[PREVIOUS FRAGMENTS]]", str(new_frags_str)).replace("[[SELFIES]]", data["selfies"]).replace(
"[[EXP_SETTINGS]]",
json.dumps(data["exp_settings"],
indent=None).replace(
'"', ''))
assistant_prompt = temp["assistant"].replace("[[FRAGMENTS]]",
json.dumps(data["frags_str"], indent=None).replace('"', ''))
data_point = {"messages": [{"role": "system", "content": temp["system"]},
{"role": "user", "content": user_prompt},
{"role": "assistant", "content": assistant_prompt}],
"identifier": data['identifier'],
"frags": data['sorted_frags']}
queries.append(data_point)
if not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.json') and not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + type_of_task + '_model_response.json'):
# submit and save query
response = query_model(client, model_name, type_of_task, data_point, temperature)
if response == {}: # if nothing was returned
response_dict = {}
else:
response_dict = response.model_dump()
with open(save_path + 'raw/pkl/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.pkl',
'wb') as file:
pickle.dump(response_dict, file)
try:
answer = json.loads(response.choices[0].message.content)
except:
try:
answer = json.loads(response.choices[0].message.content + '}')
except:
try:
answer = json.loads(response.choices[0].message.content + '"}')
except:
try:
answer = json.loads(response.choices[0].message.content + '"}]}')
except:
answer = {}
with open(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.json',
"w") as file:
json.dump(answer, file, indent=2)
if not os.path.isdir(save_path+'iterative_prompts/'):
os.mkdir(save_path+'iterative_prompts/')
with open(save_path+'iterative_prompts/test_queries_' + new_task + '.jsonl', "w") as f:
for pt in queries:
f.write(json.dumps(pt) + "\n")
with open(save_path+'iterative_prompts/test_queries_' + new_task + '_feedback.jsonl', "w") as f:
for pt in feedback_queries:
f.write(json.dumps(pt) + "\n")
return
def iterative_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name, temperature):
queries = []
for data in dataset:
if os.path.isfile(
save_path + 'raw/json/' + previous_task + '/' + data[
'identifier'] + '_' + previous_task + '_model_response.json'):
with open(save_path + 'raw/json/' + previous_task + '/' + data[
'identifier'] + '_' + previous_task + '_model_response.json',
'r') as file:
response = json.load(file)
# process response
gen_frags = []
if 'fragments' in response.keys():
for frag in response['fragments']:
gen_frags.append(frag['frag'])
# # convert to smiles
# smi_gen_frags = []
# for frag in gen_frags:
# smi_gen_frags.append(selfies_to_smiles(frag))
true_substructure = []
for frag in gen_frags:
if is_substructure(frag, data['smiles']):
true_substructure.append(frag)
not_true_substructure = len(gen_frags) - len(true_substructure)
new_frags_str = []
for frag in true_substructure:
new_frags_str.append({"frag": frag})
# create new query
temp = {
"system": "You are a chemistry model specialized in mass spectrometry. In this task, predict the most likely mass spectrometry fragments based on molecular structure and fragmentation patterns.",
"user": "<<TASK=IterativeFragmentListPrediction>> You predicted all major fragments (ordered by descending intensity) in SELFIES format based on this information:\n<<MOL>> [[SELFIES]]\n<<EXP_SETTINGS>> [[EXP_SETTINGS]]\n\n [[INVALID_SUBSTRUCTURES]] of the predicted fragments were invalid substructures, and the remaining are possible: <<FRAGMENTS>> [[PREVIOUS FRAGMENTS]]. Respond with the final list of fragments of the molecule (ordered by descending intensity) in SELFIES format produced by these experiment settings.",
"assistant": "[[FRAGMENTS]]"
}
user_prompt = temp["user"].replace("[[INVALID_SUBSTRUCTURES]]", str(not_true_substructure)).replace(
"[[PREVIOUS FRAGMENTS]]", str(new_frags_str)).replace("[[SELFIES]]", data["selfies"]).replace(
"[[EXP_SETTINGS]]",
json.dumps(data["exp_settings"],
indent=None).replace(
'"', ''))
assistant_prompt = temp["assistant"].replace("[[FRAGMENTS]]",
json.dumps(data["frags_str"], indent=None).replace('"', ''))
data_point = {"messages": [{"role": "system", "content": temp["system"]},
{"role": "user", "content": user_prompt},
{"role": "assistant", "content": assistant_prompt}],
"identifier": data['identifier'],
"frags": data['sorted_frags']}
queries.append(data_point)
if not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.json') and not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + type_of_task + '_model_response.json'):
# submit and save query
response = query_model(client, model_name, type_of_task, data_point, temperature)
if response == {}: # if nothing was returned
response_dict = {}
else:
response_dict = response.model_dump()
with open(save_path + 'raw/pkl/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.pkl',
'wb') as file:
pickle.dump(response_dict, file)
try:
answer = json.loads(response.choices[0].message.content)
except:
try:
answer = json.loads(response.choices[0].message.content + '}')
except:
try:
answer = json.loads(response.choices[0].message.content + '"}')
except:
try:
answer = json.loads(response.choices[0].message.content + '"}]}')
except:
answer = {}
with open(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.json',
"w") as file:
json.dump(answer, file, indent=2)
if not os.path.isdir(save_path+'iterative_prompts/'):
os.mkdir(save_path+'iterative_prompts/')
with open(save_path+'iterative_prompts/test_queries_' + new_task + '.jsonl', "w") as f:
for pt in queries:
f.write(json.dumps(pt) + "\n")
return
def mp_iterative_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name, temperature, max_processes, api_key):
queries = []
count = 0
processes = []
while count < len(dataset):
# for data in dataset:
while len(processes) < max_processes:
if count < len(dataset):
data = dataset[count]
if os.path.isfile(
save_path + 'raw/json/' + previous_task + '/' + data[
'identifier'] + '_' + previous_task + '_model_response.json'):
with open(save_path + 'raw/json/' + previous_task + '/' + data[
'identifier'] + '_' + previous_task + '_model_response.json',
'r') as file:
response = json.load(file)
# process response
gen_frags = []
if 'fragments' in response.keys():
for frag in response['fragments']:
gen_frags.append(frag['frag'])
true_substructure = []
for frag in gen_frags:
if is_substructure(frag, data['smiles']):
true_substructure.append(frag)
not_true_substructure = len(gen_frags) - len(true_substructure)
new_frags_str = []
for frag in true_substructure:
new_frags_str.append({"frag": frag})
# create new query
temp = {
"system": "You are a chemistry model specialized in mass spectrometry. In this task, predict the most likely mass spectrometry fragments based on molecular structure and fragmentation patterns.",
"user": "<<TASK=IterativeFragmentListPrediction>> You predicted all major fragments (ordered by descending intensity) in SELFIES format based on this information:\n<<MOL>> [[SELFIES]]\n<<EXP_SETTINGS>> [[EXP_SETTINGS]]\n\n [[INVALID_SUBSTRUCTURES]] of the predicted fragments were invalid substructures, and the remaining are possible: <<FRAGMENTS>> [[PREVIOUS FRAGMENTS]]. Respond with the final list of fragments of the molecule (ordered by descending intensity) in SELFIES format produced by these experiment settings.",
"assistant": "[[FRAGMENTS]]"
}
user_prompt = temp["user"].replace("[[INVALID_SUBSTRUCTURES]]", str(not_true_substructure)).replace(
"[[PREVIOUS FRAGMENTS]]", str(new_frags_str)).replace("[[SELFIES]]", data["selfies"]).replace(
"[[EXP_SETTINGS]]",
json.dumps(data["exp_settings"],
indent=None).replace(
'"', ''))
assistant_prompt = temp["assistant"].replace("[[FRAGMENTS]]",
json.dumps(data["frags_str"], indent=None).replace('"',
''))
data_point = {"messages": [{"role": "system", "content": temp["system"]},
{"role": "user", "content": user_prompt},
{"role": "assistant", "content": assistant_prompt}],
"identifier": data['identifier'],
"frags": data['sorted_frags']}
queries.append(data_point)
if not os.path.exists(save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + new_task + '_model_response.json') and not os.path.exists(
save_path + 'raw/json/' + new_task + '/' + data[
'identifier'] + '_' + type_of_task + '_model_response.json'):
# response = query_model(client, model_name, type_of_task, data_point, temperature)
p = mp.Process(target=mp_query, args=[api_key, model_name, type_of_task, data_point, save_path, temperature, new_task])
p.start()
processes.append(p)
count += 1
else:
break
for p in processes:
p.join()
for p in processes:
p.terminate()
processes = []
if not os.path.isdir(save_path+'iterative_prompts/'):
os.mkdir(save_path+'iterative_prompts/')
with open(save_path+'iterative_prompts/test_queries_' + new_task + '.jsonl', "w") as f:
for pt in queries:
f.write(json.dumps(pt) + "\n")
return
def iterate_fragmentation(dataset, save_path, data_path, client, model_name, n=4, max_processes=None, api_key=None):
# previous_task = 'FragmentListPrediction'
# new_task = 'IterativeFragmentListPrediction'
type_of_task = 'IterativeFragmentListPrediction'
# iterative_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name)
temperature = 0.9
for i in range(1,n+1):
if i==4:
temperature = 0.1
if i==1:
previous_task = 'FragmentListPrediction'
else:
previous_task = 'IterativeFragmentListPrediction' + str(i - 1)
# new_task = 'IterativeFragmentListPrediction' + str(i)
new_task = 'IterativeFragmentListPrediction' + str(i)
if not os.path.isdir(save_path + 'raw/json/' + new_task):
os.mkdir(save_path + 'raw/json/' + new_task)
if not os.path.isdir(save_path + 'raw/pkl/' + new_task):
os.mkdir(save_path + 'raw/pkl/' + new_task)
if max_processes!=None:
mp_iterative_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name,
temperature, max_processes, api_key)
else:
iterative_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name, temperature)
# iterative_feedback_run(previous_task, new_task, type_of_task, dataset, save_path, data_path, client, model_name, temperature)
return