Skip to content

Commit

Permalink
renaming of coloc_ml_processing, testing, and moving colocs to varp
Browse files Browse the repository at this point in the history
  • Loading branch information
tdrose committed Feb 5, 2024
1 parent a12792f commit 1de5c51
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
10 changes: 5 additions & 5 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ can be executed with our package:
adata = metaspace_to_anndata(dataset_id="2023-11-14_21h58m39s", fdr=0.1)

# Perform median filtering and quantile thresholding
# The processed data is saved as a layer `adata.layers["colocml_preprocessing"]`
# The processed data is saved as a layer `adata.layers["coloc_ml_preprocessing"]`
# It has the same dimensions as `adata.X`
colocalization.colocML_preprocessing(adata, layer="colocml_preprocessing")
colocalization.coloc_ml_preprocessing(adata, layer="colocml_preprocessing")

# Compute the pairwise colocalization metrix between all ion images
# As an input, the processed data from `adata.layers["colocml_preprocessing"]` is used
# The colocalization matrix is saved in `adata.uns["colocalization"]`
# Compute the pairwise colocalization matrix between all ion images
# As an input, the processed data from `adata.layers["coloc_ml_preprocessing"]` is used
# The colocalization matrix is saved in `adata.varp["colocalization"]`
colocalization.colocalization(adata, layer="colocml_preprocessing")


Expand Down
25 changes: 15 additions & 10 deletions metaspace_converter/colocalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from metaspace_converter.constants import COLOCALIZATION


def colocML_preprocessing(
def coloc_ml_preprocessing(
adata: AnnData,
layer: Optional[str] = "colocml_preprocessing",
layer: Optional[str] = "coloc_ml_preprocessing",
median_filter_size: Tuple[int, int] = (3, 3),
quantile_threshold: float = 0.5,
):
Expand Down Expand Up @@ -69,36 +69,41 @@ def colocML_preprocessing(
adata.layers[layer] = imarray.transpose()


def colocalization(adata: AnnData, layer: Optional[str] = "colocml_preprocessing"):
def colocalization(adata: AnnData, layer: Optional[str] = "coloc_ml_preprocessing"):
"""
Colocalization of ion images using the cosine similarity metric.
In combination with the ``colocML_preprocessing`` function, this metric performed best in the
colocML publication (https://doi.org/10.1093/bioinformatics/btaa085).
It is recommended to call the the ``colocML_preprocessing`` function beforehand.
It is recommended to call the the ``coloc_ml_preprocessing`` function beforehand.
Args:
adata: An AnnData object.
layer: Key for ``adata.layer`` from which the ionimage_data for preprocessing taken.
If ``None``, ``adata.X`` is used. ``colocML_preprocessing`` will save the preprocessed
data per default in ``adata.layer['colocml_preprocessing']``.
If ``None``, ``adata.X`` is used. ``coloc_ml_preprocessing`` will save the preprocessed
data per default in ``adata.layer['coloc_ml_preprocessing']``.
Returns:
None. The processed data is saved in ``adata.uns['colocalization']``.
None. The processed data is saved in ``adata.varp['colocalization']``.
Raises:
ValueError: If layer is not found in adata.layers.
"""

# Select data
if layer == None or layer not in adata.layers.keys():
if layer is None:
data = np.array(adata.X).transpose()
else:
elif layer in adata.layers.keys():
data = np.array(adata.layers[layer]).transpose()
else:
raise ValueError(f"Layer `{layer}` not found in adata.layers.")

# Compute colocalization
coloc = _pairwise_cosine_similarity(data)

# Save colocalization
adata.uns[COLOCALIZATION] = coloc
adata.varp[COLOCALIZATION] = coloc


def _pairwise_cosine_similarity(data: np.ndarray) -> np.ndarray:
Expand Down
28 changes: 15 additions & 13 deletions metaspace_converter/tests/colocalization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import _pytest.fixtures

from metaspace_converter import anndata_to_image_array
from metaspace_converter.colocalization import colocalization, colocML_preprocessing
from metaspace_converter.colocalization import colocalization, coloc_ml_preprocessing
from metaspace_converter.constants import COL, COLOCALIZATION, METASPACE_KEY, X, Y
from metaspace_converter.to_anndata import all_image_pixel_coordinates

Expand Down Expand Up @@ -46,26 +46,32 @@ def adata_dummy(request: "_pytest.fixtures.SubRequest") -> AnnData:
)
def test_colocml_preprocessing(adata_dummy):

COLOCML_LAYER = "colocml_preprocessing"
COLOCML_LAYER = "coloc_ml_preprocessing"

adata = adata_dummy

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


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

actual = anndata_to_image_array(adata)

# median filter
# Layer exists
assert COLOCML_LAYER in adata.layers.keys()

# Layer sizes match
assert adata.X.shape == adata.layers[COLOCML_LAYER].shape

# median filter
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
Expand All @@ -77,17 +83,13 @@ def test_colocml_preprocessing(adata_dummy):
# Quantile thresholding
adata.X[0].reshape(-1)

colocML_preprocessing(
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])

# Layer exists
assert COLOCML_LAYER in adata.layers.keys()

# Layer sizes match
assert adata.X.shape == adata.layers[COLOCML_LAYER].shape



def test_colocalization():
Expand All @@ -100,8 +102,8 @@ def test_colocalization():

colocalization(adata, layer=None)

assert COLOCALIZATION in adata.uns.keys()
assert COLOCALIZATION in adata.varp.keys()

coloc = adata.uns[COLOCALIZATION]
coloc = adata.varp[COLOCALIZATION]

assert np.all(coloc == expected)

0 comments on commit 1de5c51

Please sign in to comment.