moscot 0.5.2__tar.gz → 0.5.3__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 (79) hide show
  1. {moscot-0.5.2 → moscot-0.5.3}/PKG-INFO +1 -1
  2. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/backends/ott/output.py +32 -0
  3. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/output.py +39 -18
  4. {moscot-0.5.2 → moscot-0.5.3}/src/moscot.egg-info/PKG-INFO +1 -1
  5. moscot-0.5.3/src/moscot.egg-info/scm_version.json +8 -0
  6. moscot-0.5.2/src/moscot.egg-info/scm_version.json +0 -8
  7. {moscot-0.5.2 → moscot-0.5.3}/.gitignore +0 -0
  8. {moscot-0.5.2 → moscot-0.5.3}/.gitmodules +0 -0
  9. {moscot-0.5.2 → moscot-0.5.3}/.pre-commit-config.yaml +0 -0
  10. {moscot-0.5.2 → moscot-0.5.3}/.readthedocs.yml +0 -0
  11. {moscot-0.5.2 → moscot-0.5.3}/.run_notebooks.sh +0 -0
  12. {moscot-0.5.2 → moscot-0.5.3}/LICENSE +0 -0
  13. {moscot-0.5.2 → moscot-0.5.3}/MANIFEST.in +0 -0
  14. {moscot-0.5.2 → moscot-0.5.3}/README.rst +0 -0
  15. {moscot-0.5.2 → moscot-0.5.3}/codecov.yml +0 -0
  16. {moscot-0.5.2 → moscot-0.5.3}/pyproject.toml +0 -0
  17. {moscot-0.5.2 → moscot-0.5.3}/setup.cfg +0 -0
  18. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/__init__.py +0 -0
  19. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/_constants.py +0 -0
  20. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/_logging.py +0 -0
  21. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/_registry.py +0 -0
  22. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/_types.py +0 -0
  23. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/backends/__init__.py +0 -0
  24. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/backends/ott/__init__.py +0 -0
  25. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/backends/ott/_utils.py +0 -0
  26. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/backends/ott/solver.py +0 -0
  27. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/backends/utils.py +0 -0
  28. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/__init__.py +0 -0
  29. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/cost.py +0 -0
  30. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/__init__.py +0 -0
  31. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/_mixins.py +0 -0
  32. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/_utils.py +0 -0
  33. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/birth_death.py +0 -0
  34. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/compound_problem.py +0 -0
  35. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/manager.py +0 -0
  36. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/problems/problem.py +0 -0
  37. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/base/solver.py +0 -0
  38. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/costs/__init__.py +0 -0
  39. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/costs/_costs.py +0 -0
  40. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/costs/_utils.py +0 -0
  41. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/datasets.py +0 -0
  42. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/plotting/__init__.py +0 -0
  43. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/plotting/_plotting.py +0 -0
  44. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/plotting/_utils.py +0 -0
  45. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/__init__.py +0 -0
  46. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/_utils.py +0 -0
  47. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/cross_modality/__init__.py +0 -0
  48. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/cross_modality/_mixins.py +0 -0
  49. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/cross_modality/_translation.py +0 -0
  50. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/generic/__init__.py +0 -0
  51. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/generic/_generic.py +0 -0
  52. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/generic/_mixins.py +0 -0
  53. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/space/__init__.py +0 -0
  54. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/space/_alignment.py +0 -0
  55. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/space/_mapping.py +0 -0
  56. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/space/_mixins.py +0 -0
  57. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/spatiotemporal/__init__.py +0 -0
  58. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/spatiotemporal/_spatio_temporal.py +0 -0
  59. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/time/__init__.py +0 -0
  60. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/time/_lineage.py +0 -0
  61. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/problems/time/_mixins.py +0 -0
  62. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/py.typed +0 -0
  63. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/__init__.py +0 -0
  64. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/allTFs_dmel.txt +0 -0
  65. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/allTFs_hg38.txt +0 -0
  66. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/allTFs_mm.txt +0 -0
  67. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/human_apoptosis.txt +0 -0
  68. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/human_proliferation.txt +0 -0
  69. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/mouse_apoptosis.txt +0 -0
  70. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/_data/mouse_proliferation.txt +0 -0
  71. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/data.py +0 -0
  72. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/subset_policy.py +0 -0
  73. {moscot-0.5.2 → moscot-0.5.3}/src/moscot/utils/tagged_array.py +0 -0
  74. {moscot-0.5.2 → moscot-0.5.3}/src/moscot.egg-info/SOURCES.txt +0 -0
  75. {moscot-0.5.2 → moscot-0.5.3}/src/moscot.egg-info/dependency_links.txt +0 -0
  76. {moscot-0.5.2 → moscot-0.5.3}/src/moscot.egg-info/requires.txt +0 -0
  77. {moscot-0.5.2 → moscot-0.5.3}/src/moscot.egg-info/scm_file_list.json +0 -0
  78. {moscot-0.5.2 → moscot-0.5.3}/src/moscot.egg-info/top_level.txt +0 -0
  79. {moscot-0.5.2 → moscot-0.5.3}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: moscot
