-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathconftest.py
More file actions
69 lines (56 loc) · 2.03 KB
/
conftest.py
File metadata and controls
69 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from __future__ import annotations
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
import scanpy as sc
HERE: Path = Path(__file__).parent
@pytest.fixture(scope="module")
def adata():
# A bit cute.
from matplotlib.image import imread
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
empty_pixel = np.array([1.0, 1.0, 1.0, 0]).reshape(1, 1, -1)
image = imread(HERE.parent.parent / "docs/_static/img/Scanpy_Logo_RGB.png")
x, y = np.where(np.logical_and.reduce(~np.equal(image, empty_pixel), axis=2))
# Just using to calculate the hex coords
hexes = plt.hexbin(x, y, gridsize=(44, 100))
counts = hexes.get_array()
pixels = hexes.get_offsets()[counts != 0]
plt.close()
labels = DBSCAN(eps=20, min_samples=2).fit(pixels).labels_
order = np.argsort(labels)
adata = sc.AnnData(
make_blobs(
pd.Series(labels[order]).value_counts().values,
n_features=20,
shuffle=False,
random_state=42,
)[0],
obs={"label": pd.Categorical(labels[order].astype(str))},
obsm={"spatial": pixels[order, ::-1]},
uns={
"spatial": {
"scanpy_img": {
"images": {"hires": image},
"scalefactors": {
"tissue_hires_scalef": 1,
"spot_diameter_fullres": 10,
},
}
}
},
)
sc.pp.pca(adata)
# Adding some missing values
adata.obs["label_missing"] = adata.obs["label"].copy()
adata.obs.loc[::2, "label_missing"] = np.nan
# TODO: If we don't `copy`, something about this being an ArrayView means that all values get set to nan?
# https://github.com/scverse/anndata/issues/2348
adata.obs["1_missing"] = adata[:, "1"].X.flatten().copy()
adata.obs.loc[
adata.obsm["spatial"][:, 0] < adata.obsm["spatial"][:, 0].mean(), "1_missing"
] = np.nan
return adata