cellmapper 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. {cellmapper-0.2.0 → cellmapper-0.2.1}/CHANGELOG.md +9 -0
  2. {cellmapper-0.2.0 → cellmapper-0.2.1}/PKG-INFO +3 -1
  3. {cellmapper-0.2.0 → cellmapper-0.2.1}/pyproject.toml +2 -0
  4. cellmapper-0.2.1/src/cellmapper/_docs.py +109 -0
  5. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/check.py +5 -2
  6. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/constants.py +2 -2
  7. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/_knn_backend.py +42 -8
  8. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/cellmapper.py +93 -125
  9. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/evaluate.py +29 -27
  10. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/kernel.py +31 -52
  11. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/mapping_operator.py +12 -14
  12. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/neighbors.py +11 -17
  13. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/conftest.py +56 -3
  14. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/model/test_evaluate.py +6 -6
  15. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/model/test_kernel.py +18 -3
  16. cellmapper-0.2.1/tests/model/test_mapping_operator.py +272 -0
  17. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/model/test_query_to_reference_mapping.py +23 -21
  18. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/model/test_self_mapping.py +17 -17
  19. {cellmapper-0.2.0 → cellmapper-0.2.1}/.codecov.yaml +0 -0
  20. {cellmapper-0.2.0 → cellmapper-0.2.1}/.cruft.json +0 -0
  21. {cellmapper-0.2.0 → cellmapper-0.2.1}/.editorconfig +0 -0
  22. {cellmapper-0.2.0 → cellmapper-0.2.1}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  23. {cellmapper-0.2.0 → cellmapper-0.2.1}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  24. {cellmapper-0.2.0 → cellmapper-0.2.1}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
  25. {cellmapper-0.2.0 → cellmapper-0.2.1}/.github/workflows/build.yaml +0 -0
  26. {cellmapper-0.2.0 → cellmapper-0.2.1}/.github/workflows/release.yaml +0 -0
  27. {cellmapper-0.2.0 → cellmapper-0.2.1}/.github/workflows/test.yaml +0 -0
  28. {cellmapper-0.2.0 → cellmapper-0.2.1}/.gitignore +0 -0
  29. {cellmapper-0.2.0 → cellmapper-0.2.1}/.pre-commit-config.yaml +0 -0
  30. {cellmapper-0.2.0 → cellmapper-0.2.1}/.readthedocs.yaml +0 -0
  31. {cellmapper-0.2.0 → cellmapper-0.2.1}/.vscode/extensions.json +0 -0
  32. {cellmapper-0.2.0 → cellmapper-0.2.1}/.vscode/launch.json +0 -0
  33. {cellmapper-0.2.0 → cellmapper-0.2.1}/.vscode/settings.json +0 -0
  34. {cellmapper-0.2.0 → cellmapper-0.2.1}/LICENSE +0 -0
  35. {cellmapper-0.2.0 → cellmapper-0.2.1}/README.md +0 -0
  36. {cellmapper-0.2.0 → cellmapper-0.2.1}/biome.jsonc +0 -0
  37. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/_static/.gitkeep +0 -0
  38. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/_static/css/custom.css +0 -0
  39. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/_static/images/cellmapper_logo_dark.svg +0 -0
  40. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/_static/images/cellmapper_logo_light.svg +0 -0
  41. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/_templates/.gitkeep +0 -0
  42. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/_templates/autosummary/class.rst +0 -0
  43. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/api.md +0 -0
  44. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/changelog.md +0 -0
  45. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/conf.py +0 -0
  46. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/contributing.md +0 -0
  47. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/extensions/typed_returns.py +0 -0
  48. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/index.md +0 -0
  49. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/notebooks/tutorials/data_denoising.ipynb +0 -0
  50. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/notebooks/tutorials/index.rst +0 -0
  51. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/notebooks/tutorials/query_to_reference_mapping.ipynb +0 -0
  52. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/notebooks/tutorials/spatial_mapping.ipynb +0 -0
  53. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/notebooks/tutorials/spatial_smoothing.ipynb +0 -0
  54. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/references.bib +0 -0
  55. {cellmapper-0.2.0 → cellmapper-0.2.1}/docs/references.md +0 -0
  56. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/__init__.py +0 -0
  57. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/logging.py +0 -0
  58. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/model/embedding.py +0 -0
  59. {cellmapper-0.2.0 → cellmapper-0.2.1}/src/cellmapper/utils.py +0 -0
  60. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/data/precomputed_leiden.csv +0 -0
  61. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/model/test_embedding.py +0 -0
  62. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/model/test_neighbors.py +0 -0
  63. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/test_basic.py +0 -0
  64. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/test_check.py +0 -0
  65. {cellmapper-0.2.0 → cellmapper-0.2.1}/tests/test_utils.py +0 -0
