Skip to content

Commit

Permalink
Separated quantile thresholding and median filtering in test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdrose committed Feb 5, 2024
1 parent 94a2203 commit 7561423
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions metaspace_converter/tests/colocalization_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Tuple

import numpy as np
import pandas as pd
Expand All @@ -15,10 +15,11 @@


@pytest.fixture
def adata_dummy(request: "_pytest.fixtures.SubRequest") -> AnnData:
def adata_dummy(request: "_pytest.fixtures.SubRequest") -> Tuple[AnnData, float]:
num_ions = request.param.get("num_ions", 0)
height = request.param.get("height", 0)
width = request.param.get("width", 0)
quantile_threshold = request.param.get("quantile_threshold", 0)
shape = (num_ions, height, width)
# Create a stack of ion images where every pixel has a different value
ion_images_stack = np.arange(np.prod(shape)).reshape(shape)
Expand All @@ -32,70 +33,70 @@ def adata_dummy(request: "_pytest.fixtures.SubRequest") -> AnnData:
obs=obs,
uns={METASPACE_KEY: {"image_size": {X: width, Y: height}}},
)
return adata
return adata, quantile_threshold


@pytest.mark.parametrize(
"adata_dummy",
[
dict(num_ions=5, height=10, width=10),
dict(num_ions=0, height=10, width=10),
dict(num_ions=5, height=3, width=4),
dict(num_ions=5, height=10, width=10, quantile_threshold=0),
dict(num_ions=0, height=10, width=10, quantile_threshold=0),
dict(num_ions=5, height=3, width=4, quantile_threshold=0),
dict(num_ions=5, height=3, width=4, quantile_threshold=0.5)
],
indirect=["adata_dummy"],
)
def test_colocml_preprocessing(adata_dummy):

COLOCML_LAYER = "coloc_ml_preprocessing"

adata = adata_dummy
adata, quantile_threshold = adata_dummy

if adata.X.shape[1] == 0:
with pytest.raises(ValueError) as e_info:
coloc_ml_preprocessing(
adata, median_filter_size=(3, 3), quantile_threshold=0, layer=COLOCML_LAYER
adata, median_filter_size=(3, 3), quantile_threshold=quantile_threshold,
layer=COLOCML_LAYER
)
else:

# Quantile thresholding and median filtering is tested separately
if quantile_threshold == 0:

# Test median filtering
coloc_ml_preprocessing(
adata, median_filter_size=(3, 3), quantile_threshold=0, layer=COLOCML_LAYER
)

actual = anndata_to_image_array(adata)

# Layer exists
assert COLOCML_LAYER in adata.layers.keys()
coloc_ml_preprocessing(
adata, median_filter_size=(3, 3), quantile_threshold=quantile_threshold,
layer=COLOCML_LAYER
)

# Layer sizes match
assert adata.X.shape == adata.layers[COLOCML_LAYER].shape
actual = anndata_to_image_array(adata)

# median filter
# Check that preprocessing was done correctly.
# Probe a single pixel of the resulting array and compute the expected median filter.
x = 1
y = 1
width = adata.uns[METASPACE_KEY]["image_size"][X]
expected_pixel_features = np.median(actual[:, y - 1:y + 2, x - 1:x + 2], axis=[1, 2])
actual_pixel_features = adata.layers[COLOCML_LAYER][y * width + x, :]
np.testing.assert_allclose(actual_pixel_features, expected_pixel_features)
# Layer exists
assert COLOCML_LAYER in adata.layers.keys()

# expected_preprocessing = np.median(actual[0][:3, :3])
# observed = adata.layers[COLOCML_LAYER][adata.uns[METASPACE_KEY]["image_size"][X] + 1, 0]
# assert observed == expected_preprocessing
# Layer sizes match
assert adata.X.shape == adata.layers[COLOCML_LAYER].shape

# expected_preprocessing = np.median(actual[1][:3, :3])
# observed = adata.layers[COLOCML_LAYER][adata.uns[METASPACE_KEY]["image_size"][X] + 1, 1]
# assert observed == expected_preprocessing
# median filter
# Probe a single pixel of the resulting array and compute the expected median filter.
x = 1
y = 1
width = adata.uns[METASPACE_KEY]["image_size"][X]
print(actual[:, y - 1:y + 2, x - 1:x + 2])
expected_pixel_features = np.median(actual[:, y - 1:y + 2, x - 1:x + 2], axis=[1, 2])
actual_pixel_features = adata.layers[COLOCML_LAYER][y * width + x, :]
np.testing.assert_allclose(actual_pixel_features, expected_pixel_features)

# Quantile thresholding
else:
coloc_ml_preprocessing(
adata, median_filter_size=(3, 3), quantile_threshold=quantile_threshold,
layer=COLOCML_LAYER
)
quantile_value = quantile_threshold * adata.X.shape[0]
actual_thresholded = np.sum(adata.layers[COLOCML_LAYER] == 0, axis=0)
assert all(actual_thresholded <= quantile_value)

coloc_ml_preprocessing(
adata, median_filter_size=(3, 3), quantile_threshold=0.5, layer=COLOCML_LAYER
)

assert all(np.sum(adata.layers[COLOCML_LAYER] == 0, axis=0) <= 0.5 * adata.X.shape[0])



Expand Down

0 comments on commit 7561423

Please sign in to comment.