3
- Version: 0.5.2
3
+ Version: 0.5.3
4
4
  Summary: Multi-omic single-cell optimal transport tools
5
5
  Author: Dominik Klein, Giovanni Palla, Michal Klein, Zoe Piran, Marius Lange
6
6
  Maintainer-email: Dominik Klein <dominik.klein@helmholtz-muenchen.de>, Giovanni Palla <giovanni.palla@helmholtz-muenchen.de>, Michal Klein <michal.klein@helmholtz-muenchen.de>
@@ -1,8 +1,10 @@
1
+ import functools
1
2
  from typing import Any, Optional, Tuple, Union
2
3
 
3
4
  import jax
4
5
  import jax.numpy as jnp
5
6
  import numpy as np
7
+ import scipy.sparse as sp
6
8
  from ott.geometry import geometry, low_rank, pointcloud
7
9
  from ott.solvers.linear import sinkhorn, sinkhorn_lr
8
10
  from ott.solvers.quadratic import gromov_wasserstein, gromov_wasserstein_lr
@@ -84,6 +86,22 @@ def _ott_row_materializer(out: Any) -> Optional[RowMaterializer]:
84
86
  return None
85
87
 
86
88
 
89
+ def _mass_select_row(row: jnp.ndarray, *, value: float, k: int) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
90
+ """Select ``mode = 'mass'`` entries of one row: its ``k`` largest entries and how many to keep."""
91
+ vals, idx = jax.lax.top_k(row, k)
92
+ total = row.sum()
93
+ target = jnp.inf if value >= 1.0 else value * total
94
+ n_keep = jnp.minimum((jnp.cumsum(vals) < target).sum() + 1, k)
95
+ return vals, idx, jnp.where(row.max() > 0.0, n_keep, 0) # massless rows keep nothing
96
+
97
+
98
+ @functools.partial(jax.jit, static_argnames=("value", "k"))
99
+ def _mass_select_rows(rows: jnp.ndarray, *, value: float, k: int) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
100
+ # `top_k` over a whole block of long rows needs several times the block's memory; 256 rows at a
101
+ # time do not (and run ~3x faster on a 2048 x 92,507 block on an RTX 4090)
102
+ return jax.lax.map(functools.partial(_mass_select_row, value=value, k=k), rows, batch_size=256)
103
+
104
+
87
105
  class OTTOutput(BaseDiscreteSolverOutput):
88
106
  """Output of various :term:`OT` problems.
89
107
 
@@ -263,6 +281,20 @@ class OTTOutput(BaseDiscreteSolverOutput):
263
281
  materialize = _ott_row_materializer(self._output)
264
282
  return super()._row_materializer() if materialize is None else materialize
265
283
 
284
+ def _mass_select(self, rows: ArrayLike, *, value: float, max_k: Optional[int]) -> Tuple[sp.csr_matrix, int]:
285
+ # select on the device holding the rows; only `[b, max_k]` crosses to the host, not `[b, m]`
286
+ if max_k is None or not isinstance(rows, jax.Array):
287
+ return super()._mass_select(rows, value=value, max_k=max_k)
288
+ b, m = rows.shape
289
+ k = min(max_k, m)
290
+ vals, idx, n_keep = (np.asarray(x) for x in _mass_select_rows(rows, value=value, k=k))
291
+ keep = np.arange(k)[None, :] < n_keep[:, None]
292
+ row_ix = np.broadcast_to(np.arange(b)[:, None], keep.shape)
293
+ out = sp.csr_matrix((vals[keep], (row_ix[keep], idx[keep])), shape=(b, m))
294
+ out.eliminate_zeros()
295
+ out.sort_indices()
296
+ return out, int((n_keep == 0).sum())
297
+
266
298
  @property
267
299
  def is_linear(self) -> bool: # noqa: D102
268
300
  return isinstance(self._output, (sinkhorn.SinkhornOutput, sinkhorn_lr.LRSinkhornOutput))
@@ -34,10 +34,17 @@ def _mass_select_block(rows: np.ndarray, *, value: float, max_k: Optional[int])
34
34
  -------
35
35
  Sparsified block as a :class:`~scipy.sparse.csr_matrix` of shape ``[b, m]``.
36
36
  """
