amads.melody.similarity.melsim

amads.melody.similarity.melsim#

This is a Python wrapper for the R package ‘melsim’ (sebsilas/melsim). This wrapper seeks to allow the user to easily interface with the melsim package using the Score objects in AMADS.

Melsim is a package for computing similarity between melodies, and is being developed by Sebastian Silas (https://sebsilas.com/) and Klaus Frieler (https://www.aesthetics.mpg.de/en/the-institute/people/klaus-frieler.html).

Melsim is based on SIMILE, which was written by Daniel Müllensiefen and Klaus Frieler in 2003/2004. This package is used to compare two or more melodies pairwise across a range of similarity measures. Not all similarity measures are implemented in melsim, but the ones that are can be used in AMADS.

All of the following similarity measures are implemented and functional in melsim: Please be aware that the names of the similarity measures are case-sensitive.

Num: Name: 1 Jaccard 2 Kulczynski2 3 Russel 4 Faith 5 Tanimoto 6 Dice 7 Mozley 8 Ochiai 9 Simpson 10 cosine 11 angular 12 correlation 13 Tschuprow 14 Cramer 15 Gower 16 Euclidean 17 Manhattan 18 supremum 19 Canberra 20 Chord 21 Geodesic 22 Bray 23 Soergel 24 Podani 25 Whittaker 26 eJaccard 27 eDice 28 Bhjattacharyya 29 divergence 30 Hellinger 31 edit_sim_utf8 32 edit_sim 33 Levenshtein 34 sim_NCD 35 const 36 sim_dtw

The following similarity measures are not currently functional in melsim: 1 count_distinct (set-based) 2 tversky (set-based) 3 simple matching 4 braun_blanquet (set-based) 5 minkowski (vector-based) 6 ukkon (distribution-based) 7 sum_common (distribution-based) 8 distr_sim (distribution-based) 9 stringdot_utf8 (sequence-based) 10 pmi (special) 11 sim_emd (special)

Further to the similarity measures, melsim allows the user to specify which domain the similarity should be calculated for. This is referred to as a “transformation” in melsim, and all of the following transformations are implemented and functional:

Num: Name: 1 pitch 2 int 3 fuzzy_int 4 parsons 5 pc 6 ioi_class 7 duration_class 8 int_X_ioi_class 9 implicit_harmonies

The following transformations are not currently functional in melsim:

Num: Name: 1 ioi 2 phrase_segmentation

amads.melody.similarity.melsim.get_similarity(melody_1, melody_2, method, transformation) float[source]#

Calculate similarity between two melodies using the specified method.

Parameters:
  • melody_1 (Score) – First Score object containing a monophonic melody

  • melody_2 (Score) – Second Score object containing a monophonic melody

  • method (str) – Name of the similarity method to use from the list in the module docstring.

  • transformation (str) – Name of the transformation to use from the list in the module docstring.

Returns:

Similarity value between the two melodies

Return type:

float

Raises:

ValueError – If the number of melodies is not exactly two.

Examples

>>> from amads.core.basics import Score
>>> # Create two simple melodies using from_melody
>>> melody_1 = Score.from_melody(pitches=[60, 62, 64, 65], durations=1.0)
>>> melody_2 = Score.from_melody(pitches=[60, 62, 64, 67], durations=1.0)
>>> # Calculate similarity using Jaccard method
>>> similarity = get_similarity(melody_1, melody_2, 'Jaccard', 'pitch')
amads.melody.similarity.melsim.r_get_similarity(melody_1, melody_2, method, transformation) float[source]#

Use the melsim R package to get the similarity between two or more melodies. This version of get_similarity is designed to be used alongside r_load_melody. The user should call r_load_melody for each melody they wish to compare, and then call r_get_similarity for each pair of melodies. This is more efficient than calling get_similarity for each pair of melodies, as the melodies are only loaded once, and stored in memory for each subsequent call. Similarity measures are already cached, making this the faster way to calculate similarity between multiple melodies.

Args:

melody_1: Name of the first melody. This should have already been passed to R (see r_load_melody). melody_2: Name of the second melody. This should have already been passed to R. method: Name of the similarity method.

Returns:

The similarity value for each of the melody comparisons

Parameters:
  • melody_1 (str)

  • melody_2 (str)

  • method (str)

  • transformation (str)

Return type:

float

amads.melody.similarity.melsim.r_load_melody(melody, name)[source]#

Convert a Score to a format compatible with melsim R package.

Args:

melody: Score object containing a monophonic melody

Returns:

A melsim Melody object

Parameters:
  • melody (Score)

  • name (str)