-
Notifications
You must be signed in to change notification settings - Fork 47
various fixes and additions for IO #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a485be6
add steinbock
giovp 382dec0
add steinbock
giovp e260952
updates
giovp 5631648
fixes
giovp 62af10e
Merge branch 'io/nanostring' into io/steinbock
giovp 4cd565b
update constants and init
giovp 6c60a02
fix init
giovp 742b7df
update
giovp 9f16a34
update visium and xenium
giovp 3447c9e
readd images in xenium
giovp 15ad9d1
Merge branch 'io/steinbock' into fixes/io
giovp da512d6
fix steinbock
giovp 4dacd1f
Merge branch 'io/mcmicro' into fixes/io
giovp aac84f0
add mcmicro
giovp e761e85
pre-commit fixes
giovp 2615da1
minor fixes cosmx
giovp cfd12f2
remove temp dir
giovp 31b1d2a
io fixes
giovp 40ca0b8
tested all technologies with napari and added fixes
LucaMarconato 0560498
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b05fd8d
minor fixes
giovp f66f522
fixes
giovp 3907d68
add spatial key in obsm
giovp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| from importlib.metadata import version | ||
|
|
||
| from spatialdata_io.readers.cosmx import cosmx | ||
| from spatialdata_io.readers.mcmicro import mcmicro | ||
| from spatialdata_io.readers.steinbock import steinbock | ||
| from spatialdata_io.readers.visium import visium | ||
| from spatialdata_io.readers.xenium import xenium | ||
|
|
||
| __all__ = [ | ||
| "visium", | ||
| "xenium", | ||
| "cosmx", | ||
| "mcmicro", | ||
| "steinbock", | ||
| ] | ||
|
|
||
| __version__ = version("spatialdata-io") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from collections.abc import Mapping | ||
| from pathlib import Path | ||
| from types import MappingProxyType | ||
| from typing import Any, Union | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| from anndata import AnnData | ||
| from dask_image.imread import imread | ||
| from multiscale_spatial_image.multiscale_spatial_image import MultiscaleSpatialImage | ||
| from spatial_image import SpatialImage | ||
| from spatialdata import Image2DModel, Labels2DModel, SpatialData, TableModel | ||
|
|
||
| from spatialdata_io._constants._constants import McmicroKeys | ||
|
|
||
| __all__ = ["mcmicro"] | ||
|
|
||
|
|
||
| def mcmicro( | ||
| path: str | Path, | ||
| dataset_id: str, | ||
| imread_kwargs: Mapping[str, Any] = MappingProxyType({}), | ||
| image_models_kwargs: Mapping[str, Any] = MappingProxyType({}), | ||
| ) -> SpatialData: | ||
| """ | ||
| Read a *Mcmicro* output into a SpatialData object. | ||
| .. seealso:: | ||
| - `Mcmicro pipeline <https://mcmicro.org/>`_. | ||
| Parameters | ||
| ---------- | ||
| path | ||
| Path to the dataset. | ||
| dataset_id | ||
| Dataset identifier. | ||
| imread_kwargs | ||
| Keyword arguments to pass to the image reader. | ||
| image_models_kwargs | ||
| Keyword arguments to pass to the image models. | ||
| Returns | ||
| ------- | ||
| :class:`spatialdata.SpatialData` | ||
| """ | ||
| path = Path(path) | ||
|
|
||
| samples = os.listdir(path / McmicroKeys.IMAGES_DIR) | ||
| if len(samples) > 1: | ||
| raise ValueError("Only one sample per dataset is supported.") | ||
| if (dataset_id + McmicroKeys.IMAGE_SUFFIX) not in samples: | ||
| raise ValueError("Dataset id is not consistent with sample name.") | ||
|
|
||
| images = {} | ||
| images[dataset_id] = _get_images( | ||
| path, | ||
| dataset_id, | ||
| imread_kwargs, | ||
| image_models_kwargs, | ||
| ) | ||
| labels = {} | ||
| labels[dataset_id] = _get_labels( | ||
| path, | ||
| dataset_id, | ||
| "cell", | ||
| imread_kwargs, | ||
| image_models_kwargs, | ||
| ) | ||
| labels[f"{dataset_id}_nuclei"] = _get_labels( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just one note (nothing to do): in the example the labels of the nuclei coincide with the other cell labels. |
||
| path, | ||
| dataset_id, | ||
| "nuclei", | ||
| imread_kwargs, | ||
| image_models_kwargs, | ||
| ) | ||
|
|
||
| table = _get_table(path, dataset_id) | ||
|
|
||
| return SpatialData(images=images, labels=labels, table=table) | ||
|
|
||
|
|
||
| def _get_images( | ||
| path: Path, | ||
| sample: str, | ||
| imread_kwargs: Mapping[str, Any] = MappingProxyType({}), | ||
| image_models_kwargs: Mapping[str, Any] = MappingProxyType({}), | ||
| ) -> Union[SpatialImage, MultiscaleSpatialImage]: | ||
| image = imread(path / McmicroKeys.IMAGES_DIR / f"{sample}{McmicroKeys.IMAGE_SUFFIX}", **imread_kwargs) | ||
| return Image2DModel.parse(image, **image_models_kwargs) | ||
|
|
||
|
|
||
| def _get_labels( | ||
| path: Path, | ||
| sample: str, | ||
| labels_kind: str, | ||
| imread_kwargs: Mapping[str, Any] = MappingProxyType({}), | ||
| image_models_kwargs: Mapping[str, Any] = MappingProxyType({}), | ||
| ) -> Union[SpatialImage, MultiscaleSpatialImage]: | ||
| image = imread( | ||
| path | ||
| / McmicroKeys.LABELS_DIR | ||
| / f"{McmicroKeys.LABELS_PREFIX}{sample}" | ||
| / f"{labels_kind}{McmicroKeys.IMAGE_SUFFIX}", | ||
| **imread_kwargs, | ||
| ).squeeze() | ||
| return Labels2DModel.parse(image, **image_models_kwargs) | ||
|
|
||
|
|
||
| def _get_table( | ||
| path: Path, | ||
| sample: str, | ||
| ) -> AnnData: | ||
| table = pd.read_csv(path / McmicroKeys.QUANTIFICATION_DIR / f"{sample}{McmicroKeys.CELL_FEATURES_SUFFIX}") | ||
| markers = pd.read_csv(path / McmicroKeys.MARKERS_FILE) | ||
| markers.index = markers.marker_name | ||
| var = markers.marker_name.tolist() | ||
| coords = [McmicroKeys.COORDS_X.value, McmicroKeys.COORDS_Y.value] | ||
| adata = AnnData( | ||
| table[var].to_numpy(), | ||
| obs=table.drop(columns=var + coords), | ||
| var=markers, | ||
| obsm={"spatial": table[coords].to_numpy()}, | ||
| dtype=np.float_, | ||
| ) | ||
|
|
||
| return TableModel.parse(adata, region=sample, instance_key=McmicroKeys.INSTANCE_KEY.value) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.