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("mahler_kindertotenlieder", corpus_release="v3.2")
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("Gustav Mahler – Kindertotenlieder version v3.2")
print(f"Datapackage '{package.package_name}' @ {git_tag}")
print(f"dimcat version {dc.__version__}\n")
D
Data and software versions
--------------------------

Gustav Mahler – Kindertotenlieder version v3.2
Datapackage 'mahler_kindertotenlieder' @ v3.2
dimcat version 3.4.0
Dataset
=======
{'inputs': {'basepath': None,
            'packages': {'mahler_kindertotenlieder': ["'mahler_kindertotenlieder.measures' "
                                                      '(MuseScoreFacetName.MuseScoreMeasures)',
                                                      "'mahler_kindertotenlieder.notes' "
                                                      '(MuseScoreFacetName.MuseScoreNotes)',
                                                      "'mahler_kindertotenlieder.expanded' "
                                                      '(MuseScoreFacetName.MuseScoreHarmonies)',
                                                      "'mahler_kindertotenlieder.chords' "
                                                      '(MuseScoreFacetName.MuseScoreChords)',
                                                      "'mahler_kindertotenlieder.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 volta_mcs all_notes_qb ... musicbrainz viaf imslp pdf staff_1_ambitus staff_1_instrument staff_2_ambitus staff_2_instrument staff_3_ambitus staff_3_instrument
corpus piece
mahler_kindertotenlieder kindertotenlieder_01_nun_will_die_sonn {1: '4/4'} {1: -1} 85 84 337.0 85 84 337.0 () 1064.5 ... https://musicbrainz.org/work/57153896-db88-33d... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 62-75 (D4-Eb5) Voice 38-87 (D2-Eb6) Grand Piano 26-74 (D1-D5) Grand Piano
kindertotenlieder_02_nun_seh_ich_wohl {1: '4/4', 37: '3/2', 38: '4/4', 69: '2/4', 70... {1: -3, 15: 0, 22: -3, 29: 0, 41: 2, 49: -2, 6... 74 74 296.0 74 74 296.0 () 1209.0 ... https://musicbrainz.org/work/3ad25002-5ae4-30d... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 57-77 (A3-F5) Voice 53-76 (F3-E5) Grand Piano 24-74 (C1-D5) Grand Piano
kindertotenlieder_03_wenn_dein_mutterlein {1: '4/4', 8: '3/2', 10: '4/4', 16: '3/2', 27:... {1: -3} 70 70 348.0 70 70 348.0 () 1368.5 ... https://musicbrainz.org/work/7eb5f71b-1910-301... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 55-77 (G3-F5) Voice 55-86 (G3-D6) Grand Piano 29-69 (F1-A4) Grand Piano
kindertotenlieder_04_oft_denk_ich {1: '2/2', 9: '3/2', 10: '2/2', 15: '3/2', 16:... {1: -3} 72 71 297.0 72 71 297.0 () 1156.5 ... https://musicbrainz.org/work/2050abcb-abb0-35a... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 58-78 (Bb3-Gb5) Voice 54-86 (Gb3-D6) Grand Piano 34-77 (Bb1-F5) Grand Piano
kindertotenlieder_05_in_diesem_wetter {1: '4/4'} {1: -1, 99: 2} 139 139 556.0 139 139 556.0 () 2395.0 ... https://musicbrainz.org/work/cc00e04d-eb29-3f4... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 58-77 (Bb3-F5) Voice 48-93 (C3-A6) Grand Piano 26-80 (D1-G#5) Grand Piano

5 rows × 59 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
mahler_kindertotenlieder    1902.5
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 ('mahler_kindertotenlieder', 'kindertotenlieder_01_nun_will_die_sonn') to 1904 ('mahler_kindertotenlieder', 'kindertotenlieder_01_nun_will_die_sonn').

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 volta_mcs all_notes_qb ... musicbrainz viaf imslp pdf staff_1_ambitus staff_1_instrument staff_2_ambitus staff_2_instrument staff_3_ambitus staff_3_instrument
corpus piece
mahler_kindertotenlieder kindertotenlieder_01_nun_will_die_sonn {1: '4/4'} {1: -1} 85 84 337.0 85 84 337.0 () 1064.5 ... https://musicbrainz.org/work/57153896-db88-33d... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 62-75 (D4-Eb5) Voice 38-87 (D2-Eb6) Grand Piano 26-74 (D1-D5) Grand Piano
kindertotenlieder_02_nun_seh_ich_wohl {1: '4/4', 37: '3/2', 38: '4/4', 69: '2/4', 70... {1: -3, 15: 0, 22: -3, 29: 0, 41: 2, 49: -2, 6... 74 74 296.0 74 74 296.0 () 1209.0 ... https://musicbrainz.org/work/3ad25002-5ae4-30d... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 57-77 (A3-F5) Voice 53-76 (F3-E5) Grand Piano 24-74 (C1-D5) Grand Piano
kindertotenlieder_03_wenn_dein_mutterlein {1: '4/4', 8: '3/2', 10: '4/4', 16: '3/2', 27:... {1: -3} 70 70 348.0 70 70 348.0 () 1368.5 ... https://musicbrainz.org/work/7eb5f71b-1910-301... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 55-77 (G3-F5) Voice 55-86 (G3-D6) Grand Piano 29-69 (F1-A4) Grand Piano
kindertotenlieder_04_oft_denk_ich {1: '2/2', 9: '3/2', 10: '2/2', 15: '3/2', 16:... {1: -3} 72 71 297.0 72 71 297.0 () 1156.5 ... https://musicbrainz.org/work/2050abcb-abb0-35a... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 58-78 (Bb3-Gb5) Voice 54-86 (Gb3-D6) Grand Piano 34-77 (Bb1-F5) Grand Piano
kindertotenlieder_05_in_diesem_wetter {1: '4/4'} {1: -1, 99: 2} 139 139 556.0 139 139 556.0 () 2395.0 ... https://musicbrainz.org/work/cc00e04d-eb29-3f4... https://viaf.org/viaf/177009360/ https://imslp.org/wiki/Kindertotenlieder_(Mahl... https://imslp.org/wiki/Special:ReverseLookup/2... 58-77 (Bb3-F5) Voice 48-93 (C3-A6) Grand Piano 26-80 (D1-G#5) Grand Piano

5 rows × 59 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
Kindertotenlieder 5 438 1834 5582 595
sum 5 438 1834 5582 595
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 |
|:-------------------------|---------:|-----------:|---------:|--------:|---------:|
| mahler_kindertotenlieder |        5 |        438 |     1834 |    5582 |      595 |
| sum                      |        5 |        438 |     1834 |    5582 |      595 |

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()
440 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
mahler_kindertotenlieder kindertotenlieder_01_nun_will_die_sonn 0 1 0 0 1.0 -1 4/4 1/4 3/4 <NA> 1 <NA> <NA> firstMeasure (2,)
1 2 1 1 4.0 -1 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (3,)
2 3 2 5 4.0 -1 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (4,)
3 4 3 9 4.0 -1 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (5,)
4 5 4 13 4.0 -1 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
mahler_kindertotenlieder kindertotenlieder_01_nun_will_die_sonn 0 1 0 0 0 1.0 0 3/4 4/4 3 1 ... VII (M3, P5) (M3, P5) (7, 2, 4) (7, 2, 4), minor (b7, 2, 4) (7, 2, 4) d i VII
1 2 1 1 1 2.0 0 0 4/4 3 1 ... v (a2, a4, M6) (m3, d5, d7) (3, #4, #6, 1) (3, #4, #6, 1), minor (b3, #4, 6, 1) (3, #4, #6, 1) d i #viio2/v
2 2 1 3 3 1.0 1/2 1/2 4/4 3 1 ... v (P4, m6) (m3, P5) (2, 5, 7) (2, 5, 7), minor (2, 5, b7) (2, 5, 7) d i v64
3 2 1 4 4 1.0 3/4 3/4 4/4 3 1 ... VII (m3, M6) (m3, d5) (1, 3, #6) (1, 3, #6), minor (1, b3, 6) (1, 3, #6) d i viio6/VII
4 3 2 5 5 2.0 0 0 4/4 3 1 ... iv (a2, a4, M6) (m3, d5, d7) (b2, #3, 5, 7) (b2, #3, 5, 7), minor (b2, 3, 5, b7) (b2, #3, 5, 7) d i #viio2/iv

5 rows × 52 columns

Concatenated annotation tables contains 590 rows.
Dataset contains 590 tokens and 219 types over 5 documents.