Overview#

This notebook gives a general overview of the features included in the dataset.

Hide imports
%load_ext autoreload
%autoreload 2
import os

import dimcat as dc
import pandas as pd
import plotly.express as px
from dimcat import filters, plotting
from IPython.display import display

import utils
RESULTS_PATH = os.path.abspath(os.path.join(utils.OUTPUT_FOLDER, "overview"))
os.makedirs(RESULTS_PATH, exist_ok=True)


def make_output_path(
    filename: str,
    extension=None,
    path=RESULTS_PATH,
) -> str:
    return utils.make_output_path(filename=filename, extension=extension, path=path)


def save_figure_as(
    fig, filename, formats=("png", "pdf"), directory=RESULTS_PATH, **kwargs
):
    if formats is not None:
        for fmt in formats:
            plotting.write_image(fig, filename, directory, format=fmt, **kwargs)
    else:
        plotting.write_image(fig, filename, directory, **kwargs)

Loading data

D = utils.get_dataset("ravel_piano", corpus_release="v2.6")
package = D.inputs.get_package()
package_info = package._package.custom
git_tag = package_info.get("git_tag")
utils.print_heading("Data and software versions")
print("Maurice Ravel – Piano Pieces version v2.6")
print(f"Datapackage '{package.package_name}' @ {git_tag}")
print(f"dimcat version {dc.__version__}\n")
D
Data and software versions
--------------------------

Maurice Ravel – Piano Pieces version v2.6
Datapackage 'ravel_piano' @ v2.6
dimcat version 3.4.0
Dataset
=======
{'inputs': {'basepath': None,
            'packages': {'ravel_piano': ["'ravel_piano.measures' (MuseScoreFacetName.MuseScoreMeasures)",
                                         "'ravel_piano.notes' (MuseScoreFacetName.MuseScoreNotes)",
                                         "'ravel_piano.expanded' (MuseScoreFacetName.MuseScoreHarmonies)",
                                         "'ravel_piano.chords' (MuseScoreFacetName.MuseScoreChords)",
                                         "'ravel_piano.metadata' (FeatureName.Metadata)"]}},
 'outputs': {'basepath': None, 'packages': {}},
 'pipeline': []}
