sddb.SpatialDataCohort#
- class sddb.SpatialDataCohort(df: DataFrame, *, matched: dict[str, list[str]] | None = None, source: Source | None = None, filter: dict[str, Any] | None = None)#
An ordered, sliceable set of Dataset records from a query/search.
Attributes table#
The underlying catalog rows (a view; treat as read-only — use methods to derive new cohorts).
Methods table#
check_updates(catalog, *[, warn])Return
{pinned_uid: current_uid}for members a newer catalog version supersedes.citations(path, *[, bib_url])Write a BibTeX entry for every study in this set to
path.citations_text(*[, bib_url])Return
(BibTeX text, entry count)for the studies in this set, without writing a file.count(value)coverage()Per-column counts of present vs. unknown values.
download(dest, *[, workers, pin_versions, ...])Download every store under
dest(resumable) and return the manifest.filter(**facets)Return a new cohort keeping rows matching every
column=value(a list/tuple/set means isin).freeze()Pin this selection to its exact members and a deterministic hash.
from_manifest(m, *[, catalog])Rebuild a cohort pinned to exactly the manifest's members (offline — no catalog re-query).
groupby(field)Count members per value of
field(missing values bucketed as"unknown").index(value, [start, [stop]])Raises ValueError if the value is not present.
licenses()Count members per license.
Which modalities are present, from catalog flag columns only (no network).
split(*[, by])Group-safe deterministic split into
{fold: SpatialDataCohort}.summary()Headline numbers for this set.
to_anndata(*[, table, obs_columns, ...])Concatenate each member's table into one AnnData (outer join, obs[uid]+obs[study_id]).
to_df()Return the underlying rows as a DataFrame copy.
to_dict(*[, key, elements, dest, workers])Like
to_list(), keyed by thekeycolumn (default the full uid), which must be unique and set.to_list(*[, elements, dest, workers])Open every member as a SpatialData, in member order.
verify(*[, workers])Check the source catalog is unchanged and every pinned store still resolves.
where(mask)Return a new cohort keeping rows where the boolean
mask(aligned tomembers) is True.Attributes#
Methods#
- check_updates(catalog: Catalog, *, warn: bool = True) dict[str, str]#
Return
{pinned_uid: current_uid}for members a newer catalog version supersedes.Matches each pinned
uid[:16](stem) against the catalog's current uid for that stem (the catalog is latest-only). Warns once listing the superseded members whenwarn.
- citations(path: str | Path, *, bib_url: str | None = None) Path#
Write a BibTeX entry for every study in this set to
path.Studies without a published entry get a fallback entry that cites the dataset page, and a warning reports how many. Entries that share a DOI are merged into one; see
sddb.citations.citation_entries().
- citations_text(*, bib_url: str | None = None) tuple[str, int]#
Return
(BibTeX text, entry count)for the studies in this set, without writing a file.The count includes fallback entries and is counted after merging entries that share a DOI;
sddb.citations.citation_entries()reports the two kinds separately.
- count(value) integer -- return number of occurrences of value#
- download(dest: str | Path, *, workers: int = 4, pin_versions: bool = False, allow_version_change: bool = False, strict: bool = False) Manifest#
Download every store under
dest(resumable) and return the manifest.Raises
ManifestVersionMismatchifdestholds a manifest pinned to another catalog version (unlessallow_version_change). A missing pinned store falls back to the latest version with a loud warning unlessstrict=True(then it raisesCohortDriftError).
- filter(**facets: Any) SpatialDataCohort#
Return a new cohort keeping rows matching every
column=value(a list/tuple/set means isin).
- freeze() CohortManifest#
Pin this selection to its exact members and a deterministic hash. Offline/cheap.
The recorded filter captures effective defaults (e.g.
validation="pass"). Cohorts built by paths other thanquery/searchmay carry an empty filter.
- classmethod from_manifest(m: CohortManifest, *, catalog: Catalog | None = None) SpatialDataCohort#
Rebuild a cohort pinned to exactly the manifest's members (offline — no catalog re-query).
If
catalogis given, warn once about any members a newer version supersedes (seecheck_updates()). If that catalog has the manifest's url and its sha256 differs from the recorded one, raiseCatalogChangedError(a dated release was modified after the freeze) or, forcatalog-latest, warn. Withoutcatalognothing is fetched or hashed;verify()does the network check.
- groupby(field: str) DataFrame#
Count members per value of
field(missing values bucketed as"unknown").
- index(value[, start[, stop]]) integer -- return first index of value.#
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- split(*, by: str | Callable[[Series], Any] = 'study_id', train: float, val: float, test: float, seed: int) dict[str, SpatialDataCohort]#
Group-safe deterministic split into
{fold: SpatialDataCohort}.Each returned cohort carries the derived
SplitManifestas.split_manifest(pinned to this cohort's hash). Splitting is always group-safe (mode="group"): all members with the same group key land in the same fold.- Parameters:
- by
A column name, or a function that takes one member row (a
pd.Series) and returns its group key. A missing key puts the member in a group of its own. The manifest'sbyrecords the column, or the function's__name__("callable"for a lambda).- train
Fold ratios, counted in groups. They must sum to 1.
- val
Fold ratios, counted in groups. They must sum to 1.
- test
Fold ratios, counted in groups. They must sum to 1.
- seed
Seed for the shuffle of the sorted group keys.
Notes
For GEO data the default
by="study_id"groups by sample, not by series: a GEOstudy_idis a GSM, so the samples of one GEO series (one study, often one patient cohort) can land in different folds and leak between train and test. Group by the series instead, for example through itsgeo-gse...collection:def geo_series(row): cols = row["collections"] if row["collections"] is not None else [] return next((c for c in cols if c.startswith("geo-gse")), row["study_id"]) folds = cohort.split(by=geo_series, train=0.8, val=0.1, test=0.1, seed=0)
by="doi"is another choice when every member has a publication.
- to_anndata(*, table: str | None = None, obs_columns: Sequence[str] = (), elements: Sequence[str] | None = None, adapter: TableAdapter | None = None, gene_key: Literal['ensembl', 'symbol'] | None = None, spatial: Literal['auto', 'obsm', 'centroids'] = 'auto', max_workers: int = 8) AnnData#
Concatenate each member's table into one AnnData (outer join, obs[uid]+obs[study_id]).
gene_key="ensembl"or"symbol"re-keys every member'svar_namesto that id type before the join, so members whose sources key genes differently (HEST by Ensembl id, 10x by symbol) share columns. Features without that key are dropped with a warning; the old name stays invar["original_var_name"].None(default) joins on the stored var_names and warns when members share under half their genes.spatial="auto"(default) fillsobsm["spatial"]from the centroids of the shapes the table annotates when a member stores no coordinates (e.g. Allen MERSCOPE and MERFISH tables), matched byinstance_key."centroids"always uses the centroids,"obsm"keepsobsmas stored.See
sddb.concat.concat_tablesforobs_columns,elements,adapter,gene_key,spatialandmax_workers.
- to_dict(*, key: str = 'uid', elements: Sequence[str] | None = None, dest: str | Path | None = None, workers: int = 4) dict[str, SpatialData]#
Like
to_list(), keyed by thekeycolumn (default the full uid), which must be unique and set.elementswithdestcopies only those elements intodest/<uid>.zarrand records each copy indest/manifest.jsonas"partial", as described into_list().
- to_list(*, elements: Sequence[str] | None = None, dest: str | Path | None = None, workers: int = 4) list[SpatialData]#
Open every member as a SpatialData, in member order.
Remote stores are fetched in parallel and reused on later calls: into the sddb cache (keyed by full uid, so a republished version is fetched afresh; local stores open in place), or with
destintodest/<uid>.zarrplus a manifest, exactly asdownload().elements("kind/name") copies only those elements, plus the store metadata and the shapes or labels a kept table annotates. Withdestthey go todest/<uid>.zarr, recorded in the manifest as"partial"with the element list. A later call reuses that copy when it holds the requested elements and otherwise copies them together with the recorded ones. A laterdownload()replaces a partial copy with the whole store. A store that is already complete indestopens whole.
- verify(*, workers: int = 8) None#
Check the source catalog is unchanged and every pinned store still resolves.
If the cohort recorded its catalog's sha256 (cohorts from a
Catalog, manifests written since this field was added), the catalog is fetched again from its url and hashed first. A dated release whose bytes changed raisesCatalogChangedError; a changedcatalog-latestonly warns, unless the cohort recorded the release it resolved to, which is then checked like a dated release. If the url cannot be fetched, raiseCatalogUnavailableError: a cached copy is never used in its place. Then raiseCohortDriftErrorif any pinned store no longer resolves: a pinned version may stop resolving when a dataset is republished. Not called bydownload(), which runs its own inline liveness loop; not run by cheap ops.
- where(mask: Series) SpatialDataCohort#
Return a new cohort keeping rows where the boolean
mask(aligned tomembers) is True.