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("cpe_bach_keyboard", corpus_release="v2.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("Carl Philipp Emanuel Bach – Works for Keyboard version v2.2")
print(f"Datapackage '{package.package_name}' @ {git_tag}")
print(f"dimcat version {dc.__version__}\n")
D
Data and software versions
--------------------------

Carl Philipp Emanuel Bach – Works for Keyboard version v2.2
Datapackage 'cpe_bach_keyboard' @ v2.2
dimcat version 3.4.0
Dataset
=======
{'inputs': {'basepath': None,
            'packages': {'cpe_bach_keyboard': ["'cpe_bach_keyboard.measures' (MuseScoreFacetName.MuseScoreMeasures)",
                                               "'cpe_bach_keyboard.notes' (MuseScoreFacetName.MuseScoreNotes)",
                                               "'cpe_bach_keyboard.expanded' (MuseScoreFacetName.MuseScoreHarmonies)",
                                               "'cpe_bach_keyboard.chords' (MuseScoreFacetName.MuseScoreChords)",
                                               "'cpe_bach_keyboard.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 originalFormat pdf typesetter viaf P86 (composer) staff_1_instrument staff_1_ambitus staff_2_instrument staff_2_ambitus
corpus piece
cpe_bach_keyboard wq112n02 {1: '3/4'} {1: 2} 19 19 57.0 19 19 57.0 () 160.50 ... https://musicbrainz.org/work/4df7f68e-2f71-42e... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova https://viaf.org/viaf/292645495 Q76428 Harpsichord 50-88 (D3-E6) Harpsichord 35-66 (B1-F#4)
wq112n08 {1: '4/4'} {1: -2} 12 12 48.0 12 12 48.0 () 110.38 ... https://musicbrainz.org/work/715cb245-20aa-452... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova https://viaf.org/viaf/293044251 Q76429 Harpsichord 57-87 (A3-Eb6) Harpsichord 34-65 (Bb1-F4)
wq112n15 {1: '4/4'} {1: -1} 13 13 61.0 13 13 61.0 () 366.75 ... https://musicbrainz.org/work/2fa9c40d-556b-4a6... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova <NA> Q76430 Harpsichord 53-81 (F3-A5) Harpsichord 36-65 (C2-F4)
wq113n03 {1: '4/4'} {1: -1} 7 7 28.0 7 7 28.0 () 50.00 ... https://musicbrainz.org/work/a3421fbb-f998-4f4... mxl https://imslp.org/wiki/Special:ReverseLookup/9... Anna Yuferova https://viaf.org/viaf/294403473 Q76431 Harpsichord 52-88 (E3-E6) Harpsichord 38-62 (D2-D4)
wq114n07 {1: '4/4'} {1: -1} 4 4 54.0 4 4 54.0 () 393.25 ... https://musicbrainz.org/work/41c296c8-6c35-4f2... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova https://viaf.org/viaf/1388165272164710690001 Q76432 Harpsichord 56-86 (G#3-D6) Harpsichord 38-62 (D2-D4)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
wq57n04c {1: '2/4'} {1: -1} 52 52 104.0 104 104 208.0 () 238.75 ... https://musicbrainz.org/work/9ed3bac3-5e4b-4b6... mxl https://imslp.org/wiki/Special:ReverseLookup/2... Anna Yuferova https://viaf.org/viaf/2725165271416210690003 Q76489 Harpsichord 56-89 (G#3-F6) Harpsichord 38-67 (D2-G4)
wq57n05 {1: '2/4'} {1: -1} 224 224 448.0 224 224 448.0 () 933.25 ... https://musicbrainz.org/work/ab4979b1-205e-471... mxl https://imslp.org/wiki/Special:ReverseLookup/9... Anna Yuferova https://viaf.org/viaf/595165271274110690002 Q76490 Harpsichord 46-89 (Bb2-F6) Harpsichord 31-67 (G1-G4)
wq57n06a {1: '4/4'} {1: -4} 92 90 368.0 180 180 720.0 (34], [35]], [[91], [92) 741.33 ... https://musicbrainz.org/work/22b8fdea-d7dc-4de... mxl https://imslp.org/wiki/Special:ReverseLookup/5... Anna Yuferova https://viaf.org/viaf/9293165272355810690008 Q76491 Harpsichord 44-89 (Ab2-F6) Harpsichord 34-65 (Bb1-F4)
wq57n06b {1: '4/4'} {1: -1} 44 44 176.0 44 44 176.0 () 432.12 ... https://musicbrainz.org/work/74e15639-2300-44e... mxl https://imslp.org/wiki/Special:ReverseLookup/5... Anna Yuferova https://viaf.org/viaf/9293165272355810690008 Q76492 Harpsichord 50-87 (D3-Eb6) Harpsichord 34-67 (Bb1-G4)
wq57n06c {1: '2/4'} {1: -4} 72 70 144.0 140 140 280.0 (36], [37]], [[71], [72) 381.62 ... https://musicbrainz.org/work/0af2ab1e-5c48-4a1... mxl https://imslp.org/wiki/Special:ReverseLookup/5... Anna Yuferova https://viaf.org/viaf/9293165272355810690008 Q76493 Harpsichord 51-89 (Eb3-F6) Harpsichord 32-65 (Ab1-F4)

66 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
cpe_bach_keyboard    1766.787879
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 1754 ('cpe_bach_keyboard', 'wq119n07') to 1780 ('cpe_bach_keyboard', 'wq56n04a').

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 originalFormat pdf typesetter viaf P86 (composer) staff_1_instrument staff_1_ambitus staff_2_instrument staff_2_ambitus
corpus piece
cpe_bach_keyboard wq112n02 {1: '3/4'} {1: 2} 19 19 57.0 19 19 57.0 () 160.50 ... https://musicbrainz.org/work/4df7f68e-2f71-42e... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova https://viaf.org/viaf/292645495 Q76428 Harpsichord 50-88 (D3-E6) Harpsichord 35-66 (B1-F#4)
wq112n08 {1: '4/4'} {1: -2} 12 12 48.0 12 12 48.0 () 110.38 ... https://musicbrainz.org/work/715cb245-20aa-452... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova https://viaf.org/viaf/293044251 Q76429 Harpsichord 57-87 (A3-Eb6) Harpsichord 34-65 (Bb1-F4)
wq112n15 {1: '4/4'} {1: -1} 13 13 61.0 13 13 61.0 () 366.75 ... https://musicbrainz.org/work/2fa9c40d-556b-4a6... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova <NA> Q76430 Harpsichord 53-81 (F3-A5) Harpsichord 36-65 (C2-F4)
wq113n03 {1: '4/4'} {1: -1} 7 7 28.0 7 7 28.0 () 50.00 ... https://musicbrainz.org/work/a3421fbb-f998-4f4... mxl https://imslp.org/wiki/Special:ReverseLookup/9... Anna Yuferova https://viaf.org/viaf/294403473 Q76431 Harpsichord 52-88 (E3-E6) Harpsichord 38-62 (D2-D4)
wq114n07 {1: '4/4'} {1: -1} 4 4 54.0 4 4 54.0 () 393.25 ... https://musicbrainz.org/work/41c296c8-6c35-4f2... mxl https://imslp.org/wiki/Special:ReverseLookup/3... Anna Yuferova https://viaf.org/viaf/1388165272164710690001 Q76432 Harpsichord 56-86 (G#3-D6) Harpsichord 38-62 (D2-D4)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
wq57n04c {1: '2/4'} {1: -1} 52 52 104.0 104 104 208.0 () 238.75 ... https://musicbrainz.org/work/9ed3bac3-5e4b-4b6... mxl https://imslp.org/wiki/Special:ReverseLookup/2... Anna Yuferova https://viaf.org/viaf/2725165271416210690003 Q76489 Harpsichord 56-89 (G#3-F6) Harpsichord 38-67 (D2-G4)
wq57n05 {1: '2/4'} {1: -1} 224 224 448.0 224 224 448.0 () 933.25 ... https://musicbrainz.org/work/ab4979b1-205e-471... mxl https://imslp.org/wiki/Special:ReverseLookup/9... Anna Yuferova https://viaf.org/viaf/595165271274110690002 Q76490 Harpsichord 46-89 (Bb2-F6) Harpsichord 31-67 (G1-G4)
wq57n06a {1: '4/4'} {1: -4} 92 90 368.0 180 180 720.0 (34], [35]], [[91], [92) 741.33 ... https://musicbrainz.org/work/22b8fdea-d7dc-4de... mxl https://imslp.org/wiki/Special:ReverseLookup/5... Anna Yuferova https://viaf.org/viaf/9293165272355810690008 Q76491 Harpsichord 44-89 (Ab2-F6) Harpsichord 34-65 (Bb1-F4)
wq57n06b {1: '4/4'} {1: -1} 44 44 176.0 44 44 176.0 () 432.12 ... https://musicbrainz.org/work/74e15639-2300-44e... mxl https://imslp.org/wiki/Special:ReverseLookup/5... Anna Yuferova https://viaf.org/viaf/9293165272355810690008 Q76492 Harpsichord 50-87 (D3-Eb6) Harpsichord 34-67 (Bb1-G4)
wq57n06c {1: '2/4'} {1: -4} 72 70 144.0 140 140 280.0 (36], [37]], [[71], [72) 381.62 ... https://musicbrainz.org/work/0af2ab1e-5c48-4a1... mxl https://imslp.org/wiki/Special:ReverseLookup/5... Anna Yuferova https://viaf.org/viaf/9293165272355810690008 Q76493 Harpsichord 51-89 (Eb3-F6) Harpsichord 32-65 (Ab1-F4)

66 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
Fantasia and Fugue in C Minor 1 123 530 1979 340
Fantasia in B-flat Major 1 12 48 244 56
Fantasia in D Major 2 45 169 647 74
Fantasia in D Minor 3 25 138 685 95
Fantasia in F Major 1 13 61 231 28
Fantasia in G Major 1 14 56 252 35
Fantasia in G Minor 1 3 170 731 86
Rondo in A Minor 1 172 346 2222 308
Rondo in C Major 1 176 529 2662 435
Rondo in D Major 1 149 486 1972 425
Rondo in E Major 1 94 409 2529 557
Rondo in F Major 1 224 448 2074 366
Rondo in G Major 1 141 282 1563 283
Sonata in A Major 5 388 1149 5823 874
Sonata in A Minor 6 464 1318 5247 856
Sonata in B Minor 3 135 258 1373 277
Sonata in B-flat Major 3 393 1193 4667 890
Sonata in C Major 3 152 418 1831 231
Sonata in C Minor 1 234 703 1903 450
Sonata in D Minor 6 510 1234 5754 1174
Sonata in F Major 11 741 1747 7949 1446
Sonata in F Minor 3 204 688 2894 402
Sonata in G Major 9 717 2004 8595 1503
sum 66 5129 14386 63827 11191
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 |
|:------------------|---------:|-----------:|---------:|--------:|---------:|
| cpe_bach_keyboard |       66 |       5129 |    14386 |   63827 |    11191 |
| sum               |       66 |       5129 |    14386 |   63827 |    11191 |

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()
5165 measures over 66 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
cpe_bach_keyboard wq112n02 0 1 1 0 3.0 2 3/4 3/4 0 <NA> <NA> <NA> <NA> firstMeasure (2,) <NA> <NA> <NA> <NA> <NA>
1 2 2 3 3.0 2 3/4 3/4 0 <NA> <NA> <NA> <NA> <NA> (3,) <NA> <NA> <NA> <NA> <NA>
2 3 3 6 3.0 2 3/4 3/4 0 <NA> <NA> <NA> <NA> <NA> (4,) <NA> <NA> <NA> <NA> <NA>
3 4 4 9 3.0 2 3/4 3/4 0 <NA> <NA> <NA> <NA> <NA> (5,) <NA> <NA> <NA> <NA> <NA>
4 5 5 12 3.0 2 3/4 3/4 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
cpe_bach_keyboard wq112n02 0 1 1 0 1.0 0 0 3/4 2 1 <NA> ... I (M3, P5) (M3, P5) (1, 3, 5) (1, 3, 5), major (1, 3, 5) (1, #3, 5) D I I
1 1 1 1 1.0 1/4 1/4 3/4 2 1 <NA> ... ii (M3, M6) (m3, P5) (4, 6, 2) (4, 6, 2), major (4, 6, 2) (4, #6, 2) D I ii6
2 1 1 2 1.0 1/2 1/2 3/4 2 1 <NA> ... V (M3, P5) (M3, P5) (5, 7, 2) (5, 7, 2), major (5, 7, 2) (5, #7, 2) D I V
3 2 2 3 3.0 0 0 3/4 2 1 <NA> ... I (M3, P5) (M3, P5) (1, 3, 5) (1, 3, 5), major (1, 3, 5) (1, #3, 5) D I I
4 3 3 6 1.0 0 0 3/4 2 1 <NA> ... vi (m3, P5) (m3, P5) (6, 1, 3) (6, 1, 3), major (6, 1, 3) (#6, 1, #3) D I i/vi

5 rows × 52 columns

Concatenated annotation tables contains 10863 rows.
Dataset contains 10863 tokens and 698 types over 66 documents.