spatialcore 0.1.9__py3-none-any.whl
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.
- spatialcore/__init__.py +122 -0
- spatialcore/annotation/__init__.py +253 -0
- spatialcore/annotation/acquisition.py +529 -0
- spatialcore/annotation/annotate.py +603 -0
- spatialcore/annotation/cellxgene.py +365 -0
- spatialcore/annotation/confidence.py +802 -0
- spatialcore/annotation/discovery.py +529 -0
- spatialcore/annotation/expression.py +363 -0
- spatialcore/annotation/loading.py +529 -0
- spatialcore/annotation/markers.py +297 -0
- spatialcore/annotation/ontology.py +1282 -0
- spatialcore/annotation/patterns.py +247 -0
- spatialcore/annotation/pipeline.py +620 -0
- spatialcore/annotation/synapse.py +380 -0
- spatialcore/annotation/training.py +1457 -0
- spatialcore/annotation/validation.py +422 -0
- spatialcore/core/__init__.py +34 -0
- spatialcore/core/cache.py +118 -0
- spatialcore/core/logging.py +135 -0
- spatialcore/core/metadata.py +149 -0
- spatialcore/core/utils.py +768 -0
- spatialcore/data/gene_mappings/ensembl_to_hugo_human.tsv +86372 -0
- spatialcore/data/markers/canonical_markers.json +83 -0
- spatialcore/data/ontology_mappings/ontology_index.json +63865 -0
- spatialcore/plotting/__init__.py +109 -0
- spatialcore/plotting/benchmark.py +477 -0
- spatialcore/plotting/celltype.py +329 -0
- spatialcore/plotting/confidence.py +413 -0
- spatialcore/plotting/spatial.py +505 -0
- spatialcore/plotting/utils.py +411 -0
- spatialcore/plotting/validation.py +1342 -0
- spatialcore-0.1.9.dist-info/METADATA +213 -0
- spatialcore-0.1.9.dist-info/RECORD +36 -0
- spatialcore-0.1.9.dist-info/WHEEL +5 -0
- spatialcore-0.1.9.dist-info/licenses/LICENSE +201 -0
- spatialcore-0.1.9.dist-info/top_level.txt +1 -0
spatialcore/__init__.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SpatialCore: Standardized spatial statistics tools for computational biology.
|
|
3
|
+
|
|
4
|
+
A thin, robust wrapper around standard libraries to ensure Python and R users
|
|
5
|
+
get the exact same result for the same biological question.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.1.9"
|
|
9
|
+
|
|
10
|
+
# Track which modules are available in this installation
|
|
11
|
+
_available_modules: list[str] = []
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _try_import(module_name: str):
|
|
15
|
+
"""Attempt to import a module, returning None if unavailable."""
|
|
16
|
+
try:
|
|
17
|
+
import importlib
|
|
18
|
+
|
|
19
|
+
return importlib.import_module(f"spatialcore.{module_name}")
|
|
20
|
+
except ImportError:
|
|
21
|
+
return None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _module_unavailable(name: str):
|
|
25
|
+
"""Create a placeholder that raises helpful error on access."""
|
|
26
|
+
|
|
27
|
+
class _UnavailableModule:
|
|
28
|
+
def __init__(self, module_name: str):
|
|
29
|
+
self._name = module_name
|
|
30
|
+
|
|
31
|
+
def __getattr__(self, attr: str):
|
|
32
|
+
raise ImportError(
|
|
33
|
+
f"The '{self._name}' module is not available in this version of "
|
|
34
|
+
f"SpatialCore (v{__version__}). Available modules: {_available_modules}. "
|
|
35
|
+
f"Check https://github.com/mcap91/SpatialCore for the latest release."
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
def __repr__(self) -> str:
|
|
39
|
+
return f"<UnavailableModule: {self._name}>"
|
|
40
|
+
|
|
41
|
+
return _UnavailableModule(name)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Core module is required - fail loudly if missing
|
|
45
|
+
from spatialcore import core
|
|
46
|
+
|
|
47
|
+
_available_modules.append("core")
|
|
48
|
+
|
|
49
|
+
# Optional modules - import if available, placeholder if not
|
|
50
|
+
# Each module can be shipped independently as it becomes ready
|
|
51
|
+
|
|
52
|
+
annotation = _try_import("annotation")
|
|
53
|
+
if annotation is not None:
|
|
54
|
+
_available_modules.append("annotation")
|
|
55
|
+
else:
|
|
56
|
+
annotation = _module_unavailable("annotation")
|
|
57
|
+
|
|
58
|
+
diffusion = _try_import("diffusion")
|
|
59
|
+
if diffusion is not None:
|
|
60
|
+
_available_modules.append("diffusion")
|
|
61
|
+
else:
|
|
62
|
+
diffusion = _module_unavailable("diffusion")
|
|
63
|
+
|
|
64
|
+
nmf = _try_import("nmf")
|
|
65
|
+
if nmf is not None:
|
|
66
|
+
_available_modules.append("nmf")
|
|
67
|
+
else:
|
|
68
|
+
nmf = _module_unavailable("nmf")
|
|
69
|
+
|
|
70
|
+
plotting = _try_import("plotting")
|
|
71
|
+
if plotting is not None:
|
|
72
|
+
_available_modules.append("plotting")
|
|
73
|
+
else:
|
|
74
|
+
plotting = _module_unavailable("plotting")
|
|
75
|
+
|
|
76
|
+
r_bridge = _try_import("r_bridge")
|
|
77
|
+
if r_bridge is not None:
|
|
78
|
+
_available_modules.append("r_bridge")
|
|
79
|
+
else:
|
|
80
|
+
r_bridge = _module_unavailable("r_bridge")
|
|
81
|
+
|
|
82
|
+
spatial = _try_import("spatial")
|
|
83
|
+
if spatial is not None:
|
|
84
|
+
_available_modules.append("spatial")
|
|
85
|
+
else:
|
|
86
|
+
spatial = _module_unavailable("spatial")
|
|
87
|
+
|
|
88
|
+
stats = _try_import("stats")
|
|
89
|
+
if stats is not None:
|
|
90
|
+
_available_modules.append("stats")
|
|
91
|
+
else:
|
|
92
|
+
stats = _module_unavailable("stats")
|
|
93
|
+
|
|
94
|
+
# Internal modules (only available on dev branch, never published)
|
|
95
|
+
internal = _try_import("internal")
|
|
96
|
+
if internal is not None:
|
|
97
|
+
_available_modules.append("internal")
|
|
98
|
+
else:
|
|
99
|
+
internal = None # No placeholder - truly internal
|
|
100
|
+
|
|
101
|
+
__all__ = [
|
|
102
|
+
"__version__",
|
|
103
|
+
"core",
|
|
104
|
+
"annotation",
|
|
105
|
+
"diffusion",
|
|
106
|
+
"nmf",
|
|
107
|
+
"plotting",
|
|
108
|
+
"r_bridge",
|
|
109
|
+
"spatial",
|
|
110
|
+
"stats",
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def available_modules() -> list[str]:
|
|
115
|
+
"""Return list of modules available in this installation."""
|
|
116
|
+
return _available_modules.copy()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def print_info() -> None:
|
|
120
|
+
"""Print version and available modules."""
|
|
121
|
+
print(f"SpatialCore v{__version__}")
|
|
122
|
+
print(f"Available modules: {', '.join(_available_modules)}")
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Cell type annotation: Unified hub for CellTypist, ontology mapping, and reference data.
|
|
3
|
+
|
|
4
|
+
This module consolidates all cell typing functionality:
|
|
5
|
+
- CellTypist model training and annotation
|
|
6
|
+
- Confidence scoring and filtering
|
|
7
|
+
- Cell Ontology (CL) ID mapping
|
|
8
|
+
- Reference data loading from CellxGene, Synapse, GCS
|
|
9
|
+
- Marker gene management
|
|
10
|
+
- Validation utilities
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# Training & model management
|
|
14
|
+
from spatialcore.annotation.training import (
|
|
15
|
+
combine_references,
|
|
16
|
+
train_celltypist_model,
|
|
17
|
+
get_panel_genes,
|
|
18
|
+
get_model_gene_overlap,
|
|
19
|
+
get_training_summary,
|
|
20
|
+
HIGH_CONTRAST_PALETTE,
|
|
21
|
+
generate_color_scheme,
|
|
22
|
+
save_model_artifacts,
|
|
23
|
+
subsample_balanced,
|
|
24
|
+
DEFAULT_EXCLUDE_LABELS,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# High-level pipeline
|
|
28
|
+
from spatialcore.annotation.pipeline import (
|
|
29
|
+
train_and_annotate,
|
|
30
|
+
train_and_annotate_config,
|
|
31
|
+
TrainingConfig,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# Annotation (CellTypist wrapper)
|
|
35
|
+
from spatialcore.annotation.annotate import (
|
|
36
|
+
annotate_celltypist,
|
|
37
|
+
get_models_for_tissue,
|
|
38
|
+
get_annotation_summary,
|
|
39
|
+
TISSUE_MODEL_PRESETS,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Validation
|
|
43
|
+
from spatialcore.annotation.validation import (
|
|
44
|
+
validate_cell_type_column,
|
|
45
|
+
validate_multiple_columns,
|
|
46
|
+
CellTypeValidationResult,
|
|
47
|
+
ValidationIssue,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Confidence scoring & filtering
|
|
51
|
+
from spatialcore.annotation.confidence import (
|
|
52
|
+
transform_confidence,
|
|
53
|
+
extract_decision_scores,
|
|
54
|
+
filter_low_confidence,
|
|
55
|
+
filter_low_count_types,
|
|
56
|
+
compute_confidence_from_obsm,
|
|
57
|
+
filter_by_marker_validation,
|
|
58
|
+
ConfidenceMethod,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# Markers
|
|
62
|
+
from spatialcore.annotation.markers import (
|
|
63
|
+
load_canonical_markers,
|
|
64
|
+
match_to_canonical,
|
|
65
|
+
get_markers_for_type,
|
|
66
|
+
list_available_cell_types,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# Ontology mapping (consolidated from ontology/)
|
|
70
|
+
from spatialcore.annotation.ontology import (
|
|
71
|
+
add_ontology_ids,
|
|
72
|
+
has_ontology_ids,
|
|
73
|
+
search_ontology_index,
|
|
74
|
+
load_ontology_index,
|
|
75
|
+
create_mapping_table,
|
|
76
|
+
OntologyMappingResult,
|
|
77
|
+
UNKNOWN_CELL_TYPE_ID,
|
|
78
|
+
UNKNOWN_CELL_TYPE_NAME,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
from spatialcore.annotation.patterns import (
|
|
82
|
+
CELL_TYPE_PATTERNS,
|
|
83
|
+
get_canonical_term,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
from spatialcore.annotation.expression import (
|
|
87
|
+
evaluate_ontology_expression,
|
|
88
|
+
get_ontology_ids_in_expression,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# Reference data loading (consolidated from reference/)
|
|
92
|
+
from spatialcore.annotation.cellxgene import (
|
|
93
|
+
download_cellxgene_reference,
|
|
94
|
+
list_available_datasets,
|
|
95
|
+
load_ensembl_to_hugo_mapping,
|
|
96
|
+
normalize_gene_names,
|
|
97
|
+
check_normalization_status,
|
|
98
|
+
query_cellxgene_census,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
from spatialcore.annotation.loading import (
|
|
102
|
+
load_adata_backed,
|
|
103
|
+
subsample_adata,
|
|
104
|
+
ensure_normalized,
|
|
105
|
+
get_available_memory_gb,
|
|
106
|
+
estimate_adata_memory_gb,
|
|
107
|
+
get_loading_summary,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
from spatialcore.annotation.discovery import (
|
|
111
|
+
discover_training_data,
|
|
112
|
+
DiscoveredDataset,
|
|
113
|
+
print_discovery_summary,
|
|
114
|
+
load_local_metadata,
|
|
115
|
+
query_local_references,
|
|
116
|
+
create_metadata_template,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
# Data acquisition (Phase 1: download from sources → store to local/cloud)
|
|
120
|
+
from spatialcore.annotation.acquisition import (
|
|
121
|
+
acquire_reference,
|
|
122
|
+
resolve_uri_to_local,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
from spatialcore.annotation.synapse import (
|
|
126
|
+
download_synapse_reference,
|
|
127
|
+
authenticate_synapse,
|
|
128
|
+
get_synapse_entity_info,
|
|
129
|
+
list_synapse_folder,
|
|
130
|
+
download_synapse_folder,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
__all__ = [
|
|
134
|
+
# =====================================================================
|
|
135
|
+
# Training & Model Management
|
|
136
|
+
# =====================================================================
|
|
137
|
+
"combine_references",
|
|
138
|
+
"train_celltypist_model",
|
|
139
|
+
"get_panel_genes",
|
|
140
|
+
"get_model_gene_overlap",
|
|
141
|
+
"get_training_summary",
|
|
142
|
+
"HIGH_CONTRAST_PALETTE",
|
|
143
|
+
"generate_color_scheme",
|
|
144
|
+
"save_model_artifacts",
|
|
145
|
+
"subsample_balanced",
|
|
146
|
+
"DEFAULT_EXCLUDE_LABELS",
|
|
147
|
+
|
|
148
|
+
# =====================================================================
|
|
149
|
+
# High-Level Pipeline
|
|
150
|
+
# =====================================================================
|
|
151
|
+
"train_and_annotate",
|
|
152
|
+
"train_and_annotate_config",
|
|
153
|
+
"TrainingConfig",
|
|
154
|
+
|
|
155
|
+
# =====================================================================
|
|
156
|
+
# Annotation (CellTypist)
|
|
157
|
+
# =====================================================================
|
|
158
|
+
"annotate_celltypist",
|
|
159
|
+
"get_models_for_tissue",
|
|
160
|
+
"get_annotation_summary",
|
|
161
|
+
"TISSUE_MODEL_PRESETS",
|
|
162
|
+
|
|
163
|
+
# =====================================================================
|
|
164
|
+
# Validation
|
|
165
|
+
# =====================================================================
|
|
166
|
+
"validate_cell_type_column",
|
|
167
|
+
"validate_multiple_columns",
|
|
168
|
+
"CellTypeValidationResult",
|
|
169
|
+
"ValidationIssue",
|
|
170
|
+
|
|
171
|
+
# =====================================================================
|
|
172
|
+
# Confidence Scoring & Filtering
|
|
173
|
+
# =====================================================================
|
|
174
|
+
"transform_confidence",
|
|
175
|
+
"extract_decision_scores",
|
|
176
|
+
"filter_low_confidence",
|
|
177
|
+
"filter_low_count_types",
|
|
178
|
+
"compute_confidence_from_obsm",
|
|
179
|
+
"filter_by_marker_validation",
|
|
180
|
+
"ConfidenceMethod",
|
|
181
|
+
|
|
182
|
+
# =====================================================================
|
|
183
|
+
# Markers
|
|
184
|
+
# =====================================================================
|
|
185
|
+
"load_canonical_markers",
|
|
186
|
+
"match_to_canonical",
|
|
187
|
+
"get_markers_for_type",
|
|
188
|
+
"list_available_cell_types",
|
|
189
|
+
|
|
190
|
+
# =====================================================================
|
|
191
|
+
# Ontology Mapping
|
|
192
|
+
# =====================================================================
|
|
193
|
+
"add_ontology_ids",
|
|
194
|
+
"has_ontology_ids",
|
|
195
|
+
"search_ontology_index",
|
|
196
|
+
"load_ontology_index",
|
|
197
|
+
"create_mapping_table",
|
|
198
|
+
"OntologyMappingResult",
|
|
199
|
+
"UNKNOWN_CELL_TYPE_ID",
|
|
200
|
+
"UNKNOWN_CELL_TYPE_NAME",
|
|
201
|
+
"CELL_TYPE_PATTERNS",
|
|
202
|
+
"get_canonical_term",
|
|
203
|
+
"evaluate_ontology_expression",
|
|
204
|
+
"get_ontology_ids_in_expression",
|
|
205
|
+
|
|
206
|
+
# =====================================================================
|
|
207
|
+
# Reference Data: CellxGene
|
|
208
|
+
# =====================================================================
|
|
209
|
+
"download_cellxgene_reference",
|
|
210
|
+
"list_available_datasets",
|
|
211
|
+
"load_ensembl_to_hugo_mapping",
|
|
212
|
+
"normalize_gene_names",
|
|
213
|
+
"check_normalization_status",
|
|
214
|
+
"query_cellxgene_census",
|
|
215
|
+
|
|
216
|
+
# =====================================================================
|
|
217
|
+
# Reference Data: Loading & Memory
|
|
218
|
+
# =====================================================================
|
|
219
|
+
"load_adata_backed",
|
|
220
|
+
"subsample_adata",
|
|
221
|
+
"ensure_normalized",
|
|
222
|
+
"get_available_memory_gb",
|
|
223
|
+
"estimate_adata_memory_gb",
|
|
224
|
+
"get_loading_summary",
|
|
225
|
+
|
|
226
|
+
# =====================================================================
|
|
227
|
+
# Reference Data: Discovery
|
|
228
|
+
# =====================================================================
|
|
229
|
+
"discover_training_data",
|
|
230
|
+
"DiscoveredDataset",
|
|
231
|
+
"print_discovery_summary",
|
|
232
|
+
"load_local_metadata",
|
|
233
|
+
"query_local_references",
|
|
234
|
+
"create_metadata_template",
|
|
235
|
+
|
|
236
|
+
# =====================================================================
|
|
237
|
+
# Data Acquisition (Phase 1)
|
|
238
|
+
# =====================================================================
|
|
239
|
+
# Use acquire_reference() to download from CellxGene/Synapse and store
|
|
240
|
+
# to local filesystem or cloud storage (GCS, S3). Then use the stored
|
|
241
|
+
# paths with train_and_annotate() or combine_references().
|
|
242
|
+
"acquire_reference",
|
|
243
|
+
"resolve_uri_to_local",
|
|
244
|
+
|
|
245
|
+
# =====================================================================
|
|
246
|
+
# Reference Data: Synapse
|
|
247
|
+
# =====================================================================
|
|
248
|
+
"download_synapse_reference",
|
|
249
|
+
"authenticate_synapse",
|
|
250
|
+
"get_synapse_entity_info",
|
|
251
|
+
"list_synapse_folder",
|
|
252
|
+
"download_synapse_folder",
|
|
253
|
+
]
|