/
util.py
444 lines (367 loc) · 13.1 KB
/
util.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
import functools
import json
import logging
import os
from pathlib import Path
import random
import subprocess
from typing import Dict, List, Mapping, Optional, Tuple, Union
import numpy as np
import torch
import pandas as pd
from anndata import AnnData
import scib
from matplotlib import pyplot as plt
from matplotlib import axes
from IPython import get_ipython
from .. import logger
def gene_vocabulary():
"""
Generate the gene name2id and id2name dictionaries.
"""
pass
def set_seed(seed):
"""set random seed."""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# if n_gpu > 0:
# torch.cuda.manual_seed_all(seed)
def add_file_handler(logger: logging.Logger, log_file_path: Path):
"""
Add a file handler to the logger.
"""
h = logging.FileHandler(log_file_path)
# format showing time, name, function, and message
formatter = logging.Formatter(
"%(asctime)s-%(name)s-%(levelname)s-%(funcName)s: %(message)s",
datefmt="%H:%M:%S",
)
h.setFormatter(formatter)
h.setLevel(logger.level)
logger.addHandler(h)
def category_str2int(category_strs: List[str]) -> List[int]:
set_category_strs = set(category_strs)
name2id = {name: i for i, name in enumerate(set_category_strs)}
return [name2id[name] for name in category_strs]
def isnotebook() -> bool:
"""check whether excuting in jupyter notebook."""
try:
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return True # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
def get_free_gpu():
import subprocess
import sys
from io import StringIO
import pandas as pd
gpu_stats = subprocess.check_output(
[
"nvidia-smi",
"--format=csv",
"--query-gpu=memory.used,memory.free",
]
).decode("utf-8")
gpu_df = pd.read_csv(
StringIO(gpu_stats), names=["memory.used", "memory.free"], skiprows=1
)
print("GPU usage:\n{}".format(gpu_df))
gpu_df["memory.free"] = gpu_df["memory.free"].map(lambda x: int(x.rstrip(" [MiB]")))
idx = gpu_df["memory.free"].idxmax()
print(
"Find free GPU{} with {} free MiB".format(idx, gpu_df.iloc[idx]["memory.free"])
)
return idx
def get_git_commit():
return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip()
def histogram(
*data: List[np.ndarray],
label: List[str] = ["train", "valid"],
color: List[str] = ["blue", "red"],
figsize: Tuple[int, int] = (9, 4),
title: Optional[str] = None,
show: bool = False,
save: Optional[str] = None,
) -> axes.Axes:
"""
Plot histogram of the data.
Args:
data (List[np.ndarray]): The data to plot.
label (List[str]): The label of the data.
color (List[str]): The color of the data.
figsize (Tuple[int, int]): The size of the figure.
title (Optional[str]): The title of the figure.
show (bool): Whether to show the figure.
save (Optional[str]): The path to save the figure.
Returns:
axes.Axes: The axes of the figure.
"""
# show histogram of the clipped values
assert len(data) == len(label), "The number of data and labels must be equal."
fig, ax = plt.subplots(1, 1, figsize=figsize, dpi=150)
max_value = max(np.max(data) for data in data)
ax.hist(
[d.flatten() for d in data],
bins=np.arange(0, max_value + 1, 1) + 0.5 if max_value < 60 else 60,
label=label,
density=True,
histtype="bar",
linewidth=2,
rwidth=0.85,
color=color,
)
ax.legend()
ax.set_xlabel("counts")
ax.set_ylabel("density")
if title is not None:
ax.set_title(title)
if show:
plt.show()
if save is not None:
fig.savefig(save, bbox_inches="tight")
return ax
def _indicate_col_name(adata: AnnData, promt_str: str) -> Optional[str]:
"""
Indicate the column name of the data.
Args:
adata (AnnData): The AnnData object.
promt_str (str): The prompt string.
Returns:
Optional[str]: The column name.
"""
while True:
col_name = input(promt_str)
if col_name == "":
col_name = None
break
elif col_name in adata.var.columns:
break
elif col_name in adata.obs.columns:
break
else:
print(f"The column {col_name} is not in the data. " f"Please input again.")
return col_name
def find_required_colums(
adata: AnnData,
id: str,
configs_dir: Union[str, Path],
update: bool = False,
) -> List[Optional[str]]:
"""
Find the required columns in AnnData, including celltype column, str_celltype
column, the gene name column, and the experimental batch key.
This function asks the user to input the required column names if the first
time loading the data. The names are saved in the config file and will be
automatically loaded next time.
Args:
adata (AnnData): The AnnData object.
id (str): The id of the AnnData object, will be used as the file name for
saving the config file.
configs_dir (Union[str, Path]): The directory of saved config files.
update (bool): Whether to update the config file.
Returns:
List[Optional[str]]: The required columns, including celltype_col, str_celltype_col,
gene_col, and batch_col.
"""
if isinstance(configs_dir, str):
configs_dir = Path(configs_dir)
if not configs_dir.exists():
configs_dir.mkdir()
config_file = configs_dir / f"{id}.json"
if not config_file.exists() or update:
print(
"The config file does not exist, this may be the first time "
"loading the data. \nPlease input the required column names."
)
print(adata)
celltype_col = _indicate_col_name(
adata,
"Please input the celltype column name (skip if not applicable): ",
)
str_celltype_col = _indicate_col_name(
adata, "Please input the str_celltype column name: "
)
gene_col = _indicate_col_name(adata, "Please input the gene column name: ")
batch_col = _indicate_col_name(adata, "Please input the batch column name: ")
config = {
"celltype_col": celltype_col,
"str_celltype_col": str_celltype_col,
"gene_col": gene_col,
"batch_col": batch_col,
}
with open(config_file, "w") as f:
json.dump(config, f)
else:
with open(config_file, "r") as f:
config = json.load(f)
return [
config["celltype_col"],
config["str_celltype_col"],
config["gene_col"],
config["batch_col"],
]
def tensorlist2tensor(tensorlist, pad_value):
max_len = max(len(t) for t in tensorlist)
dtype = tensorlist[0].dtype
device = tensorlist[0].device
tensor = torch.zeros(len(tensorlist), max_len, dtype=dtype, device=device)
tensor.fill_(pad_value)
for i, t in enumerate(tensorlist):
tensor[i, : len(t)] = t
return tensor
def map_raw_id_to_vocab_id(
raw_ids: Union[np.ndarray, torch.Tensor],
gene_ids: np.ndarray,
) -> Union[np.ndarray, torch.Tensor]:
"""
Map some raw ids which are indices of the raw gene names to the indices of the
Args:
raw_ids: the raw ids to map
gene_ids: the gene ids to map to
"""
if isinstance(raw_ids, torch.Tensor):
device = raw_ids.device
dtype = raw_ids.dtype
return_pt = True
raw_ids = raw_ids.cpu().numpy()
elif isinstance(raw_ids, np.ndarray):
return_pt = False
dtype = raw_ids.dtype
else:
raise ValueError(f"raw_ids must be either torch.Tensor or np.ndarray.")
if raw_ids.ndim != 1:
raise ValueError(f"raw_ids must be 1d, got {raw_ids.ndim}d.")
if gene_ids.ndim != 1:
raise ValueError(f"gene_ids must be 1d, got {gene_ids.ndim}d.")
mapped_ids: np.ndarray = gene_ids[raw_ids]
assert mapped_ids.shape == raw_ids.shape
if return_pt:
return torch.from_numpy(mapped_ids).type(dtype).to(device)
return mapped_ids.astype(dtype)
def load_pretrained(
model: torch.nn.Module,
pretrained_params: Mapping[str, torch.Tensor],
strict: bool = False,
prefix: Optional[List[str]] = None,
verbose: bool = True,
) -> torch.nn.Module:
"""
Load pretrained weights to the model.
Args:
model (torch.nn.Module): The model to load weights to.
pretrained_params (Mapping[str, torch.Tensor]): The pretrained parameters.
strict (bool): Whether to strictly enforce that the keys in :attr:`pretrained_params`
match the keys returned by this module's :meth:`Module.state_dict`. Default to False.
prefix (List[str]): The list of prefix strings to match with the keys in
:attr:`pretrained_params`. The matched keys will be loaded. Default to None.
Returns:
torch.nn.Module: The model with pretrained weights.
"""
use_flash_attn = getattr(model, "use_fast_transformer", True)
if not use_flash_attn:
pretrained_params = {
k.replace("Wqkv.", "in_proj_"): v for k, v in pretrained_params.items()
}
if prefix is not None and len(prefix) > 0:
if isinstance(prefix, str):
prefix = [prefix]
pretrained_params = {
k: v
for k, v in pretrained_params.items()
if any(k.startswith(p) for p in prefix)
}
model_dict = model.state_dict()
if strict:
if verbose:
for k, v in pretrained_params.items():
logger.info(f"Loading parameter {k} with shape {v.shape}")
model_dict.update(pretrained_params)
model.load_state_dict(model_dict)
else:
if verbose:
for k, v in pretrained_params.items():
if k in model_dict and v.shape == model_dict[k].shape:
logger.info(f"Loading parameter {k} with shape {v.shape}")
pretrained_params = {
k: v
for k, v in pretrained_params.items()
if k in model_dict and v.shape == model_dict[k].shape
}
model_dict.update(pretrained_params)
model.load_state_dict(model_dict)
return model
# Wrapper for all scib metrics, we leave out some metrics like hvg_score, cell_cyvle,
# trajectory_conservation, because we only evaluate the latent embeddings here and
# these metrics are evaluating the reconstructed gene expressions or pseudotimes.
def eval_scib_metrics(
adata: AnnData,
batch_key: str = "str_batch",
label_key: str = "celltype",
notes: Optional[str] = None,
) -> Dict:
results = scib.metrics.metrics(
adata,
adata_int=adata,
batch_key=batch_key,
label_key=label_key,
embed="X_scGPT",
isolated_labels_asw_=False,
silhouette_=True,
hvg_score_=False,
graph_conn_=True,
pcr_=True,
isolated_labels_f1_=False,
trajectory_=False,
nmi_=True, # use the clustering, bias to the best matching
ari_=True, # use the clustering, bias to the best matching
cell_cycle_=False,
kBET_=False, # kBET return nan sometimes, need to examine
ilisi_=False,
clisi_=False,
)
if notes is not None:
logger.info(f"{notes}")
logger.info(f"{results}")
result_dict = results[0].to_dict()
logger.info(
"Biological Conservation Metrics: \n"
f"ASW (cell-type): {result_dict['ASW_label']:.4f}, graph cLISI: {result_dict['cLISI']:.4f}, "
f"isolated label silhouette: {result_dict['isolated_label_silhouette']:.4f}, \n"
"Batch Effect Removal Metrics: \n"
f"PCR_batch: {result_dict['PCR_batch']:.4f}, ASW (batch): {result_dict['ASW_label/batch']:.4f}, "
f"graph connectivity: {result_dict['graph_conn']:.4f}, graph iLISI: {result_dict['iLISI']:.4f}"
)
result_dict["avg_bio"] = np.mean(
[
result_dict["NMI_cluster/label"],
result_dict["ARI_cluster/label"],
result_dict["ASW_label"],
]
)
# remove nan value in result_dict
result_dict = {k: v for k, v in result_dict.items() if not np.isnan(v)}
return result_dict
# wrapper to make sure all methods are called only on the main process
def main_process_only(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if os.environ.get("LOCAL_RANK", "0") == "0":
return func(*args, **kwargs)
return wrapper
# class wrapper to make sure all methods are called only on the main process
class MainProcessOnly:
def __init__(self, obj):
self.obj = obj
def __getattr__(self, name):
attr = getattr(self.obj, name)
if callable(attr):
attr = main_process_only(attr)
return attr