From 1de5c51ff0e919acafe262b41cf258f0f14d22bd Mon Sep 17 00:00:00 2001 From: tdrose Date: Mon, 5 Feb 2024 10:56:00 +0100 Subject: [PATCH] renaming of coloc_ml_processing, testing, and moving colocs to varp --- docs/examples.rst | 10 +++---- metaspace_converter/colocalization.py | 25 ++++++++++------- .../tests/colocalization_test.py | 28 ++++++++++--------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index b686ebe..d07920e 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -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") diff --git a/metaspace_converter/colocalization.py b/metaspace_converter/colocalization.py index 56a7c9a..606139f 100644 --- a/metaspace_converter/colocalization.py +++ b/metaspace_converter/colocalization.py @@ -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, ): @@ -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: diff --git a/metaspace_converter/tests/colocalization_test.py b/metaspace_converter/tests/colocalization_test.py index 408cb8a..238bd75 100644 --- a/metaspace_converter/tests/colocalization_test.py +++ b/metaspace_converter/tests/colocalization_test.py @@ -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 @@ -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 @@ -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(): @@ -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)