Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ValueError on sparse array #44

Merged
merged 1 commit into from
Jun 7, 2024
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
6 changes: 6 additions & 0 deletions src/cellcharter/tl/_gmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import anndata as ad
import numpy as np
import pandas as pd
import scipy.sparse as sps
import torch
from lightkit.data import DataLoader, TensorLike, collate_tensor, dataset_from_tensors
from pycave import set_logging_level
Expand Down Expand Up @@ -103,6 +104,11 @@ def fit(self, data: TensorLike) -> GaussianMixture:
----------
The fitted Gaussian mixture.
"""
if sps.issparse(data):
raise ValueError(
"Sparse data is not supported. You may have forgotten to reduce the dimensionality of the data. Otherwise, please convert the data to a dense format."
)

if self.init_strategy == "sklearn":
if self.batch_size is None:
kmeans = KMeans(n_clusters=self.num_components, random_state=self.random_state, n_init=1)
Expand Down
22 changes: 22 additions & 0 deletions tests/tools/test_gmm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
import scipy.sparse as sps
import squidpy as sq

import cellcharter as cc


class TestCluster:
@pytest.mark.parametrize("dataset_name", ["mibitof"])
def test_sparse(self, dataset_name: str):
download_dataset = getattr(sq.datasets, dataset_name)
adata = download_dataset()
adata.X = sps.csr_matrix(adata.X)

sq.gr.spatial_neighbors(adata, coord_type="generic", delaunay=True)
cc.gr.remove_long_links(adata)

gmm = cc.tl.Cluster(n_clusters=(10))

# Check if fit raises a ValueError
with pytest.raises(ValueError):
gmm.fit(adata, use_rep=None)
Loading