{ "cells": [ { "cell_type": "markdown", "id": "configured-sherman", "metadata": {}, "source": [ "# Linking Multiple Scatter Plots\n", "\n", "In this example we're going to link multiple scatter plots and later compose them intro a grid." ] }, { "cell_type": "code", "execution_count": 1, "id": "combined-affairs", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import jscatter" ] }, { "cell_type": "markdown", "id": "7453a4c8-712a-44e5-aece-3d155716b85e", "metadata": {}, "source": [ "### Scatter Plots with 1-to-1 Correspondences\n", "\n", "The the straight-forward case, we have two scatter plots with a one-to-one correspondence of the data points. (E.g., think of comparing two embedding spaces of the same data that were generated with two different dimensionality reduction methods)" ] }, { "cell_type": "code", "execution_count": 2, "id": "383de8ff-08c8-450a-be86-c1c09acb799d", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e55a37b33a36495380fb82d2bb3c9063", "version_major": 2, "version_minor": 0 }, "text/plain": [ "GridBox(children=(HBox(children=(VBox(children=(Button(button_style='primary', icon='arrows', layout=Layout(wi…" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import jscatter\n", "import numpy as np\n", "\n", "x = np.random.rand(500)\n", "y = np.random.rand(500)\n", "\n", "scatterA = jscatter.Scatter(x=x, y=y)\n", "scatterB = jscatter.Scatter(x=x, y=y)\n", "\n", "jscatter.link([scatterA, scatterB])" ] }, { "cell_type": "markdown", "id": "17f3e32d-529d-468a-adc3-16358f517613", "metadata": {}, "source": [ "In the following we'll look at a real-world use-case for the linking feature. In the example we're going to explore four different embeddings of the [Fashion MNIST dataset](Fashion MNIST).\n", "\n", "We'll start by loading the data..." ] }, { "cell_type": "code", "execution_count": 3, "id": "195bbcf0-80cd-4070-9082-434a35a95770", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install pyarrow --quiet" ] }, { "cell_type": "code", "execution_count": 4, "id": "39e401a5-1346-4da5-b8df-4131cdf8836e", "metadata": {}, "outputs": [], "source": [ "import io\n", "import pyarrow as pa\n", "import requests\n", "\n", "r = requests.get(\n", " 'https://storage.googleapis.com/flekschas/regl-scatterplot/fashion-mnist-embeddings.arrow'\n", ")\n", "\n", "with pa.ipc.open_file(io.BytesIO(r.content)) as reader:\n", " embeddings = reader.read_pandas()\n", " embeddings['class'] = embeddings['class'].astype('category')" ] }, { "cell_type": "markdown", "id": "6ef585c7-7744-4c56-a53f-1a90ee14d4f6", "metadata": {}, "source": [ "...and then instantiate four scatters with a popping color map..." ] }, { "cell_type": "code", "execution_count": 5, "id": "9138e2aa-8276-4e94-a1b2-51de0da01d41", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b91f28727d164fb6bf7fd3790040fa84", "version_major": 2, "version_minor": 0 }, "text/plain": [ "GridBox(children=(HBox(children=(VBox(children=(Button(button_style='primary', icon='arrows', layout=Layout(wi…" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config = dict(\n", " background_color='#111111',\n", " color_by='class',\n", " color_map=[\n", " '#FFFF00',\n", " '#1CE6FF',\n", " '#FF34FF',\n", " '#FF4A46',\n", " '#008941',\n", " '#006FA6',\n", " '#A30059',\n", " '#FFDBE5',\n", " '#7A4900',\n", " '#0000A6',\n", " ],\n", ")\n", "\n", "scatter_pca = jscatter.Scatter(data=embeddings, x='pcaX', y='pcaY', **config)\n", "scatter_tsne = jscatter.Scatter(data=embeddings, x='tsneX', y='tsneY', **config)\n", "scatter_umap = jscatter.Scatter(data=embeddings, x='umapX', y='umapY', **config)\n", "scatter_cae = jscatter.Scatter(data=embeddings, x='caeX', y='caeY', **config)\n", "\n", "jscatter.link(\n", " [scatter_pca, scatter_tsne, scatter_umap, scatter_cae], rows=2, row_height=240\n", ")" ] }, { "cell_type": "markdown", "id": "9a2d124d-ab3d-4846-af05-bce410054f83", "metadata": {}, "source": [ "In clockwise order, the embeddings are: PCA, t-SNE, convolutional autoencoder, and UMAP." ] }, { "cell_type": "markdown", "id": "excessive-venture", "metadata": {}, "source": [ "### Scatter Plots with N-to-M Correspondences\n", "\n", "Now to the more complex case where we have an N-by-M correspondence between the data points.\n", "\n", "To simulate this case, we first generate some dummy grid data where the first dataset has `4` points, the second has `16`, and the third has `64` points.\n", "\n", "Hence, a point in the first scatter plot corresponds to 4 points in the second scatter plot and 16 points in the third scatter plot.\n", "\n", "To be able to determine the correspondences between points, we need to have a column that specifies the membership of points to a common set of groups. In this examply we have four groups (`0` to `3`) and all points in the three datasets assigned to one of these three groups." ] }, { "cell_type": "code", "execution_count": 6, "id": "automated-producer", "metadata": {}, "outputs": [], "source": [ "X1, Y1 = np.mgrid[0:2:1, 0:2:1]\n", "X2, Y2 = np.mgrid[0:4:1, 0:4:1]\n", "X3, Y3 = np.mgrid[0:8:1, 0:8:1]\n", "\n", "df1 = pd.DataFrame(\n", " np.concatenate(\n", " (\n", " np.expand_dims(X1.flatten(), axis=1),\n", " np.expand_dims(Y1.flatten(), axis=1),\n", " np.expand_dims(np.repeat(np.arange(4), 1), axis=1),\n", " ),\n", " axis=1,\n", " ),\n", " columns=['x', 'y', 'group'],\n", ")\n", "df1.group = df1.group.astype('category')\n", "\n", "df2 = pd.DataFrame(\n", " np.concatenate(\n", " (\n", " np.expand_dims(X2.flatten(), axis=1),\n", " np.expand_dims(Y2.flatten(), axis=1),\n", " np.expand_dims(np.repeat(np.arange(4), 4), axis=1),\n", " ),\n", " axis=1,\n", " ),\n", " columns=['x', 'y', 'group'],\n", ")\n", "df2.group = df2.group.astype('category')\n", "\n", "df3 = pd.DataFrame(\n", " np.concatenate(\n", " (\n", " np.expand_dims(X3.flatten(), axis=1),\n", " np.expand_dims(Y3.flatten(), axis=1),\n", " np.expand_dims(np.repeat(np.arange(4), 16), axis=1),\n", " ),\n", " axis=1,\n", " ),\n", " columns=['x', 'y', 'group'],\n", ")\n", "df3.group = df3.group.astype('category')" ] }, { "cell_type": "markdown", "id": "e114b8bf-92a2-4005-bc98-9cbd8042b2d0", "metadata": {}, "source": [ "For instance, in the second dataset (`df2`) we see that the first four points are assigned to group `0`. So either one of these four points from dataset two correspond to the first point of the first dataset (`df1`) as that point is also assigned to group `0`." ] }, { "cell_type": "code", "execution_count": 7, "id": "ffbb9853-d792-4537-8e97-102b7a8be0e4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | x | \n", "y | \n", "group | \n", "
|---|---|---|---|
| 0 | \n", "0 | \n", "0 | \n", "0 | \n", "
| 1 | \n", "0 | \n", "1 | \n", "0 | \n", "
| 2 | \n", "0 | \n", "2 | \n", "0 | \n", "
| 3 | \n", "0 | \n", "3 | \n", "0 | \n", "
| 4 | \n", "1 | \n", "0 | \n", "1 | \n", "
| 5 | \n", "1 | \n", "1 | \n", "1 | \n", "