Skip to content

Commit

Permalink
Got the tests to pass, more or less.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Dec 13, 2024
1 parent 462b05d commit bdbb9a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/singler/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

def _factorize(x: Sequence) -> Tuple[list, numpy.ndarray]:
f = biocutils.Factor.from_sequence(x, sort_levels=False)
print(f)
return f.levels, numpy.array(f.codes, dtype=numpy.uint32)


Expand Down
55 changes: 26 additions & 29 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
_stable_union,
_clean_matrix,
)
import numpy as np
from mattress import tatamize
from summarizedexperiment import SummarizedExperiment
import numpy
import summarizedexperiment


def test_factorize():
Expand All @@ -19,10 +18,6 @@ def test_factorize():
assert list(lev) == ["C", "D", "A", "B"]
assert (ind == [0, 1, 2, 3, 0, 2]).all()

# Handles None-ness.
lev, ind = _factorize([1, None, 5, None, 3, None])
assert list(lev) == ["1", "5", "3"]
assert (ind == [0, -1, 1, -1, 2, -1]).all()

def test_intersect():
# Preserves the order in the first argument.
Expand Down Expand Up @@ -82,42 +77,44 @@ def test_union():


def test_clean_matrix():
out = np.random.rand(20, 10)
out = numpy.random.rand(20, 10)
features = ["FEATURE_" + str(i) for i in range(out.shape[0])]

ptr, feats = _clean_matrix(
out2, feats = _clean_matrix(
out, features, assay_type=None, check_missing=True, num_threads=1
)
assert feats == features
assert (ptr.row(1) == out[1, :]).all()
assert (ptr.column(2) == out[:, 2]).all()
assert (out2[1, :] == out[1, :]).all()
assert (out2[:, 2] == out[:, 2]).all()

ptr, feats = _clean_matrix(
out2, feats = _clean_matrix(
out, features, assay_type=None, check_missing=False, num_threads=1
)
assert feats == features
assert (ptr.row(3) == out[3, :]).all()
assert (ptr.column(4) == out[:, 4]).all()
assert (out2[3, :] == out[3, :]).all()
assert (out2[:, 4] == out[:, 4]).all()

tmp = np.copy(out)
tmp[0, 5] = np.nan
ptr, feats = _clean_matrix(
tmp = numpy.copy(out)
tmp[0, 5] = numpy.nan
out2, feats = _clean_matrix(
tmp, features, assay_type=None, check_missing=True, num_threads=1
)
assert feats == features[1:]
assert (ptr.row(2) == out[3, :]).all()
assert (ptr.column(4) == out[1:, 4]).all()

ptr = tatamize(out)
ptr2, feats = _clean_matrix(
ptr, features, assay_type=None, check_missing=True, num_threads=1
)
assert ptr2.ptr == ptr.ptr
assert (out2[2, :] == out[3, :]).all()
assert (out2[:, 4] == out[1:, 4]).all()

se = SummarizedExperiment({"counts": out})
ptr, feats = _clean_matrix(
se = summarizedexperiment.SummarizedExperiment({"counts": out})
out2, feats = _clean_matrix(
se, features, assay_type="counts", check_missing=True, num_threads=1
)
assert feats == features
assert (ptr.row(1) == out[1, :]).all()
assert (ptr.column(2) == out[:, 2]).all()
assert (out2[1, :] == out[1, :]).all()
assert (out2[:, 2] == out[:, 2]).all()

se2 = se.set_row_names(features)
out2, feats = _clean_matrix(
se2, None, assay_type="counts", check_missing=True, num_threads=1
)
assert feats.as_list() == features
assert (out2[1, :] == out[1, :]).all()
assert (out2[:, 2] == out[:, 2]).all()

0 comments on commit bdbb9a8

Please sign in to comment.