Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pytometry/tools/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from scipy import interpolate


def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True):
def normalize_arcsinh(adata: AnnData, cofactor=5, inplace: bool = True):
"""Inverse hyperbolic sine transformation.

Args:
adata : AnnData object
cofactor (float): all values are divided by this
cofactor (float or pandas.Series): all values are divided by this
factor before arcsinh transformation recommended value for
cyTOF data is 5 and for flow data 150.
inplace (bool, optional): Return a copy instead of writing to adata.
Expand All @@ -20,7 +20,27 @@ def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True):
adata object
"""
adata = adata if inplace else adata.copy()
adata.X = np.arcsinh(adata.X / cofactor)
# check inputs

if hasattr(cofactor, "__len__") and (not isinstance(cofactor, str)):
# perform trafo per marker
len_param = len(cofactor)
if len_param == adata.n_vars:
for idx, marker in enumerate(adata.var_names):
# get correct row
row_idx = cofactor.index == marker
cofactor_tmp = cofactor[row_idx][0]
# transform adata values using the biexponential function
adata.X[:, idx] = np.arcsinh(adata.X[:, idx] / cofactor_tmp)
else:
print(
"One of the parameters has the incorrect length. Return"
" adata without normalising."
)
else: # integer values do not have len attribute
# use one cofactor on the entire dataset
adata.X = np.arcsinh(adata.X / cofactor)

return None if inplace else adata


Expand Down
8 changes: 8 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def test_normalize_arcsinh2():
assert isinstance(adata2, anndata._core.anndata.AnnData)


def test_normalize_arcsinh3():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
cofactor = pandas.Series(numpy.repeat(1, adata.n_vars), index=adata.var_names)
adata2 = normalize_arcsinh(adata, cofactor=cofactor, inplace=False)
assert isinstance(adata2, anndata._core.anndata.AnnData)


def test_normalize_biexp():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
Expand Down