-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
1397 lines (1213 loc) · 47.8 KB
/
utils.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Any, Callable, Dict, Iterable, List, Tuple, Optional, Union
import pandas as pd
import numpy as np
import os
import dgl
import yaml
import random
import torch
import torch.nn as nn
from rdkit import Chem
from rdkit.Chem import AllChem, DataStructs
from mordred import Calculator, descriptors
import pubchempy as pcp
from huggingface_hub import snapshot_download
from transformers import AutoTokenizer, AutoModel, AutoModelForMaskedLM
from mol2vec.features import (
MolSentence,
DfVec,
sentences2vec,
mol2alt_sentence,
mol2sentence,
)
from gensim.models import word2vec
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dropout, Dense
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow import keras
from catboost import CatBoostRegressor
import xgboost as xgb
import lightgbm as lgb
import matplotlib.pyplot as plt
class PreprocessSMILES:
def __init__(self, directory, base_on="smiles", task="train"):
self.directory = directory
self.base_on_smiles = base_on
self.task = task
def load_data(self, filename):
return pd.read_csv(self.directory + filename)
def preprocess_data(
self,
filename: str,
filename_descriptors: str,
property_: str = "ad7e6027-00b8-4c27-918c-d1561f949ad8",
) -> pd.DataFrame:
"""
Load data from a csv file and process it.
Parameters
----------
filename : str
Path to a csv file with the data.
property_ : str, optional
Property name to filter the data by, by default 'ad7e6027-00b8-4c27-918c-d1561f949ad8'
Returns
-------
pd.DataFrame
Processed data as a pandas DataFrame.
"""
df = self.load_data(filename)
descriptors = self.load_data(filename_descriptors)
if "train" in filename and self.task == "train":
df = df[(df["oil_property_param_title"] == property_)]
df = df[df['smiles'].notna()]
df = (
df.groupby(["blend_id", "smiles"])
.agg({"oil_property_param_value": "mean"})
.reset_index()
)
df["mol"] = df["smiles"].apply(Chem.MolFromSmiles)
df["canonical_smiles"] = df["smiles"].apply(
lambda x: (
Chem.MolToSmiles(
Chem.MolFromSmiles(x), isomericSmiles=True, canonical=True
)
if x is not None
else None
)
)
rows_to_drop = df[df['mol'].isnull()].index
df = df.drop(df[df['blend_id'].isin(
df.loc[rows_to_drop, 'blend_id'])].index)
df["descriptors_array"] = (
pd.merge(df, descriptors, on="smiles", how="inner") # ! change
.iloc[:, 6:]
.values.tolist()
)
return df
def calculate_similarity(self, smiles_list):
"""
Calculates molecular similarity scores for a list of SMILES strings.
Args:
- smiles_list (list): List of SMILES strings
Returns:
- similarity_vectors (list): List of similarity vectors for each compound pair
"""
fps = [
AllChem.GetMorganFingerprintAsBitVect(Chem.MolFromSmiles(smiles), 2)
for smiles in smiles_list
]
similarity_vectors = []
for i in range(len(smiles_list)):
for j in range(i + 1, len(smiles_list)):
similarity_score = DataStructs.TanimotoSimilarity(
fps[i], fps[j])
similarity_vectors.append(similarity_score)
return np.array(similarity_vectors)
def concatenate_sentences(self, vec_list) -> List:
"""
Concatenate multiple lists of numpy arrays into a single list of numpy arrays.
Parameters
----------
column_list : List[List[np.ndarray]]
List of lists of numpy arrays to concatenate.
Returns
-------
List[np.ndarray]
Concatenated numpy arrays.
"""
vec_arrays = [x.sentence for x in vec_list]
concatenated_vec = np.concatenate(vec_arrays)
return concatenated_vec
def preprocess_mol2vec(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Preprocess SMILES data to mol2vec embeddings.
Parameters
----------
df : pd.DataFrame
DataFrame with SMILES data, where each row is a sample and contains the SMILES string in a column named 'smiles'.
Returns
-------
pd.DataFrame
Preprocessed data with mol2vec embeddings, where each row is a sample and contains the mol2vec embeddings in a column named 'mol2vec'.
"""
model = word2vec.Word2Vec.load(
self.directory + "/embed_model/model_300dim.pkl")
df["sentence"] = df.apply(
lambda x: MolSentence(mol2alt_sentence(x["mol"], 1)), axis=1
)
grouped_df = (
df.groupby("blend_id")["sentence"]
.apply(self.concatenate_sentences)
.reset_index()
)
grouped_df["mol2vec"] = [
DfVec(x) for x in sentences2vec(
grouped_df["sentence"],
model,
unseen="UNK")]
grouped_df["mol2vec"] = grouped_df["mol2vec"].apply(lambda x: x.vec)
return grouped_df
def mol_to_dgl_graph(self, mol: Chem.Mol) -> dgl.DGLGraph:
"""
Convert RDKit molecule object to DGL graph.
Parameters
----------
mol : Chem.Mol
RDKit molecule object.
Returns
-------
dgl.DGLGraph
DGL graph representing the molecule.
"""
graph = dgl.DGLGraph()
num_atoms = mol.GetNumAtoms()
graph.add_nodes(num_atoms)
for bond in mol.GetBonds():
graph.add_edges(bond.GetBeginAtomIdx(), bond.GetEndAtomIdx())
graph.add_edges(bond.GetEndAtomIdx(), bond.GetBeginAtomIdx())
node_feats = torch.tensor([[atom.GetAtomicNum()]
for atom in mol.GetAtoms()], dtype=torch.float32)
graph.ndata['feat'] = node_feats
edge_feats = []
for bond in mol.GetBonds():
bond_type = bond.GetBondTypeAsDouble()
edge_feats.append([bond_type])
edge_feats.append([bond_type])
graph.edata['type'] = torch.tensor(edge_feats, dtype=torch.float32)
return graph
def generate_graphs(self, df: pd.DataFrame) -> List[dgl.DGLGraph]:
"""
Convert RDKit molecule objects in a dataframe to DGL graphs.
Parameters
----------
df : pd.DataFrame
Dataframe with a column named 'mol' containing RDKit molecule objects.
Returns
-------
List[dgl.DGLGraph]
List of DGL graphs representing molecules in the input dataframe.
"""
return df["mol"].apply(self.mol_to_dgl_graph).tolist()
def embed_smiles(self, df: pd.DataFrame, model: str) -> pd.DataFrame:
"""
Preprocess SMILES data to a specific language model.
Parameters
----------
df : pd.DataFrame
DataFrame with SMILES data, where each row is a sample and contains the SMILES string in a column named 'smiles'.
model : str
Hugging Face model identifier.
Returns
-------
pd.DataFrame
Preprocessed data with language model embeddings, where each row is a sample and contains the language model embeddings in a column named after the model identifier.
"""
def process_row(row):
smiles = row[self.base_on_smiles]
encoded_inputs = tokenizer(
smiles, padding=True, truncation=True, return_tensors="pt"
)
with torch.no_grad():
outputs = model(**encoded_inputs)
embeddings = outputs.pooler_output
# if embeddings.size(1) < max_length:
# padding = torch.zeros(embeddings.size(0), max_length - embeddings.size(1), embeddings.size(2))
# embeddings = torch.cat((embeddings, padding), dim=1)
return np.array(embeddings[0].tolist())
model_path = "embed_model/" + model
if not os.path.isdir(model_path):
_ = snapshot_download(
repo_id=model,
cache_dir=self.directory +
model_path)
if model == "ibm/MoLFormer-XL-both-10pct":
model_path = (
self.directory +
model_path +
"/models--ibm--MoLFormer-XL-both-10pct/snapshots/7b12d946c181a37f6012b9dc3b002275de070314")
# ? Why Error Tokenizer class MolformerTokenizer does not exist or is not currently imported.
model = AutoModelForMaskedLM.from_pretrained(model_path)
else:
model_path = (
self.directory +
model_path +
"/models--DeepChem--ChemBERTa-10M-MTR/snapshots/b65d0a6af3156071d9519e867d695aa265bb393f")
model = AutoModel.from_pretrained(model_path)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_path)
df["embeddings"] = df.apply(process_row, axis=1)
return df
def smiles2sentence(
self,
df: pd.DataFrame,
task: str = "test") -> pd.DataFrame:
"""
Aggregate SMILES data by blend_id and oil_property_param_title to form sentences.
Parameters
----------
df : pd.DataFrame
DataFrame with SMILES data, where each row is a sample and contains the SMILES string in a column named 'smiles'.
Returns
-------
pd.DataFrame
Aggregated data with sentences formed by concatenating SMILES strings by blend_id and oil_property_param_title.
"""
def sum_arrays(arrays):
arrays_np = np.array(arrays)
summed_array = np.sum(arrays_np, axis=0)
return np.array(summed_array)
if task == "train":
df = (
df.groupby(by=["blend_id"])
.agg(
func={
"canonical_smiles": lambda x: ", ".join(x),
"smiles": lambda x: ", ".join(x),
"descriptors_array": lambda x: [
sum_arrays(arr) for arr in zip(*x)
],
"oil_property_param_value": "mean",
}
)
.reset_index()
)
df["similarity_vectors"] = df["smiles"].apply(
lambda x: self.calculate_similarity(x.split(", "))
)
else:
df = (
df.groupby(by=["blend_id"])
.agg(
func={
"canonical_smiles": lambda x: ", ".join(x),
"smiles": lambda x: ", ".join(x),
"descriptors_array": lambda x: [
sum_arrays(arr) for arr in zip(*x)
],
}
)
.reset_index()
)
df["similarity_vectors"] = df["smiles"].apply(
lambda x: self.calculate_similarity(x.split(", "))
)
return df
def xy_split(
self, df: pd.DataFrame, column: str = "mol2vec"
) -> Tuple[np.ndarray, np.ndarray]:
"""
Split data into X and y, where X is a numpy array of dtype=object and y is a numpy array of dtype=float64.
Parameters
----------
df : pd.DataFrame
DataFrame with SMILES data, where each row is a sample and contains the SMILES string in one or more columns named in columns.
columns : List[str]
List of column names containing SMILES strings.
Returns
-------
Tuple[np.ndarray, np.ndarray]
A tuple of X (an object numpy array of shape (n_samples, n_columns)) and y (a float64 numpy array of shape (n_samples,)).
"""
target = df.groupby('blend_id')[
'oil_property_param_value'].mean().dropna()
blend_id_without_nulls = df.groupby(
'blend_id')['oil_property_param_value'].mean().dropna().index.tolist()
df = df[df["blend_id"].isin(blend_id_without_nulls)][column]
X = np.vstack(np.array(df, dtype=object))
y = target.values.astype(np.float64)
return X, y
class GetDescriptors:
def __init__(self, dataframe):
self.dataframe = dataframe
self.smiles_df = self.make_unique_smiles_df()
def make_unique_smiles_df(self):
unique_smiles = self.dataframe['smiles'].dropna().unique()
smiles_df = pd.DataFrame(
{'id': range(len(unique_smiles)), 'smiles': unique_smiles})
smiles_df = smiles_df.dropna(subset=['smiles'])
smiles_df['mol'] = smiles_df['smiles'].apply(
lambda x: Chem.MolFromSmiles(x))
return smiles_df
def number_of_atoms(self, atom_list):
for i in atom_list:
self.smiles_df['num_of_{}_atoms'.format(i)] = self.smiles_df['mol'].apply(
lambda x: len(x.GetSubstructMatches(Chem.MolFromSmiles(i))))
self.smiles_df['hs_mol'] = self.smiles_df['mol'].apply(
lambda x: Chem.AddHs(x))
self.smiles_df['num_of_atoms'] = self.smiles_df['hs_mol'].apply(
lambda x: x.GetNumAtoms())
self.smiles_df['num_of_heavy_atoms'] = self.smiles_df['hs_mol'].apply(
lambda x: x.GetNumHeavyAtoms())
def calculate_descriptors(self):
calc = Calculator(descriptors, ignore_3D=True)
df_desc = calc.pandas(self.smiles_df["mol"])
df_desc = df_desc.dropna()
# df_desc = df_desc.select_dtypes(include=np.number).astype('float32')
# df_desc = df_desc.loc[:, df_desc.var() > 0.0]
# df_descN = pd.DataFrame(MinMaxScaler().fit_transform(df_desc), columns=df_desc.columns)
self.smiles_df = pd.concat([self.smiles_df, df_desc], axis=1)
def download_descriptors(self, property_list):
from tqdm import tqdm
data = []
for i in tqdm(self.smiles_df['smiles'], desc="Download properties"):
props = pcp.get_properties(property_list, i, "smiles")
data.append(props)
rows = []
columns = data[0][0].keys()
for i in tqdm(range(len(data)), desc="Processing data"):
rows.append(data[i][0].values())
props_df = pd.DataFrame(data=rows, columns=columns)
self.smiles_df = pd.concat([self.smiles_df, props_df], axis=1)
class SimpleRegressions:
def __init__(self, X, y, test_X=None):
if X is None or y is None:
raise ValueError("X and y cannot be None")
if not isinstance(X, (list, np.ndarray)):
raise ValueError("X must be a list or numpy array")
if not isinstance(y, (list, np.ndarray)):
raise ValueError("y must be a list or numpy array")
if len(X) != len(y):
raise ValueError("Length of X and y must be the same")
self.test_X = test_X
self.X = X
self.y = y
self.catboost = None
self.lgbm = None
self.gb_regressor = None
self.ridge_regressor = None
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=0.2, shuffle=True, random_state=42)
self.s_caler = StandardScaler()
self.y_train_scaled = self.s_caler.fit_transform(
self.y_train.reshape(-1, 1)
).flatten()
self.y_test_scaled = self.s_caler.transform(
self.y_test.reshape(-1, 1)
).flatten()
def evaluation(self, model):
if model is None:
raise ValueError("Model cannot be None")
prediction = model.predict(self.X_test)
prediction = self.s_caler.inverse_transform(
prediction.reshape(-1, 1)).flatten()
mae = mean_absolute_error(self.y_test, prediction)
mse = mean_squared_error(self.y_test, prediction)
plt.figure(figsize=(15, 10))
plt.plot(prediction, "red", label="prediction", linewidth=1.0)
plt.plot(self.y_test, "green", label="actual", linewidth=1.0)
plt.legend()
plt.ylabel("oil_property_param_value")
plt.title("MAE {}, MSE {}".format(round(mae, 4), round(mse, 4)))
plt.show()
print("MAE score:", round(mae, 4))
print("MSE score:", round(mse, 4))
def fit_and_evaluate(self):
print("Catboost")
self.catboost = CatBoostRegressor(
iterations=2000,
learning_rate=0.1,
loss_function="RMSE",
random_seed=1)
self.catboost.fit(
self.X_train,
self.y_train_scaled,
eval_set=(self.X_test, self.y_test_scaled),
verbose=1,
)
self.evaluation(self.catboost)
print("\nLGBMRegressor")
self.lgbm = lgb.LGBMRegressor(
n_estimators=2000, learning_rate=0.1, random_state=3, n_jobs=1
)
self.lgbm.fit(
self.X_train,
self.y_train_scaled,
eval_set=[(self.X_test, self.y_test_scaled)],
)
self.evaluation(self.lgbm)
print("\nGradientBoostingRegressor")
self.gb_regressor = GradientBoostingRegressor(
n_estimators=2000, learning_rate=0.1, random_state=45
)
self.gb_regressor.fit(self.X_train, self.y_train_scaled)
self.evaluation(self.gb_regressor)
print("\nStack model")
self.ridge_train()
def ridge_train(self):
catboost_prediction_train = self.catboost.predict(self.X_train)
lgbm_prediction_train = self.lgbm.predict(self.X_train)
gb_prediction_train = self.gb_regressor.predict(self.X_train)
predictions_df_train = pd.DataFrame(
{
"CatBoost_Prediction": catboost_prediction_train,
"LGBM_Prediction": lgbm_prediction_train,
"GradientBoosting_Prediction": gb_prediction_train,
}
)
catboost_prediction_test = self.catboost.predict(self.X_test)
lgbm_prediction_test = self.lgbm.predict(self.X_test)
gb_prediction_test = self.gb_regressor.predict(self.X_test)
predictions_df_test = pd.DataFrame(
{
"CatBoost_Prediction": catboost_prediction_test,
"LGBM_Prediction": lgbm_prediction_test,
"GradientBoosting_Prediction": gb_prediction_test,
}
)
self.ridge_regressor = GradientBoostingRegressor(n_estimators=1000)
self.ridge_regressor.fit(predictions_df_train, self.y_train_scaled)
prediction = self.ridge_regressor.predict(predictions_df_test)
prediction = self.s_caler.inverse_transform(
prediction.reshape(-1, 1)).flatten()
mae = mean_absolute_error(self.s_caler.inverse_transform(
self.y_test_scaled.reshape(-1, 1)).flatten(), prediction, )
mse = mean_squared_error(self.s_caler.inverse_transform(
self.y_test_scaled.reshape(-1, 1)).flatten(), prediction, )
plt.figure(figsize=(15, 10))
plt.plot(prediction, "red", label="prediction", linewidth=1.0)
plt.plot(
self.s_caler.inverse_transform(self.y_test_scaled.reshape(-1, 1)).flatten(),
"green",
label="actual",
linewidth=1.0,
)
plt.legend()
plt.ylabel("oil_property_param_value")
plt.title("MAE {}, MSE {}".format(round(mae, 4), round(mse, 4)))
plt.show()
print("MAE score:", round(mae, 4))
print("MSE score:", round(mse, 4))
return self.ridge_regressor
def ridge_test(self):
catboost_prediction_test = self.catboost.predict(
self.test_X, thread_count=1)
lgbm_prediction_test = self.lgbm.predict(self.test_X, num_threads=1)
gb_prediction_test = self.gb_regressor.predict(self.test_X)
predictions_df_test = pd.DataFrame(
{
"CatBoost_Prediction": catboost_prediction_test,
"LGBM_Prediction": lgbm_prediction_test,
"GradientBoosting_Prediction": gb_prediction_test,
}
)
# ridge_regressor = self.ridge_train()
preds = self.ridge_regressor.predict(predictions_df_test)
return (
self.ridge_regressor,
self.catboost,
self.lgbm,
self.gb_regressor,
self.s_caler.inverse_transform(preds.reshape(-1, 1)).flatten(),
)
class SmallNN:
def __init__(self, X: np.ndarray, y: np.ndarray, config):
self.config = config
self.model = None
if X is None or y is None:
raise ValueError("X and y cannot be None")
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=0.1, shuffle=True, random_state=42)
self.s_caler = MinMaxScaler()
self.y_train_scaled = self.s_caler.fit_transform(
self.y_train.reshape(-1, 1)
).flatten()
self.y_test_scaled = self.s_caler.transform(
self.y_test.reshape(-1, 1)
).flatten()
def load_config(self, file_path: str):
with open(file_path, "r") as f:
self.config = yaml.safe_load(f)
return self.config["SmallNN"]
def neural_model(self) -> keras.Model:
np.random.seed(1)
score = []
kfold = KFold(n_splits=self.config["n_splits"], shuffle=True)
model = Sequential()
model.add(
Dense(
self.config["neurons"],
input_dim=self.X_train.shape[1],
activation=self.config["activation"],
)
)
model.add(
Dense(
self.config["neurons"],
activation=self.config["activation"]))
model.add(Dense(1, activation=self.config["output_activation"]))
opt = keras.optimizers.Adam(learning_rate=self.config["learning_rate"])
model.compile(
loss="mean_squared_error",
optimizer=opt,
metrics=["mean_absolute_error"])
rlrop = ReduceLROnPlateau(
monitor="val_loss",
factor=self.config["lr_reduction_factor"],
patience=self.config["lr_patience"],
)
for train, validation in kfold.split(
self.X_train, self.y_train_scaled):
model.fit(
self.X_train[train],
self.y_train_scaled[train],
epochs=self.config["epochs"],
batch_size=self.config["batch_size"],
callbacks=[rlrop],
verbose=self.config["verbose"],
validation_data=(
self.X_train[validation],
self.y_train_scaled[validation],
),
)
score.append(model.evaluate(self.X_test, self.y_test_scaled))
return model, score
def evaluation(self, model: keras.Model):
if model is None:
raise ValueError("Model cannot be None")
prediction = model.predict(self.X_test)
prediction = self.s_caler.inverse_transform(
prediction.reshape(-1, 1)).flatten()
mae = mean_absolute_error(self.y_test, prediction)
mse = mean_squared_error(self.y_test, prediction)
plt.figure(figsize=(15, 10))
plt.plot(prediction, "red", label="prediction", linewidth=1.0)
plt.plot(self.y_test, "green", label="actual", linewidth=1.0)
plt.legend()
plt.ylabel("oil_property_param_value")
plt.title("MAE {}, MSE {}".format(round(mae, 4), round(mse, 4)))
plt.show()
print("MAE score:", round(mae, 4))
print("MSE score:", round(mse, 4))
def fit_and_evaluate(self):
self.model, score = self.neural_model()
self.evaluation(self.model)
def predict(self, X):
if self.model is None:
raise ValueError("The model has not been trained yet.")
y_pred_scaled = self.model.predict(X)
y_pred_original = self.s_caler.inverse_transform(
y_pred_scaled.reshape(-1, 1)
).flatten()
return y_pred_original
class LstmRegressor:
def __init__(
self,
units=50,
dropout_rate=0.2,
loss="loss",
optimizer="rmsprop",
epochs=20,
batch_size=64,
neurons_1=689,
neurons_2=64,
scaler=None,
):
self.units = units
self.neurons_1 = neurons_1
self.neurons_2 = neurons_2
self.dropout_rate = dropout_rate
self.optimizer = optimizer
self.epochs = epochs
self.batch_size = batch_size
self.model = None
self.reduce_lr = ReduceLROnPlateau(monitor="loss")
self.scaler = scaler
self.loss_ = loss
def create_model(self, input_shape):
model = Sequential()
model.add(
LSTM(
units=self.neurons_1,
return_sequences=True,
input_shape=input_shape))
model.add(Dropout(self.dropout_rate))
model.add(LSTM(units=self.neurons_2, return_sequences=True))
model.add(Dropout(self.dropout_rate))
model.add(LSTM(units=self.units, return_sequences=True))
model.add(Dropout(self.dropout_rate))
model.add(LSTM(units=self.units))
model.add(Dropout(self.dropout_rate))
model.add(Dense(units=1))
model.compile(optimizer=self.optimizer, loss="mean_squared_error")
return model
def fit(self, X_train, y_train, X_test, y_test):
input_shape = (X_train.shape[1], 1)
if self.scaler:
y_train_scaled = self.scaler.fit_transform(
y_train.reshape(-1, 1)).flatten()
y_test_scaled = self.scaler.transform(
y_test.reshape(-1, 1)).flatten()
else:
y_train_scaled = y_train
y_test_scaled = y_test
self.model = KerasRegressor(
build_fn=self.create_model,
input_shape=input_shape,
epochs=self.epochs,
batch_size=self.batch_size,
verbose=1,
)
checkpoint = self.create_model_checkpoint(
filepath="best_model_weights.h5",
monitor="val_loss",
save_best_only=True)
history = self.model.fit(
X_train,
y_train_scaled,
validation_data=(X_test, y_test_scaled),
callbacks=[self.reduce_lr, checkpoint],
)
self.plot_training_history(history)
y_pred_scaled = self.model.predict(X_test)
y_pred_original = self.scaler.inverse_transform(
y_pred_scaled.reshape(-1, 1)
).flatten()
return self.model, y_pred_original
def predict(self, X):
if self.model is None:
raise ValueError("The model has not been trained yet.")
y_pred_scaled = self.model.predict(X)
y_pred_original = self.scaler.inverse_transform(
y_pred_scaled.reshape(-1, 1)
).flatten()
return y_pred_original
def plot_training_history(self, history):
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(history.history[self.loss_],
label="Training Loss", color="blue")
plt.plot(history.history["val_" + self.loss_],
label="Validation Loss", color="red")
plt.title("Loss")
plt.xlabel("Epochs")
plt.legend()
plt.title("Metrics")
plt.xlabel("Epochs")
plt.legend(loc="upper right")
plt.tight_layout()
plt.show()
def create_model_checkpoint(
self,
filepath,
monitor="val_mean_absolute_error",
save_best_only=True,
save_weights_only=True,
mode="auto",
verbose=0,
):
checkpoint = ModelCheckpoint(
filepath=filepath,
monitor=monitor,
save_best_only=save_best_only,
save_weights_only=save_weights_only,
mode=mode,
verbose=verbose,
)
return checkpoint
class Dataset:
def __init__(self, data_x, data_y=None):
super(Dataset, self).__init__()
self.data_x = data_x
self.data_y = data_y
def __len__(self):
return len(self.data_x)
def __getitem__(self, idx):
if self.data_y is not None:
return self.data_x[idx], self.data_y[idx]
else:
return self.data_x[idx]
def augment_data(self, x_, y_):
copy_x = x_.copy()
new_x = []
new_y = y_.copy()
dim = x_.shape[2]
k = int(0.3 * dim)
for i in range(x_.shape[0]):
idx = random.sample(range(dim), k=k)
copy_x[i, :, idx] = 0
new_x.append(copy_x[i])
return np.stack(new_x, axis=0), new_y
class DataLoader:
def __init__(self, main_array, static_cols, dynamic_cols, task="train"):
self.main_array = main_array
self.static_cols = static_cols
self.dynamic_cols = dynamic_cols
self.data_x = self.combine_features()
self.task = task
if self.task == "train":
self.data_y = np.array(
main_array["oil_property_param_value"]).reshape(-1, 1)
else:
self.data_y = None
def process_similarity_vectors(self, similarity_vectors):
if (
len(similarity_vectors) == 0
): # если была только 1 молекула, то сходства нет - 0
return np.array([0, 0, 0, 0, 0, 0])
else:
from scipy.stats import kurtosis
min_value = np.min(similarity_vectors)
mean_value = np.mean(similarity_vectors)
median_value = np.median(similarity_vectors)
max_value = np.max(similarity_vectors)
kurtosis_value = kurtosis(similarity_vectors)
std_value = np.std(similarity_vectors)
processed_vector = np.array(
[
min_value,
mean_value,
median_value,
max_value,
std_value,
kurtosis_value,
]
)
return processed_vector
def combine_features(self):
new_vecs = []
add_dynamic_max_len = 0
fixed_len = 0
fixed_arrays = [self.main_array[col] for col in self.static_cols]
fixed_len = sum(len(arr[0]) for arr in fixed_arrays)
if len(self.dynamic_cols) > 0:
add_dynamic_max_len = 6
dynamic_col_data = self.main_array[self.dynamic_cols[0]]
for i in range(len(self.main_array)):
vec_ = np.array([])
for arr in fixed_arrays:
vec_ = np.concatenate([vec_, arr[i]])
if dynamic_col_data is not None:
similarity_vectors = dynamic_col_data[i]
processed_vector = self.process_similarity_vectors(
similarity_vectors
)
pad_len = add_dynamic_max_len - processed_vector.shape[0]
processed_vector_padded = np.pad(
processed_vector,
(0, pad_len),
mode="constant",
constant_values=0,
)
vec_ = np.concatenate([vec_, processed_vector_padded])
new_vecs.append(vec_)
else:
for i in range(len(self.main_array)):
vec_ = np.array([])
for arr in fixed_arrays:
vec_ = np.concatenate([vec_, arr[i]])
new_vecs.append(vec_)
max_len = fixed_len + add_dynamic_max_len
return np.array(new_vecs).reshape(len(self.main_array), 1, max_len)
def get_dataset(self):
if self.data_y is not None:
print(f"Shape of data_x: {self.data_x.shape}")
print(f"Shape of data_y: {self.data_y.shape}")
return Dataset(self.data_x, self.data_y)
else:
print(f"Shape of data_x: {self.data_x.shape}")
return Dataset(self.data_x), None
class ConvRegressor(nn.Module):
def __init__(self, config):
super(ConvRegressor, self).__init__()
self.name = "ConvRegressor"
self.config = config
self.conv_block = nn.Sequential(
nn.Conv1d(1, 8, 5, stride=1, padding=0),
nn.Dropout(0.3),
nn.Conv1d(8, 8, 5, stride=1, padding=0),
nn.ReLU(),
nn.Conv1d(8, 16, 5, stride=2, padding=0),
nn.Dropout(0.3),
nn.AvgPool1d(11),
nn.Conv1d(16, 4, 3, stride=3, padding=0),
nn.Flatten(),
)
self.linear = nn.Sequential(
nn.Linear(self.config["Conv"]["input_layer"], 1024),
nn.Dropout(0.3),
nn.ReLU(),
nn.Linear(1024, 512),
nn.Dropout(0.3),
nn.ReLU(),
)
self.head1 = nn.Linear(512, 1)
self.loss1 = nn.MSELoss()
self.loss3 = nn.L1Loss()
def forward(self, x, y=None):
if y is None:
out = self.conv_block(x)
out = self.head1(self.linear(out))
return out
else:
out = self.conv_block(x)
out = self.head1(self.linear(out))
loss1 = 0.4 * self.loss1(out, y) + 0.6 * self.loss3(out, y)
return loss1
class LSTMRegressor(nn.Module):
def __init__(self, config):
super(LSTMRegressor, self).__init__()
self.name = "LSTMRegressor"
self.config = config
self.lstm = nn.LSTM(
self.config["alter_shapes"][1],
self.config["batch_size_train"],
num_layers=2,
batch_first=True,
)
self.linear = nn.Sequential(
nn.Linear(self.config["Lstm"]["input_layer"], 1024),