diff --git a/notebooks/spatialdata_tutorials/7_image_params.ipynb b/notebooks/spatialdata_tutorials/7_image_params.ipynb index 4f0ae8c..fa72e25 100644 --- a/notebooks/spatialdata_tutorials/7_image_params.ipynb +++ b/notebooks/spatialdata_tutorials/7_image_params.ipynb @@ -119,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -137,6 +137,29 @@ "\n", "# Example: Assuming `data_array` is your xArray DataArray with dims (channels, height, width)\n", "def process_xarray(data_array, sigma=5, downsize_factor=0.1):\n", + " \"\"\"\n", + " Processes a 3D xarray by applying Gaussian smoothing and downscaling on each 2D slice.\n", + "\n", + " Parameters:\n", + " - data_array: xarray.DataArray\n", + " A 3D xarray where the first dimension represents slices, and the next two dimensions\n", + " represent 2D spatial data.\n", + " - sigma: float, optional (default=5)\n", + " The standard deviation for the Gaussian kernel used in smoothing.\n", + " - downsize_factor: float, optional (default=0.1)\n", + " The factor by which each 2D slice is resized during downscaling.\n", + "\n", + " Returns:\n", + " - smoothed_and_downsized: numpy.ndarray\n", + " A 3D numpy array with processed slices, where the first dimension corresponds to\n", + " the original slicing dimension, and the remaining dimensions are the downsized\n", + " 2D spatial data.\n", + "\n", + " Notes:\n", + " - The function loops through each slice of the input data, applies Gaussian smoothing,\n", + " downsizes the smoothed array, and stacks the results back into a single 3D array.\n", + " - Ensure that `data_array` has the correct shape and data types for processing.\n", + " \"\"\"\n", " processed_slices = []\n", " \n", " for i in range(data_array.shape[0]): # Loop over the first dimension\n", @@ -150,11 +173,10 @@ " \n", " processed_slices.append(downscaled)\n", " \n", - " smoothed_and_downsized=np.stack(processed_slices , axis=0)\n", - " # Stack back into an xArray\n", - " \n", + " smoothed_and_downsized = np.stack(processed_slices, axis=0)\n", " return smoothed_and_downsized\n", "\n", + "\n", "# Example usage\n", "# Assuming your xArray is named `image_array`\n", "smoothed_and_downsized = process_xarray(imarray, sigma=5, downsize_factor=0.5)\n", diff --git a/notebooks/spatialdata_tutorials/9_sender_centric_analysis.ipynb b/notebooks/spatialdata_tutorials/9_sender_centric_analysis.ipynb deleted file mode 100644 index 0114615..0000000 --- a/notebooks/spatialdata_tutorials/9_sender_centric_analysis.ipynb +++ /dev/null @@ -1,183 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import numpy as np\n", - "import pandas as pd\n", - "import seaborn as sns\n", - "import matplotlib.pyplot as plt\n", - "import spatialdata_io\n", - "import spatialdata as sd\n", - "from tqdm import tqdm\n", - "import scanpy as sc\n", - "import networkx as nx\n", - "import sys\n", - "sys.path.append(\"../../src\")\n", - "import troutpy " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "xenium_path_cropped='/media/sergio/Discovair_final/mousebrain_prime_crop_points2regions_annotated.zarr'\n", - "output_path='/media/sergio/Discovair_final/analysis_crop'\n", - "sdata=sd.read_zarr(xenium_path_cropped)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd\n", - "import numpy as np\n", - "import networkx as nx\n", - "\n", - "def find_gene_groups(sdata, threshold=0.7, key='table',output_path:str='',save=True):\n", - " # Extract the expression data from the provided key in sdata\n", - " adata = sdata[key]\n", - " expression = pd.DataFrame(adata.X.todense(), columns=adata.var.index)\n", - " \n", - " # Compute correlations and structure the results\n", - " correlations = np.corrcoef(expression.T)\n", - " results = pd.DataFrame(correlations, index=expression.columns, columns=expression.columns)\n", - " \n", - " # Unstack to create a DataFrame of gene pairs with correlation values\n", - " sorted_Res = results.unstack().reset_index().sort_values(by=0)\n", - " sorted_Res.columns = ['gene_1', 'gene_2', 'correlation']\n", - " \n", - " # Filter pairs where genes are different and correlation exceeds the threshold\n", - " goodcomb = sorted_Res[sorted_Res['gene_1'] != sorted_Res['gene_2']]\n", - " filtered_pairs = goodcomb[goodcomb['correlation'] > threshold]\n", - " \n", - " # Build a graph from the filtered gene pairs\n", - " G = nx.Graph()\n", - " G.add_edges_from(filtered_pairs[['gene_1', 'gene_2']].values)\n", - " \n", - " # Find connected components as groups of highly correlated genes\n", - " gene_groups = {gene: i + 1 for i, component in enumerate(nx.connected_components(G)) for gene in component}\n", - " \n", - " # Create a DataFrame with all genes, assigning \"No group\" to ungrouped genes\n", - " gene_to_group = [{'gene': gene, 'group': gene_groups.get(gene, \"No group\")} for gene in expression.columns]\n", - " gene_groups_df = pd.DataFrame(gene_to_group)\n", - " if save==True:\n", - " gene_groups_df.to_csv(os.path.join(output_path,'genes_grouped_by_intracellular_expression.csv'))\n", - "\n", - " return gene_groups_df\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "gene_groups_intracellular_expression = find_gene_groups(sdata, threshold=0.7, key='table',output_path=output_path)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "extracellular_proportion=calculate_extracellular_proportion(sdata, output_path,save=False)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "extracellular_proportion=calculate_extracellular_proportion(sdata, output_path,save=False)\n", - "for i, group in enumerate(gene_groups):\n", - " print(f\"Group {i+1}: {group}\")\n", - " sc.pl.dotplot(adata,group,groupby=key,swap_axes=True)\n", - " extra_filt=extracellular_proportion.loc[extracellular_proportion.index.isin(group),:].sort_values(by='extracellular_proportion')\n", - " plt.figure()\n", - " extra_filt.plot(kind='barh',stacked=True,width=0.95)\n", - " plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "genes_of_interest.to_csv('/media/sergio/Discovair_final/genes_of_interest.csv')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "exrna=sdata['extracellular_transcripts']\n", - "for i, group in enumerate(gene_groups):\n", - " genes_of_interest=exrna[exrna['feature_name'].compute().isin(group)].compute()\n", - " for g in group:\n", - " \n", - " # Create a figure with two subplots: one for adata_annotated, one for adata\n", - " fig, axs = plt.subplots(1, 1, ) # 1 row, 2 columns\n", - " \n", - " # Plot spatial map from adata\n", - " sc.pl.spatial(adata, color=g, cmap='YlGnBu', title=g,\n", - " ax=axs, show=False, spot_size=20,vmax='p99')\n", - " # Plot spatial map from adata_annotated\n", - " plt.scatter(genes_of_interest.loc[genes_of_interest['feature_name']==g,'x'],genes_of_interest.loc[genes_of_interest['feature_name']==g,'y'],color='red',s=1)\n", - "\n", - " # Adjust layout and show the combined plot\n", - " plt.tight_layout()\n", - " plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "spatial_exrna", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.15" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}