Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit fa925ee

Browse files
faster table conversion
1 parent 626c3d8 commit fa925ee

1 file changed

Lines changed: 5 additions & 15 deletions

File tree

  • spatialdata_xenium_explorer/core

spatialdata_xenium_explorer/core/table.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def write_gene_counts(
2929

3030
log.info(f"Writing table with {adata.n_vars} columns")
3131
counts = adata.X if layer is None else adata.layers[layer]
32-
counts = csr_matrix(counts)
32+
counts = csr_matrix(counts.T)
3333

3434
feature_keys = list(adata.var_names) + ["Total transcripts"]
3535
feature_ids = feature_keys
@@ -45,23 +45,13 @@ def write_gene_counts(
4545
"feature_types": feature_types,
4646
}
4747

48-
data, indices, indptr = [], [], [0]
49-
50-
for i in range(adata.n_vars):
51-
row_indices = counts[:, i].nonzero()[0]
52-
data.append(counts[row_indices, i].data)
53-
indices.append(row_indices)
54-
indptr.append(indptr[-1] + len(row_indices))
55-
5648
total_counts = counts.sum(1).A1
5749
loc = total_counts > 0
58-
data.append(total_counts[loc])
59-
indices.append(np.where(loc)[0])
60-
indptr.append(indptr[-1] + loc.sum())
6150

62-
data = np.concatenate(data)
63-
indices = np.concatenate(indices)
64-
indptr = np.array(indptr)
51+
data = np.concatenate([counts.data, total_counts[loc]])
52+
indices = np.concatenate([counts.indices, np.where(loc)[0]])
53+
indptr = counts.indptr
54+
indptr = np.append(indptr, indptr[-1] + loc.sum())
6555

6656
cell_id = np.ones((adata.n_obs, 2))
6757
cell_id[:, 0] = np.arange(adata.n_obs)

0 commit comments

Comments
 (0)