{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d1f5f17d-9949-4707-b553-e03c15bc59ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import dadac \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aeb8a19b-be6d-48d8-86e0-479b4d6d99cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1842842, 5)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.fromfile(\"bb/ordered_data.npy\", dtype = np.float64)\n",
    "x = x.reshape((x.shape[0]//5, 5))\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "550c24bc-062c-4e9a-83ab-5fb51aa44dc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are running in a notebook maybe the timing output will break, but everything should be fine \n"
     ]
    }
   ],
   "source": [
    "data = dadac.Data(x) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "83e48b9f-1739-4b4c-aeea-87fb732c02e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Building the KDtree v2:\n",
      "\tTotal time: 1.573s\n",
      "\n",
      "knn search:\n",
      "\tTotal time: 19.789s\n",
      "\n",
      "ID estimation:\n",
      "\tID value: 3.920865\n",
      "\tTotal time: 0.398s\n",
      "\n",
      "Density and k* estimation:\n",
      "\tTotal time: 4.784s\n",
      "\n",
      "H1: Preliminary cluster assignment\n",
      "\tFinding putative centers: 0.159s\n",
      "\tFinding actual centers:   0.714s\n",
      "\tTentative clustering:     0.149s\n",
      "\tFinalizing clustering:    0.003s\n",
      "\n",
      "\tFound 6048 clusters\n",
      "\tTotal time: 1.026s\n",
      "\n",
      "H2: Finding border points\n",
      "\tTotal time: 0.729s\n",
      "\n",
      "H3: Merging clusters\n",
      "Using dense implementation\n",
      "\tFinding merges:   0.032s\n",
      "\tCluster merging:  0.344s\n",
      "\tFinal operations: 0.086s\n",
      "\n",
      "\tFound 14543 possible merges\n",
      "\tSurviving clusters 1817\n",
      "\tTotal time: 0.462s\n",
      "\n"
     ]
    }
   ],
   "source": [
    "data.compute_distances(300)\n",
    "data.compute_id_2NN()\n",
    "#data.id = 4\n",
    "data.compute_density_kstarNN()\n",
    "data.compute_clustering_ADP(1.97, halo = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "314b516a-02ec-412a-bc7e-1de8f790e429",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.920865231328582\n"
     ]
    }
   ],
   "source": [
    "den_gt = data.log_den\n",
    "den_comp = np.fromfile(\"bb/ordered_density.npy\", np.float64)\n",
    "print(data.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "92225324-e798-4388-a022-b6bb8906768c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.09105674761858006"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.average(np.abs(den_comp - den_gt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "67a8bfdf-f1c1-421a-ab06-0ed9dc57b1b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3.64725383, -4.53602916, -4.30573383, ...,  7.18148125,\n",
       "       -6.25416517, -3.54944958])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "den_gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a7093d5f-d40b-43d9-a4d0-1f70638d9bbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3.71523428, -4.60212517, -4.37644339, ...,  6.98639965,\n",
       "       -6.30713081, -3.61603928])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "den_comp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f02c64d5-18b8-4b88-a756-e00fa9295e64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-9.772275924682617 -1.761254217048486 max diff 8.011021707634132\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "979479"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "a = np.argmax(np.abs(den_comp - den_gt))\n",
    "print(den_comp[a], den_gt[a], \"max diff\", np.abs(den_comp[a] - den_gt[a]))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cd441b5f-f7d8-476c-9b8f-2c1a7f92380f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.kstar[a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1338cf31-e49d-47da-bbb4-1225db9aefd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x75ebcd477e90>]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 32448 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(np.abs(den_comp - den_gt), \".-\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a309b4e5-0ae4-4b55-a971-6fb05320c49b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x75eb98cc4090>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 1211252 characters]",
      "text/plain": [
       "<Figure size 1200x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "f = np.where(data.cluster_assignment != -1)\n",
    "#plt.scatter(x[f,0], x[f,1], c = den_gt[f], s = 0.1, norm = \"log\", cmap= \"inferno\")\n",
    "plt.figure(figsize=(12,10))\n",
    "plt.scatter(x[:,0], x[:,2], c = den_gt, s = 0.2, cmap= \"inferno\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f4b69f72-a7d2-46f0-9935-cf348b943ad2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35940 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "_ = plt.hist2d(x[:,0],x[:,1], bins = 100, norm = \"log\", cmap = \"inferno\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f7693e27-fd32-41e4-9153-12c67b1a2a67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x75eb90706990>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 394012 characters]",
      "text/plain": [
       "<Figure size 1200x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "fig = plt.figure(figsize=(12,10))\n",
    "ax = fig.add_subplot( projection='3d')\n",
    "f = np.where(data.cluster_assignment != -1)\n",
    "\n",
    "#ax.scatter(x[:,0], x[:,1], x[:,2], c = den_gt, s = 0.2, cmap= \"inferno\")\n",
    "ax.scatter(x[f,0], x[f,1], x[f,2], c = data.cluster_assignment[f], s = 0.2, cmap= \"inferno\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cd10d59-0d44-4841-937f-acbf64921fa3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nope",
   "language": "python",
   "name": "nope"
  },
  "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
