Skip to content

Commit 04643f9

Browse files
add magic with reverse norm order (#797)
Former-commit-id: 458fd8d
1 parent fa8ce75 commit 04643f9

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

openproblems/tasks/denoising/methods/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
from .magic import knn_naive
88
from .magic import magic
99
from .magic import magic_approx
10+
from .magic import magic_approx_reverse_norm
11+
from .magic import magic_reverse_norm

openproblems/tasks/denoising/methods/magic.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717

1818

19-
def _magic(adata, solver, normtype="sqrt", **kwargs):
19+
def _magic(adata, solver, normtype="sqrt", reverse_norm_order=False, **kwargs):
2020
from magic import MAGIC
2121

2222
if normtype == "sqrt":
@@ -28,11 +28,19 @@ def _magic(adata, solver, normtype="sqrt", **kwargs):
2828
else:
2929
raise NotImplementedError
3030

31-
X, libsize = scprep.normalize.library_size_normalize(
32-
adata.obsm["train"], rescale=1, return_library_size=True
33-
)
31+
X = adata.obsm["train"]
32+
if reverse_norm_order:
33+
# inexplicably, this sometimes performs better
34+
X = scprep.utils.matrix_transform(X, norm_fn)
35+
X, libsize = scprep.normalize.library_size_normalize(
36+
X, rescale=1, return_library_size=True
37+
)
38+
else:
39+
X, libsize = scprep.normalize.library_size_normalize(
40+
X, rescale=1, return_library_size=True
41+
)
42+
X = scprep.utils.matrix_transform(X, norm_fn)
3443

35-
X = scprep.utils.matrix_transform(X, norm_fn)
3644
Y = MAGIC(solver=solver, **kwargs, verbose=False).fit_transform(
3745
X, genes="all_genes"
3846
)
@@ -52,13 +60,27 @@ def magic(adata, test=False):
5260
return _magic(adata, solver="exact", normtype="sqrt")
5361

5462

63+
@_magic_method(
64+
method_name="MAGIC (reversed normalization)",
65+
)
66+
def magic_reverse_norm(adata, test=False):
67+
return _magic(adata, solver="exact", normtype="sqrt", reverse_norm_order=True)
68+
69+
5570
@_magic_method(
5671
method_name="MAGIC (approximate)",
5772
)
5873
def magic_approx(adata, test=False):
5974
return _magic(adata, solver="approximate", normtype="sqrt")
6075

6176

77+
@_magic_method(
78+
method_name="MAGIC (approximate, reversed normalization)",
79+
)
80+
def magic_approx_reverse_norm(adata, test=False):
81+
return _magic(adata, solver="approximate", normtype="sqrt", reverse_norm_order=True)
82+
83+
6284
@method(
6385
method_name="KNN smoothing",
6486
paper_name="KNN Smoothing (baseline)",

0 commit comments

Comments
 (0)