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
2 changes: 1 addition & 1 deletion pytometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

"""

__version__ = "0.1.0" # denote a pre-release for 0.1.0 with 0.1a1
__version__ = "0.1.1" # denote a pre-release for 0.1.0 with 0.1a1

from . import plotting as pl
from . import preprocessing as pp
Expand Down
8 changes: 7 additions & 1 deletion pytometry/preprocessing/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
from ._process_data import compensate, create_comp_mat, find_indexes, split_signal
from ._process_data import (
_dummy_spillover,
compensate,
create_comp_mat,
find_indexes,
split_signal,
)
23 changes: 20 additions & 3 deletions pytometry/preprocessing/_process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def create_comp_mat(spillmat: pd.DataFrame, relevant_data: str = "") -> pd.DataF
Returns:
pd.DataFrame of the compensation matrix.
"""
comp_mat = np.linalg.inv(spillmat)

if relevant_data == "":
comp_mat = np.linalg.inv(spillmat)
compens = pd.DataFrame(comp_mat, columns=list(spillmat.columns))
else:
comp_mat = np.linalg.inv(spillmat)
compens = pd.DataFrame(comp_mat, columns=relevant_data)

return compens
Expand Down Expand Up @@ -135,7 +135,7 @@ def compensate(
f" '{matrix_type}'."
)
# To Do: add checks that this input is correct
if adata.uns["meta"]["spill"] is not None:
elif adata.uns["meta"]["spill"] is not None:
compens = adata.uns["meta"]["spill"]
else:
raise KeyError(f"Did not find .uns['meta']['spill'] nor '{comp_matrix}'.")
Expand Down Expand Up @@ -244,3 +244,20 @@ def split_signal(
adata._inplace_subset_var(indx)

return None if inplace else adata


# create test compensation matrix
def _dummy_spillover(n_rows=10, row_names=None) -> pd.DataFrame:
"""Create dummy spillover matrix for testing.

Args:
n_rows (int, optional): Number of rows and columns_. Defaults to 10.
row_names (index or array-like, optional): Index to use for the resulting
dataframe. Also used as column names. Defaults to None.

Returns:
pd.DataFrame: A dummy spillover matrix with 2's on the diagonal
"""
tmp_mat = np.diag(np.ones(n_rows) * 2)
dummy_spill = pd.DataFrame(data=tmp_mat, index=row_names, columns=row_names)
return dummy_spill
2 changes: 1 addition & 1 deletion pytometry/tools/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True):
"""
adata = adata if inplace else adata.copy()
adata.X = np.arcsinh(adata.X / cofactor)
return adata if inplace else None
return None if inplace else adata


def normalize_logicle(
Expand Down
92 changes: 92 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,102 @@
import anndata
import numpy
import pandas
import readfcs

from pytometry.preprocessing import _dummy_spillover, compensate, create_comp_mat
from pytometry.read_write import read_fcs
from pytometry.tools import normalize_arcsinh, normalize_biExp, normalize_logicle


# test read function
def test_read_fcs():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
assert isinstance(adata, anndata._core.anndata.AnnData)


# test compensate
def test_compensate():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
compensate(adata)
assert isinstance(adata, anndata._core.anndata.AnnData)


def test_compensate_inplace():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
adata2 = compensate(adata, inplace=False)
assert isinstance(adata2, anndata._core.anndata.AnnData)


def test_spill():
n_rows = 12
spillmat = _dummy_spillover(n_rows=n_rows)
assert isinstance(spillmat, pandas.DataFrame)
assert spillmat.shape == (n_rows, n_rows)


def test_create_comp_mat():
n_rows = 5
spillmat = _dummy_spillover(n_rows=n_rows)
comp_mat = create_comp_mat(spillmat)
assert numpy.sum(comp_mat.values) == n_rows * 0.5


# test custom dummy compensation
def test_compensate2():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
# use extra compensation matrix
spillmat = _dummy_spillover(
n_rows=adata.uns["meta"]["spill"].shape[0],
row_names=adata.uns["meta"]["spill"].index,
)
adata2 = compensate(
adata, comp_matrix=spillmat, matrix_type="spillover", inplace=False
)
assert adata2.X.sum() != adata.X.sum()


# test return types for normalization
def test_normalize_arcsinh():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
normalize_arcsinh(adata, cofactor=1, inplace=True)
assert isinstance(adata, anndata._core.anndata.AnnData)


def test_normalize_arcsinh2():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
adata2 = normalize_arcsinh(adata, cofactor=1, inplace=False)
assert isinstance(adata2, anndata._core.anndata.AnnData)


def test_normalize_biexp():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
normalize_biExp(adata, inplace=True)
assert isinstance(adata, anndata._core.anndata.AnnData)


def test_normalize_biexp2():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
adata2 = normalize_biExp(adata, inplace=False)
assert isinstance(adata2, anndata._core.anndata.AnnData)


def test_normalize_logicle():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
normalize_logicle(adata, inplace=True)
assert isinstance(adata, anndata._core.anndata.AnnData)


def test_normalize_logicle2():
path_data = readfcs.datasets.example()
adata = read_fcs(path_data)
adata2 = normalize_logicle(adata, inplace=False)
assert isinstance(adata2, anndata._core.anndata.AnnData)