{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c59a32fe",
   "metadata": {},
   "source": [
    "# A Quick Start to Use the Package `repytah`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ad6c87f",
   "metadata": {},
   "source": [
    "This notebook demonstrates how to use `repytah` to create aligned hierarchies for a music-based data stream. Before diving into the details, we’ll walk through a brief example program."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a68450e8",
   "metadata": {},
   "source": [
    "The example input is a csv file containing Chroma feature vectors for each beat of Chopin's Mazurka Op.30, No.1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7011e94b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Standard imports\n",
    "import scipy.io as sio\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Import the package repytah\n",
    "from repytah import *\n",
    "\n",
    "# Make the images clear\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'retina'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bc0dd16",
   "metadata": {},
   "source": [
    "### To skip visualizing the middle steps and get the output directly, try the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0c2cc701",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28544 characters]\n",
      "text/plain": [
       "<Figure size 720x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 535,
       "width": 648
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34520 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 481,
       "width": 592
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 49300 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 481,
       "width": 642
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 58252 characters]\n",
      "text/plain": [
       "<Figure size 720x864 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 698,
       "width": 656
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 49788 characters]\n",
      "text/plain": [
       "<Figure size 720x936 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 753,
       "width": 648
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "file_in = load_ex_data('../repytah/data/mazurka30-1.csv').to_numpy()\n",
    "file_out = \"hierarchical_out_file.mat\"\n",
    "num_fv_per_shingle = 12\n",
    "thresh = 0.02\n",
    "csv_to_aligned_hierarchies(file_in, file_out, num_fv_per_shingle, thresh, True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12a014bf",
   "metadata": {},
   "source": [
    "### To visualize the middle steps, try the following code:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e08eec81",
   "metadata": {},
   "source": [
    "Load the input file and modify the music based data to a matrix representation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "53f17d85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 459656 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 263,
       "width": 257
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Load the input file\n",
    "file_in = load_ex_data('../repytah/data/mazurka30-1.csv').to_numpy()\n",
    "fv_mat = file_in\n",
    "\n",
    "# Number of feature vectors per shingle\n",
    "num_fv_per_shingle = 12\n",
    "\n",
    "# Create the self-dissimilarity matrix\n",
    "self_dissim_mat = create_sdm(fv_mat, num_fv_per_shingle)\n",
    "\n",
    "# Produce a visualization\n",
    "SDM = plt.imshow(self_dissim_mat, cmap=\"RdBu\")\n",
    "plt.title('Self-Dissimilarity Matrix')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aab3c714",
   "metadata": {},
   "source": [
    "Threshold the above matrix:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7966647f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 48504 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 263,
       "width": 257
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "song_length = self_dissim_mat.shape[0]\n",
    "thresh = 0.02\n",
    "\n",
    "# Threshold the SDM to produce a binary matrix \n",
    "thresh_dist_mat = (self_dissim_mat <= thresh) \n",
    "\n",
    "# Produce a visualization\n",
    "SDM = plt.imshow(thresh_dist_mat, cmap=\"Greys\")\n",
    "plt.title('Thresholded Matrix')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdc0d567",
   "metadata": {},
   "source": [
    "Find and group each pair of repeats:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b64ff7bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_lst = find_initial_repeats(thresh_dist_mat, np.arange(1, song_length + 1), 0)\n",
    "complete_lst = find_complete_list(all_lst, song_length)\n",
    "mat_no_overlaps, key_no_overlaps = remove_overlaps(complete_lst, song_length)[1:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "432a3414",
   "metadata": {},
   "source": [
    "Find the essential structure components of the song and build the aligned hierarchies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d42a53aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28544 characters]\n",
      "text/plain": [
       "<Figure size 720x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 535,
       "width": 648
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34520 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 481,
       "width": 592
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 49300 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 481,
       "width": 642
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 58252 characters]\n",
      "text/plain": [
       "<Figure size 720x864 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 698,
       "width": 656
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 49788 characters]\n",
      "text/plain": [
       "<Figure size 720x936 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 753,
       "width": 648
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "output_tuple = hierarchical_structure(mat_no_overlaps, key_no_overlaps, song_length, vis=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08390378",
   "metadata": {},
   "source": [
    "To visualize the repeated structures in the actual music score, the examples in color yellow and purple are shown:\n",
    "<img src=\"pictures/score.jpg\" width=\"700\">\n",
    "<img src=\"pictures/score1.jpg\" width=\"700\">\n",
    "<img src=\"pictures/score2.jpg\" width=\"700\">"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30a2ff64",
   "metadata": {},
   "source": [
    "This notebook is just a quick walk-through that demonstrates how to use the package `repytah`, more technical details are in the [`example`](https://github.com/smith-tinkerlab/repytah/blob/main/docs/example_vignette.ipynb) notebook."
   ]
  }
 ],
 "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
