# muspy.metrics¶

Objective metrics.

This module provides common objective metrics in music generation. These objective metrics could be used to evaluate a music generation system by comparing the statistical difference between the training data and the generated samples.

## Functions¶

• drum_in_pattern_rate

• drum_pattern_consistency

• empty_beat_rate

• empty_measure_rate

• groove_consistency

• n_pitch_classes_used

• n_pitches_used

• pitch_class_entropy

• pitch_entropy

• pitch_in_scale_rate

• pitch_range

• polyphony

• polyphony_rate

• scale_consistency

muspy.metrics.drum_in_pattern_rate(music, meter)[source]

Return the ratio of drum notes in a certain drum pattern.

The drum-in-pattern rate is defined as the ratio of the number of notes in a certain scale to the total number of notes. Only drum tracks are considered. Return NaN if no drum note is found. This metric is used in [1].

$drum\_in\_pattern\_rate = \frac{ \#(drum\_notes\_in\_pattern)}{\#(drum\_notes)}$
Parameters
Returns

Drum-in-pattern rate.

Return type

float

muspy.drum_pattern_consistency()

Compute the largest drum-in-pattern rate.

References

1. Hao-Wen Dong, Wen-Yi Hsiao, Li-Chia Yang, and Yi-Hsuan Yang, “MuseGAN: Multi-track sequential generative adversarial networks for symbolic music generation and accompaniment,” in Proceedings of the 32nd AAAI Conference on Artificial Intelligence (AAAI), 2018.

muspy.metrics.drum_pattern_consistency(music)[source]

Return the largest drum-in-pattern rate.

The drum pattern consistency is defined as the largest drum-in-pattern rate over duple and triple meters. Only drum tracks are considered. Return NaN if no drum note is found.

$drum\_pattern\_consistency = \max_{meter}{ drum\_in\_pattern\_rate(meter)}$
Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Drum pattern consistency.

Return type

float

muspy.drum_in_pattern_rate()

Compute the ratio of drum notes in a certain drum pattern.

muspy.metrics.empty_beat_rate(music)[source]

Return the ratio of empty beats.

The empty-beat rate is defined as the ratio of the number of empty beats (where no note is played) to the total number of beats. Return NaN if song length is zero. This metric is also implemented in Pypianoroll [1].

$empty\_beat\_rate = \frac{\#(empty\_beats)}{\#(beats)}$
Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Empty-beat rate.

Return type

float

muspy.empty_measure_rate()

Compute the ratio of empty measures.

References

1. Hao-Wen Dong, Wen-Yi Hsiao, and Yi-Hsuan Yang, “Pypianoroll: Open Source Python Package for Handling Multitrack Pianorolls,” in Late-Breaking Demos of the 18th International Society for Music Information Retrieval Conference (ISMIR), 2018.

muspy.metrics.empty_measure_rate(music, measure_resolution)[source]

Return the ratio of empty measures.

The empty-measure rate is defined as the ratio of the number of empty measures (where no note is played) to the total number of measures. Note that this metric only works for songs with a constant time signature. Return NaN if song length is zero. This metric is used in [1].

$empty\_measure\_rate = \frac{\#(empty\_measures)}{\#(measures)}$
Parameters
Returns

Empty-measure rate.

Return type

float

muspy.empty_beat_rate()

Compute the ratio of empty beats.

References

1. Hao-Wen Dong, Wen-Yi Hsiao, Li-Chia Yang, and Yi-Hsuan Yang, “MuseGAN: Multi-track sequential generative adversarial networks for symbolic music generation and accompaniment,” in Proceedings of the 32nd AAAI Conference on Artificial Intelligence (AAAI), 2018.

muspy.metrics.groove_consistency(music, measure_resolution)[source]

Return the groove consistency.

The groove consistency is defined as the mean hamming distance of the neighboring measures.

$groove\_consistency = 1 - \frac{1}{T - 1} \sum_{i = 1}^{T - 1}{ d(G_i, G_{i + 1})}$

Here, $$T$$ is the number of measures, $$G_i$$ is the binary onset vector of the $$i$$-th measure (a one at position that has an onset, otherwise a zero), and $$d(G, G')$$ is the hamming distance between two vectors $$G$$ and $$G'$$. Note that this metric only works for songs with a constant time signature. Return NaN if the number of measures is less than two. This metric is used in [1].

Parameters
Returns

Groove consistency.

Return type

float

References

1. Shih-Lun Wu and Yi-Hsuan Yang, “The Jazz Transformer on the Front Line: Exploring the Shortcomings of AI-composed Music through Quantitative Measures”, in Proceedings of the 21st International Society for Music Information Retrieval Conference, 2020.

muspy.metrics.n_pitch_classes_used(music)[source]

Return the number of unique pitch classes used.

Drum tracks are ignored.

Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Number of unique pitch classes used.

Return type

int

muspy.n_pitches_used()

Compute the number of unique pitches used.

muspy.metrics.n_pitches_used(music)[source]

Return the number of unique pitches used.

Drum tracks are ignored.

Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Number of unique pitch used.

Return type

int

muspy.n_pitch_class_used()

Compute the number of unique pitch classes used.

muspy.metrics.pitch_class_entropy(music)[source]

Return the entropy of the normalized note pitch class histogram.

The pitch class entropy is defined as the Shannon entropy of the normalized note pitch class histogram. Drum tracks are ignored. Return NaN if no note is found. This metric is used in [1].

$pitch\_class\_entropy = -\sum_{i = 0}^{11}{ P(pitch\_class=i) \times \log_2 P(pitch\_class=i)}$
Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Pitch class entropy.

Return type

float

muspy.pitch_entropy()

Compute the entropy of the normalized pitch histogram.

References

1. Shih-Lun Wu and Yi-Hsuan Yang, “The Jazz Transformer on the Front Line: Exploring the Shortcomings of AI-composed Music through Quantitative Measures”, in Proceedings of the 21st International Society for Music Information Retrieval Conference, 2020.

muspy.metrics.pitch_entropy(music)[source]

Return the entropy of the normalized note pitch histogram.

The pitch entropy is defined as the Shannon entropy of the normalized note pitch histogram. Drum tracks are ignored. Return NaN if no note is found.

$pitch\_entropy = -\sum_{i = 0}^{127}{ P(pitch=i) \log_2 P(pitch=i)}$
Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Pitch entropy.

Return type

float

muspy.pitch_class_entropy()

Compute the entropy of the normalized pitch class histogram.

muspy.metrics.pitch_in_scale_rate(music, root, mode)[source]

Return the ratio of pitches in a certain musical scale.

The pitch-in-scale rate is defined as the ratio of the number of notes in a certain scale to the total number of notes. Drum tracks are ignored. Return NaN if no note is found. This metric is used in [1].

$pitch\_in\_scale\_rate = \frac{\#(notes\_in\_scale)}{\#(notes)}$
Parameters
Returns

Pitch-in-scale rate.

Return type

float

muspy.scale_consistency()

Compute the largest pitch-in-class rate.

References

1. Hao-Wen Dong, Wen-Yi Hsiao, Li-Chia Yang, and Yi-Hsuan Yang, “MuseGAN: Multi-track sequential generative adversarial networks for symbolic music generation and accompaniment,” in Proceedings of the 32nd AAAI Conference on Artificial Intelligence (AAAI), 2018.

muspy.metrics.pitch_range(music)[source]

Return the pitch range.

Drum tracks are ignored. Return zero if no note is found.

Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Pitch range.

Return type

int

muspy.metrics.polyphony(music)[source]

Return the average number of pitches being played concurrently.

The polyphony is defined as the average number of pitches being played at the same time, evaluated only at time steps where at least one pitch is on. Drum tracks are ignored. Return NaN if no note is found.

$polyphony = \frac{ \#(pitches\_when\_at\_least\_one\_pitch\_is\_on) }{ \#(time\_steps\_where\_at\_least\_one\_pitch\_is\_on) }$
Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Polyphony.

Return type

float

muspy.polyphony_rate()

Compute the ratio of time steps where multiple pitches are on.

muspy.metrics.polyphony_rate(music, threshold=2)[source]

Return the ratio of time steps where multiple pitches are on.

The polyphony rate is defined as the ratio of the number of time steps where multiple pitches are on to the total number of time steps. Drum tracks are ignored. Return NaN if song length is zero. This metric is used in [1], where it is called polyphonicity.

$polyphony\_rate = \frac{ \#(time\_steps\_where\_multiple\_pitches\_are\_on) }{ \#(time\_steps) }$
Parameters
• music (muspy.Music) – Music object to evaluate.

• threshold (int, default: 2) – Threshold of number of pitches to count into the numerator.

Returns

Polyphony rate.

Return type

float

muspy.polyphony()

Compute the average number of pitches being played at the same time.

References

1. Hao-Wen Dong, Wen-Yi Hsiao, Li-Chia Yang, and Yi-Hsuan Yang, “MuseGAN: Multi-track sequential generative adversarial networks for symbolic music generation and accompaniment,” in Proceedings of the 32nd AAAI Conference on Artificial Intelligence (AAAI), 2018.

muspy.metrics.scale_consistency(music)[source]

Return the largest pitch-in-scale rate.

The scale consistency is defined as the largest pitch-in-scale rate over all major and minor scales. Drum tracks are ignored. Return NaN if no note is found. This metric is used in [1].

$scale\_consistency = \max_{root, mode}{ pitch\_in\_scale\_rate(root, mode)}$
Parameters

music (muspy.Music) – Music object to evaluate.

Returns

Scale consistency.

Return type

float

muspy.pitch_in_scale_rate()