filtered_D = filters.HasHarmonyLabelsFilter(keep_values=[True]).process(D)
all_metadata = filtered_D.get_metadata()
assert len(all_metadata) > 0, "No pieces selected for analysis."
all_metadata
TimeSig KeySig last_mc last_mn length_qb last_mc_unfolded last_mn_unfolded length_qb_unfolded all_notes_qb n_onsets ... pdf wikidata musicbrainz viaf staff_1_ambitus staff_1_instrument staff_2_ambitus staff_2_instrument staff_3_ambitus staff_3_instrument
corpus piece
ravel_piano Ravel_-_Jeux_dEau {1: '4/4', 2: '2/4', 3: '4/4', 8: '2/4', 9: '4... {1: 4} 88 85 333.25 88 85 333.25 1143.40 4362 ... https://imslp.org/wiki/Special:ReverseLookup/3175 https://www.wikidata.org/wiki/Q1684076 https://musicbrainz.org/work/512fddf1-7ae1-3b7... https://viaf.org/viaf/179014537/ 30-95 (F#1-B6) Piano 30-94 (F#1-Bb6) Piano 40-61 (E2-C#4) Piano
Ravel_-_Miroirs_III._Une_Barque_sur_l'ocean {1: '2/4', 12: '3/4', 15: '2/4', 24: '3/4', 27... {1: 3, 77: -3, 90: 3} 151 143 380.00 151 143 380.00 1228.44 4713 ... https://imslp.org/wiki/Special:ReverseLookup/9... https://www.wikidata.org/wiki/Q770354 https://musicbrainz.org/work/88888e07-c508-3e4... https://viaf.org/viaf/185362313/ 29-95 (F1-B6) Piano 26-90 (D1-F#6) Piano <NA> Piano
Ravel_-_Miroirs_IV._Alborada_del_gracioso {1: '6/8', 30: '3/8', 31: '9/8', 35: '6/8', 36... {1: -1, 43: 4, 56: 2, 166: 0, 181: 4, 189: 2} 229 229 738.75 229 229 738.75 2538.81 4390 ... https://imslp.org/wiki/Special:ReverseLookup/9... https://www.wikidata.org/wiki/Q770354 https://musicbrainz.org/work/6e2f3c13-0ae9-340... https://viaf.org/viaf/185362313/ 30-95 (F#1-B6) Piano 25-90 (C#1-F#6) Piano <NA> Piano

3 rows × 57 columns

mean_composition_years = utils.corpus_mean_composition_years(all_metadata)
chronological_order = mean_composition_years.index.to_list()
corpus_colors = dict(zip(chronological_order, utils.CORPUS_COLOR_SCALE))
corpus_names = {
    corp: utils.get_corpus_display_name(corp) for corp in chronological_order
}
chronological_corpus_names = list(corpus_names.values())
corpus_name_colors = {
    corpus_names[corp]: color for corp, color in corpus_colors.items()
}
mean_composition_years
corpus
ravel_piano    1903.333333
Name: mean_composition_year, dtype: float64

Composition dates#

This section relies on the dataset’s metadata.

valid_composed_start = pd.to_numeric(all_metadata.composed_start, errors="coerce")
valid_composed_end = pd.to_numeric(all_metadata.composed_end, errors="coerce")
print(
    f"Composition dates range from {int(valid_composed_start.min())} {valid_composed_start.idxmin()} "
    f"to {int(valid_composed_end.max())} {valid_composed_end.idxmax()}."
)
Composition dates range from 1901 ('ravel_piano', 'Ravel_-_Jeux_dEau') to 1905 ('ravel_piano', "Ravel_-_Miroirs_III._Une_Barque_sur_l'ocean").

Mean composition years per corpus#

def make_summary(metadata_df):
    piece_is_annotated = metadata_df.label_count > 0
    return metadata_df[piece_is_annotated].copy()
Hide source
summary = make_summary(all_metadata)
bar_data = pd.concat(
    [
        mean_composition_years.rename("year"),
        summary.groupby(level="corpus").size().rename("pieces"),
    ],
    axis=1,
).reset_index()

N = len(summary)
fig = px.bar(
    bar_data,
    x="year",
    y="pieces",
    color="corpus",
    color_discrete_map=corpus_colors,
    title=f"Temporal coverage of the {N} annotated pieces in the Distant Listening Corpus",
)
fig.update_traces(width=5)
fig.update_layout(**utils.STD_LAYOUT)
fig.update_traces(width=5)
save_figure_as(fig, "pieces_timeline_bars")
fig.show()
summary
TimeSig KeySig last_mc last_mn length_qb last_mc_unfolded last_mn_unfolded length_qb_unfolded all_notes_qb n_onsets ... pdf wikidata musicbrainz viaf staff_1_ambitus staff_1_instrument staff_2_ambitus staff_2_instrument staff_3_ambitus staff_3_instrument
corpus piece
ravel_piano Ravel_-_Jeux_dEau {1: '4/4', 2: '2/4', 3: '4/4', 8: '2/4', 9: '4... {1: 4} 88 85 333.25 88 85 333.25 1143.40 4362 ... https://imslp.org/wiki/Special:ReverseLookup/3175 https://www.wikidata.org/wiki/Q1684076 https://musicbrainz.org/work/512fddf1-7ae1-3b7... https://viaf.org/viaf/179014537/ 30-95 (F#1-B6) Piano 30-94 (F#1-Bb6) Piano 40-61 (E2-C#4) Piano
Ravel_-_Miroirs_III._Une_Barque_sur_l'ocean {1: '2/4', 12: '3/4', 15: '2/4', 24: '3/4', 27... {1: 3, 77: -3, 90: 3} 151 143 380.00 151 143 380.00 1228.44 4713 ... https://imslp.org/wiki/Special:ReverseLookup/9... https://www.wikidata.org/wiki/Q770354 https://musicbrainz.org/work/88888e07-c508-3e4... https://viaf.org/viaf/185362313/ 29-95 (F1-B6) Piano 26-90 (D1-F#6) Piano <NA> Piano
Ravel_-_Miroirs_IV._Alborada_del_gracioso {1: '6/8', 30: '3/8', 31: '9/8', 35: '6/8', 36... {1: -1, 43: 4, 56: 2, 166: 0, 181: 4, 189: 2} 229 229 738.75 229 229 738.75 2538.81 4390 ... https://imslp.org/wiki/Special:ReverseLookup/9... https://www.wikidata.org/wiki/Q770354 https://musicbrainz.org/work/6e2f3c13-0ae9-340... https://viaf.org/viaf/185362313/ 30-95 (F#1-B6) Piano 25-90 (C#1-F#6) Piano <NA> Piano

3 rows × 57 columns

Composition years histogram#

Hide source
hist_data = summary.reset_index()
hist_data.corpus = hist_data.corpus.map(corpus_names)
fig = px.histogram(
    hist_data,
    x="composed_end",
    color="corpus",
    labels=dict(
        composed_end="decade",
        count="pieces",
    ),
    color_discrete_map=corpus_name_colors,
    title=f"Temporal coverage of the {N} annotated pieces in the Distant Listening Corpus",
)
fig.update_traces(xbins=dict(size=10))
fig.update_layout(**utils.STD_LAYOUT)
fig.update_legends(font=dict(size=16))
save_figure_as(fig, "pieces_timeline_histogram", height=1250)
fig.show()

Dimensions#

Overview#

def make_overview_table(groupby, group_name="pieces"):
    n_groups = groupby.size().rename(group_name)
    absolute_numbers = dict(
        measures=groupby.last_mn.sum(),
        length=groupby.length_qb.sum(),
        notes=groupby.n_onsets.sum(),
        labels=groupby.label_count.sum(),
    )
    absolute = pd.DataFrame.from_dict(absolute_numbers)
    absolute = pd.concat([n_groups, absolute], axis=1)
    sum_row = pd.DataFrame(absolute.sum(), columns=["sum"]).T
    absolute = pd.concat([absolute, sum_row])
    return absolute


absolute = make_overview_table(summary.groupby("workTitle"))
# print(absolute.astype(int).to_markdown())
absolute.astype(int)
pieces measures length notes labels
Jeux d'eau 1 85 333 4362 257
Miroirs 2 372 1118 9103 604
sum 3 457 1452 13465 861
def summarize_dataset(D):
    all_metadata = D.get_metadata()
    summary = make_summary(all_metadata)
    return make_overview_table(summary.groupby(level=0))


corpus_summary = summarize_dataset(D)
print(corpus_summary.astype(int).to_markdown())
|             |   pieces |   measures |   length |   notes |   labels |
|:------------|---------:|-----------:|---------:|--------:|---------:|
| ravel_piano |        3 |        457 |     1452 |   13465 |      861 |
| sum         |        3 |        457 |     1452 |   13465 |      861 |

Measures#

all_measures = D.get_feature("measures")
print(
    f"{len(all_measures.index)} measures over {len(all_measures.groupby(level=[0,1]))} files."
)
all_measures.head()
640 measures over 5 files.
mc mn quarterbeats duration_qb keysig timesig act_dur mc_offset numbering_offset dont_count barline breaks repeats next
corpus piece i
ravel_piano Ravel_-_Jeux_dEau 0 1 1 0 4.0 4 4/4 1 0 <NA> <NA> <NA> <NA> firstMeasure (2,)
1 2 2 4 2.0 4 2/4 1/2 0 <NA> <NA> <NA> <NA> <NA> (3,)
2 3 3 6 4.0 4 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (4,)
3 4 4 10 4.0 4 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (5,)
4 5 5 14 4.0 4 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (6,)
all_measures.get_default_analysis().plot_grouped()

Harmony labels#

All symbols, independent of the local key (the mode of which changes their semantics).

try:
    all_annotations = D.get_feature("harmonylabels").df
except Exception:
    all_annotations = pd.DataFrame()
n_annotations = len(all_annotations.index)
includes_annotations = n_annotations > 0
if includes_annotations:
    display(all_annotations.head())
    print(f"Concatenated annotation tables contains {all_annotations.shape[0]} rows.")
    no_chord = all_annotations.root.isna()
    if no_chord.sum() > 0:
        print(
            f"{no_chord.sum()} of them are not chords. Their values are:"
            f" {all_annotations.label[no_chord].value_counts(dropna=False).to_dict()}"
        )
    all_chords = all_annotations[~no_chord].copy()
    print(
        f"Dataset contains {all_chords.shape[0]} tokens and {len(all_chords.chord.unique())} types over "
        f"{len(all_chords.groupby(level=[0,1]))} documents."
    )
    all_annotations["corpus_name"] = all_annotations.index.get_level_values(0).map(
        utils.get_corpus_display_name
    )
    all_chords["corpus_name"] = all_chords.index.get_level_values(0).map(
        utils.get_corpus_display_name
    )
else:
    print("Dataset contains no annotations.")
mc mn quarterbeats quarterbeats_all_endings duration_qb mc_onset mn_onset timesig staff voice ... numeral_or_applied_to_numeral intervals_over_bass intervals_over_root scale_degrees scale_degrees_and_mode scale_degrees_major scale_degrees_minor globalkey localkey chord
corpus piece i
ravel_piano Ravel_-_Jeux_dEau 0 1 1 0 0 1.5 0 0 4/4 3 1 ... I (M3, P5, M7) (M3, P5, M7) (1, 3, 5, 7) (1, 3, 5, 7), major (1, 3, 5, 7) (1, #3, 5, #7) E I IM7(9)
1 1 1 3/2 3/2 0.5 3/8 3/8 4/4 3 1 ... IV (M3, P5, M7) (M3, P5, M7) (4, 6, 1, 3) (4, 6, 1, 3), major (4, 6, 1, 3) (4, #6, 1, #3) E I IVM7
2 1 1 2 2 1.5 1/2 1/2 4/4 3 1 ... I (M3, P5, M7) (M3, P5, M7) (1, 3, 5, 7) (1, 3, 5, 7), major (1, 3, 5, 7) (1, #3, 5, #7) E I IM7(9)
3 1 1 7/2 7/2 0.5 7/8 7/8 4/4 3 1 ... IV (M3, P5, M7) (M3, P5, M7) (4, 6, 1, 3) (4, 6, 1, 3), major (4, 6, 1, 3) (4, #6, 1, #3) E I IVM7
4 2 2 4 4 2.0 0 0 2/4 3 1 ... I (M3, P5, M7) (M3, P5, M7) (1, 3, 5, 7) (1, 3, 5, 7), major (1, 3, 5, 7) (1, #3, 5, #7) E I IM7(9)

5 rows × 51 columns

Concatenated annotation tables contains 861 rows.
Dataset contains 861 tokens and 276 types over 3 documents.