{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "86473a6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  0 -1  0  1  1 -1 -1 -1 -1 -1  2 -1  2  3  3  3  3 -1 -1 -1  4  5  5\n",
      "  5  4  4  4 -1  4 -1  1  3  3  3  6  6  6  6  6  6 -1  6  6  6  6  7  7\n",
      "  7  7  7  7  3  6  6  6  6  6  6]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11700 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",
    "res = runDBSCAN(tlist, eps=30, min_samples=2)\n",
    "print(res.labels_)\n",
    "tlsh_dendrogram(tlist, labelList=res.labels_)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4d3291fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "reading in dataDir/mb_1K.csv\n",
      "written  dataDir/mb_1K_dbscan_out.txt\n",
      "DBSCAN ran in 0.8683 seconds\n",
      "nclusters= 82\n",
      "nDistCalc= 1046895\n",
      "\n",
      "reading in dataDir/mb_2K.csv\n",
      "written  dataDir/mb_2K_dbscan_out.txt\n",
      "DBSCAN ran in 3.6286 seconds\n",
      "nclusters= 163\n",
      "nDistCalc= 4187002\n",
      "\n",
      "reading in dataDir/mb_3K.csv\n",
      "written  dataDir/mb_3K_dbscan_out.txt\n",
      "DBSCAN ran in 7.4558 seconds\n",
      "nclusters= 231\n",
      "nDistCalc= 9257878\n",
      "\n",
      "reading in dataDir/mb_4K.csv\n",
      "written  dataDir/mb_4K_dbscan_out.txt\n",
      "DBSCAN ran in 13.6460 seconds\n",
      "nclusters= 285\n",
      "nDistCalc= 16694273\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from pylib.tlsh_lib import *\n",
    "import time\n",
    "\n",
    "# (tlist, labelList) = tlsh_csvfile(\"dataDir/mb_10K.csv\")\n",
    "for size in range(1,5):\n",
    "    datafile = \"dataDir/mb_\" + str(size) + \"K.csv\"\n",
    "    print(\"reading in \" + datafile)\n",
    "    (tlist, labelList) = tlsh_csvfile(datafile)\n",
    "    if (tlist is not None):\n",
    "        resetDistCalc()\n",
    "        tic = time.perf_counter()\n",
    "        res = runDBSCAN(tlist, eps=30, min_samples=2, algorithm='auto')\n",
    "        toc = time.perf_counter()\n",
    "        nDistCalc = lookupDistCalc()\n",
    "        \n",
    "        outfile = \"dataDir/mb_\" + str(size) + \"K_dbscan_out.txt\"\n",
    "        outputClusters(outfile, tlist, res.labels_, labelList)\n",
    "        \n",
    "        print(f\"DBSCAN ran in {toc - tic:0.4f} seconds\")\n",
    "        nclusters = max(res.labels_)\n",
    "        print(\"nclusters=\", nclusters)\n",
    "        print(\"nDistCalc=\", nDistCalc)\n",
    "        # print(res.labels_)\n",
    "        print()\n",
    "    # end if\n",
    "# end for\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "990ef513",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t\t\tDBSCAN\n",
      "size\ttime\tnclust\tndist\n",
      "1K\t0.8863\t82\t1046895\n",
      "2K\t3.4965\t163\t4187002\n",
      "3K\t7.5469\t231\t9257878\n",
      "4K\t13.6727\t285\t16694273\n",
      "5K\t20.9427\t336\t25877733\n",
      "6K\t29.8737\t391\t37019158\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16044 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pylib.tlsh_lib import *\n",
    "import time\n",
    "\n",
    "# (tlist, labelList) = tlsh_csvfile(\"dataDir/mb_10K.csv\")\n",
    "\n",
    "print(\"\\t\\t\\tDBSCAN\")\n",
    "print(\"size\\ttime\\tnclust\\tndist\")\n",
    "\n",
    "largeK = 7\n",
    "dbs_time = [0] * (largeK)\n",
    "\n",
    "for size in range(1, largeK):\n",
    "    datafile = \"dataDir/mb_\" + str(size) + \"K.csv\"\n",
    "    (tlist, labelList) = tlsh_csvfile(datafile)\n",
    "    if (tlist is not None):        \n",
    "        resetDistCalc()\n",
    "        tic = time.perf_counter()\n",
    "        dbs_res = runDBSCAN(tlist, eps=30, min_samples=2, algorithm='auto')\n",
    "        toc = time.perf_counter()\n",
    "        dbs_nDistCalc = lookupDistCalc()\n",
    "        dbs_time[size] = toc-tic\n",
    "        dtime          = f\"{dbs_time[size]:0.4f}\"\n",
    "        dbs_nclusters = max(dbs_res.labels_)\n",
    "        \n",
    "        print( str(size) + \"K\\t\" + dtime + \"\\t\" + str(dbs_nclusters) + \"\\t\" + str(dbs_nDistCalc)  )\n",
    "    # end if\n",
    "# end for\n",
    "\n",
    "srange = [x for x in range(0, largeK*1000, 1000)]\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "line2, = plt.plot(srange, dbs_time,  label='DBSCAN')\n",
    "plt.legend(handles=[line2], loc='upper left')\n",
    "plt.xlabel('Size dataset', fontsize=15)\n",
    "plt.ylabel('time (sec)', fontsize=15)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "765b8c79",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c1d9e09",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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
}
