{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63de2e1c-ddc8-4d89-afe6-cf55c6990fb4",
   "metadata": {},
   "source": [
    "# Track Similarity Using Cosine Distance Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7a018076-8a65-4ee8-a1ec-03e1dc74583e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "from glob import glob\n",
    "from multiprocessing import Pool\n",
    "from absl import logging\n",
    "import pandas as pd\n",
    "import json\n",
    "\n",
    "\n",
    "\n",
    "logging.set_verbosity(logging.DEBUG)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d99abf2d-8b47-4204-b104-de91d5c1c672",
   "metadata": {},
   "source": [
    "## Load Processed File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a6a78663-58c5-4c73-b237-d68ca5789a75",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_df = pd.read_json('files_00.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5192b99f-779d-4840-98d5-fac7eb7ac26c",
   "metadata": {},
   "source": [
    "## Columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "908401b9-9997-410c-9fee-76bd49ef2799",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['album', 'genre', 'title', 'artist', 'script', 'originaldate',\n",
       "       'musicbrainz album release country', 'bit_rate', 'hfc_mean',\n",
       "       'gfcc_mean', 'mfcc_mean', 'erbbands_mean', 'melbands_mean',\n",
       "       'barkbands_mean', 'dissonance_mean', 'average_loudness',\n",
       "       'zerocrossingrate_mean', 'silence_rate_60dB_mean',\n",
       "       'silence_rate_20dB_mean', 'silence_rate_30dB_mean',\n",
       "       'pitch_salience_mean', 'bpm', 'onset_rate', 'beats_count',\n",
       "       'beats_loudness_mean', 'danceability', 'beats_position',\n",
       "       'beats_loudness_band_ratio_mean', 'hpcp_mean', 'thpcp', 'key_key',\n",
       "       'key_scale', 'key_strength', 'chords_key', 'chords_strength_mean',\n",
       "       'tuning_frequency', 'chords_number_rate', 'chords_changes_rate',\n",
       "       'tuning_diatonic_strength', 'chords_histogram', 'isrc',\n",
       "       'replaygain_track_gain'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_df.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54b8883f-19b0-4f5a-ab3e-7aa464a627c5",
   "metadata": {},
   "source": [
    "## Features Of Interest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ddcfb80a-99e9-4423-ad0a-cf0407bf0b7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "float_features = ['hfc_mean', 'dissonance_mean', 'average_loudness', 'zerocrossingrate_mean',\n",
    "                  'silence_rate_60dB_mean', 'silence_rate_20dB_mean', 'silence_rate_30dB_mean', 'pitch_salience_mean', \n",
    "                 'bpm', 'onset_rate', 'beats_count', 'beats_loudness_mean', 'danceability', 'key_strength', \n",
    "                  'chords_strength_mean', 'tuning_frequency', 'chords_number_rate', 'chords_changes_rate', 'tuning_diatonic_strength']\n",
    "text_features = ['artist']\n",
    "categorical_features = ['script', 'key_scale', 'key_key', 'key_scale', 'chords_key']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab295691-6494-4f69-9576-112e35452a21",
   "metadata": {},
   "source": [
    "## Floating Point Features "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cfc72395-f808-4aae-bb68-a75225e2f7da",
   "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>hfc_mean</th>\n",
       "      <th>dissonance_mean</th>\n",
       "      <th>average_loudness</th>\n",
       "      <th>zerocrossingrate_mean</th>\n",
       "      <th>silence_rate_60dB_mean</th>\n",
       "      <th>silence_rate_20dB_mean</th>\n",
       "      <th>silence_rate_30dB_mean</th>\n",
       "      <th>pitch_salience_mean</th>\n",
       "      <th>bpm</th>\n",
       "      <th>onset_rate</th>\n",
       "      <th>beats_count</th>\n",
       "      <th>beats_loudness_mean</th>\n",
       "      <th>danceability</th>\n",
       "      <th>key_strength</th>\n",
       "      <th>chords_strength_mean</th>\n",
       "      <th>tuning_frequency</th>\n",
       "      <th>chords_number_rate</th>\n",
       "      <th>chords_changes_rate</th>\n",
       "      <th>tuning_diatonic_strength</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>40.520855</td>\n",
       "      <td>0.486380</td>\n",
       "      <td>0.970608</td>\n",
       "      <td>0.104324</td>\n",
       "      <td>0.045990</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.995238</td>\n",
       "      <td>0.597461</td>\n",
       "      <td>172.265198</td>\n",
       "      <td>4.145257</td>\n",
       "      <td>437</td>\n",
       "      <td>0.039046</td>\n",
       "      <td>1.242281</td>\n",
       "      <td>0.593836</td>\n",
       "      <td>0.509609</td>\n",
       "      <td>434.193115</td>\n",
       "      <td>0.003257</td>\n",
       "      <td>0.094964</td>\n",
       "      <td>0.606693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>56.731720</td>\n",
       "      <td>0.467688</td>\n",
       "      <td>0.906720</td>\n",
       "      <td>0.086715</td>\n",
       "      <td>0.036585</td>\n",
       "      <td>0.999166</td>\n",
       "      <td>0.743798</td>\n",
       "      <td>0.413461</td>\n",
       "      <td>133.015488</td>\n",
       "      <td>5.279484</td>\n",
       "      <td>495</td>\n",
       "      <td>0.096347</td>\n",
       "      <td>1.273488</td>\n",
       "      <td>0.426767</td>\n",
       "      <td>0.419562</td>\n",
       "      <td>434.193115</td>\n",
       "      <td>0.002293</td>\n",
       "      <td>0.091080</td>\n",
       "      <td>0.419655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>26.856716</td>\n",
       "      <td>0.472796</td>\n",
       "      <td>0.935827</td>\n",
       "      <td>0.065036</td>\n",
       "      <td>0.013289</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.568401</td>\n",
       "      <td>102.389565</td>\n",
       "      <td>2.941207</td>\n",
       "      <td>588</td>\n",
       "      <td>0.043500</td>\n",
       "      <td>1.122556</td>\n",
       "      <td>0.854901</td>\n",
       "      <td>0.527282</td>\n",
       "      <td>434.193115</td>\n",
       "      <td>0.000959</td>\n",
       "      <td>0.050548</td>\n",
       "      <td>0.707127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8.800311</td>\n",
       "      <td>0.379960</td>\n",
       "      <td>0.840005</td>\n",
       "      <td>0.023373</td>\n",
       "      <td>0.050633</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.994550</td>\n",
       "      <td>0.566881</td>\n",
       "      <td>107.086555</td>\n",
       "      <td>4.414543</td>\n",
       "      <td>253</td>\n",
       "      <td>0.034583</td>\n",
       "      <td>1.179295</td>\n",
       "      <td>0.715905</td>\n",
       "      <td>0.495073</td>\n",
       "      <td>445.369720</td>\n",
       "      <td>0.004569</td>\n",
       "      <td>0.097715</td>\n",
       "      <td>0.688414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13.242675</td>\n",
       "      <td>0.436703</td>\n",
       "      <td>0.591821</td>\n",
       "      <td>0.030819</td>\n",
       "      <td>0.135966</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.981911</td>\n",
       "      <td>0.567838</td>\n",
       "      <td>74.176552</td>\n",
       "      <td>0.532486</td>\n",
       "      <td>129</td>\n",
       "      <td>0.031826</td>\n",
       "      <td>0.803998</td>\n",
       "      <td>0.809170</td>\n",
       "      <td>0.630369</td>\n",
       "      <td>443.060425</td>\n",
       "      <td>0.006631</td>\n",
       "      <td>0.019892</td>\n",
       "      <td>0.571882</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    hfc_mean  dissonance_mean  average_loudness  zerocrossingrate_mean  \\\n",
       "0  40.520855         0.486380          0.970608               0.104324   \n",
       "1  56.731720         0.467688          0.906720               0.086715   \n",
       "2  26.856716         0.472796          0.935827               0.065036   \n",
       "3   8.800311         0.379960          0.840005               0.023373   \n",
       "4  13.242675         0.436703          0.591821               0.030819   \n",
       "\n",
       "   silence_rate_60dB_mean  silence_rate_20dB_mean  silence_rate_30dB_mean  \\\n",
       "0                0.045990                1.000000                0.995238   \n",
       "1                0.036585                0.999166                0.743798   \n",
       "2                0.013289                1.000000                1.000000   \n",
       "3                0.050633                1.000000                0.994550   \n",
       "4                0.135966                1.000000                0.981911   \n",
       "\n",
       "   pitch_salience_mean         bpm  onset_rate  beats_count  \\\n",
       "0             0.597461  172.265198    4.145257          437   \n",
       "1             0.413461  133.015488    5.279484          495   \n",
       "2             0.568401  102.389565    2.941207          588   \n",
       "3             0.566881  107.086555    4.414543          253   \n",
       "4             0.567838   74.176552    0.532486          129   \n",
       "\n",
       "   beats_loudness_mean  danceability  key_strength  chords_strength_mean  \\\n",
       "0             0.039046      1.242281      0.593836              0.509609   \n",
       "1             0.096347      1.273488      0.426767              0.419562   \n",
       "2             0.043500      1.122556      0.854901              0.527282   \n",
       "3             0.034583      1.179295      0.715905              0.495073   \n",
       "4             0.031826      0.803998      0.809170              0.630369   \n",
       "\n",
       "   tuning_frequency  chords_number_rate  chords_changes_rate  \\\n",
       "0        434.193115            0.003257             0.094964   \n",
       "1        434.193115            0.002293             0.091080   \n",
       "2        434.193115            0.000959             0.050548   \n",
       "3        445.369720            0.004569             0.097715   \n",
       "4        443.060425            0.006631             0.019892   \n",
       "\n",
       "   tuning_diatonic_strength  \n",
       "0                  0.606693  \n",
       "1                  0.419655  \n",
       "2                  0.707127  \n",
       "3                  0.688414  \n",
       "4                  0.571882  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_df[float_features][:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "735f7b59-e5aa-46c4-9897-a90d687c32e9",
   "metadata": {},
   "source": [
    "## Categorical Features "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9a3150ca-6d6b-4abc-9986-02c2c9fa2b49",
   "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>script</th>\n",
       "      <th>key_scale</th>\n",
       "      <th>key_key</th>\n",
       "      <th>key_scale</th>\n",
       "      <th>chords_key</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Latn</td>\n",
       "      <td>minor</td>\n",
       "      <td>A</td>\n",
       "      <td>minor</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Latn</td>\n",
       "      <td>minor</td>\n",
       "      <td>A</td>\n",
       "      <td>minor</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Latn</td>\n",
       "      <td>major</td>\n",
       "      <td>C</td>\n",
       "      <td>major</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Latn</td>\n",
       "      <td>minor</td>\n",
       "      <td>B</td>\n",
       "      <td>minor</td>\n",
       "      <td>F#</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Latn</td>\n",
       "      <td>minor</td>\n",
       "      <td>C</td>\n",
       "      <td>minor</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  script key_scale key_key key_scale chords_key\n",
       "0   Latn     minor       A     minor          A\n",
       "1   Latn     minor       A     minor          F\n",
       "2   Latn     major       C     major          C\n",
       "3   Latn     minor       B     minor         F#\n",
       "4   Latn     minor       C     minor          C"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_df[categorical_features][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f4ccb51-8cab-426f-9bdf-43f94ba44316",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8467674-d00f-4ee3-a40a-5f2449c7d9ba",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5909f66-9cee-4f10-bea6-eef63dbaab70",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:artist-discovery] *",
   "language": "python",
   "name": "conda-env-artist-discovery-py"
  },
  "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
