-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathstep3_config.py
196 lines (151 loc) · 7.73 KB
/
step3_config.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
import functools
import sys
import optuna
import scanpy as sc
from fun2code import fun2code_dict
from optuna.integration.wandb import WeightsAndBiasesCallback
import wandb
from dance.transforms.cell_feature import CellPCA, CellSVD, WeightedFeaturePCA
from dance.transforms.filter import FilterGenesPercentile, FilterGenesRegression
from dance.transforms.interface import AnnDataTransform
from dance.transforms.misc import Compose, SetConfig
from dance.transforms.normalize import ScaleFeature, ScTransformR
def set_method_name(func):
"""Get method name to name the optimization option."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
method_name = func.__name__ + "_"
result = func(method_name, *args, **kwargs)
return result
except Exception as e:
print(f"{func.__name__}{args}\n==> {e}")
raise e
return wrapper
@set_method_name
def cell_pca(method_name: str, trial: optuna.Trial):
return CellPCA(n_components=trial.suggest_int(method_name + "n_components", 200, 5000))
@set_method_name
def cell_weighted_pca(method_name: str, trial: optuna.Trial):
return WeightedFeaturePCA(n_components=trial.suggest_int(method_name + "n_components", 200, 5000))
@set_method_name
def cell_svd(method_name: str, trial: optuna.Trial):
return CellSVD(n_components=trial.suggest_int(method_name + "n_components", 200, 5000))
@set_method_name
def Filter_gene_by_regress_score(method_name: str, trial: optuna.Trial):
return FilterGenesRegression(
method=trial.suggest_categorical(method_name + "method", ["enclasc", "seurat3", "scmap"]),
num_genes=trial.suggest_int(method_name + "num_genes", 5000, 6000))
@set_method_name
def highly_variable_genes(method_name: str, trial: optuna.Trial):
method_name = str(sys._getframe().f_code.co_name) + "_"
return AnnDataTransform(sc.pp.highly_variable_genes, min_mean=trial.suggest_float(
method_name + "min_mean", 0.0025,
0.03), max_mean=trial.suggest_float(method_name + "min_mean", 1.5,
4.5), min_disp=trial.suggest_float(method_name + "min_disp", 0.25, 0.75),
span=trial.suggest_float(method_name + "span", 0.2,
1.0), n_bins=trial.suggest_int(method_name + "n_bins", 10, 30),
flavor=trial.suggest_categorical(method_name + "flavor", ['seurat', 'cell_ranger']))
@set_method_name
def filter_gene_by_percentile(method_name: str, trial: optuna.Trial):
return FilterGenesPercentile(min_val=trial.suggest_int(method_name + "min_val", 1, 10),
max_val=trial.suggest_int(method_name + "max_val", 90, 99),
mode=trial.suggest_categorical(method_name + "mode", ["sum", "var", "cv", "rv"]))
@set_method_name
def filter_gene_by_count(method_name: str, trial: optuna.Trial):
method = trial.suggest_categorical(method_name + "method", ['min_counts', 'min_cells', 'max_counts', 'max_cells'])
if method == "min_counts":
num = trial.suggest_int(method_name + "num", 2, 10)
if method == "min_cells":
num = trial.suggest_int(method_name + "num", 2, 10)
if method == "max_counts":
num = trial.suggest_int(method_name + "num", 500, 1000)
if method == "max_cells":
num = trial.suggest_int(method_name + "num", 500, 1000)
return AnnDataTransform(sc.pp.filter_genes, **{method: num})
@set_method_name
def log1p(method_name: str, trial: optuna.Trial):
return AnnDataTransform(sc.pp.log1p, base=trial.suggest_int(method_name + "base", 2, 10))
@set_method_name
def scTransform(method_name: str, trial: optuna.Trial):
return ScTransformR(min_cells=trial.suggest_int(method_name + "min_cells", 1, 10))
@set_method_name
def scaleFeature(method_name: str, trial: optuna.Trial): #eps未优化
return ScaleFeature(mode=trial.suggest_categorical(method_name +
"mode", ["normalize", "standardize", "minmax", "l2"]))
@set_method_name
def normalize_total(method_name: str, trial: optuna.Trial):
exclude_highly_expressed = trial.suggest_categorical(method_name + "exclude_highly_expressed", [False, True])
if exclude_highly_expressed:
max_fraction = trial.suggest_float(method_name + "max_fraction", 0.04, 0.1)
return AnnDataTransform(sc.pp.normalize_total,
target_sum=trial.suggest_categorical(method_name + "target_sum", [1e4, 1e5, 1e6]),
exclude_highly_expressed=exclude_highly_expressed, max_fraction=max_fraction,
key_added="n_counts")
else:
return AnnDataTransform(sc.pp.normalize_total,
target_sum=trial.suggest_categorical(method_name + "target_sum", [1e4, 1e5, 1e6]),
exclude_highly_expressed=exclude_highly_expressed, max_fraction=max_fraction,
key_added="n_counts")
@set_method_name
def filter_cell_by_count(method_name: str, trial: optuna.Trial):
method = trial.suggest_categorical(method_name + "method", ['min_counts', 'min_genes', 'max_counts', 'max_genes'])
if method == "min_counts":
num = trial.suggest_int(method_name + "num", 2, 10)
if method == "min_genes":
num = trial.suggest_int(method_name + "num", 2, 10)
if method == "max_counts":
num = trial.suggest_int(method_name + "num", 500, 1000)
if method == "max_genes":
num = trial.suggest_int(method_name + "num", 500, 1000)
return AnnDataTransform(sc.pp.filter_cells, **{method: num})
# # 获取当前文件中的所有函数
# functions = [(name,obj) for name, obj in inspect.getmembers(
# sys.modules[__name__]) if inspect.isfunction(obj)]
# print(functions)
# # 遍历并装饰每个函数
# for name, function in functions:
# if name != "set_method_name": # 排除装饰器函数本身
# print(function)
# setattr(__name__, name, set_method_name(function))
def get_transforms(trial, fun_list, set_data_config=True):
"""Obtain the Compose of the preprocessing function according to the preprocessing
function."""
transforms = []
for f_str in fun_list:
fun_i = eval(f_str)
transforms.append(fun_i(trial))
if "highly_variable_genes" in fun_list and "log1p" not in fun_list[:fun_list.index('"highly_variable_genes"')]:
return None
if set_data_config:
data_config = {"label_channel": "cell_type"}
feature_name = {"cell_svd", "cell_weighted_pca", "cell_pca"} & set(fun_list)
if feature_name:
data_config.update({"feature_channel": fun2code_dict[feature_name].name})
transforms.append(SetConfig(data_config))
return transforms
def log_in_wandb(wandbc=None):
"""Decorate optimization functions."""
def decorator(func):
def wrapper(*args, **kwargs):
wandb_decorator = wandbc.track_in_wandb()
decorator_function = wandb_decorator(func)
result = decorator_function(*args, **kwargs)
wandb.log(result)
values = list(result.values())
if len(values) == 1:
return values[0]
else:
return tuple(values)
return wrapper
return decorator
def get_optimizer(project, objective, n_trials=2):
"""Get optimizer."""
wandb_kwargs = {"project": project}
wandbc = WeightsAndBiasesCallback(wandb_kwargs=wandb_kwargs, as_multirun=True)
decorator = log_in_wandb(wandbc)
decorator_function = decorator(objective)
study = optuna.create_study()
def wrapper():
study.optimize(decorator_function, n_trials=n_trials, callbacks=[wandbc])
return wrapper