@@ -10,6 +10,15 @@ and this project adheres to [Semantic Versioning][].
10
10
 
11
11
  ## [Unreleased]
12
12
 
13
+ ## [v0.2.1]
14
+ ### Changed
15
+ - Move some duplicated docstrings into a central _docs.py file {pr}`41`
16
+
17
+ ### Added
18
+ - Added some tests for edge cases in the `MappingOperator` class {pr}`41`
19
+ - Treat faiss-cpu and faiss-gpu separately {pr}`41`
20
+
21
+
13
22
  ## [v0.2.0]
14
23
 
15
24
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cellmapper
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: k-NN-based mapping of cells across representations
5
5
  Project-URL: Documentation, https://cellmapper.readthedocs.io/
6
6
  Project-URL: Homepage, https://github.com/quadbio/cellmapper
@@ -36,6 +36,7 @@ Classifier: Programming Language :: Python :: 3.12
36
36
  Classifier: Programming Language :: Python :: 3.13
37
37
  Requires-Python: >=3.10
38
38
  Requires-Dist: anndata>=0.11
39
+ Requires-Dist: docrep
39
40
  Requires-Dist: numpy
40
41
  Requires-Dist: packaging
41
42
  Requires-Dist: pandas
@@ -63,6 +64,7 @@ Requires-Dist: sphinxcontrib-bibtex>=1; extra == 'doc'
63
64
  Requires-Dist: sphinxext-opengraph; extra == 'doc'
64
65
  Provides-Extra: test
65
66
  Requires-Dist: coverage; extra == 'test'
67
+ Requires-Dist: faiss-cpu; extra == 'test'
66
68
  Requires-Dist: pytest; extra == 'test'
67
69
  Requires-Dist: squidpy>=1.6; extra == 'test'
68
70
  Provides-Extra: tutorials
