Skip to content

Commit

Permalink
fix visium axes ordering for many versions AND cytassist. (#94)
Browse files Browse the repository at this point in the history
* update

* update precomit

* fix precommit
  • Loading branch information
giovp authored Jan 9, 2024
1 parent 28ecd83 commit 3ff4935
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ __pycache__/

# other
_version.py
.code-workspace
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ default_stages:
minimum_pre_commit_version: 2.16.0
repos:
- repo: https://github.com/psf/black
rev: 23.10.1
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
rev: v3.1.0
hooks:
- id: prettier
- repo: https://github.com/asottile/blacken-docs
Expand All @@ -23,7 +23,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.1
rev: v1.7.1
hooks:
- id: mypy
additional_dependencies: [numpy, types-PyYAML]
Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_io/readers/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from spatialdata_io.readers._utils._read_10x_h5 import _read_10x_h5

PathLike = Union[os.PathLike, str]
PathLike = Union[os.PathLike, str] # type:ignore[type-arg]

try:
from numpy.typing import NDArray
Expand Down
10 changes: 8 additions & 2 deletions src/spatialdata_io/readers/visium.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def visium(
elif (path / "spatial" / VisiumKeys.SPOTS_FILE_2).exists() or (
tissue_positions_file is not None and str(VisiumKeys.SPOTS_FILE_2) in str(tissue_positions_file)
):
read_coords = partial(pd.read_csv, header=1, index_col=0)
read_coords = partial(pd.read_csv, header=0, index_col=0)
tissue_positions_file = (
path / "spatial" / VisiumKeys.SPOTS_FILE_2
if tissue_positions_file is None
Expand All @@ -151,7 +151,13 @@ def visium(
raise ValueError(f"Cannot find `tissue_positions` file in `{path}`.")
coords = read_coords(tissue_positions_file)

coords.columns = ["in_tissue", "array_row", "array_col", "pxl_col_in_fullres", "pxl_row_in_fullres"]
# to handle spaceranger_version < 2.0.0, where no column names are provided
# in fact, from spaceranger 2.0.0, the column names are provided in the file, and
# are "pxl_col_in_fullres", "pxl_row_in_fullres" are inverted.
# But, in the case of CytAssist, the column names provided but the image is flipped
# so we need to invert the columns.
if "in_tissue" not in coords.columns or "CytAssist" in str(fullres_image_file):
coords.columns = ["in_tissue", "array_row", "array_col", "pxl_col_in_fullres", "pxl_row_in_fullres"]

adata.obs = pd.merge(adata.obs, coords, how="left", left_index=True, right_index=True)
coords = adata.obs[[VisiumKeys.SPOTS_X, VisiumKeys.SPOTS_Y]].values
Expand Down

0 comments on commit 3ff4935

Please sign in to comment.