{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f1696373-d690-4d9f-9dd6-319279d9b2a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pylcs\n",
    "import numpy as np\n",
    "from tqdm.notebook import tqdm\n",
    "import sklearn.svm\n",
    "import sklearn.kernel_ridge\n",
    "import plotly.express as px\n",
    "import sklearn.metrics\n",
    "import sklearn.model_selection\n",
    "import sklearn.manifold\n",
    "import optuna\n",
    "import scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "37e29778-e091-4b1b-8e99-0e46f91789c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pylcs\n",
    "import fasta\n",
    "from pathlib import Path\n",
    "from math import log2\n",
    "import numpy as np\n",
    "from collections import Counter\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ba6d800f-79c1-4a63-bee1-cbb28ca8ec6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sequence</th>\n",
       "      <th>target</th>\n",
       "      <th>set</th>\n",
       "      <th>validation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MESNHKSGDGLSGTQKEAALRALVQRTGYSLVQENGQRKYGGPPPG...</td>\n",
       "      <td>41.945567</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MALVFVYGTLKRGQPNHRVLRDGAHGSAAFRARGRTLEPYPLVIAG...</td>\n",
       "      <td>51.352307</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEK...</td>\n",
       "      <td>57.839199</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MCSLGLFPPPPPRGQVTLYEHNNELVTGSSYESPPPDFRGQWINLP...</td>\n",
       "      <td>51.721653</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MSKEERPGREEILECQVMWEPDSKKNTQMDRFRAAVGAACGLALES...</td>\n",
       "      <td>43.020855</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7153</th>\n",
       "      <td>MPEDQAGAAMEEASPYSLLDICLNFLTTHLEKFCSARQDGTLCLQE...</td>\n",
       "      <td>50.801858</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7154</th>\n",
       "      <td>MASDTPESLMALCTDFCLRNLDGTLGYLLDKETLRLHPDIFLPSEI...</td>\n",
       "      <td>53.311340</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7155</th>\n",
       "      <td>MAAPRPSPAISVSVSAPAFYAPQKKFGPVVAPKPKVNPFRPGDSEP...</td>\n",
       "      <td>60.921549</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7156</th>\n",
       "      <td>MGNAPSHSSEDEAAAAGGEGWGPHQDWAAVSGTTPGPGVAAPALPP...</td>\n",
       "      <td>49.987949</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7157</th>\n",
       "      <td>MAASRSTRVTRSTVGLNGLDESFCGRTLRNRSIAHPEEISSNSQVR...</td>\n",
       "      <td>42.947435</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7158 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               sequence     target    set  \\\n",
       "0     MESNHKSGDGLSGTQKEAALRALVQRTGYSLVQENGQRKYGGPPPG...  41.945567  train   \n",
       "1     MALVFVYGTLKRGQPNHRVLRDGAHGSAAFRARGRTLEPYPLVIAG...  51.352307   test   \n",
       "2     MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEK...  57.839199   test   \n",
       "3     MCSLGLFPPPPPRGQVTLYEHNNELVTGSSYESPPPDFRGQWINLP...  51.721653  train   \n",
       "4     MSKEERPGREEILECQVMWEPDSKKNTQMDRFRAAVGAACGLALES...  43.020855  train   \n",
       "...                                                 ...        ...    ...   \n",
       "7153  MPEDQAGAAMEEASPYSLLDICLNFLTTHLEKFCSARQDGTLCLQE...  50.801858  train   \n",
       "7154  MASDTPESLMALCTDFCLRNLDGTLGYLLDKETLRLHPDIFLPSEI...  53.311340  train   \n",
       "7155  MAAPRPSPAISVSVSAPAFYAPQKKFGPVVAPKPKVNPFRPGDSEP...  60.921549  train   \n",
       "7156  MGNAPSHSSEDEAAAAGGEGWGPHQDWAAVSGTTPGPGVAAPALPP...  49.987949   test   \n",
       "7157  MAASRSTRVTRSTVGLNGLDESFCGRTLRNRSIAHPEEISSNSQVR...  42.947435   test   \n",
       "\n",
       "      validation  \n",
       "0          False  \n",
       "1          False  \n",
       "2          False  \n",
       "3          False  \n",
       "4          False  \n",
       "...          ...  \n",
       "7153       False  \n",
       "7154       False  \n",
       "7155       False  \n",
       "7156       False  \n",
       "7157       False  \n",
       "\n",
       "[7158 rows x 4 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_file = \"FLIP/meltome/splits/human_cell.csv\"\n",
    "# input_file = \"FLIP/meltome/splits/mixed_split.csv\"\n",
    "# input_file = \"FLIP/meltome/splits/human.csv\"\n",
    "# split_df = pd.read_csv(\"FLIP/meltome/splits/mixed_split.csv\")\n",
    "dataset_name = Path(input_file).stem\n",
    "split_df = pd.read_csv(input_file)\n",
    "split_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "32295c96-1c3f-49fe-bc77-06988ab1b9ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(44, 34350, 469.0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sequence_lengths = [len(x) for x in split_df[\"sequence\"].tolist()]\n",
    "np.min(sequence_lengths), np.max(sequence_lengths), np.median(sequence_lengths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "39115ae8-2a18-4525-a175-020e9bb78ca5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sequence</th>\n",
       "      <th>target</th>\n",
       "      <th>set</th>\n",
       "      <th>validation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MALVFVYGTLKRGQPNHRVLRDGAHGSAAFRARGRTLEPYPLVIAG...</td>\n",
       "      <td>51.352307</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEK...</td>\n",
       "      <td>57.839199</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>MESESESGAAADTPPLETLSFHGDEEIIEVVELDPGPPDPDDLAQE...</td>\n",
       "      <td>48.286298</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>MAFWCQRDSYAREFTTTVVSCCPAELQTEGSNGKKEVLSGFQVVLE...</td>\n",
       "      <td>47.725895</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>MTLQELVHKAASCYMDRVAVCFDECNNQLPVYYTYKTVVNAASELS...</td>\n",
       "      <td>47.951626</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7143</th>\n",
       "      <td>MEFAAENEGKSGGGLHSVAEGVRLSPEPGREGVRDLAGAEEFGGGE...</td>\n",
       "      <td>43.069161</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7144</th>\n",
       "      <td>MSVMDLANTCSSFQSDLDFCSDCGSVLPLPGAQDTVTCIRCGFNIN...</td>\n",
       "      <td>50.046440</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7149</th>\n",
       "      <td>MMAADIPRVTTPLSSLVQVPQEEDRQEEEVTTMILEDDSWVQEAVL...</td>\n",
       "      <td>52.380485</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7156</th>\n",
       "      <td>MGNAPSHSSEDEAAAAGGEGWGPHQDWAAVSGTTPGPGVAAPALPP...</td>\n",
       "      <td>49.987949</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7157</th>\n",
       "      <td>MAASRSTRVTRSTVGLNGLDESFCGRTLRNRSIAHPEEISSNSQVR...</td>\n",
       "      <td>42.947435</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1366 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               sequence     target   set  \\\n",
       "1     MALVFVYGTLKRGQPNHRVLRDGAHGSAAFRARGRTLEPYPLVIAG...  51.352307  test   \n",
       "2     MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEK...  57.839199  test   \n",
       "8     MESESESGAAADTPPLETLSFHGDEEIIEVVELDPGPPDPDDLAQE...  48.286298  test   \n",
       "11    MAFWCQRDSYAREFTTTVVSCCPAELQTEGSNGKKEVLSGFQVVLE...  47.725895  test   \n",
       "12    MTLQELVHKAASCYMDRVAVCFDECNNQLPVYYTYKTVVNAASELS...  47.951626  test   \n",
       "...                                                 ...        ...   ...   \n",
       "7143  MEFAAENEGKSGGGLHSVAEGVRLSPEPGREGVRDLAGAEEFGGGE...  43.069161  test   \n",
       "7144  MSVMDLANTCSSFQSDLDFCSDCGSVLPLPGAQDTVTCIRCGFNIN...  50.046440  test   \n",
       "7149  MMAADIPRVTTPLSSLVQVPQEEDRQEEEVTTMILEDDSWVQEAVL...  52.380485  test   \n",
       "7156  MGNAPSHSSEDEAAAAGGEGWGPHQDWAAVSGTTPGPGVAAPALPP...  49.987949  test   \n",
       "7157  MAASRSTRVTRSTVGLNGLDESFCGRTLRNRSIAHPEEISSNSQVR...  42.947435  test   \n",
       "\n",
       "      validation  \n",
       "1          False  \n",
       "2          False  \n",
       "8          False  \n",
       "11         False  \n",
       "12         False  \n",
       "...          ...  \n",
       "7143       False  \n",
       "7144       False  \n",
       "7149       False  \n",
       "7156       False  \n",
       "7157       False  \n",
       "\n",
       "[1366 rows x 4 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "split_df[split_df[\"set\"] == \"test\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3b5c9769-ed2d-4635-8738-7e8701d0bf7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# rng = np.random.default_rng(23)\n",
    "# train_size = 2**10\n",
    "# test_size = 2**8\n",
    "train_df = split_df[(split_df[\"set\"] == \"train\") & (split_df[\"validation\"] != True)]\n",
    "# train_idx = rng.choice(len(train_df), size=train_size, replace=False)\n",
    "# train_df = train_df.iloc[train_idx]\n",
    "valid_df = split_df[(split_df[\"set\"] == \"train\") & (split_df[\"validation\"] == True)]\n",
    "test_df = split_df[split_df[\"set\"] == \"test\"]\n",
    "# test_idx = rng.choice(len(test_df), size=test_size, replace=False)\n",
    "# test_df = test_df.iloc[test_idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ac851454-6593-4d00-b793-33a68346d7c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sequence</th>\n",
       "      <th>target</th>\n",
       "      <th>set</th>\n",
       "      <th>validation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MESNHKSGDGLSGTQKEAALRALVQRTGYSLVQENGQRKYGGPPPG...</td>\n",
       "      <td>41.945567</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MCSLGLFPPPPPRGQVTLYEHNNELVTGSSYESPPPDFRGQWINLP...</td>\n",
       "      <td>51.721653</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MSKEERPGREEILECQVMWEPDSKKNTQMDRFRAAVGAACGLALES...</td>\n",
       "      <td>43.020855</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MRSSCVLLTALVALAAYYVYIPLPGSVSDPWKLMLLDATFRGAQQV...</td>\n",
       "      <td>60.513499</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MNYARFITAASAARNPSPIRTMTDILSRGPKSMISLAGGLPNPNMF...</td>\n",
       "      <td>56.774612</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7151</th>\n",
       "      <td>MWERLNCAAEDFYSRLLQKFNEEKKGIRKDPFLYEADVQVQLISKG...</td>\n",
       "      <td>48.740474</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7152</th>\n",
       "      <td>MEAAETEAEAAALEVLAEVAGILEPVGLQEEAELPAKILVEFVVDS...</td>\n",
       "      <td>47.743331</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7153</th>\n",
       "      <td>MPEDQAGAAMEEASPYSLLDICLNFLTTHLEKFCSARQDGTLCLQE...</td>\n",
       "      <td>50.801858</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7154</th>\n",
       "      <td>MASDTPESLMALCTDFCLRNLDGTLGYLLDKETLRLHPDIFLPSEI...</td>\n",
       "      <td>53.311340</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7155</th>\n",
       "      <td>MAAPRPSPAISVSVSAPAFYAPQKKFGPVVAPKPKVNPFRPGDSEP...</td>\n",
       "      <td>60.921549</td>\n",
       "      <td>train</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5149 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               sequence     target    set  \\\n",
       "0     MESNHKSGDGLSGTQKEAALRALVQRTGYSLVQENGQRKYGGPPPG...  41.945567  train   \n",
       "3     MCSLGLFPPPPPRGQVTLYEHNNELVTGSSYESPPPDFRGQWINLP...  51.721653  train   \n",
       "4     MSKEERPGREEILECQVMWEPDSKKNTQMDRFRAAVGAACGLALES...  43.020855  train   \n",
       "5     MRSSCVLLTALVALAAYYVYIPLPGSVSDPWKLMLLDATFRGAQQV...  60.513499  train   \n",
       "6     MNYARFITAASAARNPSPIRTMTDILSRGPKSMISLAGGLPNPNMF...  56.774612  train   \n",
       "...                                                 ...        ...    ...   \n",
       "7151  MWERLNCAAEDFYSRLLQKFNEEKKGIRKDPFLYEADVQVQLISKG...  48.740474  train   \n",
       "7152  MEAAETEAEAAALEVLAEVAGILEPVGLQEEAELPAKILVEFVVDS...  47.743331  train   \n",
       "7153  MPEDQAGAAMEEASPYSLLDICLNFLTTHLEKFCSARQDGTLCLQE...  50.801858  train   \n",
       "7154  MASDTPESLMALCTDFCLRNLDGTLGYLLDKETLRLHPDIFLPSEI...  53.311340  train   \n",
       "7155  MAAPRPSPAISVSVSAPAFYAPQKKFGPVVAPKPKVNPFRPGDSEP...  60.921549  train   \n",
       "\n",
       "      validation  \n",
       "0          False  \n",
       "3          False  \n",
       "4          False  \n",
       "5          False  \n",
       "6          False  \n",
       "...          ...  \n",
       "7151       False  \n",
       "7152       False  \n",
       "7153       False  \n",
       "7154       False  \n",
       "7155       False  \n",
       "\n",
       "[5149 rows x 4 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f2ee6f8-7b15-4eec-ab01-aff89ac0c647",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sequence</th>\n",
       "      <th>target</th>\n",
       "      <th>set</th>\n",
       "      <th>validation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>MRKRTEPVALEHERCAAAGSSSSGSAAAALDADCRLKQNLRLTGPA...</td>\n",
       "      <td>53.032091</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>MADDLEQQSQGWLSSWLPTWRPTSMSQLKNVEARILQCLQNKFLAR...</td>\n",
       "      <td>44.226511</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>MAAEEEEVDSADTGERSGWLTGWLPTWCPTSISHLKEAEEKMLKCV...</td>\n",
       "      <td>49.116455</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>MAGTYSSTLKTLEDLTLDSGYGAGDSCRSLSLSSSKSNSQALNSSA...</td>\n",
       "      <td>42.949469</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>MSAKAISEQTGKELLYKFICTTSAIQNRFKYARVTPDTDWARLLQD...</td>\n",
       "      <td>50.648207</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7051</th>\n",
       "      <td>MNASSEGESFAGSVQIPGGTTVLVELTPDIHICGICKQQFNNLDAF...</td>\n",
       "      <td>48.105416</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7075</th>\n",
       "      <td>MEAEETMECLQEFPEHHKMILDRLNEQREQDRFTDITLIVDGHHFK...</td>\n",
       "      <td>45.869794</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7102</th>\n",
       "      <td>MQAVVPLNKMTAISPEPQTLASTEQNEVPRVVTSGEQEAILRGNAA...</td>\n",
       "      <td>50.025723</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7129</th>\n",
       "      <td>MAQASLLACEGLAGVSLVPTAASKKMMLSQIASKQAENGERAGSPD...</td>\n",
       "      <td>48.729643</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7147</th>\n",
       "      <td>MSTKNFRVSDGDWICPDKKCGNVNFARRTSCNRCGREKTTEAKMMK...</td>\n",
       "      <td>62.961801</td>\n",
       "      <td>train</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>643 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               sequence     target    set  \\\n",
       "43    MRKRTEPVALEHERCAAAGSSSSGSAAAALDADCRLKQNLRLTGPA...  53.032091  train   \n",
       "47    MADDLEQQSQGWLSSWLPTWRPTSMSQLKNVEARILQCLQNKFLAR...  44.226511  train   \n",
       "48    MAAEEEEVDSADTGERSGWLTGWLPTWCPTSISHLKEAEEKMLKCV...  49.116455  train   \n",
       "57    MAGTYSSTLKTLEDLTLDSGYGAGDSCRSLSLSSSKSNSQALNSSA...  42.949469  train   \n",
       "80    MSAKAISEQTGKELLYKFICTTSAIQNRFKYARVTPDTDWARLLQD...  50.648207  train   \n",
       "...                                                 ...        ...    ...   \n",
       "7051  MNASSEGESFAGSVQIPGGTTVLVELTPDIHICGICKQQFNNLDAF...  48.105416  train   \n",
       "7075  MEAEETMECLQEFPEHHKMILDRLNEQREQDRFTDITLIVDGHHFK...  45.869794  train   \n",
       "7102  MQAVVPLNKMTAISPEPQTLASTEQNEVPRVVTSGEQEAILRGNAA...  50.025723  train   \n",
       "7129  MAQASLLACEGLAGVSLVPTAASKKMMLSQIASKQAENGERAGSPD...  48.729643  train   \n",
       "7147  MSTKNFRVSDGDWICPDKKCGNVNFARRTSCNRCGREKTTEAKMMK...  62.961801  train   \n",
       "\n",
       "      validation  \n",
       "43          True  \n",
       "47          True  \n",
       "48          True  \n",
       "57          True  \n",
       "80          True  \n",
       "...          ...  \n",
       "7051        True  \n",
       "7075        True  \n",
       "7102        True  \n",
       "7129        True  \n",
       "7147        True  \n",
       "\n",
       "[643 rows x 4 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "valid_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e8b86a19-7a5c-4a73-91d3-9752b79e7cee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sequence</th>\n",
       "      <th>target</th>\n",
       "      <th>set</th>\n",
       "      <th>validation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MALVFVYGTLKRGQPNHRVLRDGAHGSAAFRARGRTLEPYPLVIAG...</td>\n",
       "      <td>51.352307</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEK...</td>\n",
       "      <td>57.839199</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>MESESESGAAADTPPLETLSFHGDEEIIEVVELDPGPPDPDDLAQE...</td>\n",
       "      <td>48.286298</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>MAFWCQRDSYAREFTTTVVSCCPAELQTEGSNGKKEVLSGFQVVLE...</td>\n",
       "      <td>47.725895</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>MTLQELVHKAASCYMDRVAVCFDECNNQLPVYYTYKTVVNAASELS...</td>\n",
       "      <td>47.951626</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7143</th>\n",
       "      <td>MEFAAENEGKSGGGLHSVAEGVRLSPEPGREGVRDLAGAEEFGGGE...</td>\n",
       "      <td>43.069161</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7144</th>\n",
       "      <td>MSVMDLANTCSSFQSDLDFCSDCGSVLPLPGAQDTVTCIRCGFNIN...</td>\n",
       "      <td>50.046440</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7149</th>\n",
       "      <td>MMAADIPRVTTPLSSLVQVPQEEDRQEEEVTTMILEDDSWVQEAVL...</td>\n",
       "      <td>52.380485</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7156</th>\n",
       "      <td>MGNAPSHSSEDEAAAAGGEGWGPHQDWAAVSGTTPGPGVAAPALPP...</td>\n",
       "      <td>49.987949</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7157</th>\n",
       "      <td>MAASRSTRVTRSTVGLNGLDESFCGRTLRNRSIAHPEEISSNSQVR...</td>\n",
       "      <td>42.947435</td>\n",
       "      <td>test</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1366 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               sequence     target   set  \\\n",
       "1     MALVFVYGTLKRGQPNHRVLRDGAHGSAAFRARGRTLEPYPLVIAG...  51.352307  test   \n",
       "2     MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEK...  57.839199  test   \n",
       "8     MESESESGAAADTPPLETLSFHGDEEIIEVVELDPGPPDPDDLAQE...  48.286298  test   \n",
       "11    MAFWCQRDSYAREFTTTVVSCCPAELQTEGSNGKKEVLSGFQVVLE...  47.725895  test   \n",
       "12    MTLQELVHKAASCYMDRVAVCFDECNNQLPVYYTYKTVVNAASELS...  47.951626  test   \n",
       "...                                                 ...        ...   ...   \n",
       "7143  MEFAAENEGKSGGGLHSVAEGVRLSPEPGREGVRDLAGAEEFGGGE...  43.069161  test   \n",
       "7144  MSVMDLANTCSSFQSDLDFCSDCGSVLPLPGAQDTVTCIRCGFNIN...  50.046440  test   \n",
       "7149  MMAADIPRVTTPLSSLVQVPQEEDRQEEEVTTMILEDDSWVQEAVL...  52.380485  test   \n",
       "7156  MGNAPSHSSEDEAAAAGGEGWGPHQDWAAVSGTTPGPGVAAPALPP...  49.987949  test   \n",
       "7157  MAASRSTRVTRSTVGLNGLDESFCGRTLRNRSIAHPEEISSNSQVR...  42.947435  test   \n",
       "\n",
       "      validation  \n",
       "1          False  \n",
       "2          False  \n",
       "8          False  \n",
       "11         False  \n",
       "12         False  \n",
       "...          ...  \n",
       "7143       False  \n",
       "7144       False  \n",
       "7149       False  \n",
       "7156       False  \n",
       "7157       False  \n",
       "\n",
       "[1366 rows x 4 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2170cd26-f721-494b-b8cb-905159abc3f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5149, 643, 1366)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_df), len(valid_df), len(test_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6c2a64f0-f021-41e1-9086-06073d4f40ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Export sequences to text file in order to compute LCS in parallel\n",
    "# After runnig this cell, use the pairwise_distances.py script to compute (upper triangular part of symmetric) LCS matrices.\n",
    "# The script supports options for computing submatrices, to be used for distributing the computation on a cluster. The simplest example is:\n",
    "# $ python3 pairwise_distances.py sequences_FLIP_human_cell.txt sequences_FLIP_human_cell.npy\n",
    "# Then use the notebook \"Assemble Distance Matrices.npy\" to assemble submatrices and compute the \n",
    "# lower triangular entries. Output the result into a file \"lcs_FLIP_human_cell.npy\"\n",
    "\n",
    "all_sequences = [x.sequence for x in split_df.itertuples()]\n",
    "with open(f\"sequences_FLIP_{dataset_name}.txt\", \"w+\") as fh:\n",
    "    for seq in all_sequences:\n",
    "        fh.write(seq)\n",
    "        fh.write(\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4b35cd25-5f3a-49d8-a20a-1428dabb47c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This cell assumes that LCS matrices have been generated using the external script, as described in the previous cell\n",
    "\n",
    "lcs_matrix = np.load(f\"lcs_FLIP_{dataset_name}.npy\").astype(\"uint16\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9152122e-9484-475c-b516-9d1d3009f7e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "harmonic_series = np.hstack([[0], np.cumsum(1/np.arange(1, 2**20))])\n",
    "def compute_harmonic_edit_distance(la, lb, lcs):\n",
    "    return 2*harmonic_series[la+lb-lcs] - harmonic_series[la] - harmonic_series[lb]\n",
    "\n",
    "def compute_edit_distance(la, lb, lcs):\n",
    "    return la+lb-2*lcs\n",
    "\n",
    "def compute_steinhaus_distance(la, lb, lcs):\n",
    "    ed = compute_edit_distance(la, lb, lcs)\n",
    "    return 2*ed / (la + lb + ed)\n",
    "\n",
    "def extract_lcs(lcs_matrix, row_df, col_df):\n",
    "    row_seqs = [x.sequence for x in row_df.itertuples()]\n",
    "    col_seqs = [x.sequence for x in col_df.itertuples()]\n",
    "    lcs = np.zeros((len(row_seqs), len(col_seqs)), dtype=\"uint16\")\n",
    "    col_idx = np.array(col_df.index.values, dtype=int)\n",
    "    for i in range(len(row_seqs)):\n",
    "            k_i = row_df.index[i]\n",
    "            lcs[i, :] = lcs_matrix[k_i, col_idx]\n",
    "    return lcs\n",
    "\n",
    "def extract_dataset(lcs_matrix, train_df, df):\n",
    "    dataset = {}\n",
    "    dataset[\"sequences\"] = [x.sequence for x in df.itertuples()]\n",
    "    dataset[\"lengths\"] = np.array([len(x) for x in dataset[\"sequences\"]])\n",
    "    train_lengths = np.array([len(x.sequence) for x in train_df.itertuples()])\n",
    "    dataset[\"target\"] = df[\"target\"]\n",
    "    dataset[\"lcs\"] = extract_lcs(lcs_matrix, df, train_df).astype(\"uint16\")\n",
    "    dataset[\"harmonic_distance\"] = compute_harmonic_edit_distance(\n",
    "        dataset[\"lengths\"][:, None], \n",
    "        train_lengths[None, :], \n",
    "        dataset[\"lcs\"]\n",
    "    )\n",
    "    dataset[\"edit_distance\"] = compute_edit_distance(\n",
    "        dataset[\"lengths\"][:, None], \n",
    "        train_lengths[None, :], \n",
    "        dataset[\"lcs\"]\n",
    "    )\n",
    "    dataset[\"steinhaus_distance\"] = compute_steinhaus_distance(\n",
    "        dataset[\"lengths\"][:, None], \n",
    "        train_lengths[None, :], \n",
    "        dataset[\"lcs\"]\n",
    "    )\n",
    "    return dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "901f31d7-e0f1-441b-a10e-6fa668431f4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train = extract_dataset(lcs_matrix, train_df, train_df)\n",
    "valid = extract_dataset(lcs_matrix, train_df, valid_df)\n",
    "test = extract_dataset(lcs_matrix, train_df, test_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "96dbefab-9d4b-483d-9365-7936ab323af3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.568010330200195"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[\"lcs\"].nbytes / 2**20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "761e23f4-523c-49a5-bfac-bcc9310547fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "202.27204132080078"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[\"harmonic_distance\"].nbytes / 2**20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a846bc2e-5179-4b43-88b5-3be09894d375",
   "metadata": {},
   "outputs": [],
   "source": [
    "import optuna\n",
    "import optuna.samplers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "eecb966a-7e37-4795-8164-81fb68fa4e86",
   "metadata": {},
   "outputs": [],
   "source": [
    "def eval_parameters(params, distance_key, train, eval):\n",
    "    gamma = params[\"gamma\"]\n",
    "    C = params[\"C\"]\n",
    "\n",
    "    train[\"kernel\"] = np.exp(-gamma*train[distance_key]**2)\n",
    "    eval[\"kernel\"] = np.exp(-gamma*eval[distance_key]**2)\n",
    "\n",
    "    classifier = sklearn.svm.SVR(C=C, kernel=\"precomputed\", verbose=False, shrinking=False, max_iter=2**16)\n",
    "    classifier.fit(train[\"kernel\"], train[\"target\"])\n",
    "\n",
    "    prediction = classifier.predict(eval[\"kernel\"])\n",
    "    # valid_accuracy = sklearn.metrics.accuracy_score(eval[\"target\"], prediction)\n",
    "\n",
    "    valid_spearman = scipy.stats.spearmanr(prediction, eval[\"target\"]).statistic\n",
    "    return valid_spearman"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "32235d5e-0637-4d2a-a9d7-672ce4282cea",
   "metadata": {},
   "outputs": [],
   "source": [
    "def objective(trial, distance_key, train, eval, gamma_range=[1e-1, 1e1]):\n",
    "    params = {}\n",
    "    params[\"gamma\"] = trial.suggest_float(\"gamma\", gamma_range[0], gamma_range[1], log=True)\n",
    "    params[\"C\"] = trial.suggest_float(\"C\", 1e-2, 1e2, log=True)\n",
    "    val = eval_parameters(params, distance_key, train, eval)\n",
    "    return -val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "141578fb-1a8b-4195-bade-95375d1ae222",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43057616718755853"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eval_parameters({\"C\": 1, \"gamma\": 1}, \"steinhaus_distance\", train, valid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "599805fa-c663-45fd-9806-90c854431064",
   "metadata": {},
   "outputs": [],
   "source": [
    "def objective_harmonic(trial):\n",
    "    return objective(trial, \"harmonic_distance\", train, valid, gamma_range=[1e-1, 1e1])\n",
    "\n",
    "def objective_edit(trial):\n",
    "    return objective(trial, \"edit_distance\", train, valid, gamma_range=[1e-4, 1e-2])\n",
    "\n",
    "def objective_steinhaus(trial):\n",
    "    return objective(trial, \"steinhaus_distance\", train, valid, gamma_range=[1e-1, 1e1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cbf953c6-115c-4b8a-8517-6ccd8afa9a28",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[I 2024-08-07 17:09:53,815] A new study created in memory with name: no-name-fbbd0dc9-b72d-4194-9623-f2088b7ae612\n",
      "/Users/bob/venv/python3.11/lib/python3.11/site-packages/sklearn/svm/_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=65536).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "[I 2024-08-07 17:09:56,962] Trial 0 finished with value: 0.005451879088899447 and parameters: {'gamma': 1.0829184866889259, 'C': 61.35506425409843}. Best is trial 0 with value: 0.005451879088899447.\n",
      "[I 2024-08-07 17:09:57,247] Trial 1 finished with value: -0.32636508988222596 and parameters: {'gamma': 3.395623408235136, 'C': 0.1347669426130217}. Best is trial 1 with value: -0.32636508988222596.\n",
      "[I 2024-08-07 17:09:57,647] Trial 2 finished with value: 0.08529833065105805 and parameters: {'gamma': 0.27675197352313574, 'C': 5.557613501759315}. Best is trial 1 with value: -0.32636508988222596.\n",
      "[I 2024-08-07 17:09:57,932] Trial 3 finished with value: -0.38561035780516756 and parameters: {'gamma': 0.2159128083221639, 'C': 0.37133840345513924}. Best is trial 3 with value: -0.38561035780516756.\n",
      "[I 2024-08-07 17:09:58,230] Trial 4 finished with value: -0.40201404525620965 and parameters: {'gamma': 1.722283712449511, 'C': 0.4443450837028267}. Best is trial 4 with value: -0.40201404525620965.\n",
      "[I 2024-08-07 17:09:58,547] Trial 5 finished with value: 0.01931407515497963 and parameters: {'gamma': 0.10114158667194365, 'C': 34.365979721157416}. Best is trial 4 with value: -0.40201404525620965.\n",
      "[I 2024-08-07 17:09:58,830] Trial 6 finished with value: -0.3042807827982148 and parameters: {'gamma': 5.887014110300643, 'C': 0.15908848917427898}. Best is trial 4 with value: -0.40201404525620965.\n",
      "[W 2024-08-07 17:10:02,104] Trial 7 failed with parameters: {'gamma': 1.5106495666125255, 'C': 81.97995282341462} because of the following error: KeyboardInterrupt().\n",
      "Traceback (most recent call last):\n",
      "  File \"/Users/bob/venv/python3.11/lib/python3.11/site-packages/optuna/study/_optimize.py\", line 196, in _run_trial\n",
      "    value_or_values = func(trial)\n",
      "                      ^^^^^^^^^^^\n",
      "  File \"/var/folders/x0/1v_6blrx48j61qyzlstn_6xc0000gn/T/ipykernel_14193/3377943962.py\", line 2, in objective_harmonic\n",
      "    return objective(trial, \"harmonic_distance\", train, valid, gamma_range=[1e-1, 1e1])\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/var/folders/x0/1v_6blrx48j61qyzlstn_6xc0000gn/T/ipykernel_14193/989954307.py\", line 5, in objective\n",
      "    val = eval_parameters(params, distance_key, train, eval)\n",
      "          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/var/folders/x0/1v_6blrx48j61qyzlstn_6xc0000gn/T/ipykernel_14193/1188972501.py\", line 9, in eval_parameters\n",
      "    classifier.fit(train[\"kernel\"], train[\"target\"])\n",
      "  File \"/Users/bob/venv/python3.11/lib/python3.11/site-packages/sklearn/base.py\", line 1351, in wrapper\n",
      "    return fit_method(estimator, *args, **kwargs)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/bob/venv/python3.11/lib/python3.11/site-packages/sklearn/svm/_base.py\", line 250, in fit\n",
      "    fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)\n",
      "  File \"/Users/bob/venv/python3.11/lib/python3.11/site-packages/sklearn/svm/_base.py\", line 329, in _dense_fit\n",
      "    ) = libsvm.fit(\n",
      "        ^^^^^^^^^^^\n",
      "  File \"sklearn/svm/_libsvm.pyx\", line 265, in sklearn.svm._libsvm.fit\n",
      "KeyboardInterrupt\n",
      "[W 2024-08-07 17:10:02,107] Trial 7 failed with value None.\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[22], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m studies \u001b[38;5;241m=\u001b[39m {}\n\u001b[1;32m      2\u001b[0m study \u001b[38;5;241m=\u001b[39m optuna\u001b[38;5;241m.\u001b[39mcreate_study(sampler\u001b[38;5;241m=\u001b[39moptuna\u001b[38;5;241m.\u001b[39msamplers\u001b[38;5;241m.\u001b[39mTPESampler(seed\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m23\u001b[39m))\n\u001b[0;32m----> 3\u001b[0m \u001b[43mstudy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptimize\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobjective_harmonic\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m32\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m      4\u001b[0m studies[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mharmonic\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m study\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/optuna/study/study.py:451\u001b[0m, in \u001b[0;36mStudy.optimize\u001b[0;34m(self, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[1;32m    348\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21moptimize\u001b[39m(\n\u001b[1;32m    349\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m    350\u001b[0m     func: ObjectiveFuncType,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    357\u001b[0m     show_progress_bar: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m    358\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    359\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Optimize an objective function.\u001b[39;00m\n\u001b[1;32m    360\u001b[0m \n\u001b[1;32m    361\u001b[0m \u001b[38;5;124;03m    Optimization is done by choosing a suitable set of hyperparameter values from a given\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    449\u001b[0m \u001b[38;5;124;03m            If nested invocation of this method occurs.\u001b[39;00m\n\u001b[1;32m    450\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m--> 451\u001b[0m     \u001b[43m_optimize\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    452\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstudy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m    453\u001b[0m \u001b[43m        \u001b[49m\u001b[43mfunc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    454\u001b[0m \u001b[43m        \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    455\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    456\u001b[0m \u001b[43m        \u001b[49m\u001b[43mn_jobs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_jobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    457\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mtuple\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43misinstance\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mIterable\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    458\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    459\u001b[0m \u001b[43m        \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    460\u001b[0m \u001b[43m        \u001b[49m\u001b[43mshow_progress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_progress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    461\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/optuna/study/_optimize.py:62\u001b[0m, in \u001b[0;36m_optimize\u001b[0;34m(study, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[1;32m     60\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m     61\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m---> 62\u001b[0m         \u001b[43m_optimize_sequential\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     63\u001b[0m \u001b[43m            \u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     64\u001b[0m \u001b[43m            \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     65\u001b[0m \u001b[43m            \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     66\u001b[0m \u001b[43m            \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     67\u001b[0m \u001b[43m            \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     68\u001b[0m \u001b[43m            \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     69\u001b[0m \u001b[43m            \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     70\u001b[0m \u001b[43m            \u001b[49m\u001b[43mreseed_sampler_rng\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m     71\u001b[0m \u001b[43m            \u001b[49m\u001b[43mtime_start\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m     72\u001b[0m \u001b[43m            \u001b[49m\u001b[43mprogress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprogress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     73\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     74\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m     75\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m:\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/optuna/study/_optimize.py:159\u001b[0m, in \u001b[0;36m_optimize_sequential\u001b[0;34m(study, func, n_trials, timeout, catch, callbacks, gc_after_trial, reseed_sampler_rng, time_start, progress_bar)\u001b[0m\n\u001b[1;32m    156\u001b[0m         \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m    158\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 159\u001b[0m     frozen_trial \u001b[38;5;241m=\u001b[39m \u001b[43m_run_trial\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    160\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m    161\u001b[0m     \u001b[38;5;66;03m# The following line mitigates memory problems that can be occurred in some\u001b[39;00m\n\u001b[1;32m    162\u001b[0m     \u001b[38;5;66;03m# environments (e.g., services that use computing containers such as GitHub Actions).\u001b[39;00m\n\u001b[1;32m    163\u001b[0m     \u001b[38;5;66;03m# Please refer to the following PR for further details:\u001b[39;00m\n\u001b[1;32m    164\u001b[0m     \u001b[38;5;66;03m# https://github.com/optuna/optuna/pull/325.\u001b[39;00m\n\u001b[1;32m    165\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m gc_after_trial:\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/optuna/study/_optimize.py:247\u001b[0m, in \u001b[0;36m_run_trial\u001b[0;34m(study, func, catch)\u001b[0m\n\u001b[1;32m    240\u001b[0m         \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mShould not reach.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    242\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m    243\u001b[0m     frozen_trial\u001b[38;5;241m.\u001b[39mstate \u001b[38;5;241m==\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mFAIL\n\u001b[1;32m    244\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m func_err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    245\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(func_err, catch)\n\u001b[1;32m    246\u001b[0m ):\n\u001b[0;32m--> 247\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m func_err\n\u001b[1;32m    248\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m frozen_trial\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/optuna/study/_optimize.py:196\u001b[0m, in \u001b[0;36m_run_trial\u001b[0;34m(study, func, catch)\u001b[0m\n\u001b[1;32m    194\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m get_heartbeat_thread(trial\u001b[38;5;241m.\u001b[39m_trial_id, study\u001b[38;5;241m.\u001b[39m_storage):\n\u001b[1;32m    195\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 196\u001b[0m         value_or_values \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    197\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mTrialPruned \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    198\u001b[0m         \u001b[38;5;66;03m# TODO(mamu): Handle multi-objective cases.\u001b[39;00m\n\u001b[1;32m    199\u001b[0m         state \u001b[38;5;241m=\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mPRUNED\n",
      "Cell \u001b[0;32mIn[21], line 2\u001b[0m, in \u001b[0;36mobjective_harmonic\u001b[0;34m(trial)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mobjective_harmonic\u001b[39m(trial):\n\u001b[0;32m----> 2\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mobjective\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mharmonic_distance\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgamma_range\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1e-1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1e1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[19], line 5\u001b[0m, in \u001b[0;36mobjective\u001b[0;34m(trial, distance_key, train, eval, gamma_range)\u001b[0m\n\u001b[1;32m      3\u001b[0m params[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgamma\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m trial\u001b[38;5;241m.\u001b[39msuggest_float(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgamma\u001b[39m\u001b[38;5;124m\"\u001b[39m, gamma_range[\u001b[38;5;241m0\u001b[39m], gamma_range[\u001b[38;5;241m1\u001b[39m], log\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m      4\u001b[0m params[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mC\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m trial\u001b[38;5;241m.\u001b[39msuggest_float(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mC\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m1e-2\u001b[39m, \u001b[38;5;241m1e2\u001b[39m, log\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m----> 5\u001b[0m val \u001b[38;5;241m=\u001b[39m \u001b[43meval_parameters\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdistance_key\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43meval\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m-\u001b[39mval\n",
      "Cell \u001b[0;32mIn[18], line 9\u001b[0m, in \u001b[0;36meval_parameters\u001b[0;34m(params, distance_key, train, eval)\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28meval\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mkernel\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mexp(\u001b[38;5;241m-\u001b[39mgamma\u001b[38;5;241m*\u001b[39m\u001b[38;5;28meval\u001b[39m[distance_key]\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m)\n\u001b[1;32m      8\u001b[0m classifier \u001b[38;5;241m=\u001b[39m sklearn\u001b[38;5;241m.\u001b[39msvm\u001b[38;5;241m.\u001b[39mSVR(C\u001b[38;5;241m=\u001b[39mC, kernel\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprecomputed\u001b[39m\u001b[38;5;124m\"\u001b[39m, verbose\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, shrinking\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, max_iter\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m16\u001b[39m)\n\u001b[0;32m----> 9\u001b[0m \u001b[43mclassifier\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrain\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mkernel\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtarget\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     11\u001b[0m prediction \u001b[38;5;241m=\u001b[39m classifier\u001b[38;5;241m.\u001b[39mpredict(\u001b[38;5;28meval\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mkernel\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m     12\u001b[0m \u001b[38;5;66;03m# valid_accuracy = sklearn.metrics.accuracy_score(eval[\"target\"], prediction)\u001b[39;00m\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/sklearn/base.py:1351\u001b[0m, in \u001b[0;36m_fit_context.<locals>.decorator.<locals>.wrapper\u001b[0;34m(estimator, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1344\u001b[0m     estimator\u001b[38;5;241m.\u001b[39m_validate_params()\n\u001b[1;32m   1346\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m config_context(\n\u001b[1;32m   1347\u001b[0m     skip_parameter_validation\u001b[38;5;241m=\u001b[39m(\n\u001b[1;32m   1348\u001b[0m         prefer_skip_nested_validation \u001b[38;5;129;01mor\u001b[39;00m global_skip_validation\n\u001b[1;32m   1349\u001b[0m     )\n\u001b[1;32m   1350\u001b[0m ):\n\u001b[0;32m-> 1351\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfit_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43mestimator\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/sklearn/svm/_base.py:250\u001b[0m, in \u001b[0;36mBaseLibSVM.fit\u001b[0;34m(self, X, y, sample_weight)\u001b[0m\n\u001b[1;32m    247\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[LibSVM]\u001b[39m\u001b[38;5;124m\"\u001b[39m, end\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    249\u001b[0m seed \u001b[38;5;241m=\u001b[39m rnd\u001b[38;5;241m.\u001b[39mrandint(np\u001b[38;5;241m.\u001b[39miinfo(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mi\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mmax)\n\u001b[0;32m--> 250\u001b[0m \u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msample_weight\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msolver_type\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkernel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrandom_seed\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mseed\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    251\u001b[0m \u001b[38;5;66;03m# see comment on the other call to np.iinfo in this file\u001b[39;00m\n\u001b[1;32m    253\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mshape_fit_ \u001b[38;5;241m=\u001b[39m X\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(X, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshape\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m (n_samples,)\n",
      "File \u001b[0;32m~/venv/python3.11/lib/python3.11/site-packages/sklearn/svm/_base.py:329\u001b[0m, in \u001b[0;36mBaseLibSVM._dense_fit\u001b[0;34m(self, X, y, sample_weight, solver_type, kernel, random_seed)\u001b[0m\n\u001b[1;32m    315\u001b[0m libsvm\u001b[38;5;241m.\u001b[39mset_verbosity_wrap(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mverbose)\n\u001b[1;32m    317\u001b[0m \u001b[38;5;66;03m# we don't pass **self.get_params() to allow subclasses to\u001b[39;00m\n\u001b[1;32m    318\u001b[0m \u001b[38;5;66;03m# add other parameters to __init__\u001b[39;00m\n\u001b[1;32m    319\u001b[0m (\n\u001b[1;32m    320\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msupport_,\n\u001b[1;32m    321\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msupport_vectors_,\n\u001b[1;32m    322\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_n_support,\n\u001b[1;32m    323\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdual_coef_,\n\u001b[1;32m    324\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mintercept_,\n\u001b[1;32m    325\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_probA,\n\u001b[1;32m    326\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_probB,\n\u001b[1;32m    327\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfit_status_,\n\u001b[1;32m    328\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_num_iter,\n\u001b[0;32m--> 329\u001b[0m ) \u001b[38;5;241m=\u001b[39m \u001b[43mlibsvm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    330\u001b[0m \u001b[43m    \u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    331\u001b[0m \u001b[43m    \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    332\u001b[0m \u001b[43m    \u001b[49m\u001b[43msvm_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msolver_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    333\u001b[0m \u001b[43m    \u001b[49m\u001b[43msample_weight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msample_weight\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    334\u001b[0m \u001b[43m    \u001b[49m\u001b[43mclass_weight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mclass_weight_\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mempty\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    335\u001b[0m \u001b[43m    \u001b[49m\u001b[43mkernel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkernel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    336\u001b[0m \u001b[43m    \u001b[49m\u001b[43mC\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mC\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    337\u001b[0m \u001b[43m    \u001b[49m\u001b[43mnu\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnu\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    338\u001b[0m \u001b[43m    \u001b[49m\u001b[43mprobability\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprobability\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    339\u001b[0m \u001b[43m    \u001b[49m\u001b[43mdegree\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdegree\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    340\u001b[0m \u001b[43m    \u001b[49m\u001b[43mshrinking\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshrinking\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    341\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtol\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtol\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    342\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcache_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcache_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    343\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcoef0\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcoef0\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    344\u001b[0m \u001b[43m    \u001b[49m\u001b[43mgamma\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_gamma\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    345\u001b[0m \u001b[43m    \u001b[49m\u001b[43mepsilon\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mepsilon\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    346\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmax_iter\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax_iter\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    347\u001b[0m \u001b[43m    \u001b[49m\u001b[43mrandom_seed\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrandom_seed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    348\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    350\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_warn_from_fit_status()\n",
      "File \u001b[0;32msklearn/svm/_libsvm.pyx:265\u001b[0m, in \u001b[0;36msklearn.svm._libsvm.fit\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "studies = {}\n",
    "study = optuna.create_study(sampler=optuna.samplers.TPESampler(seed=23))\n",
    "study.optimize(objective_harmonic, n_trials=32)\n",
    "studies[\"harmonic\"] = study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7af2240d-f6de-4800-9c8b-964300e558b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "study = optuna.create_study(sampler=optuna.samplers.TPESampler(seed=23))\n",
    "study.optimize(objective_edit, n_trials=32)\n",
    "studies[\"edit\"] = study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "619bd43f-64d2-411b-be27-df11c7228633",
   "metadata": {},
   "outputs": [],
   "source": [
    "study = optuna.create_study(sampler=optuna.samplers.TPESampler(seed=23))\n",
    "study.optimize(objective_steinhaus, n_trials=32)\n",
    "studies[\"steinhaus\"] = study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc95337e-cae4-4793-b828-32a6fdb873d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "eval_parameters(studies[\"harmonic\"].best_params, \"harmonic_distance\", train=train, eval=test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "839d1a29-c940-4af7-a036-f90c1bb1c2d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "eval_parameters(studies[\"edit\"].best_params, \"edit_distance\", train=train, eval=test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac78dc6a-39d5-4878-b16b-fd684d45b4d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "eval_parameters(studies[\"steinhaus\"].best_params, \"steinhaus_distance\", train=train, eval=test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c036b53b-56fb-4a5c-9745-d4868596661a",
   "metadata": {},
   "outputs": [],
   "source": [
    "{k: v.best_params for k, v in studies.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "316987cc-480e-48b7-92b4-635a0254f554",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[t-SNE] Computing 91 nearest neighbors...\n",
      "[t-SNE] Indexed 5149 samples in 0.016s...\n",
      "[t-SNE] Computed neighbors for 5149 samples in 0.214s...\n",
      "[t-SNE] Computed conditional probabilities for sample 1000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 2000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 3000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 4000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 5000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 5149 / 5149\n",
      "[t-SNE] Mean sigma: 0.130571\n",
      "[t-SNE] KL divergence after 250 iterations with early exaggeration: 78.661522\n",
      "[t-SNE] KL divergence after 8192 iterations: 1.891862\n"
     ]
    }
   ],
   "source": [
    "tsne = sklearn.manifold.TSNE(metric=\"precomputed\", n_iter=2**13, init=\"random\", n_jobs=-1, verbose=True, random_state=42)\n",
    "coordinates = {}\n",
    "coordinates[\"harmonic\"] = tsne.fit_transform(train[\"harmonic_distance\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2248b6ab-eac0-4252-80a6-1036f7108b0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[t-SNE] Computing 91 nearest neighbors...\n",
      "[t-SNE] Indexed 5149 samples in 0.008s...\n",
      "[t-SNE] Computed neighbors for 5149 samples in 0.256s...\n",
      "[t-SNE] Computed conditional probabilities for sample 1000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 2000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 3000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 4000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 5000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 5149 / 5149\n",
      "[t-SNE] Mean sigma: 54.059876\n",
      "[t-SNE] KL divergence after 250 iterations with early exaggeration: 96.235718\n",
      "[t-SNE] KL divergence after 2048 iterations: 3.486958\n"
     ]
    }
   ],
   "source": [
    "tsne = sklearn.manifold.TSNE(metric=\"precomputed\", n_iter=2**11, init=\"random\", n_jobs=-1, verbose=True, random_state=42)\n",
    "coordinates[\"edit\"] = tsne.fit_transform(train[\"edit_distance\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b8a1483c-2946-459f-a948-5f67c6ceaaa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[t-SNE] Computing 91 nearest neighbors...\n",
      "[t-SNE] Indexed 5149 samples in 0.010s...\n",
      "[t-SNE] Computed neighbors for 5149 samples in 0.240s...\n",
      "[t-SNE] Computed conditional probabilities for sample 1000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 2000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 3000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 4000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 5000 / 5149\n",
      "[t-SNE] Computed conditional probabilities for sample 5149 / 5149\n",
      "[t-SNE] Mean sigma: 0.089259\n",
      "[t-SNE] KL divergence after 250 iterations with early exaggeration: 83.892639\n",
      "[t-SNE] KL divergence after 2048 iterations: 2.250279\n"
     ]
    }
   ],
   "source": [
    "tsne = sklearn.manifold.TSNE(metric=\"precomputed\", n_iter=2**11, init=\"random\", n_jobs=-1, verbose=True, random_state=42)\n",
    "coordinates[\"steinhaus\"] = tsne.fit_transform(train[\"steinhaus_distance\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d6567229-3c39-43a1-8670-4fce395c0d74",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.graph_objs as go\n",
    "traces = []\n",
    "label_colors = train[\"target\"]\n",
    "text = train[\"target\"]\n",
    "for distance, coords in coordinates.items():\n",
    "    traces.append({\"x\": coords[:, 0], \"y\": coords[:, 1], \"name\": distance, \"type\": \"scatter\", \"mode\": \"markers\", \"text\": text, \"marker\": {\"size\": 4, \"color\": label_colors}})\n",
    "layout = {\"coloraxis\": {\"showscale\": True}}\n",
    "# go.Figure(data=traces[0:1], layout=layout).show()\n",
    "# go.Figure(data=traces[1:2], layout=layout).show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "176f411e-ed36-423d-95e8-39610a81f183",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "15763dd4-f959-45e6-bb11-d7fc80a5e33b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 208304 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 216808 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 228016 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mpl\n",
    "cmap = mpl.colormaps[\"plasma\"]\n",
    "for trace in traces: #trace = traces[0]\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.set_xticks([])\n",
    "    ax.set_yticks([])\n",
    "    ax.scatter(x=trace[\"x\"], y=trace[\"y\"], c=trace[\"marker\"][\"color\"], s=trace[\"marker\"][\"size\"], alpha=0.8, edgecolor=\"none\", cmap=cmap)\n",
    "    plt.savefig(f\"TSNE_{dataset_name}_{trace['name']}.png\", dpi=600, bbox_inches=\"tight\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c208af1e-ad6d-4a0d-8d98-186a064b7598",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(plt.savefig)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
