-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_sample_test_EXAMPLE.py
150 lines (91 loc) · 3.66 KB
/
random_sample_test_EXAMPLE.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 19:53:18 2023
@author: chingchinglam
"""
from datetime import date
import random
import sys
import os
import pandas as pd
sys.path.insert(1, os.getcwd()+'/scripts/')
import model_training_v3 as mt
today = date.today()
date_str=today.strftime("%d%m%Y")
this_python_script_name= sys.argv[0].split('/')[-1][:-3]
#test_df = test_df1[test_df1['code']<100]
def Onepot(training_df, test_df, model ='RF', descriptor_arr = '2-bond+'):
training_df['type'] = 'train'
test_df['type'] = 'test'
len_training_code = max(training_df['code'].tolist())
len_training_idx = max(training_df['idx'].tolist())
test_code_ls = [i+len_training_code+1 for i in test_df['code'].tolist()]
test_idx_ls = [i+len_training_idx+1 for i in test_df['idx'].tolist()]
test_df2=test_df[['type','reaction']].copy()
test_df2['code'] = test_code_ls
test_df2['idx'] = test_idx_ls
ele_steps=pd.concat([training_df,test_df2])
ele_steps.to_csv('ele_steps.csv')
test=mt.run_ML(ele_steps)
test.generate_data(method='m3',descriptor_arr =descriptor_arr)
print('done: test.generate_data()')
#test.status_df.to_csv('status.csv')
training_code_ls=[test.rev_pass_idx_dict.get(i) for i in ele_steps[ele_steps['type']=='train']['code'].unique().tolist()
if i in test.pass_idx_ls ]
test_code_ls=[test.rev_pass_idx_dict.get(i) for i in ele_steps[ele_steps['type']=='test']['code'].unique().tolist()
if i in test.pass_idx_ls ]
how_ls=[training_code_ls,test_code_ls]
test.split_data(by='rxn',incl='r',test_size=0.2, how=how_ls)
#print('idx: ', str(looping_ls[idx][1]))
test.training(model=model)
test.evaluation()
original_code_ls = [i-len_training_code-1 for i in test.label_df['rxn_idx'].tolist()]
#original_idx_ls = [i-len_training_idx-1 for i in test.label_df['idx'].tolist()]
label_df2=test.label_df[['pred_label','actual_label']].copy()
label_df2['rxn_idx']=original_code_ls
label_df2['idx']=original_code_ls
return label_df2
def parah_code(given_list):
first_value=given_list[0]
first_code=0
new_code_ls=[0]
for v in given_list[1:]:
if v != first_value:
first_value=v
first_code += 1
new_code_ls.append(first_code)
else:
new_code_ls.append(first_code)
return new_code_ls
def give_random_sample(filename):
data = pd.read_csv(filename)
random_numbers = []
while len(random_numbers) < 200:
va=random.randint(0, data['code'].max())
if va not in random_numbers:
random_numbers.append(va)
random_code_ls=sorted(random_numbers)
#print(len(random_code_ls))
sample_df=data.loc[data['code'].isin(random_code_ls)].copy()
sample_df['code'] = parah_code(sample_df['code'].tolist())
sample_df['idx'] = [i for i in range(0,len(sample_df))]
return sample_df
##########
filename = './dataset/cyclo_data_v2_16072024.csv'
model_ls = []
test_df_ls=[]
train_df_ls=[]
for i in range(0,10):
model_ls.append('_'+str(i))
sample_df=give_random_sample(filename)
test_df = sample_df[sample_df['code']<100]
training_df = sample_df[(sample_df['code']>=100)]
test_df_ls.append(test_df)
train_df_ls.append(training_df)
keyword = 'cyclo_2b+'
label_df_ls=[]
for test,train,mod in zip(test_df_ls,train_df_ls,model_ls):
result_df=Onepot(train,test, model = 'RF',descriptor_arr = '2-bond+')
result_df.to_csv(keyword +mod+'_'+date_str+'.csv')
label_df_ls.append(result_df)