A Quick Start to Use the Package repytah

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.

The example input is a csv file containing Chroma feature vectors for each beat of Chopin’s Mazurka Op.30, No.1.

[1]:
# Standard imports
import scipy.io as sio
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Import the package repytah
from repytah import *

# Make the images clear
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

To skip visualizing the middle steps and get the output directly, try the following code:

[2]:
file_in = load_ex_data('../repytah/data/mazurka30-1.csv').to_numpy()
file_out = "hierarchical_out_file.mat"
num_fv_per_shingle = 12
thresh = 0.02
csv_to_aligned_hierarchies(file_in, file_out, num_fv_per_shingle, thresh, True)
_images/quick_start_5_0.png
_images/quick_start_5_1.png
_images/quick_start_5_2.png
_images/quick_start_5_3.png
_images/quick_start_5_4.png

To visualize the middle steps, try the following code:

Load the input file and modify the music based data to a matrix representation:

[4]:
# Load the input file
file_in = load_ex_data('../repytah/data/mazurka30-1.csv').to_numpy()
fv_mat = file_in

# Number of feature vectors per shingle
num_fv_per_shingle = 12

# Create the self-dissimilarity matrix
self_dissim_mat = create_sdm(fv_mat, num_fv_per_shingle)

# Produce a visualization
SDM = plt.imshow(self_dissim_mat, cmap="RdBu")
plt.title('Self-Dissimilarity Matrix')
plt.show()
_images/quick_start_8_0.png

Threshold the above matrix:

[12]:
song_length = self_dissim_mat.shape[0]
thresh = 0.02

# Threshold the SDM to produce a binary matrix
thresh_dist_mat = (self_dissim_mat <= thresh)

# Produce a visualization
SDM = plt.imshow(thresh_dist_mat, cmap="Greys")
plt.title('Thresholded Matrix')
plt.show()
_images/quick_start_10_0.png

Find and group each pair of repeats:

[13]:
all_lst = find_initial_repeats(thresh_dist_mat, np.arange(1, song_length + 1), 0)
complete_lst = find_complete_list(all_lst, song_length)
mat_no_overlaps, key_no_overlaps = remove_overlaps(complete_lst, song_length)[1:3]

Find the essential structure components of the song and build the aligned hierarchies:

[14]:
output_tuple = hierarchical_structure(mat_no_overlaps, key_no_overlaps, song_length, vis=True)
_images/quick_start_14_0.png
_images/quick_start_14_1.png
_images/quick_start_14_2.png
_images/quick_start_14_3.png
_images/quick_start_14_4.png

To visualize the repeated structures in the actual music score, the examples in color yellow and purple are shown: a359912523d2461293d1562eeab8aafd 50685bd15b884efaa9746909c94bdc49 f43c9dcd5cae44e2bfd23f654c8d67f8

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.