Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 257 additions & 38 deletions sandbox_harald.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,59 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 36,
"id": "125833fd",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/tim.treis/Documents/GitHub/spatialdata-plot/src/spatialdata_plot/pl/basic.py:11: AccessorRegistrationWarning: registration of accessor <class 'spatialdata_plot.pl.basic.PlotAccessor'> under name 'pl' for type <class 'spatialdata._core._spatialdata.SpatialData'> is overriding a preexisting attribute with the same name.\n",
" \n"
]
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 37,
"id": "633f35c7",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/tim.treis/anaconda3/envs/spatialdata/lib/python3.10/site-packages/geopandas/_compat.py:123: UserWarning: The Shapely GEOS version (3.11.1-CAPI-1.17.1) is incompatible with the GEOS version PyGEOS was compiled with (3.10.4-CAPI-1.16.2). Conversions between both will be slow.\n",
" warnings.warn(\n",
"/Users/tim.treis/Documents/GitHub/spatialdata/spatialdata/_compat.py:18: UserWarning: Geopandas was set to use PyGEOS, changing to shapely 2.0 with:\n",
"\n",
"\tgeopandas.options.use_pygeos = True\n",
"\n",
"If you intended to use PyGEOS, set the option to False.\n",
" warnings.warn(\n",
"/Users/tim.treis/anaconda3/envs/spatialdata/lib/python3.10/site-packages/anndata/experimental/pytorch/_annloader.py:18: UserWarning: Сould not load pytorch.\n",
" warnings.warn(\"Сould not load pytorch.\")\n"
]
}
],
"outputs": [],
"source": [
"import spatialdata as sd\n",
"import spatialdata_plot\n",
"import numpy as np\n",
"import pyarrow as pa\n",
"from anndata import AnnData\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"from typing import Union"
"from typing import Union\n",
"from numpy.random import default_rng\n",
"\n",
"RNG = default_rng()\n",
"from spatialdata._core.models import (\n",
" Image2DModel,\n",
" Labels2DModel,\n",
" Labels3DModel,\n",
" PointsModel,\n",
" PolygonsModel,\n",
" ShapesModel,\n",
" TableModel,\n",
")\n"
]
},
{
Expand All @@ -52,17 +67,17 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 38,
"id": "86f42824",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"no parent found for <ome_zarr.reader.Label object at 0x16a03acb0>: None\n",
"no parent found for <ome_zarr.reader.Label object at 0x16a0504f0>: None\n",
"no parent found for <ome_zarr.reader.Label object at 0x16a052290>: None\n",
"no parent found for <ome_zarr.reader.Label object at 0x16a0081c0>: None\n",
"no parent found for <ome_zarr.reader.Label object at 0x16a002860>: None\n",
"no parent found for <ome_zarr.reader.Label object at 0x16a008340>: None\n",
"/Users/tim.treis/anaconda3/envs/spatialdata/lib/python3.10/site-packages/anndata/_core/anndata.py:1828: UserWarning: Observation names are not unique. To make them unique, call `.obs_names_make_unique`.\n",
" utils.warn_names_duplicates(\"obs\")\n"
]
Expand All @@ -78,47 +93,251 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 39,
"id": "0519ddd6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/qg/qgc908995g3fc8qtss2fsbhhxyxxj4/T/ipykernel_10284/749062723.py:38: FutureWarning: X.dtype being converted to np.float32 from float64. In the next version of anndata (0.9) conversion will not be automatic. Pass dtype explicitly to avoid this warning. Pass `AnnData(X, dtype=X.dtype, ...)` to get the future behavour.\n",
" adata = AnnData(RNG.normal(size=(30, 10)), obs=pd.DataFrame(RNG.normal(size=(30, 3)), columns=[\"a\", \"b\", \"c\"]))\n",
"/Users/tim.treis/anaconda3/envs/spatialdata/lib/python3.10/site-packages/anndata/_core/anndata.py:121: ImplicitModificationWarning: Transforming to str index.\n",
" warnings.warn(\"Transforming to str index.\", ImplicitModificationWarning)\n"
]
},
{
"data": {
"text/plain": [
"pyarrow.Table\n",
"x: double\n",
"y: double\n",
"points_assignment0: int64\n",
"----\n",
"x: [[0.8413944235105699,0.5615364450210023,-0.5556994053539558,1.3616613476380617,1.3414766906774243,...,-0.5651286351605515,1.2442477539941565,-1.008172775800909,0.9597715912866557,-0.46395487249913125]]\n",
"y: [[1.033588506324896,-1.3432120527072624,-0.3173451786855313,-1.0882631464969956,-1.1932496234882468,...,-0.6987319002728575,0.22930304650553632,0.6932577098741612,-0.5447879481915016,0.32888317716312865]]\n",
"points_assignment0: [[0,1,1,2,2,...,0,1,2,0,2]]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def _get_points() -> dict[str, pa.Table]:\n",
" name = \"points\"\n",
" var_names = [np.arange(3), [\"genex\", \"geney\"]]\n",
"\n",
" out = {}\n",
" for i, v in enumerate(var_names):\n",
" name = f\"{name}_{i}\"\n",
" arr = RNG.normal(size=(1000, 2))\n",
" # randomly assign some values from v to the points\n",
" points_assignment0 = pd.Series(RNG.choice(v, size=arr.shape[0]))\n",
" annotations = pa.table(\n",
" {\"points_assignment0\": points_assignment0},\n",
" )\n",
" out[name] = PointsModel.parse(coords=arr, annotations=annotations)\n",
" return out\n",
"\n",
"\n",
"from geopandas import GeoDataFrame\n",
"from shapely.geometry import MultiPolygon, Polygon\n",
"\n",
"\n",
"\n",
"GeoDataFrame(\n",
" {\n",
" \"geometry\": [\n",
" Polygon(((0, 0), (0, 10), (10, 10), (10, 0))),\n",
" ]\n",
" }\n",
")\n",
"\n",
"images = { \n",
" 'data1': sd.Image2DModel.parse(np.random.normal(size=(1, 100, 100)), dims=('c', 'y', 'x')),\n",
"} \n",
"\n",
"instance_key = \"instance_id\"\n",
"region_key = \"annotated_region\"\n",
"\n",
"adata = AnnData(RNG.normal(size=(30, 10)), obs=pd.DataFrame(RNG.normal(size=(30, 3)), columns=[\"a\", \"b\", \"c\"]))\n",
"adata.obs[instance_key] = [\"data1\"] * 3 + [\"data2\"] * 7 + [\"data3\"] * 20\n",
"adata.obs[region_key] = [\"data1\"] * 3 + [\"data2\"] * 7 + [\"data3\"] * 20\n",
"table = TableModel.parse(adata=adata, instance_key=instance_key, region_key=region_key)\n",
"sdata = sd.SpatialData(images=images, table=table, points=_get_points())\n",
"\n",
"sdata.points[\"points_0\"]"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "d0dceb0b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POLYGON ((2435.069566 6500.284703, 2476.390162 6535.178868, 2534.993939 6471.307336, 2743.287325 6256.796237, 2892.512438 6045.393994, 3110.132394 5597.718655, 3175.960311 5399.368749, 3130.896017 5374.899385, 3077.489401 5557.30352, 2850.542875 6011.196572, 2712.19876 6214.826674, 2493.024376 6434.001058, 2435.069566 6500.284703))\n",
"POLYGON ((2099.296063 6218.987584, 2143.972428 6257.045228, 2145.859029 6257.045228, 2146.338866 6256.459168, 2151.558392 6260.866921, 2210.003484 6332.509292, 2221.323093 6321.189682, 2222.09221 6320.430889, 2259.341867 6351.887251, 2357.789117 6236.588669, 2668.674769 5824.665181, 2827.226451 5490.463105, 2914.2115 5257.242323, 2873.763161 5235.279366, 2874.087247 5234.50716, 2845.513743 5219.940275, 2776.791914 5182.625157, 2600.279926 5537.095953, 2404.421965 5857.308174, 2099.296063 6218.987584))\n",
"POLYGON ((1915.9413 6061.89444, 1940.749011 6082.843905, 1940.219455 6083.477881, 2099.296063 6218.987584, 2404.421965 5857.308174, 2600.279926 5537.095953, 2776.791914 5182.625157, 2524.716288 5045.751152, 2468.153524 5171.805312, 2258.305709 5529.323812, 1917.88592 6059.383848, 1915.9413 6061.89444))\n",
"POLYGON ((1751.729672 5923.22201, 1915.9413 6061.89444, 1917.88592 6059.383848, 2258.305709 5529.323812, 2468.153524 5171.805312, 2524.716288 5045.751152, 2356.035402 4954.159479, 2281.622133 5132.944606, 2135.505877 5387.87084, 1911.668207 5717.409631, 1751.729672 5923.22201))\n",
"POLYGON ((1685.872345 5868.689447, 1743.695193 5916.437102, 1751.729672 5923.22201, 1911.668207 5717.409631, 2135.505877 5387.87084, 2281.622133 5132.944606, 2356.035402 4954.159479, 2299.438471 4923.428033, 2295.815906 4922.282422, 2190.900796 5146.937604, 2029.934809 5399.884154, 1792.318353 5710.318557, 1685.872345 5868.689447))\n",
"POLYGON ((2259.341867 6351.887251, 2435.069566 6500.284703, 2493.024376 6434.001058, 2712.19876 6214.826674, 2850.542875 6011.196572, 3077.489401 5557.30352, 3130.896017 5374.899385, 2914.2115 5257.242323, 2827.226451 5490.463105, 2668.674769 5824.665181, 2357.789117 6236.588669, 2259.341867 6351.887251))\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/tim.treis/Documents/GitHub/spatialdata-plot/src/spatialdata_plot/pl/basic.py:11: AccessorRegistrationWarning: registration of accessor <class 'spatialdata_plot.pl.basic.PlotAccessor'> under name 'pl' for type <class 'spatialdata._core._spatialdata.SpatialData'> is overriding a preexisting attribute with the same name.\n",
" class PlotAccessor:\n"
]
}
],
"source": [
"for key, value in merfish.polygons.items():\n",
" for geometry in value.geometry:\n",
" print(geometry)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "0eecde65",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'library_id'"
"SpatialData object with:\n",
"├── Images\n",
"│ └── 'rasterized': SpatialImage[cyx] (1, 522, 575)\n",
"├── Points\n",
"│ └── 'single_molecule': pyarrow.Table shape: (3714642, 3) (2D points)\n",
"├── Polygons\n",
"│ └── 'anatomical': GeoDataFrame shape: (6, 1) (2D polygons)\n",
"├── Shapes\n",
"│ └── 'cells': AnnData with osbm.spatial (2399, 2)\n",
"└── Table\n",
" └── AnnData object with n_obs × n_vars = 2399 × 268\n",
" obs: 'cell_id'\n",
" uns: 'spatialdata_attrs': AnnData (2399, 268)"
]
},
"execution_count": 5,
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mibi.pp.get_region_key()"
"merfish# .pl.imshow().pl.render_polygon()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 70,
"id": "35def7ca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0.267004, 0.004874, 0.329415, 1.0)]"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cmap = plt.cm.viridis\n",
"[cmap(i) for i in np.linspace(0, 1, len(merfish.polygons.keys()))]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "2a6d74c0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['point16', 'point23', 'point8']"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"channels = [1,2]\n",
"\n",
"image_names = []\n",
"n_channels = []\n",
"for image_name, image in mibi.images.items():\n",
" image_names.append(image_name)\n",
" n_channels.append(image.shape[0])\n",
" \n",
"channels_in_image = pd.DataFrame({\n",
" \"image_name\": image_names,\n",
" \"n_channels\": n_channels\n",
"})\n",
" \n",
"# 3) drop images that don't have enough channels for the selection\n",
"channels_in_image = channels_in_image.query(\"n_channels-1 >= \" + str(max(channels)))\n",
"valid_images = channels_in_image.image_name.values.tolist() \n",
"\n",
"valid_images\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "cc2a2d5f",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'mibitof' is not defined",
"evalue": "name 'imc' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m mibitof\n",
"\u001b[0;31mNameError\u001b[0m: name 'mibitof' is not defined"
"Cell \u001b[0;32mIn[44], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m imc\n",
"\u001b[0;31mNameError\u001b[0m: name 'imc' is not defined"
]
}
],
"source": [
"mibitof"
"imc"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d258be1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0b78a4b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -143,7 +362,7 @@
" uns: 'spatialdata_attrs': AnnData (2399, 268)"
]
},
"execution_count": 6,
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -174,7 +393,7 @@
" uns: 'spatialdata_attrs': AnnData (5906, 31053)"
]
},
"execution_count": 7,
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -220,7 +439,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mPathNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m codex \u001b[39m=\u001b[39m sd\u001b[39m.\u001b[39;49mread_zarr(\u001b[39m'\u001b[39;49m\u001b[39m/home/voehring/voehring/projects/2023-01-15_spatial_data/codex_all.zarr\u001b[39;49m\u001b[39m'\u001b[39;49m)\n",
"Cell \u001b[0;32mIn[69], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m codex \u001b[39m=\u001b[39m sd\u001b[39m.\u001b[39;49mread_zarr(\u001b[39m'\u001b[39;49m\u001b[39m/home/voehring/voehring/projects/2023-01-15_spatial_data/codex_all.zarr\u001b[39;49m\u001b[39m'\u001b[39;49m)\n",
"File \u001b[0;32m~/Documents/GitHub/spatialdata/spatialdata/_io/read.py:81\u001b[0m, in \u001b[0;36mread_zarr\u001b[0;34m(store)\u001b[0m\n\u001b[1;32m 77\u001b[0m store \u001b[39m=\u001b[39m Path(store)\n\u001b[1;32m 79\u001b[0m fmt \u001b[39m=\u001b[39m SpatialDataFormatV01()\n\u001b[0;32m---> 81\u001b[0m f \u001b[39m=\u001b[39m zarr\u001b[39m.\u001b[39;49mopen(store, mode\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mr\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n\u001b[1;32m 82\u001b[0m images \u001b[39m=\u001b[39m {}\n\u001b[1;32m 83\u001b[0m labels \u001b[39m=\u001b[39m {}\n",
"File \u001b[0;32m~/anaconda3/envs/spatialdata/lib/python3.10/site-packages/zarr/convenience.py:122\u001b[0m, in \u001b[0;36mopen\u001b[0;34m(store, mode, zarr_version, path, **kwargs)\u001b[0m\n\u001b[1;32m 120\u001b[0m \u001b[39mreturn\u001b[39;00m open_group(_store, mode\u001b[39m=\u001b[39mmode, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[1;32m 121\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 122\u001b[0m \u001b[39mraise\u001b[39;00m PathNotFoundError(path)\n",
"\u001b[0;31mPathNotFoundError\u001b[0m: nothing found at path ''"
Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_plot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from importlib.metadata import version

from . import pl, pp, tl
from . import pl, pp, tl, utils

__all__ = ["pl", "pp", "tl"]

Expand Down
Loading