{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clustering\n",
    "\n",
    "This example will show you how to perform dimensionality reduction and visualize \n",
    "any resulting clusters. We will also show how certain preprocessing steps can \n",
    "be done using `preprocess.clean_data`.\n",
    "\n",
    "### Datasets\n",
    "\n",
    "This example makes use of the `ros-mito` data set which contains features \n",
    "extracted from high-content images."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "from hcitools import datasets, plot, analysis, preprocess\n",
    "\n",
    "# Load dataset\n",
    "ros = datasets.load_dataset('ros-mito')\n",
    "\n",
    "# Plotly renderer\n",
    "plot.set_renderer('notebook')  # Use this when running notebook\n",
    "plot.set_renderer('iframe_connected')  # Use this when rendering docs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Preprocessing\n",
    "meta = ['Well', 'Row', 'Column', 'Timepoint', 'Compound', 'Conc']\n",
    "df, dropped, LOG = preprocess.clean_data(\n",
    "    data=ros,\n",
    "    metacols=meta,\n",
    "    dropna=True,\n",
    "    drop_low_var=0.0,\n",
    "    corr_thresh=0.9,\n",
    "    verbose=True\n",
    ")\n",
    "df = df.set_index(meta)\n",
    "\n",
    "# Generate clusters with default arguments\n",
    "proj, expvar = analysis.dim_reduction(data=df, method=['pca', 'tsne'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"720px\"\n",
       "    height=\"420\"\n",
       "    src=\"iframe_figures/figure_3.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot PCA components\n",
    "fig = plot.pca_comps(proj, expvar, n_comps=3)\n",
    "fig.update_layout(width=700, height=400)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"770px\"\n",
       "    height=\"470\"\n",
       "    src=\"iframe_figures/figure_4.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Compare 2 compounds\n",
    "fig = plot.clusters(proj, 'Sorafenib Tosylate', 'Imatinib mesylate', 'tsne')\n",
    "fig.update_layout(width=750, height=450)\n",
    "\n",
    "fig.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 ('hcitools')",
   "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.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "da8dbd75bce17023e286ecaa5b28898715c4e19720edbfe0df36bac9075c0ff0"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
