Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 9, 2024
1 parent 0f201bb commit 9d89bbf
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions notebooks/examples/technology_spacem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from pathlib import Path\n",
"\n",
"import anndata as ad\n",
"import json\n",
"import matplotlib\n",
"import numpy as np\n",
"import pandas as pd\n",
Expand Down Expand Up @@ -601,7 +601,11 @@
"metadata": {},
"outputs": [],
"source": [
"well_shapes_instance = adata.obs.loc[(adata.obs.region==layout_name) & (adata.obs.slide_id==slide_id) & (adata.obs.well_id==well_id)].iloc[0].instance_id\n",
"well_shapes_instance = (\n",
" adata.obs.loc[(adata.obs.region == layout_name) & (adata.obs.slide_id == slide_id) & (adata.obs.well_id == well_id)]\n",
" .iloc[0]\n",
" .instance_id\n",
")\n",
"well_polygon = spacem_sdata.shapes[layout_name].geometry.iloc[well_shapes_instance]\n",
"# Note that Shapely has XY order, but we use YX. Bounds: min_x, min_y, max_x, max_y\n",
"# Add some offset to visualize a bit more of the image\n",
Expand Down Expand Up @@ -639,21 +643,19 @@
"%matplotlib inline\n",
"\n",
"# FIXME: wrong image region is plotted with bounding box query\n",
"#sdata_cropped = spacem_sdata.query.bounding_box(\n",
"# sdata_cropped = spacem_sdata.query.bounding_box(\n",
"# axes=YX,\n",
"# min_coordinate=min_coordinate_yx,\n",
"# max_coordinate=max_coordinate_yx,\n",
"# target_coordinate_system=COORD_SYS_GLOBAL,\n",
"#)\n",
"# )\n",
"sdata_cropped = spacem_sdata\n",
"fig = (\n",
" sdata_cropped.pl.render_images(elements=ablation_marks_image_name, palette=[\"gray\", \"blue\"])\n",
" .pl.render_labels(\n",
" elements=ablation_marks_labels_name, color=INSTANCE_KEY, cmap=\"viridis\", fill_alpha=1.0\n",
" )\n",
" #.pl.render_shapes(\n",
" .pl.render_labels(elements=ablation_marks_labels_name, color=INSTANCE_KEY, cmap=\"viridis\", fill_alpha=1.0)\n",
" # .pl.render_shapes(\n",
" # elements=maldi_regions_name, fill_alpha=0.0, outline=True, outline_color=\"mediumvioletred\"\n",
" #)\n",
" # )\n",
" .pl.show(\n",
" coordinate_systems=COORD_SYS_GLOBAL,\n",
" title=\"Ablation marks\",\n",
Expand Down Expand Up @@ -697,9 +699,10 @@
"%matplotlib inline\n",
"\n",
"(\n",
" sdata_cropped.pl.render_images(elements=cells_image_name, palette=[\"gray\", \"lime\"])\n",
" .pl.render_labels(elements=cells_labels_name, fill_alpha=1.0)\n",
" #.pl.render_shapes(elements=maldi_regions_name, fill_alpha=0.0, outline=True, outline_color=\"mediumvioletred\")\n",
" sdata_cropped.pl.render_images(elements=cells_image_name, palette=[\"gray\", \"lime\"]).pl.render_labels(\n",
" elements=cells_labels_name, fill_alpha=1.0\n",
" )\n",
" # .pl.render_shapes(elements=maldi_regions_name, fill_alpha=0.0, outline=True, outline_color=\"mediumvioletred\")\n",
" .pl.show(coordinate_systems=COORD_SYS_GLOBAL, title=f\"Cell segmentation (pre_maldi)\")\n",
")\n",
"None"
Expand Down Expand Up @@ -741,7 +744,7 @@
"source": [
"# Here, we automatically select the first non-zero ion with highest variance,\n",
"# but any other ion from var_names can be selected manually.\n",
"_adata_roi = adata[adata.obs.region==ablation_marks_labels_name]\n",
"_adata_roi = adata[adata.obs.region == ablation_marks_labels_name]\n",
"selected_ion = _adata_roi.var_names[np.nanargmax(np.nanvar(_adata_roi.X, axis=0))]\n",
"selected_ion"
]
Expand Down Expand Up @@ -805,7 +808,7 @@
"source": [
"# Here, we automatically select the first non-zero ion with highest variance,\n",
"# but any other ion from var_names can be selected manually.\n",
"_adata_roi = adata[adata.obs.region==cells_labels_name]\n",
"_adata_roi = adata[adata.obs.region == cells_labels_name]\n",
"selected_ion = _adata_roi.var_names[np.nanargmax(np.nanvar(_adata_roi.X, axis=0))]\n",
"selected_ion"
]
Expand Down Expand Up @@ -877,7 +880,7 @@
],
"source": [
"adata = spacem_sdata.tables[\"table\"]\n",
"cells_adata = adata[adata.obs.object_type==\"cells\", :]\n",
"cells_adata = adata[adata.obs.object_type == \"cells\", :]\n",
"not_nan_ions = np.all(np.isfinite(cells_adata.X), axis=0)\n",
"cells_adata = cells_adata[:, not_nan_ions]\n",
"cells_adata.shape"
Expand Down Expand Up @@ -983,13 +986,22 @@
"\n",
"# Re-index batches before data integration to ensure matching batch numbers exist over all biological conditions.\n",
"# (Otherwise, biological differences can get lost.)\n",
"\n",
"\n",
"def reindex_batch(df: pd.DataFrame, batch_key: str) -> pd.DataFrame:\n",
" batches = df[batch_key].unique()\n",
" indexed = range(len(batches))\n",
" mapping = dict(zip(batches, indexed))\n",
" df[batch_key] = df[batch_key].replace(mapping)\n",
" return df\n",
"cells_adata.obs = cells_adata.obs.groupby(\"condition\", observed=True).apply(lambda group: reindex_batch(group, \"replicate\"), include_groups=True).reset_index(level=\"condition\", drop=True).sort_index()\n",
"\n",
"\n",
"cells_adata.obs = (\n",
" cells_adata.obs.groupby(\"condition\", observed=True)\n",
" .apply(lambda group: reindex_batch(group, \"replicate\"), include_groups=True)\n",
" .reset_index(level=\"condition\", drop=True)\n",
" .sort_index()\n",
")\n",
"\n",
"sc.external.pp.bbknn(cells_adata, batch_key=\"replicate\")\n",
"sc.tl.leiden(cells_adata)\n",
Expand Down Expand Up @@ -1031,7 +1043,9 @@
"\n",
"# Since ion names are not very expressive, label the plot with the first molecule candidate.\n",
"# (The ion is not necessarily this candidate molecule.)\n",
"cells_adata.var[\"first_molecule_name\"] = cells_adata.var[\"moleculeNames\"].map(lambda names_json: next(iter(json.loads(names_json)), \"\")[:50])\n",
"cells_adata.var[\"first_molecule_name\"] = cells_adata.var[\"moleculeNames\"].map(\n",
" lambda names_json: next(iter(json.loads(names_json)), \"\")[:50]\n",
")\n",
"sc.pl.rank_genes_groups(cells_adata, sharey=False, gene_symbols=\"first_molecule_name\")"
]
},
Expand Down Expand Up @@ -1075,7 +1089,9 @@
"best_ion_per_group = cells_adata.uns[\"rank_genes_groups\"][\"names\"][0]\n",
"# Extend obs for plotting single conditions\n",
"for group in groups:\n",
" cells_adata.obs[group] = pd.Categorical([group if t==group else np.nan for t in cells_adata.obs[CONDITION_COL].values], categories=groups)\n",
" cells_adata.obs[group] = pd.Categorical(\n",
" [group if t == group else np.nan for t in cells_adata.obs[CONDITION_COL].values], categories=groups\n",
" )\n",
"sc.pl.umap(cells_adata, color=groups)\n",
"sc.pl.umap(cells_adata, color=best_ion_per_group, cmap=\"viridis_r\")"
]
Expand Down

0 comments on commit 9d89bbf

Please sign in to comment.