-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathquery.py
More file actions
282 lines (218 loc) · 7.8 KB
/
query.py
File metadata and controls
282 lines (218 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from __future__ import annotations
from typing import (
Any,
Mapping,
Sequence,
Union,
runtime_checkable,
)
import numpy as np
import numpy.typing as npt
import pyarrow as pa
from anndata import AnnData
from typing_extensions import Protocol, Self, TypedDict
from .. import DataFrame
from .. import ReadIter
from .. import SparseRead
from .. import measurement
from .. import types as base_types
from ..options import BatchSize
from ..options import PlatformConfig
from ..options import ReadPartitions
from ..options import ResultOrder
from ..options import ResultOrderStr
_RO_AUTO = ResultOrder.AUTO
class AxisColumnNames(TypedDict, total=False):
"""Specifies column names for experiment axis query read operations.
Lifecycle: maturing
"""
obs: Sequence[str] | None
"""obs columns to use. All columns if ``None`` or not present."""
var: Sequence[str] | None
"""var columns to use. All columns if ``None`` or not present."""
@runtime_checkable
class ExperimentAxisQuery(Protocol):
"""Axis-based query against a SOMA Experiment.
ExperimentAxisQuery allows easy selection and extraction of data from a
single :class:`Measurement` in an :class:`Experiment`, by obs/var (axis) coordinates
and/or value filter.
The primary use for this class is slicing :class:`Experiment` ``X`` layers by obs or
var value and/or coordinates. Slicing on :class:`SparseNDArray` ``X`` matrices is
supported; :class:`DenseNDArray` is not supported at this time.
Lifecycle: maturing
"""
def obs(
self,
*,
column_names: Sequence[str] | None = None,
batch_size: BatchSize = BatchSize(),
partitions: ReadPartitions | None = None,
result_order: ResultOrderStr = _RO_AUTO,
platform_config: PlatformConfig | None = None,
) -> ReadIter[pa.Table]:
"""Returns ``obs`` as an `Arrow table
<https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`_
iterator.
Lifecycle: maturing
"""
...
def var(
self,
*,
column_names: Sequence[str] | None = None,
batch_size: BatchSize = BatchSize(),
partitions: ReadPartitions | None = None,
result_order: ResultOrderStr = _RO_AUTO,
platform_config: PlatformConfig | None = None,
) -> ReadIter[pa.Table]:
"""Returns ``var`` as an `Arrow table
<https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`_
iterator.
Lifecycle: maturing
"""
...
def obs_joinids(self) -> pa.IntegerArray:
"""Returns ``obs`` ``soma_joinids`` as an Arrow array.
Lifecycle: maturing
"""
...
def var_joinids(self) -> pa.IntegerArray:
"""Returns ``var`` ``soma_joinids`` as an Arrow array.
Lifecycle: maturing
"""
...
@property
def n_obs(self) -> int:
"""The number of ``obs`` axis query results.
Lifecycle: maturing
"""
...
@property
def n_vars(self) -> int:
"""The number of ``var`` axis query results.
Lifecycle: maturing
"""
...
@property
def indexer(self) -> "AxisIndexer":
"""A ``soma_joinid`` indexer for both ``obs`` and ``var`` axes.
Lifecycle: maturing
"""
...
def X(
self,
layer_name: str,
*,
batch_size: BatchSize = BatchSize(),
partitions: ReadPartitions | None = None,
result_order: ResultOrderStr = _RO_AUTO,
platform_config: PlatformConfig | None = None,
) -> SparseRead:
"""Returns an ``X`` layer as a sparse read.
Args:
layer_name: The X layer name to return.
batch_size: The size of batches that should be returned from a read.
See :class:`BatchSize` for details.
partitions: Specifies that this is part of a partitioned read,
and which partition to include, if present.
result_order: the order to return results, specified as a
:class:`~ResultOrder` or its string value.
platform_config: platform-specific configuration; keys are SOMA
implementation names.
Lifecycle: maturing
"""
...
def obsp(self, layer: str) -> SparseRead:
"""Returns an ``obsp`` layer as a sparse read.
Lifecycle: maturing
"""
...
def varp(self, layer: str) -> SparseRead:
"""Returns a ``varp`` layer as a sparse read.
Lifecycle: maturing
"""
...
def obsm(self, layer: str) -> SparseRead:
"""Returns an ``obsm`` layer as a sparse read.
Lifecycle: maturing
"""
...
def varm(self, layer: str) -> SparseRead:
"""Returns a ``varm`` layer as a sparse read.
Lifecycle: maturing
"""
...
def obs_scene_ids(self) -> pa.Array:
"""Returns a pyarrow array with scene ids that contain obs from this
query.
Lifecycle: experimental
"""
...
def var_scene_ids(self) -> pa.Array:
"""Return a pyarrow array with scene ids that contain var from this
query.
Lifecycle: experimental
"""
...
def to_anndata(
self,
X_name: str,
*,
column_names: AxisColumnNames | None = None,
X_layers: Sequence[str] = (),
obsm_layers: Sequence[str] = (),
obsp_layers: Sequence[str] = (),
varm_layers: Sequence[str] = (),
varp_layers: Sequence[str] = (),
drop_levels: bool = False,
) -> AnnData:
"""Executes the query and return result as an ``AnnData`` in-memory object.
Args:
X_name: The X layer to read and return in the ``X`` slot.
column_names: The columns in the ``var`` and ``obs`` dataframes
to read.
X_layers: Additional X layers to read and return
in the ``layers`` slot.
obsm_layers: Additional obsm layers to read and return in the obsm slot.
obsp_layers: Additional obsp layers to read and return in the obsp slot.
varm_layers: Additional varm layers to read and return in the varm slot.
varp_layers: Additional varp layers to read and return in the varp slot.
drop_levels: Indicate whether unused categories on axis frames should be
dropped. By default, False, the categories which are present
in the SOMA Experiment and not present in the query output
are not dropped.
Lifecycle: maturing
"""
...
# Context management
def close(self) -> None:
"""Releases resources associated with this query.
This method must be idempotent.
Lifecycle: maturing
"""
...
def __enter__(self) -> Self: ...
def __exit__(self, *_: Any) -> None: ...
Numpyable = Union[pa.Array, pa.ChunkedArray, npt.NDArray[np.int64]]
"""Things that can be converted to a NumPy array."""
@runtime_checkable
class AxisIndexer(Protocol):
"""Given a query, provides index-building services for obs/var axis.
Lifecycle: maturing
"""
def by_obs(self, coords: Numpyable) -> npt.NDArray[np.intp]:
"""Reindex the coords (soma_joinids) over the ``obs`` axis."""
...
def by_var(self, coords: Numpyable) -> npt.NDArray[np.intp]:
"""Reindex for the coords (soma_joinids) over the ``var`` axis."""
...
class Experimentish(Protocol):
"""The API we need from an Experiment."""
@property
def ms(self) -> Mapping[str, measurement.Measurement]: ...
@property
def obs(self) -> DataFrame: ...
@property
def context(self) -> base_types.ContextBase | None: ...
@property
def obs_spatial_presence(self) -> DataFrame: ...