{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quickstart\n",
    "\n",
    "## Using the Colormap Client\n",
    "\n",
    "Using the notebook utilities provided by `evo-sdk-common`, we can easily interact with the Colormap API in a jupyter notebook environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.notebooks import ServiceManagerWidget\n",
    "\n",
    "manager = await ServiceManagerWidget.with_auth_code(\n",
    "    client_id=\"your-client-id\",\n",
    "    cache_location=\"./notebook-data\",\n",
    ").login()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ColormapAPIClient\n",
    "\n",
    "The `ColormapAPIClient` wraps endpoint functionality to provide a cohesive interface to the underlying API implementation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.colormaps.client import ColormapAPIClient\n",
    "\n",
    "service_client = ColormapAPIClient.from_context(manager)\n",
    "\n",
    "service_health = (\n",
    "    await service_client.get_service_health()\n",
    ")  # Implemented for the visualisation service currently (where Colormap API is deployed)\n",
    "print(f\"Colormap Service is {service_health.status.name.lower()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a continuous colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.colormaps.data import ContinuousColormap\n",
    "\n",
    "colormap = ContinuousColormap(\n",
    "    colors=[[0, 0, 0], [123, 123, 255], [255, 255, 255]],\n",
    "    attribute_controls=[0.1, 0.2, 0.3],\n",
    "    gradient_controls=[0.1, 0.2, 0.3],\n",
    ")\n",
    "\n",
    "colormap_metadata = await service_client.create_colormap(colormap, \"continuous 1\")\n",
    "colormap_id = colormap_metadata.id\n",
    "print(f\"Created colormap: {colormap_id}: {colormap_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a discrete colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.colormaps.data import DiscreteColormap\n",
    "\n",
    "colormap = DiscreteColormap(\n",
    "    colors=[[0, 0, 0], [123, 123, 123], [255, 255, 255]],\n",
    "    end_points=[0.1, 0.2],\n",
    "    end_inclusive=[True, False],\n",
    ")\n",
    "\n",
    "colormap_metadata = await service_client.create_colormap(colormap, \"discrete 1\")\n",
    "colormap_id = colormap_metadata.id\n",
    "print(f\"Created colormap: {colormap_id}: {colormap_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a category colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.colormaps.data import CategoryColormap\n",
    "\n",
    "colormap = CategoryColormap(\n",
    "    colors=[[0, 0, 0], [123, 123, 123], [255, 255, 255]],\n",
    "    map=[\"a\", \"b\", \"c\"],\n",
    ")\n",
    "\n",
    "colormap_metadata = await service_client.create_colormap(colormap, \"category 1\")\n",
    "colormap_id = colormap_metadata.id\n",
    "print(f\"Created colormap: {colormap_id}: {colormap_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get a colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colormap_metadata = await service_client.get_colormap_by_id(colormap_id)\n",
    "print(f\"Got colormap: {colormap_metadata.id}: {colormap_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get all colormaps on a workspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colormaps = await service_client.get_colormap_collection()\n",
    "for colormap in colormaps:\n",
    "    print(f\"{colormap.id}: {colormap.name} [{colormap.created_at}]\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create an association"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from uuid import UUID\n",
    "\n",
    "from evo.colormaps.data import Association\n",
    "\n",
    "object_id = UUID(\"object-uuid\")  # add your GO object id here\n",
    "\n",
    "association = Association(\n",
    "    attribute_id=\"attribute-id\",  # and add your GO attribute id here\n",
    "    colormap_id=colormap_id,\n",
    ")\n",
    "\n",
    "association_metadata = await service_client.create_association(object_id, association)\n",
    "print(f\"Created association: {association_metadata.id}: {association_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bulk create associations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "object_id = UUID(\"object-uuid\")  # add your GO object id here\n",
    "\n",
    "associations = [\n",
    "    Association(\n",
    "        attribute_id=\"attribute-id_1\",  # and add your GO attribute id here\n",
    "        colormap_id=colormap_id,\n",
    "    ),\n",
    "    Association(\n",
    "        attribute_id=\"attribute-uuid_2\",  # and add your GO attribute id here\n",
    "        colormap_id=colormap_id,\n",
    "    ),\n",
    "]\n",
    "\n",
    "associations_metadata = await service_client.create_batch_associations(object_id, [])\n",
    "for association_metadata in associations_metadata:\n",
    "    print(f\"Created association: {association_metadata.id}: {association_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get an association"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "association_metadata = await service_client.get_association(object_id, association_metadata.id)\n",
    "print(f\"Got association: {association_metadata.id}: {association_metadata.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get all associations on a GO object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "associations = await service_client.get_association_collection(object_id)\n",
    "for association in associations:\n",
    "    print(f\"{association.id}: {association.name}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "evo-python-sdk",
   "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.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