37
- _, m = rows.shape
38
- order = np.argsort(rows, axis=1)[:, ::-1] # descending
39
- sorted_vals = np.take_along_axis(rows, order, axis=1)
40
- totals = sorted_vals.sum(axis=1, keepdims=True)
37
+ n, m = rows.shape
38
+ if max_k is not None and max_k < m:
39
+ # at most `max_k` entries are kept: sort only the `max_k` largest, not the whole row
40
+ top = np.argpartition(rows, m - max_k, axis=1)[:, m - max_k :]
41
+ order = np.take_along_axis(top, np.argsort(np.take_along_axis(rows, top, axis=1), axis=1)[:, ::-1], axis=1)
42
+ sorted_vals = np.take_along_axis(rows, order, axis=1)
43
+ totals = rows.sum(axis=1, keepdims=True)
44
+ else:
45
+ order = np.argsort(rows, axis=1)[:, ::-1] # descending
46
+ sorted_vals = np.take_along_axis(rows, order, axis=1)
47
+ totals = sorted_vals.sum(axis=1, keepdims=True)
41
48
  cum = np.cumsum(sorted_vals, axis=1)
42
49
  # `value >= 1` keeps everything (avoids float cumsum vs. sum mismatch -> exact reconstruction).
43
50
  target = np.full_like(totals, np.inf) if value >= 1.0 else value * totals
@@ -47,10 +54,12 @@ def _mass_select_block(rows: np.ndarray, *, value: float, max_k: Optional[int])
47
54
  if max_k is not None:
48
55
  k_per_row = np.minimum(k_per_row, max_k)
49
56
  k_per_row = np.minimum(k_per_row, m)
50
- keep_sorted = np.arange(m)[None, :] < k_per_row[:, None]
51
- sel = np.zeros(rows.shape, dtype=bool)
52
- np.put_along_axis(sel, order, keep_sorted, axis=1)
53
- return sp.csr_matrix(np.where(sel, rows, 0.0))
57
+ keep_sorted = np.arange(order.shape[1])[None, :] < k_per_row[:, None]
58
+ row_ix = np.broadcast_to(np.arange(n)[:, None], order.shape)
59
+ out = sp.csr_matrix((sorted_vals[keep_sorted], (row_ix[keep_sorted], order[keep_sorted])), shape=(n, m))
60
+ out.eliminate_zeros()
61
+ out.sort_indices()
62
+ return out
54
63
 
55
64
 
56
65
  def _massless_rows(rows: np.ndarray) -> np.ndarray:
@@ -64,13 +73,8 @@ def _massless_rows(rows: np.ndarray) -> np.ndarray:
64
73
  return ~(rows.max(axis=1) > 0.0)
65
74
 
66
75
 
67
- def _sparsify_block(
68
- rows: np.ndarray, *, mode: str, thr: Optional[float], value: Optional[float], max_k: Optional[int]
69
- ) -> sp.csr_matrix:
70
- """Apply the sparsification criterion to a block of transport-matrix rows."""
71
- if mode == "mass":
72
- assert value is not None # validated in `sparsify`
73
- return _mass_select_block(rows, value=value, max_k=max_k)
76
+ def _threshold_block(rows: np.ndarray, *, thr: float) -> sp.csr_matrix:
77
+ """Set the entries of a block of transport-matrix rows below ``thr`` to :math:`0`."""
74
78
  rows = np.array(rows) # writable copy
