jinysun commited on
Commit
d3bbf03
·
verified ·
1 Parent(s): 4e41bd2

Update tool/orbital.py

Browse files
Files changed (1) hide show
  1. tool/orbital.py +92 -93
tool/orbital.py CHANGED
@@ -1,94 +1,93 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Created on Wed Oct 30 09:14:55 2024
4
-
5
- @author: BM109X32G-10GPU-02
6
- """
7
-
8
-
9
- from sklearn.metrics import confusion_matrix
10
- import matplotlib.pyplot as plt
11
- import numpy as np
12
- from rdkit.Chem import AllChem
13
- from sklearn.datasets import make_blobs
14
- import json
15
- import numpy as np
16
- import math
17
-
18
- from scipy import sparse
19
- from sklearn.metrics import median_absolute_error,r2_score, mean_absolute_error,mean_squared_error
20
- from langchain.tools import BaseTool
21
- import pandas as pd
22
- import matplotlib.pyplot as plt
23
- from rdkit import Chem
24
- import pickle
25
- from sklearn.ensemble import RandomForestRegressor
26
-
27
-
28
- def split_string(string):
29
-
30
- result = []
31
-
32
- for char in string:
33
-
34
- result.append(char)
35
-
36
- return result
37
-
38
- def main(sm):
39
-
40
-
41
- inchis = list([sm])
42
- rts = list([0])
43
-
44
- smiles, targets,features = [], [],[]
45
- for i, inc in enumerate(inchis):
46
- mol = Chem.MolFromSmiles(inc)
47
- if mol is None:
48
- continue
49
- else:
50
- smi =AllChem. GetMorganFingerprintAsBitVect(mol,1024)
51
- smi = smi.ToBitString()
52
- a = split_string(smi)
53
- a = np.array(a)
54
- #smi = Chem.MolToSmiles(mol)
55
- features.append(a)
56
- targets.append(rts[i])
57
-
58
-
59
-
60
- features = np.asarray(features)
61
- targets = np.asarray(targets)
62
- X_test=features
63
- Y_test=targets
64
- n_features=10
65
-
66
- model = RandomForestRegressor(n_estimators=100)
67
- load_homo = pickle.load(open(r"tool/orbital/homo.pkl", 'rb'))
68
- load_lumo = pickle.load(open(r"tool/orbital/lumo.pkl", 'rb'))
69
- # model = load_model('C:/Users/sunjinyu/Desktop/FingerID Reference/drug-likeness/CNN/single_model.h5')
70
- Y_homo= load_homo.predict(X_test)
71
- Y_lumo = load_lumo.predict(X_test)
72
- homo = float(Y_homo)
73
- lumo = float(Y_lumo)
74
- return homo, lumo
75
-
76
- class homolumo_predictor(BaseTool):
77
- name: str = "homolumo_predictor"
78
- description: str = (
79
- "Input SMILES , returns the HOMO/LUMO (Highest Occupied Molecular Orbital (HOMO) \
80
- and Lowest Unoccupied Molecular Orbital)."
81
- )
82
- def __init__(self):
83
- super().__init__()
84
- def _run(self, smiles: str) -> str:
85
- mol = Chem.MolFromSmiles(smiles)
86
- if mol is None:
87
- return "Invalid SMILES string"
88
- Y_homo, Y_lumo = main( str(smiles) )
89
- return f"The HOMO is predicted to be {'{:.2f}'.format(Y_homo)} eV , the LUMO is predicted to be {'{:.2f}'.format(Y_lumo)} eV"
90
-
91
- async def _arun(self, smiles: str) -> str:
92
- """Use the tool asynchronously."""
93
- raise NotImplementedError()
94
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Wed Oct 30 09:14:55 2024
4
+
5
+ @author: BM109X32G-10GPU-02
6
+ """
7
+
8
+
9
+
10
+ import numpy as np
11
+ from rdkit.Chem import AllChem
12
+ from sklearn.datasets import make_blobs
13
+ import json
14
+ import numpy as np
15
+ import math
16
+
17
+ from scipy import sparse
18
+ from sklearn.metrics import median_absolute_error,r2_score, mean_absolute_error,mean_squared_error
19
+ from langchain.tools import BaseTool
20
+ import pandas as pd
21
+
22
+ from rdkit import Chem
23
+ import pickle
24
+ from sklearn.ensemble import RandomForestRegressor
25
+
26
+
27
+ def split_string(string):
28
+
29
+ result = []
30
+
31
+ for char in string:
32
+
33
+ result.append(char)
34
+
35
+ return result
36
+
37
+ def main(sm):
38
+
39
+
40
+ inchis = list([sm])
41
+ rts = list([0])
42
+
43
+ smiles, targets,features = [], [],[]
44
+ for i, inc in enumerate(inchis):
45
+ mol = Chem.MolFromSmiles(inc)
46
+ if mol is None:
47
+ continue
48
+ else:
49
+ smi =AllChem. GetMorganFingerprintAsBitVect(mol,1024)
50
+ smi = smi.ToBitString()
51
+ a = split_string(smi)
52
+ a = np.array(a)
53
+ #smi = Chem.MolToSmiles(mol)
54
+ features.append(a)
55
+ targets.append(rts[i])
56
+
57
+
58
+
59
+ features = np.asarray(features)
60
+ targets = np.asarray(targets)
61
+ X_test=features
62
+ Y_test=targets
63
+ n_features=10
64
+
65
+ model = RandomForestRegressor(n_estimators=100)
66
+ load_homo = pickle.load(open(r"tool/orbital/homo.pkl", 'rb'))
67
+ load_lumo = pickle.load(open(r"tool/orbital/lumo.pkl", 'rb'))
68
+ # model = load_model('C:/Users/sunjinyu/Desktop/FingerID Reference/drug-likeness/CNN/single_model.h5')
69
+ Y_homo= load_homo.predict(X_test)
70
+ Y_lumo = load_lumo.predict(X_test)
71
+ homo = float(Y_homo)
72
+ lumo = float(Y_lumo)
73
+ return homo, lumo
74
+
75
+ class homolumo_predictor(BaseTool):
76
+ name: str = "homolumo_predictor"
77
+ description: str = (
78
+ "Input SMILES , returns the HOMO/LUMO (Highest Occupied Molecular Orbital (HOMO) \
79
+ and Lowest Unoccupied Molecular Orbital)."
80
+ )
81
+ def __init__(self):
82
+ super().__init__()
83
+ def _run(self, smiles: str) -> str:
84
+ mol = Chem.MolFromSmiles(smiles)
85
+ if mol is None:
86
+ return "Invalid SMILES string"
87
+ Y_homo, Y_lumo = main( str(smiles) )
88
+ return f"The HOMO is predicted to be {'{:.2f}'.format(Y_homo)} eV , the LUMO is predicted to be {'{:.2f}'.format(Y_lumo)} eV"
89
+
90
+ async def _arun(self, smiles: str) -> str:
91
+ """Use the tool asynchronously."""
92
+ raise NotImplementedError()
 
93