Skip to content

spatial_neighbors breaks when transform='spectral' #1014

@tjbencomo

Description

@tjbencomo

Description

When specifying transform='spectral' in sq.gr.spatial_neighbors, I encounter a ValueError: inconsistent shapes error

Setting transform='cosine' works fine and does not produce an error

This error seems to be reproducible in multiple Xenium datasets

Not sure if the error is due to an incorrect combination of parameter settings or squidpy bug

Minimal reproducible example

Using the Xenium V1 Lung Cancer FFPE dataset used in the Squidpy Xenium tutorial

import os
import matplotlib.pyplot as plt
import scanpy as sc
import seaborn as sns
import spatialdata as sd
import squidpy as sq
from spatialdata_io import xenium

baseDir = "/hpc/temp/setty_m/tbencomo/Xenium_V1_humanLung_Cancer_FFPE"
zarr_path = os.path.join(baseDir, "Xenium.zarr")

sdata = sd.read_zarr(zarr_path)
adata = sdata.tables["table"]

adata.layers["counts"] = adata.X.copy()
sc.pp.normalize_total(adata, inplace=True)
sc.pp.log1p(adata)
sc.pp.pca(adata)
sc.pp.neighbors(adata)
sc.tl.umap(adata)
sc.tl.leiden(adata)

# This works - no issues
sq.gr.spatial_neighbors(adata, coord_type="generic", delaunay=True)

# Also works no issues
sq.gr.spatial_neighbors(
    adata,
    coord_type="generic",
    delaunay=True,
    transform="cosine",
    key_added="spatial_cosine",
)

# This throws an error
sq.gr.spatial_neighbors(
    adata,
    coord_type="generic",
    delaunay=True,
    transform="spectral",
    key_added="spatial_spectral",
)

# Also throws an error
sq.gr.spatial_neighbors(
    adata,
    coord_type="generic",
    transform="spectral",
    key_added="spatial_spectral",
)

# Also throws an error
sq.gr.spatial_neighbors(
    adata,
    delaunay=True,
    transform="spectral",
    key_added="spatial_spectral",
)

# Also errors
sq.gr.spatial_neighbors(
    adata,
    transform="spectral",
    key_added="spatial_spectral",
)

Traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[24], line 1
----> 1 sq.gr.spatial_neighbors(
      2     adata,
      3     coord_type="generic",
      4     delaunay=True,
      5     transform="spectral",
      6     key_added="spatial_spectral",
      7 )

File [~/micromamba/envs/spatial_v1/lib/python3.12/site-packages/squidpy/gr/_build.py:244](http://gizmok22.fhcrc.org:56252/lab/tree/mcc-spatial/notebooks/micromamba/envs/spatial_v1/lib/python3.12/site-packages/squidpy/gr/_build.py#line=243), in spatial_neighbors(adata, spatial_key, elements_to_coordinate_systems, table_key, library_key, coord_type, n_neighs, radius, delaunay, n_rings, percentile, transform, set_diag, key_added, copy)
    242     Dst = block_diag([m[1] for m in mats], format="csr")[ixs, :][:, ixs]
    243 else:
--> 244     Adj, Dst = _build_fun(adata)
    246 neighs_key = Key.uns.spatial_neighs(key_added)
    247 conns_key = Key.obsp.spatial_conn(key_added)

File [~/micromamba/envs/spatial_v1/lib/python3.12/site-packages/squidpy/gr/_build.py:323](http://gizmok22.fhcrc.org:56252/lab/tree/mcc-spatial/notebooks/micromamba/envs/spatial_v1/lib/python3.12/site-packages/squidpy/gr/_build.py#line=322), in _spatial_neighbor(adata, spatial_key, coord_type, n_neighs, radius, delaunay, n_rings, transform, set_diag, percentile)
    321 # check transform
    322 if transform == Transform.SPECTRAL:
--> 323     Adj = _transform_a_spectral(Adj)
    324 elif transform == Transform.COSINE:
    325     Adj = _transform_a_cosine(Adj)

File [~/micromamba/envs/spatial_v1/lib/python3.12/site-packages/squidpy/gr/_build.py:460](http://gizmok22.fhcrc.org:56252/lab/tree/mcc-spatial/notebooks/micromamba/envs/spatial_v1/lib/python3.12/site-packages/squidpy/gr/_build.py#line=459), in _transform_a_spectral(a)
    457     return a
    459 degrees = np.squeeze(np.array(np.sqrt(1.0 [/](http://gizmok22.fhcrc.org:56252/) a.sum(axis=0))))
--> 460 a = a.multiply(outer(a.indices, a.indptr, degrees))
    461 a.eliminate_zeros()
    463 return a

File [~/micromamba/envs/spatial_v1/lib/python3.12/site-packages/scipy/sparse/_compressed.py:476](http://gizmok22.fhcrc.org:56252/lab/tree/mcc-spatial/notebooks/micromamba/envs/spatial_v1/lib/python3.12/site-packages/scipy/sparse/_compressed.py#line=475), in _cs_matrix.multiply(self, other)
    474     data = np.multiply(ret.data, other2d[ret.row].ravel())
    475 else:
--> 476     raise ValueError("inconsistent shapes")
    477 ret.data = data.view(np.ndarray).ravel()
    478 return ret

ValueError: inconsistent shapes

Version

1.6.5

...

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions