-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
164 lines (132 loc) · 3.93 KB
/
Copy pathutils.py
File metadata and controls
164 lines (132 loc) · 3.93 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
import typing as T
import logging as lg
import multiprocessing as mp
import sys
from functools import partial
from pathlib import Path
import h5py
import numpy as np
import torch
from omegaconf import OmegaConf
from rdkit import Chem, DataStructs
from rdkit.Chem import AllChem
from tqdm import tqdm
logLevels = {0: lg.ERROR, 1: lg.WARNING, 2: lg.INFO, 3: lg.DEBUG}
LOGGER_NAME = "DTI"
def get_logger(logger_name: str = None) -> lg.Logger:
if logger_name is None:
logger_name = LOGGER_NAME
return lg.getLogger(logger_name)
logg = get_logger()
def config_logger(
file: T.Union[Path, None],
fmt: str,
level: bool = 2,
use_stdout: bool = True,
):
"""
Create and configure the logger
:param file: Can be a Path or None -- if a Path, log messages will be written to the file at Path
:type file: T.Union[Path, None]
:param fmt: Formatting string for the log messages
:type fmt: str
:param level: Level of verbosity
:type level: int
:param use_stdout: Whether to also log messages to stdout
:type use_stdout: bool
:return:
"""
module_logger = lg.getLogger(LOGGER_NAME)
module_logger.setLevel(logLevels[level])
formatter = lg.Formatter(fmt)
if file is not None:
fh = lg.FileHandler(file)
fh.setFormatter(formatter)
module_logger.addHandler(fh)
if use_stdout:
sh = lg.StreamHandler(sys.stdout)
sh.setFormatter(formatter)
module_logger.addHandler(sh)
lg.propagate = False
return module_logger
def set_random_seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)
def canonicalize(smiles):
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
return Chem.MolToSmiles(mol, isomericSmiles=True)
else:
return None
def smiles2morgan(s, radius=2, nBits=2048):
"""
Convert smiles into Morgan Fingerprint.
:param smile: SMILES string
:type smile: str
:return: Morgan fingerprint
:rtype: np.ndarray
"""
try:
s = canonicalize(s)
mol = Chem.MolFromSmiles(s)
features_vec = AllChem.GetMorganFingerprintAsBitVect(mol, radius, nBits=nBits)
features = np.zeros((1,))
DataStructs.ConvertToNumpyArray(features_vec, features)
except Exception as e:
logg.error(e)
logg.error(
f"Failed to convert SMILES to Morgan Fingerprint: {s} convert to all 0 features"
)
features = np.zeros((nBits,))
return features
def get_config(experiment_id, mol_feat, prot_feat):
data_cfg = {
"batch_size": 32,
"num_workers": 0,
"precompute": True,
"mol_feat": mol_feat,
"prot_feat": prot_feat,
}
model_cfg = {
"latent_size": 1024,
}
training_cfg = {
"n_epochs": 50,
"every_n_val": 1,
}
cfg = {
"data": data_cfg,
"model": model_cfg,
"training": training_cfg,
"experiment_id": experiment_id,
}
return OmegaConf.structured(cfg)
def _hdf5_load_partial_func(k, file_path):
"""
Helper function for load_hdf5_parallel
"""
with h5py.File(file_path, "r") as fi:
emb = torch.from_numpy(fi[k][:])
return emb
def load_hdf5_parallel(file_path, keys, n_jobs=-1):
"""
Load keys from hdf5 file into memory
:param file_path: Path to hdf5 file
:type file_path: str
:param keys: List of keys to get
:type keys: list[str]
:return: Dictionary with keys and records in memory
:rtype: dict
"""
torch.multiprocessing.set_sharing_strategy("file_system")
if (n_jobs == -1) or (n_jobs > mp.cpu_count()):
n_jobs = mp.cpu_count()
with mp.Pool(processes=n_jobs) as pool:
all_embs = list(
tqdm(
pool.imap(partial(_hdf5_load_partial_func, file_path=file_path), keys),
total=len(keys),
)
)
embeddings = {k: v for k, v in zip(keys, all_embs)}
return embeddings