{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "392a7bec",
   "metadata": {},
   "source": [
    "# Step 1 Create a dendrogram from nef.csv\n",
    "There is more details on dendrogram at https://wiki.jarvis.trendmicro.com/display/PRO/Creating+Malware+Dendrograms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82cc0811",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22680 characters]\n",
      "text/plain": [
       "<Figure size 1080x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pylib.tlsh_lib import *\n",
    "\n",
    "(tlist, labelList) = tlsh_csvfile(\"nef.csv\")\n",
    "\n",
    "tlsh_dendrogram(tlist, labelList=labelList)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "462f9a18",
   "metadata": {},
   "source": [
    "# Step 2 Create a dendrogram with your own labels\n",
    "Use your own list to label each point\n",
    "In the example below I label them with the cluster number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aed2fcfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 4 1 2 2 2 2 1 6 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 3 2 3 0 2 0 0 0 3 3\n",
      " 3 3 3 3 5 3 3 3 3 1 1 1 1 1 1 0 3 3 3 3 3 3]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11416 characters]\n",
      "text/plain": [
       "<Figure size 1080x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pylib.tlsh_lib import *\n",
    "\n",
    "(tlist, labelList) = tlsh_csvfile(\"nef.csv\")\n",
    "\n",
    "clusterNumber = assignCluster(tlist, n_clusters=7)\n",
    "\n",
    "print(clusterNumber)\n",
    "\n",
    "tlsh_dendrogram(tlist, labelList=clusterNumber)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cee8bd8e",
   "metadata": {},
   "source": [
    "# Step 3 Create a dendrogram for a particular cluster\n",
    "Focus on a cluster / section of the dendrogram\n",
    "In the example below I use assignCluster() to assign 7 clusters and show the dendrogram for cluster 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d32d9eb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30140 characters]\n",
      "text/plain": [
       "<Figure size 1080x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pylib.tlsh_lib import *\n",
    "\n",
    "(tlist, labelList) = tlsh_csvfile(\"nef.csv\")\n",
    "\n",
    "clusterNumber = assignCluster(tlist, n_clusters=7)\n",
    "\n",
    "(t3, l3) = selectCluster(tlist, clusterNumber, clusterIdx=3, labelList=labelList)\n",
    "\n",
    "tlsh_dendrogram(t3, labelList=l3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2b72f2e",
   "metadata": {},
   "source": [
    "# Step 4 Create a dendrogram for a different cluster\n",
    "show the dendrogram for cluster 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ed8f5fc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14372 characters]\n",
      "text/plain": [
       "<Figure size 1080x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pylib.tlsh_lib import *\n",
    "\n",
    "(tlist, labelList) = tlsh_csvfile(\"nef.csv\")\n",
    "\n",
    "clusterNumber = assignCluster(tlist, n_clusters=7)\n",
    "\n",
    "(t2, l2) = selectCluster(tlist, clusterNumber, clusterIdx=2, labelList=labelList)\n",
    "\n",
    "tlsh_dendrogram(t2, labelList=l2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
