Skip to content

Commit

Permalink
Merge pull request #10 from nvictus/feat-any-dataframe
Browse files Browse the repository at this point in the history
Modifications to support arbitrary dataframe, label column, and color code input
  • Loading branch information
nvictus authored May 15, 2024
2 parents 421e448 + b68241e commit dd670a2
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 130 deletions.
116 changes: 107 additions & 9 deletions demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,145 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "c761d03a-1a96-48f0-a72c-c2ae63d317f8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: ANYWIDGET_HMR=1\n"
]
}
],
"source": [
"%env ANYWIDGET_HMR=1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a6d50740-1061-42ff-9fd1-18f234f5cab0",
"metadata": {},
"outputs": [],
"source": [
"from io import BytesIO\n",
"import json\n",
"\n",
"from eigen_tour import Widget\n",
"import pathlib"
"import pathlib\n",
"import numpy as np\n",
"import polars as pl\n",
"import pandas as pd\n",
"import pyarrow as pa\n",
"import seaborn as sns\n",
"import matplotlib as mpl"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "f848616e-4182-48b9-88fa-acd0f2cb131d",
"metadata": {},
"outputs": [],
"source": [
"apple_vintage = {\n",
" \"green\": \"#5ebd3e\",\n",
" \"yellow\": \"#ffb900\",\n",
" \"orange\": \"#f78200\",\n",
" \"red\": \"#e23838\",\n",
" \"violet\": \"#973999\",\n",
" \"blue\": \"#009cdf\"\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "4133037f-27ad-45b6-a55e-d0930459e656",
"metadata": {},
"source": [
"## HCT116"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "261450a4-baff-404f-bbf2-c17071ea42c4",
"metadata": {},
"outputs": [],
"source": [
"data = pathlib.Path(\"data/eigs.arrow\").read_bytes()"
"data = pathlib.Path(\"data/eigs.arrow\").read_bytes()\n",
"\n",
"df = pl.read_ipc(data).to_pandas()\n",
"df[\"name\"] = df[\"name\"].map(lambda x: \"A2\" if x == \"A0\" else \"B4\" if x == \"B2\" else x)\n",
"axis_fields = [\"E1\", \"E2\", \"E3\", \"E4\", \"E5\", \"E6\"]\n",
"label_colors = {\"A1\": apple_vintage[\"red\"], \"A2\": apple_vintage[\"yellow\"], \"B0\": apple_vintage[\"green\"], \"B1\": apple_vintage[\"blue\"], \"B4\": apple_vintage[\"violet\"]}\n",
"label_field = \"name\"\n",
"\n",
"scale = 100 / np.linalg.norm(df[\"E1\"])\n",
"df[axis_fields] *= scale"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "9653616e-a1c3-4b5c-a585-84c85a33172b",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9ba6f7fd3f4a41f5a909b8abd3e1721e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Widget(axis_fields=['E1', 'E2', 'E3', 'E4', 'E5', 'E6'], data=b'ARROW1\\x00\\x00\\xff\\xff\\xff\\xff@\\x06\\x00\\x00\\x1…"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Widget(data=data)"
"Widget(\n",
" df=df, \n",
" axis_fields=axis_fields,\n",
" label_field=label_field, \n",
" label_colors=label_colors\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "297f032e-8f93-46b8-933c-6b0a5753a1c1",
"id": "d7ff2d8e-6280-409c-8eec-990ade9e6265",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4163b846-6f94-4bb3-8db4-dd629474f4b1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "529f8582-4dda-4fe8-81ba-f55f3e5884f7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "25640dcd-bdbb-49e6-9c13-400f3ba2ee90",
"id": "420140b1-70c0-42a9-8a80-a38b18f5d065",
"metadata": {},
"outputs": [],
"source": []
Expand Down
33 changes: 17 additions & 16 deletions js/Overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export class Overlay {
annotate?: (renderer: Renderer) => void;
legend?: Legend;
anchors?: d3.Selection<SVGGElement, string, SVGSVGElement, unknown>;
#dataset: string;

constructor(public renderer: Renderer) {
this.figure = d3.select(renderer.gl.canvas.parentNode as HTMLElement);
constructor(public renderer: Renderer, dataset: string) {
let canvas = renderer.gl.canvas as HTMLCanvasElement;
this.figure = d3.select(canvas.parentNode as HTMLElement);
let self = this;
this.#dataset = dataset;

this.epochSlider = this.figure
.insert("input", ":first-child")
Expand Down Expand Up @@ -162,15 +165,17 @@ export class Overlay {
}

get width() {
return this.renderer.gl.canvas.clientWidth;
let canvas = this.renderer.gl.canvas as HTMLCanvasElement;
return canvas.clientWidth;
}

get height() {
return this.renderer.gl.canvas.clientHeight;
let canvas = this.renderer.gl.canvas as HTMLCanvasElement;
return canvas.clientHeight;
}

get dataset() {
return utils.getDataset() as keyof typeof utils.legendTitle;
return this.#dataset;
}

resize() {
Expand Down Expand Up @@ -201,8 +206,8 @@ export class Overlay {
}
}

init() {
this.initLegend();
init(legendData: [string, d3.RGBColor][]) {
this.initLegend(legendData);
this.resize();
this.drawAxes();
if (this.annotate !== undefined) {
Expand Down Expand Up @@ -295,17 +300,13 @@ export class Overlay {
});
}

initLegend() {
let data = utils.zip(
utils.getLabelNames(false, this.dataset),
utils.baseColors,
);
this.legend = new Legend(data, {
initLegend(legendData: [string, d3.RGBColor][]) {
this.legend = new Legend(legendData, {
root: this.svg,
title: utils.legendTitle[this.dataset],
title: utils.legendTitle[this.dataset as keyof typeof utils.legendTitle],
margin: {
left: utils.legendLeft[this.dataset],
right: utils.legendRight[this.dataset],
left: utils.legendLeft[this.dataset as keyof typeof utils.legendLeft],
right: utils.legendRight[this.dataset as keyof typeof utils.legendRight],
},
});
this.legend.on("select", (classes) => {
Expand Down
Loading

0 comments on commit dd670a2

Please sign in to comment.