75
79
  rows[rows < thr] = 0.0
76
80
  return sp.csr_matrix(rows)
@@ -277,6 +281,15 @@ class BaseDiscreteSolverOutput(BaseSolverOutput, abc.ABC):
277
281
 
278
282
  return materialize
279
283
 
284
+ def _mass_select(self, rows: ArrayLike, *, value: float, max_k: Optional[int]) -> tuple[sp.csr_matrix, int]:
285
+ """Apply ``mode = 'mass'`` to a block of rows from :meth:`_row_materializer`.
286
+
287
+ Returns the sparsified block and its number of rows carrying no mass. Outputs whose rows live
288
+ on an accelerator override this to select there and copy only the kept entries to the host.
289
+ """
290
+ rows = np.asarray(rows)
291
+ return _mass_select_block(rows, value=value, max_k=max_k), int(_massless_rows(rows).sum())
292
+
280
293
  def sparsify(
281
294
  self,
282
295
  mode: Literal["threshold", "percentile", "min_row", "mass"],
@@ -295,6 +308,8 @@ class BaseDiscreteSolverOutput(BaseSolverOutput, abc.ABC):
295
308
  :term:`Gromov-Wasserstein`/:term:`fused Gromov-Wasserstein`, their :term:`low-rank` counterparts,
296
309
  and already materialized matrices - never hold more than ``[batch_size, m]`` at a time. Other
297
310
  outputs fall back to pushing indicator columns, whose cost depends on the output's :meth:`push`.
311
+ For :mod:`ott` outputs, ``mode = 'mass'`` with ``max_k`` selects the entries on the device holding
312
+ the rows and copies only ``[batch_size, max_k]`` of each block to the host.
298
313
 
299
314
  Rows carrying no mass keep no entries and take no part in choosing a threshold, whichever
300
315
  ``mode`` is used - no threshold can make them non-empty, and letting them take part would
@@ -379,9 +394,15 @@ class BaseDiscreteSolverOutput(BaseSolverOutput, abc.ABC):
379
394
  tmaps_sparse: list[sp.csr_matrix] = []
380
395
  n_massless = 0
381
396
  for ixs in row_blocks:
382
- block = np.asarray(materialize(ixs))
383
- n_massless += int(_massless_rows(block).sum())
384
- tmaps_sparse.append(_sparsify_block(block, mode=mode, thr=thr, value=value, max_k=max_k))
397
+ if mode == "mass":
398
+ assert value is not None # validated above
399
+ block, n_empty = self._mass_select(materialize(ixs), value=value, max_k=max_k)
400
+ else:
401
+ assert thr is not None
402
+ rows = np.asarray(materialize(ixs))
403
+ block, n_empty = _threshold_block(rows, thr=thr), int(_massless_rows(rows).sum())
404
+ n_massless += n_empty
405
+ tmaps_sparse.append(block)
385
406
  if n_massless:
386
407
  logger.warning(
387
408
  f"`{n_massless}` row(s) of the transport matrix carry no mass and are left empty; "
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: moscot
3
- Version: 0.5.2
3
+ Version: 0.5.3
4
4
  Summary: Multi-omic single-cell optimal transport tools
5
5
  Author: Dominik Klein, Giovanni Palla, Michal Klein, Zoe Piran, Marius Lange
6
6
  Maintainer-email: Dominik Klein <dominik.klein@helmholtz-muenchen.de>, Giovanni Palla <giovanni.palla@helmholtz-muenchen.de>, Michal Klein <michal.klein@helmholtz-muenchen.de>
@@ -0,0 +1,8 @@
1
+ {
2
+ "tag": "0.5.3",
3
+ "distance": 0,
4
+ "node": "gd6e1bb10398cb9ad1e8577552439a6c571a4903a",
5
+ "dirty": false,
6
+ "branch": "HEAD",
7
+ "node_date": "2026-10-14"
8
+ }
@@ -1,8 +0,0 @@
1
- {
2
- "tag": "0.5.2",
3
- "distance": 0,
4
- "node": "g440093ccbb8e70de209157d91da839c55b897821",
5
- "dirty": false,
6
- "branch": "HEAD",
7
- "node_date": "2026-08-25"
8
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes