Overview#
This notebook gives a general overview of the features included in the dataset.
Show 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("scarlatti_sonatas", corpus_release="v2.4")
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("Domenico Scarlatti – Keyboard Sonatas version v2.4")
print(f"Datapackage '{package.package_name}' @ {git_tag}")
print(f"dimcat version {dc.__version__}\n")
D
Data and software versions
--------------------------
Domenico Scarlatti – Keyboard Sonatas version v2.4
Datapackage 'scarlatti_sonatas' @ v2.4
dimcat version 3.4.0
Dataset
=======
{'inputs': {'basepath': None,
'packages': {'scarlatti_sonatas': ["'scarlatti_sonatas.measures' (MuseScoreFacetName.MuseScoreMeasures)",
"'scarlatti_sonatas.notes' (MuseScoreFacetName.MuseScoreNotes)",
"'scarlatti_sonatas.expanded' (MuseScoreFacetName.MuseScoreHarmonies)",
"'scarlatti_sonatas.chords' (MuseScoreFacetName.MuseScoreChords)",
"'scarlatti_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 | ... | typesetter | wikidata | viaf | musicbrainz | imslp | staff_1_ambitus | staff_1_instrument | staff_2_ambitus | staff_2_instrument | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| corpus | piece | |||||||||||||||||||||
| scarlatti_sonatas | K001 | {1: '4/4'} | {1: -1} | 31 | 31 | 124.0 | 62 | 62 | 248.0 | () | 264.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | https://musescore.com/pongpob_c_ | https://www.wikidata.org/wiki/Q6132112 | https://viaf.org/viaf/178852701/ | https://musicbrainz.org/work/4dad1f72-5f8d-3a6... | https://imslp.org/wiki/Keyboard_Sonata_in_D_mi... | 38-84 (D2-C6) | Harpsichord | 40-69 (E2-A4) | Harpsichord |
| K002 | {1: '3/8'} | {1: 1} | 78 | 78 | 117.0 | 156 | 156 | 234.0 | () | 215.00 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | https://musescore.com/leandro15 | https://www.wikidata.org/wiki/Q67199887 | https://viaf.org/viaf/293775750/ | https://musicbrainz.org/work/a973d150-3732-42c... | https://imslp.org/wiki/Keyboard_Sonata_in_G_ma... | 55-81 (G3-A5) | Harpsichord | 38-74 (D2-D5) | Harpsichord | |
| K003 | {1: '2/2'} | {1: 0} | 96 | 94 | 376.0 | 192 | 188 | 752.0 | () | 692.00 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199888 | https://viaf.org/viaf/292516333/ | https://musicbrainz.org/work/c8066208-1574-4d8... | https://imslp.org/wiki/Keyboard_Sonata_in_A_mi... | 57-84 (A3-C6) | Harpsichord | 36-64 (C2-E4) | Harpsichord | |
| K004 | {1: '4/4'} | {1: -2} | 41 | 39 | 156.0 | 82 | 78 | 312.0 | () | 359.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199889 | https://viaf.org/viaf/292628173/ | https://musicbrainz.org/work/7db8669e-717d-4a5... | https://imslp.org/wiki/Keyboard_Sonata_in_G_mi... | 55-84 (G3-C6) | Harpsichord | 38-70 (D2-Bb4) | Harpsichord | |
| K005 | {1: '3/8'} | {1: -1} | 90 | 90 | 135.0 | 180 | 180 | 270.0 | () | 309.25 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q74594868 | https://viaf.org/viaf/293025261/ | https://musicbrainz.org/work/7f08449b-45b3-406... | https://imslp.org/wiki/Keyboard_Sonata_in_D_mi... | 50-84 (D3-C6) | Harpsichord | 38-69 (D2-A4) | Harpsichord | |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | |
| K096 | {1: '3/8'} | {1: 2} | 211 | 211 | 316.5 | 422 | 422 | 633.0 | () | 928.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199923 | https://viaf.org/viaf/181668778/ | https://musicbrainz.org/work/be495691-1719-39e... | https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... | 52-86 (E3-D6) | Harpsichord | 33-72 (A1-C5) | Harpsichord | |
| K097 | {1: '3/8'} | {1: -2} | 247 | 247 | 370.5 | 494 | 494 | 741.0 | () | 763.75 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q96373981 | https://viaf.org/viaf/292389041/ | https://musicbrainz.org/work/d0d92e6c-5605-435... | https://imslp.org/wiki/Keyboard_Sonata_in_G_mi... | 51-87 (Eb3-Eb6) | Harpsichord | 38-74 (D2-D5) | Harpsichord | |
| K098 | {1: '3/8'} | {1: 1} | 113 | 113 | 169.5 | 226 | 226 | 339.0 | () | 426.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | https://musescore.com/user/339551 | https://www.wikidata.org/wiki/Q67199924 | https://viaf.org/viaf/177261308/ | https://musicbrainz.org/work/e8830311-b09e-486... | https://imslp.org/wiki/Keyboard_Sonata_in_E_mi... | 54-84 (F#3-C6) | Harpsichord | 38-73 (D2-C#5) | Harpsichord | |
| K099 | {1: '3/4'} | {1: -3} | 86 | 86 | 258.0 | 172 | 172 | 516.0 | () | 692.75 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q74594835 | https://viaf.org/viaf/179274368/ | https://musicbrainz.org/work/5afa216c-8efe-467... | https://imslp.org/wiki/Keyboard_Sonata_in_C_mi... | 55-84 (G3-C6) | Harpsichord | 36-84 (C2-C6) | Harpsichord | |
| K100 | {1: '12/8'} | {1: 0} | 50 | 50 | 300.0 | 100 | 100 | 600.0 | () | 709.00 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199925 | https://viaf.org/viaf/292663647/ | https://musicbrainz.org/work/2dd5e550-d27a-490... | https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... | 55-84 (G3-C6) | Harpsichord | 36-84 (C2-C6) | Harpsichord |
69 rows × 60 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
scarlatti_sonatas 1740.862319
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 1738 ('scarlatti_sonatas', 'K001') to 1749 ('scarlatti_sonatas', 'K098').
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()
Show 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 | ... | typesetter | wikidata | viaf | musicbrainz | imslp | staff_1_ambitus | staff_1_instrument | staff_2_ambitus | staff_2_instrument | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| corpus | piece | |||||||||||||||||||||
| scarlatti_sonatas | K001 | {1: '4/4'} | {1: -1} | 31 | 31 | 124.0 | 62 | 62 | 248.0 | () | 264.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | https://musescore.com/pongpob_c_ | https://www.wikidata.org/wiki/Q6132112 | https://viaf.org/viaf/178852701/ | https://musicbrainz.org/work/4dad1f72-5f8d-3a6... | https://imslp.org/wiki/Keyboard_Sonata_in_D_mi... | 38-84 (D2-C6) | Harpsichord | 40-69 (E2-A4) | Harpsichord |
| K002 | {1: '3/8'} | {1: 1} | 78 | 78 | 117.0 | 156 | 156 | 234.0 | () | 215.00 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | https://musescore.com/leandro15 | https://www.wikidata.org/wiki/Q67199887 | https://viaf.org/viaf/293775750/ | https://musicbrainz.org/work/a973d150-3732-42c... | https://imslp.org/wiki/Keyboard_Sonata_in_G_ma... | 55-81 (G3-A5) | Harpsichord | 38-74 (D2-D5) | Harpsichord | |
| K003 | {1: '2/2'} | {1: 0} | 96 | 94 | 376.0 | 192 | 188 | 752.0 | () | 692.00 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199888 | https://viaf.org/viaf/292516333/ | https://musicbrainz.org/work/c8066208-1574-4d8... | https://imslp.org/wiki/Keyboard_Sonata_in_A_mi... | 57-84 (A3-C6) | Harpsichord | 36-64 (C2-E4) | Harpsichord | |
| K004 | {1: '4/4'} | {1: -2} | 41 | 39 | 156.0 | 82 | 78 | 312.0 | () | 359.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199889 | https://viaf.org/viaf/292628173/ | https://musicbrainz.org/work/7db8669e-717d-4a5... | https://imslp.org/wiki/Keyboard_Sonata_in_G_mi... | 55-84 (G3-C6) | Harpsichord | 38-70 (D2-Bb4) | Harpsichord | |
| K005 | {1: '3/8'} | {1: -1} | 90 | 90 | 135.0 | 180 | 180 | 270.0 | () | 309.25 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q74594868 | https://viaf.org/viaf/293025261/ | https://musicbrainz.org/work/7f08449b-45b3-406... | https://imslp.org/wiki/Keyboard_Sonata_in_D_mi... | 50-84 (D3-C6) | Harpsichord | 38-69 (D2-A4) | Harpsichord | |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | |
| K096 | {1: '3/8'} | {1: 2} | 211 | 211 | 316.5 | 422 | 422 | 633.0 | () | 928.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199923 | https://viaf.org/viaf/181668778/ | https://musicbrainz.org/work/be495691-1719-39e... | https://imslp.org/wiki/Keyboard_Sonata_in_D_ma... | 52-86 (E3-D6) | Harpsichord | 33-72 (A1-C5) | Harpsichord | |
| K097 | {1: '3/8'} | {1: -2} | 247 | 247 | 370.5 | 494 | 494 | 741.0 | () | 763.75 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q96373981 | https://viaf.org/viaf/292389041/ | https://musicbrainz.org/work/d0d92e6c-5605-435... | https://imslp.org/wiki/Keyboard_Sonata_in_G_mi... | 51-87 (Eb3-Eb6) | Harpsichord | 38-74 (D2-D5) | Harpsichord | |
| K098 | {1: '3/8'} | {1: 1} | 113 | 113 | 169.5 | 226 | 226 | 339.0 | () | 426.50 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | https://musescore.com/user/339551 | https://www.wikidata.org/wiki/Q67199924 | https://viaf.org/viaf/177261308/ | https://musicbrainz.org/work/e8830311-b09e-486... | https://imslp.org/wiki/Keyboard_Sonata_in_E_mi... | 54-84 (F#3-C6) | Harpsichord | 38-73 (D2-C#5) | Harpsichord | |
| K099 | {1: '3/4'} | {1: -3} | 86 | 86 | 258.0 | 172 | 172 | 516.0 | () | 692.75 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q74594835 | https://viaf.org/viaf/179274368/ | https://musicbrainz.org/work/5afa216c-8efe-467... | https://imslp.org/wiki/Keyboard_Sonata_in_C_mi... | 55-84 (G3-C6) | Harpsichord | 36-84 (C2-C6) | Harpsichord | |
| K100 | {1: '12/8'} | {1: 0} | 50 | 50 | 300.0 | 100 | 100 | 600.0 | () | 709.00 | ... | https://imslp.org/wiki/Special:ReverseLookup/3... | Tom Schreyer | https://www.wikidata.org/wiki/Q67199925 | https://viaf.org/viaf/292663647/ | https://musicbrainz.org/work/2dd5e550-d27a-490... | https://imslp.org/wiki/Keyboard_Sonata_in_C_ma... | 55-84 (G3-C6) | Harpsichord | 36-84 (C2-C6) | Harpsichord |
69 rows × 60 columns
Composition years histogram#
Show 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 | |
|---|---|---|---|---|---|
| Sonata | 69 | 5560 | 13706 | 64287 | 12490 |
| sum | 69 | 5560 | 13706 | 64287 | 12490 |
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 |
|:------------------|---------:|-----------:|---------:|--------:|---------:|
| scarlatti_sonatas | 69 | 5560 | 13706 | 64287 | 12490 |
| sum | 69 | 5560 | 13706 | 64287 | 12490 |
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()
5581 measures over 69 files.
| mc | mn | quarterbeats | duration_qb | keysig | timesig | act_dur | mc_offset | numbering_offset | dont_count | barline | breaks | repeats | next | volta | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| corpus | piece | i | |||||||||||||||
| scarlatti_sonatas | K001 | 0 | 1 | 1 | 0 | 4.0 | -1 | 4/4 | 1 | 0 | <NA> | <NA> | <NA> | <NA> | firstMeasure | (2,) | <NA> |
| 1 | 2 | 2 | 4 | 4.0 | -1 | 4/4 | 1 | 0 | <NA> | <NA> | <NA> | <NA> | <NA> | (3,) | <NA> | ||
| 2 | 3 | 3 | 8 | 4.0 | -1 | 4/4 | 1 | 0 | <NA> | <NA> | <NA> | <NA> | <NA> | (4,) | <NA> | ||
| 3 | 4 | 4 | 12 | 4.0 | -1 | 4/4 | 1 | 0 | <NA> | <NA> | <NA> | <NA> | <NA> | (5,) | <NA> | ||
| 4 | 5 | 5 | 16 | 4.0 | -1 | 4/4 | 1 | 0 | <NA> | <NA> | <NA> | <NA> | <NA> | (6,) | <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 | |||||||||||||||||||||
| scarlatti_sonatas | K001 | 0 | 1 | 1 | 0 | 1.5 | 0 | 0 | 4/4 | 2 | 1 | <NA> | ... | i | (m3, P5) | (m3, P5) | (1, 3, 5) | (1, 3, 5), minor | (1, b3, 5) | (1, 3, 5) | d | i | i |
| 1 | 1 | 1 | 3/2 | 0.5 | 3/8 | 3/8 | 4/4 | 2 | 1 | <NA> | ... | V | (M3, P5) | (M3, P5) | (5, #7, 2) | (5, #7, 2), minor | (5, 7, 2) | (5, #7, 2) | d | i | V | ||
| 2 | 1 | 1 | 2 | 1.5 | 1/2 | 1/2 | 4/4 | 2 | 1 | <NA> | ... | i | (m3, P5) | (m3, P5) | (1, 3, 5) | (1, 3, 5), minor | (1, b3, 5) | (1, 3, 5) | d | i | i | ||
| 3 | 1 | 1 | 7/2 | 0.5 | 7/8 | 7/8 | 4/4 | 2 | 1 | <NA> | ... | V | (M3, P5) | (M3, P5) | (5, #7, 2) | (5, #7, 2), minor | (5, 7, 2) | (5, #7, 2) | d | i | V | ||
| 4 | 2 | 2 | 4 | 1.5 | 0 | 0 | 4/4 | 2 | 1 | <NA> | ... | i | (m3, P5) | (m3, P5) | (1, 3, 5) | (1, 3, 5), minor | (1, b3, 5) | (1, 3, 5) | d | i | i |
5 rows × 52 columns
Concatenated annotation tables contains 12245 rows.
3 of them are not chords. Their values are: {'ii%6(9)/i': 2, 'ii%/vi': 1}
Dataset contains 12242 tokens and 731 types over 69 documents.