@@ -24,6 +24,7 @@ classifiers = [
24
24
  dynamic = [ "version" ]
25
25
  dependencies = [
26
26
  "anndata>=0.11",
27
+ "docrep",
27
28
  "numpy",
28
29
  "packaging",
29
30
  "pandas",
@@ -56,6 +57,7 @@ optional-dependencies.doc = [
56
57
  ]
57
58
  optional-dependencies.test = [
58
59
  "coverage",
60
+ "faiss-cpu",
59
61
  "pytest",
60
62
  "squidpy>=1.6",
61
63
  ]
@@ -0,0 +1,109 @@
1
+ """Shared documentation for cellmapper."""
2
+
3
+ from docrep import DocstringProcessor
4
+
5
+ __all__ = ["d"]
6
+
7
+ _t = """\
8
+ t
9
+ Number of diffusion time steps. This parameter controls the degree of
10
+ smoothing applied by the diffusion operator. Larger values lead to more
11
+ smoothing."""
12
+
13
+ _diffusion_method = """\
14
+ diffusion_method
15
+ Method for computing powers of the mapping matrix (only valid in self-mapping mode). Options are "iterative" for
16
+ iterative matrix multiplication (inspired by MAGIC :cite:`van2018recovering`) and "spectral" for
17
+ eigendecomposition-based approach. """
18
+
19
+ _prediction_postfix = """\
20
+ prediction_postfix
21
+ Postfix to add to mapped variables to identify them as predictions."""
22
+
23
+ _symmetrize = """\
24
+ symmetrize
25
+ If True, create a symmetrize connectivity matrix. Only valid for square matrices (self-mapping).
26
+ If None (default), uses True for self-mapping and False for cross-mapping."""
27
+
28
+ _self_edges = """\
29
+ self_edges
30
+ Control self-edges (diagonal entries) for square matrices (self-mapping).
31
+ If None (default), uses False for self-mapping (scanpy style) and None for cross-mapping.
32
+ This controls whether or not the kernel used to compute the connectivities is supplied with self-edges.
33
+ It does not determine whether the final connectivity matrix has self edges. For example, the `umap`
34
+ kernel expectes self-edges, but does not produce them in the final connectivity matrix."""
35
+
36
+ _knn_method = """\
37
+ knn_method
38
+ Method for computing k-nearest neighbors. Options include:
39
+ - "sklearn": Scikit-learn's NearestNeighbors. See https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html
40
+ - "pynndescent": Pynndescent's approximate nearest neighbors. See https://pynndescent.readthedocs.io/en/latest/
41
+ - "rapids": RAPIDS cuML's NearestNeighbors (GPU). See https://docs.rapids.ai/api/cuml/stable/api.html#cuml.neighbors.NearestNeighbors
42
+ - "faiss-cpu": Facebook AI Similarity Search (FAISS) on CPU. See https://faiss.ai/
43
+ - "faiss-gpu": Facebook AI Similarity Search (FAISS) on GPU. See https://faiss.ai/
44
+
45
+
46
+ All methods return exactly `n_neighbors` neighbors, including the reference cell itself (in self-mapping mode)."""
47
+
48
+ _only_yx = """\
49
+ only_yx
50
+ If True, only compute the xy neighbors. In self-mapping mode, this is
51
+ automatically set to True for efficiency since all neighbor matrices contain the same information.
52
+ This is faster, but not suitable for Jaccard or HNOCA methods in cross-mapping mode."""
53
+
54
+ _kernel_method = """\
55
+ kernel_method
56
+ Method to use for computing the mapping matrix. Options include:
57
+
58
+ - "jaccard": Jaccard similarity. Inspired by GLUE :cite:`cao2022multi`
59
+ - "gauss": Gaussian kernel with (global) bandwith equal to the mean distance.
60
+ - "scarches": scArches kernel. Inspired by scArches :cite:`lotfollahi2022mapping`
61
+ - "inverse_distance": Inverse distance kernel.
62
+ - "random": Random kernel, useful for testing.
63
+ - "hnoca": HNOCA kernel. Inspired by HNOCA-tools :cite:`he2024integrated`
64
+ - "equal": All neighbors are equally weighted (1/n_neighbors).
65
+ - "umap": UMAP fuzzy simplicial set connectivities. Only available for self-mapping with true k-NN graphs."""
66
+
67
+ _comparison_method = """\
68
+ comparison_method
69
+ Method to use for comparing the mapping results. Options include:
70
+
71
+ - "pearson": Pearson correlation coefficient.
72
+ - "spearman": Spearman rank correlation coefficient.
73
+ - "js": Jenson-Shanon divergence.
74
+ - "rmse": Root Mean Square Error."""
75
+
76
+ _layer_key = """\
77
+ layer_key
78
+ Key in `self.query.layers` to use as the original expression. Use "X" to use `self.query.X`."""
79
+
80
+
81
+ _n_neighbors = """\
82
+ n_neighbors
83
+ Number of nearest neighbors. This parameter controls the sparsity of the connectivity matrix. """
84
+
85
+ _use_rep = """\
86
+ use_rep
87
+ Data representation based on which to find nearest neighbors. If None, a fallback representation will be
88
+ computed automatically. """
89
+
90
+ _knn_dist_metric = """\
91
+ knn_dist_metric
92
+ Distance metric to use for nearest neighbors. See the knn algorithms documentation for details. """
93
+
94
+
95
+ d = DocstringProcessor(
96
+ t=_t,
97
+ diffusion_method=_diffusion_method,
98
+ prediction_postfix=_prediction_postfix,
99
+ symmetrize=_symmetrize,
100
+ self_edges=_self_edges,
101
+ knn_method=_knn_method,
102
+ only_yx=_only_yx,
103
+ kernel_method=_kernel_method,
104
+ comparison_method=_comparison_method,
105
+ layer_key=_layer_key,
106
+ n_neighbors=_n_neighbors,
107
+ use_rep=_use_rep,
108
+ knn_dist_metric=_knn_dist_metric,
109
+ )
@@ -61,7 +61,9 @@ INSTALL_HINTS = types.SimpleNamespace(
61
61
  "https://docs.rapids.ai/install/.",
62
62
  cupy="To speed up k-NN search on GPU, you may install cuPy following the guide from "
63
63
  "https://docs.rapids.ai/install/.",
64
- faiss="To speed up k-NN search on GPU, you may install faiss following the guide from "
64
+ faiss_cpu="To speed up k-NN search on CPU, you may install faiss following the guide from "
65
+ "https://github.com/facebookresearch/faiss/blob/main/INSTALL.md",
66
+ faiss_gpu="To speed up k-NN search on GPU, you may install faiss following the guide from "
65
67
  "https://github.com/facebookresearch/faiss/blob/main/INSTALL.md",
66
68
  pynndescent="To use fast approximate k-NN search, install pynndescent: pip install pynndescent",
67
69
  )
@@ -69,7 +71,8 @@ INSTALL_HINTS = types.SimpleNamespace(
69
71
  CHECKERS = {
70
72
  "cuml": Checker("cuml", vmin=None, install_hint=INSTALL_HINTS.cuml),
71
73
  "cupy": Checker("cupy", vmin=None, install_hint=INSTALL_HINTS.cupy),
72
- "faiss": Checker("faiss", package_name="faiss", vmin="1.7.0", install_hint=INSTALL_HINTS.faiss),
74
+ "faiss-cpu": Checker("faiss", package_name="faiss-cpu", vmin="1.7.0", install_hint=INSTALL_HINTS.faiss_cpu),
75
+ "faiss-gpu": Checker("faiss", package_name="faiss", vmin="1.7.0", install_hint=INSTALL_HINTS.faiss_gpu),
73
76
  "pynndescent": Checker("pynndescent", vmin=None, install_hint=INSTALL_HINTS.pynndescent),
74
77
  }
75
78
 
@@ -6,8 +6,8 @@ class PackageConstants:
6
6
  SKLEARN_WARNING_CUTOFF: int = 50000
7
7
 
8
8
  # Default mapping methods
9
- DEFAULT_SELF_MAPPING_METHOD: str = "umap"
10
- DEFAULT_CROSS_MAPPING_METHOD: str = "gauss"
9
+ DEFAULT_SELF_MAPPING_KERNEL_METHOD: str = "umap"
10
+ DEFAULT_CROSS_MAPPING_KERNEL_METHOD: str = "gauss"
11
11
 
12
12
  # Kernel method categories
13
13
  JACCARD_BASED_KERNELS = {"jaccard", "hnoca"}
@@ -50,7 +50,7 @@ class _SklearnBackend(_KNNBackend):
50
50
  return distances, indices
51
51
 
52
52
 
53
- class _FaissBackend(_KNNBackend):
53
+ class _FaissCpuBackend(_KNNBackend):
54
54
  def __init__(
55
55
  self,
56
56
  n_neighbors: int,
@@ -58,7 +58,36 @@ class _FaissBackend(_KNNBackend):
58
58
  random_state: int = 0,
59
59
  **kwargs: Any,
60
60
  ):
61
- check_deps("faiss")
61
+ check_deps("faiss-cpu")
62
+ import faiss
63
+
64
+ self.faiss = faiss
65
+ self._index = None
66
+
67
+ def fit(self, data: np.ndarray) -> None:
68
+ dims = data.shape[1]
69
+ index = self.faiss.IndexFlatL2(dims)
70
+ # Ensure data is float32 and C-contiguous
71
+ data_f32 = np.ascontiguousarray(data.astype(np.float32))
72
+ index.add(data_f32)
73
+ self._index = index
74
+
75
+ def query(self, points: np.ndarray, k: int) -> tuple[np.ndarray, np.ndarray]:
76
+ # Ensure points are float32 and C-contiguous
77
+ points_f32 = np.ascontiguousarray(points.astype(np.float32))
78
+ distances, indices = self._index.search(points_f32, k)
79
+ return distances, indices
80
+
81
+
82
+ class _FaissGpuBackend(_KNNBackend):
83
+ def __init__(
84
+ self,
85
+ n_neighbors: int,
86
+ metric: str,
87
+ random_state: int = 0,
88
+ **kwargs: Any,
89
+ ):
90
+ check_deps("faiss-gpu")
62
91
  import faiss
63
92
 
64
93
  self.faiss = faiss
@@ -69,11 +98,15 @@ class _FaissBackend(_KNNBackend):
69
98
  dims = data.shape[1]
70
99
  flat = self.faiss.IndexFlatL2(dims)
71
100
  gpu_index = self.faiss.index_cpu_to_gpu(self.res, 0, flat)
72
- gpu_index.add(data.astype(np.float32))
101
+ # Ensure data is float32 and C-contiguous
102
+ data_f32 = np.ascontiguousarray(data.astype(np.float32))
103
+ gpu_index.add(data_f32)
73
104
  self._index = gpu_index
74
105
 
75
106
  def query(self, points: np.ndarray, k: int) -> tuple[np.ndarray, np.ndarray]:
76
- distances, indices = self._index.search(points.astype(np.float32), k)
107
+ # Ensure points are float32 and C-contiguous
108
+ points_f32 = np.ascontiguousarray(points.astype(np.float32))
109
+ distances, indices = self._index.search(points_f32, k)
77
110
  return distances, indices
78
111
 
79
112
 
@@ -148,16 +181,17 @@ class _PyNNDescentBackend(_KNNBackend):
148
181
 
149
182
  _BACKENDS = {
150
183
  "sklearn": _SklearnBackend,
151
- "faiss": _FaissBackend,
184
+ "faiss-cpu": _FaissCpuBackend,
185
+ "faiss-gpu": _FaissGpuBackend,
152
186
  "rapids": _RapidsBackend,
153
187
  "pynndescent": _PyNNDescentBackend,
154
188
  }
155
189
 
156
190
 
157
- def get_backend(method: str, n_neighbors: int, metric: str, random_state: int = 0, **kwargs: Any) -> _KNNBackend:
191
+ def get_backend(knn_method: str, n_neighbors: int, metric: str, random_state: int = 0, **kwargs: Any) -> _KNNBackend:
158
192
  """Factory to get a configured KNN backend."""
159
193
  try:
160
- backend_cls = _BACKENDS[method]
194
+ backend_cls = _BACKENDS[knn_method]
161
195
  except KeyError:
162
- raise ValueError(f"Unknown method: {method}. Supported methods: {list(_BACKENDS)}") from KeyError
196
+ raise ValueError(f"Unknown method: {knn_method}. Supported methods: {list(_BACKENDS)}") from KeyError
163
197
  return backend_cls(n_neighbors=n_neighbors, metric=metric, random_state=random_state, **kwargs)