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("wf_bach_sonatas", corpus_release="v2.3")
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("Wilhelm Friedemann Bach – Piano Sonatas version v2.3")
print(f"Datapackage '{package.package_name}' @ {git_tag}")
print(f"dimcat version {dc.__version__}\n")
D
Data and software versions
--------------------------

Wilhelm Friedemann Bach – Piano Sonatas version v2.3
Datapackage 'wf_bach_sonatas' @ v2.3
dimcat version 3.4.0
Dataset
=======
{'inputs': {'basepath': None,
            'packages': {'wf_bach_sonatas': ["'wf_bach_sonatas.measures' (MuseScoreFacetName.MuseScoreMeasures)",
                                             "'wf_bach_sonatas.notes' (MuseScoreFacetName.MuseScoreNotes)",
                                             "'wf_bach_sonatas.expanded' (MuseScoreFacetName.MuseScoreHarmonies)",
                                             "'wf_bach_sonatas.chords' (MuseScoreFacetName.MuseScoreChords)",
                                             "'wf_bach_sonatas.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 ... originalFormat P86 (composer) musicbrainz viaf imslp pdf staff_1_ambitus staff_1_instrument staff_2_ambitus staff_2_instrument
corpus piece
wf_bach_sonatas F001_n08a {1: '4/4'} {1: 0} 63 63 252.0 126 126 504.0 () 602.75 ... cap Q107277 https://musicbrainz.org/work/4cf167ee-4601-4d0... <NA> https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3838 52-89 (E3-F6) Piano 33-76 (A1-E5) Piano
F001_n08b {1: '3/4'} {1: 0} 42 40 126.0 40 40 120.0 (8], [9]], [[17], [18) 237.00 ... cap Q107277 https://musicbrainz.org/work/fd2561b4-869d-4af... <NA> https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3838 60-86 (C4-D6) Piano 36-71 (C2-B4) Piano
F001_n08c {1: '3/8', 17: '3/4', 18: '3/8', 81: '3/4', 82... {1: 0} 101 101 154.5 202 202 309.0 () 390.50 ... cap Q107277 https://musicbrainz.org/work/32703d10-01ac-4e5... <NA> https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3838 59-88 (B3-E6) Piano 31-69 (G1-A4) Piano
F002_n07a {1: '4/4'} {1: 0} 64 62 256.0 124 124 496.0 (20], [21]], [[63], [64) 400.25 ... cap Q107277 https://musicbrainz.org/work/90fefc58-8ebe-46e... https://viaf.org/viaf/6344152502815710800004 https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3837 58-85 (A#3-C#6) Piano 37-69 (C#2-A4) Piano
F002_n07b {1: '3/4'} {1: 0} 44 44 132.0 44 44 132.0 () 312.75 ... cap Q107277 https://musicbrainz.org/work/3ff1582c-3fea-429... https://viaf.org/viaf/6344152502815710800004 https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3837 58-86 (Bb3-D6) Piano 40-67 (E2-G4) Piano
F002_n07c {1: '3/4'} {1: 0} 68 66 204.0 156 156 468.0 (24], [25]], [[67], [68) 416.25 ... cap Q107277 https://musicbrainz.org/work/d4a16857-8e9c-455... https://viaf.org/viaf/6344152502815710800004 https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3837 55-88 (G3-E6) Piano 36-72 (C2-C5) Piano
F003_n04a {1: '2/4'} {1: 2} 85 83 166.0 170 166 332.0 () 431.79 ... cap Q107277 https://musicbrainz.org/work/1be761f0-fbb7-4d8... https://viaf.org/viaf/185957894 https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... https://imslp.org/wiki/Special:ReverseLookup/3834 47-86 (B2-D6) Piano 38-76 (D2-E5) Piano
F003_n04b {1: '3/4'} {1: 2} 64 64 192.0 128 128 384.0 () 538.50 ... cap Q107277 https://musicbrainz.org/work/b5ac6def-d156-4a8... https://viaf.org/viaf/185957894 https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... https://imslp.org/wiki/Special:ReverseLookup/3834 54-85 (F#3-C#6) Piano 40-71 (E2-B4) Piano
F003_n04c {1: '2/2'} {1: 2} 110 108 432.0 220 216 864.0 () 1107.92 ... cap Q107277 https://musicbrainz.org/work/e8ab4cdb-56dc-4b8... https://viaf.org/viaf/185957894 https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... https://imslp.org/wiki/Special:ReverseLookup/3834 55-86 (G3-D6) Piano 35-74 (B1-D5) Piano

9 rows × 55 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
wf_bach_sonatas    1762.166667
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 1745 ('wf_bach_sonatas', 'F003_n04a') to 1784 ('wf_bach_sonatas', 'F002_n07a').

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 ... originalFormat P86 (composer) musicbrainz viaf imslp pdf staff_1_ambitus staff_1_instrument staff_2_ambitus staff_2_instrument
corpus piece
wf_bach_sonatas F001_n08a {1: '4/4'} {1: 0} 63 63 252.0 126 126 504.0 () 602.75 ... cap Q107277 https://musicbrainz.org/work/4cf167ee-4601-4d0... <NA> https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3838 52-89 (E3-F6) Piano 33-76 (A1-E5) Piano
F001_n08b {1: '3/4'} {1: 0} 42 40 126.0 40 40 120.0 (8], [9]], [[17], [18) 237.00 ... cap Q107277 https://musicbrainz.org/work/fd2561b4-869d-4af... <NA> https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3838 60-86 (C4-D6) Piano 36-71 (C2-B4) Piano
F001_n08c {1: '3/8', 17: '3/4', 18: '3/8', 81: '3/4', 82... {1: 0} 101 101 154.5 202 202 309.0 () 390.50 ... cap Q107277 https://musicbrainz.org/work/32703d10-01ac-4e5... <NA> https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3838 59-88 (B3-E6) Piano 31-69 (G1-A4) Piano
F002_n07a {1: '4/4'} {1: 0} 64 62 256.0 124 124 496.0 (20], [21]], [[63], [64) 400.25 ... cap Q107277 https://musicbrainz.org/work/90fefc58-8ebe-46e... https://viaf.org/viaf/6344152502815710800004 https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3837 58-85 (A#3-C#6) Piano 37-69 (C#2-A4) Piano
F002_n07b {1: '3/4'} {1: 0} 44 44 132.0 44 44 132.0 () 312.75 ... cap Q107277 https://musicbrainz.org/work/3ff1582c-3fea-429... https://viaf.org/viaf/6344152502815710800004 https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3837 58-86 (Bb3-D6) Piano 40-67 (E2-G4) Piano
F002_n07c {1: '3/4'} {1: 0} 68 66 204.0 156 156 468.0 (24], [25]], [[67], [68) 416.25 ... cap Q107277 https://musicbrainz.org/work/d4a16857-8e9c-455... https://viaf.org/viaf/6344152502815710800004 https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... https://imslp.org/wiki/Special:ReverseLookup/3837 55-88 (G3-E6) Piano 36-72 (C2-C5) Piano
F003_n04a {1: '2/4'} {1: 2} 85 83 166.0 170 166 332.0 () 431.79 ... cap Q107277 https://musicbrainz.org/work/1be761f0-fbb7-4d8... https://viaf.org/viaf/185957894 https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... https://imslp.org/wiki/Special:ReverseLookup/3834 47-86 (B2-D6) Piano 38-76 (D2-E5) Piano
F003_n04b {1: '3/4'} {1: 2} 64 64 192.0 128 128 384.0 () 538.50 ... cap Q107277 https://musicbrainz.org/work/b5ac6def-d156-4a8... https://viaf.org/viaf/185957894 https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... https://imslp.org/wiki/Special:ReverseLookup/3834 54-85 (F#3-C#6) Piano 40-71 (E2-B4) Piano
F003_n04c {1: '2/2'} {1: 2} 110 108 432.0 220 216 864.0 () 1107.92 ... cap Q107277 https://musicbrainz.org/work/e8ab4cdb-56dc-4b8... https://viaf.org/viaf/185957894 https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... https://imslp.org/wiki/Special:ReverseLookup/3834 55-86 (G3-D6) Piano 35-74 (B1-D5) Piano

9 rows × 55 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
Keyboard Sonata in C major 6 376 1124 4957 924
Keyboard Sonata in D major 3 255 790 3594 829
sum 9 631 1914 8551 1753
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 |
|:----------------|---------:|-----------:|---------:|--------:|---------:|
| wf_bach_sonatas |        9 |        631 |     1914 |    8551 |     1753 |
| sum             |        9 |        631 |     1914 |    8551 |     1753 |

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()
635 measures over 9 files.
mc mn quarterbeats duration_qb keysig timesig act_dur mc_offset numbering_offset dont_count barline breaks repeats next volta markers jump_bwd jump_fwd play_until
corpus piece i
wf_bach_sonatas F001_n08a 0 1 1 0 4.0 0 4/4 1 0 <NA> <NA> <NA> <NA> firstMeasure (2,) <NA> <NA> <NA> <NA> <NA>
1 2 2 4 4.0 0 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (3,) <NA> <NA> <NA> <NA> <NA>
2 3 3 8 4.0 0 4/4 1 0 <NA> <NA> <NA> line <NA> (4,) <NA> <NA> <NA> <NA> <NA>
3 4 4 12 4.0 0 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (5,) <NA> <NA> <NA> <NA> <NA>
4 5 5 16 4.0 0 4/4 1 0 <NA> <NA> <NA> <NA> <NA> (6,) <NA> <NA> <NA> <NA> <NA>
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 duration_qb mc_onset mn_onset timesig staff voice volta ... 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
wf_bach_sonatas F001_n08a 0 1 1 0 4.0 0 0 4/4 2 1 <NA> ... I (M3, P5) (M3, P5) (1, 3, 5) (1, 3, 5), major (1, 3, 5) (1, #3, 5) C I I
1 2 2 4 2.0 0 0 4/4 2 1 <NA> ... IV (P4, M6) (M3, P5) (1, 4, 6) (1, 4, 6), major (1, 4, 6) (1, 4, #6) C I IV64
2 2 2 6 0.5 1/2 1/2 4/4 2 1 <NA> ... I (P4, M6) (P4, M6) (1, 4, 6) (1, 4, 6), major (1, 4, 6) (1, 4, #6) C I I(64)
3 2 2 13/2 1.5 5/8 5/8 4/4 2 1 <NA> ... I (M3, P5) (M3, P5) (1, 3, 5) (1, 3, 5), major (1, 3, 5) (1, #3, 5) C I I
4 3 3 8 2.0 0 0 4/4 2 1 <NA> ... V (M3, P5, m7) (M3, P5, m7) (5, 7, 2, 4) (5, 7, 2, 4), major (5, 7, 2, 4) (5, #7, 2, 4) C I V7

5 rows × 52 columns

Concatenated annotation tables contains 1724 rows.
Dataset contains 1724 tokens and 314 types over 9 documents.