Annotations#

Hide imports
%load_ext autoreload
%autoreload 2

import os

import dimcat as dc
import ms3
import plotly.express as px
from dimcat import groupers, plotting

import utils
Hide source
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

Hide source
D = utils.get_dataset("handel_keyboard", 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("Georg Friedrich Händel – Grobschmied Variations (The Harmonious Blacksmith), HWV 430 version v2.4")
print(f"Datapackage '{package.package_name}' @ {git_tag}")
print(f"dimcat version {dc.__version__}\n")
D
Data and software versions
--------------------------

Georg Friedrich Händel – Grobschmied Variations (The Harmonious Blacksmith), HWV 430 version v2.4
Datapackage 'handel_keyboard' @ v2.4
dimcat version 3.4.0
Dataset
=======
{'inputs': {'basepath': None,
            'packages': {'handel_keyboard': ["'handel_keyboard.measures' (MuseScoreFacetName.MuseScoreMeasures)",
                                             "'handel_keyboard.notes' (MuseScoreFacetName.MuseScoreNotes)",
                                             "'handel_keyboard.expanded' (MuseScoreFacetName.MuseScoreHarmonies)",
                                             "'handel_keyboard.chords' (MuseScoreFacetName.MuseScoreChords)",
                                             "'handel_keyboard.metadata' (FeatureName.Metadata)"]}},
 'outputs': {'basepath': None, 'packages': {}},
 'pipeline': []}
filtered_D = D.apply_step("HasHarmonyLabelsFilter")
all_metadata = filtered_D.get_metadata()
assert len(all_metadata) > 0, "No pieces selected for analysis."
chronological_corpus_names = all_metadata.get_corpus_names()

DCML harmony labels#

Hide source
all_annotations = filtered_D.get_feature("DcmlAnnotations")
is_annotated_mask = all_metadata.label_count > 0
is_annotated_index = all_metadata.index[is_annotated_mask]
annotated_notes = filtered_D.get_feature("notes").subselect(is_annotated_index)
print(f"The annotated pieces have {len(annotated_notes)} notes.")
The annotated pieces have 1721 notes.
all_chords = filtered_D.get_feature("harmonylabels")
print(
    f"{len(all_annotations)} annotations, of which {len(all_chords)} are harmony labels."
)
346 annotations, of which 346 are harmony labels.

Harmony labels#

Unigrams#

For computing unigram statistics, the tokens need to be grouped by their occurrence within a major or a minor key because this changes their meaning. To that aim, the annotated corpus needs to be sliced into contiguous localkey segments which are then grouped into a major (is_minor=False) and a minor group.

root_durations = (
    all_chords[all_chords.root.between(-5, 6)]
    .groupby(["root", "chord_type"])
    .duration_qb.sum()
)
# sort by stacked bar length:
# root_durations = root_durations.sort_values(key=lambda S: S.index.get_level_values(0).map(S.groupby(level=0).sum()),
# ascending=False)
bar_data = root_durations.reset_index()
bar_data.root = bar_data.root.map(ms3.fifths2iv)
fig = px.bar(
    bar_data,
    x="root",
    y="duration_qb",
    color="chord_type",
    title="Distribution of chord types over chord roots",
    labels=dict(
        root="Chord root expressed as interval above the local (or secondary) tonic",
        duration_qb="duration in quarter notes",
        chord_type="chord type",
    ),
)
fig.update_layout(**utils.STD_LAYOUT)
save_figure_as(fig, "chord_type_distribution_over_scale_degrees_absolute_stacked_bars")
fig.show()
relative_roots = all_chords[
    ["numeral", "duration_qb", "relativeroot", "localkey_is_minor", "chord_type"]
].copy()
relative_roots["relativeroot_resolved"] = ms3.transform(
    relative_roots, ms3.resolve_relative_keys, ["relativeroot", "localkey_is_minor"]
)
has_rel = relative_roots.relativeroot_resolved.notna()
relative_roots.loc[has_rel, "localkey_is_minor"] = relative_roots.loc[
    has_rel, "relativeroot_resolved"
].str.islower()
relative_roots["root"] = ms3.transform(
    relative_roots, ms3.roman_numeral2fifths, ["numeral", "localkey_is_minor"]
)
chord_type_frequency = all_chords.chord_type.value_counts()
replace_rare = ms3.map_dict(
    {t: "other" for t in chord_type_frequency[chord_type_frequency < 500].index}
)
relative_roots["type_reduced"] = relative_roots.chord_type.map(replace_rare)
# is_special = relative_roots.chord_type.isin(('It', 'Ger', 'Fr'))
# relative_roots.loc[is_special, 'root'] = -4
root_durations = (
    relative_roots.groupby(["root", "type_reduced"])
    .duration_qb.sum()
    .sort_values(ascending=False)
)
bar_data = root_durations.reset_index()
bar_data.root = bar_data.root.map(ms3.fifths2iv)
root_order = (
    bar_data.groupby("root")
    .duration_qb.sum()
    .sort_values(ascending=False)
    .index.to_list()
)
fig = px.bar(
    bar_data,
    x="root",
    y="duration_qb",
    color="type_reduced",
    barmode="group",
    log_y=True,
    color_discrete_map=utils.TYPE_COLORS,
    category_orders=dict(
        root=root_order,
        type_reduced=relative_roots.type_reduced.value_counts().index.to_list(),
    ),
    labels=dict(
        root="intervallic difference between chord root to the local or secondary tonic",
        duration_qb="duration in quarter notes",
        type_reduced="chord type",
    ),
    width=1000,
    height=400,
)
fig.update_layout(
    **utils.STD_LAYOUT,
    legend=dict(
        orientation="h",
        xanchor="right",
        x=1,
        y=1,
    ),
)
save_figure_as(fig, "chord_type_distribution_over_scale_degrees_absolute_grouped_bars")
fig.show()
print(
    f"Reduced to {len(set(bar_data.iloc[:,:2].itertuples(index=False, name=None)))} types. "
    f"Paper cites the sum of types in major and types in minor (see below), treating them as distinct."
)
Reduced to 7 types. Paper cites the sum of types in major and types in minor (see below), treating them as distinct.
dim_or_aug = bar_data[
    bar_data.root.str.startswith("a") | bar_data.root.str.startswith("d")
].duration_qb.sum()
complete = bar_data.duration_qb.sum()
print(
    f"On diminished or augmented scale degrees: {dim_or_aug} / {complete} = {dim_or_aug / complete}"
)
On diminished or augmented scale degrees: 0.0 / 218.0 = 0.0
chords_by_mode = groupers.ModeGrouper().process(all_chords)
chords_by_mode.format = "scale_degree"

Whole dataset#

unigram_proportions = chords_by_mode.get_default_analysis()
unigram_proportions.make_ranking_table()
mode major
chord_and_mode scale_degrees duration_qb proportion proportion_%
rank
1 I, major (1, 3, 5) 59.50 0.272936 27.29 %
2 I6, major (3, 5, 1) 35.00 0.160550 16.06 %
3 V, major (5, 7, 2) 19.25 0.088303 8.83 %
4 IV, major (4, 6, 1) 17.00 0.077982 7.8 %
5 V6, major (7, 2, 5) 9.00 0.041284 4.13 %
6 I/V, major (5, 7, 2) 8.00 0.036697 3.67 %
7 ii6/V, major (1, 3, 6) 6.00 0.027523 2.75 %
8 V7, major (5, 7, 2, 4) 5.50 0.025229 2.52 %
9 ii6, major (4, 6, 2) 4.50 0.020642 2.06 %
10 V/V, major (2, #4, 6) 4.50 0.020642 2.06 %
11 viio6, major (2, 4, 7) 3.50 0.016055 1.61 %
12 V2, major (4, 5, 7, 2) 3.00 0.013761 1.38 %
13 V65, major (7, 2, 4, 5) 3.00 0.013761 1.38 %
14 ii, major (2, 4, 6) 3.00 0.013761 1.38 %
15 V(64), major (5, 1, 3) 3.00 0.013761 1.38 %
16 I6/V, major (7, 2, 5) 3.00 0.013761 1.38 %
17 vi, major (6, 1, 3) 2.75 0.012615 1.26 %
18 V(4), major (5, 1, 2) 2.50 0.011468 1.15 %
19 ii65/V, major (1, 3, 5, 6) 2.50 0.011468 1.15 %
20 iii6, major (5, 7, 3) 2.50 0.011468 1.15 %
21 vi/V, major (3, 5, 7) 2.00 0.009174 0.92 %
22 IV6, major (6, 1, 4) 1.50 0.006881 0.69 %
23 ii/V, major (6, 1, 3) 1.00 0.004587 0.46 %
24 V(64)/V, major (2, 5, 7) 1.00 0.004587 0.46 %
25 V2/V, major (1, 2, #4, 6) 1.00 0.004587 0.46 %
26 V6(4), major (1, 2, 5) 1.00 0.004587 0.46 %
27 IV(9), major (4, 6, 1) 1.00 0.004587 0.46 %
28 V7/V, major (2, #4, 6, 1) 1.00 0.004587 0.46 %
29 viio, major (7, 2, 4) 1.00 0.004587 0.46 %
30 IV64, major (1, 4, 6) 1.00 0.004587 0.46 %
31 IV6/V, major (3, 5, 1) 1.00 0.004587 0.46 %
32 ii65, major (4, 6, 1, 2) 1.00 0.004587 0.46 %
33 viio6/V, major (6, 1, #4) 1.00 0.004587 0.46 %
34 I6(11), major (3, 5, 1) 1.00 0.004587 0.46 %
35 vii%43, major (4, 6, 7, 2) 1.00 0.004587 0.46 %
36 ii7, major (2, 4, 6, 1) 0.50 0.002294 0.23 %
37 iii%65/V, major (2, 4, 6, 7) 0.50 0.002294 0.23 %
38 iii, major (3, 5, 7) 0.50 0.002294 0.23 %
39 V64/V, major (6, 2, #4) 0.50 0.002294 0.23 %
40 vi6, major (1, 3, 6) 0.50 0.002294 0.23 %
41 vi7, major (6, 1, 3, 5) 0.50 0.002294 0.23 %
42 vii%65, major (2, 4, 6, 7) 0.50 0.002294 0.23 %
43 V43, major (2, 4, 5, 7) 0.50 0.002294 0.23 %
44 viio64, major (4, 7, 2) 0.50 0.002294 0.23 %
chords_by_mode.apply_step("Counter")
count
mode corpus piece chord_and_mode scale_degrees
major handel_keyboard hwv430d_Grobschmied_Aria I, major (1, 3, 5) 12
I6, major (3, 5, 1) 8
V6, major (7, 2, 5) 4
ii6/V, major (1, 3, 6) 4
V, major (5, 7, 2) 3
... ... ... ...
hwv430d_Grobschmied_Var5 ii6/V, major (1, 3, 6) 2
I/V, major (5, 7, 2) 2
ii, major (2, 4, 6) 1
V65, major (7, 2, 4, 5) 1
viio6, major (2, 4, 7) 1

111 rows × 1 columns

chords_by_mode.format = "scale_degree"
chords_by_mode.get_default_analysis().make_ranking_table()
mode major
chord_and_mode scale_degrees duration_qb proportion proportion_%
rank
1 I, major (1, 3, 5) 59.50 0.272936 27.29 %
2 I6, major (3, 5, 1) 35.00 0.160550 16.06 %
3 V, major (5, 7, 2) 19.25 0.088303 8.83 %
4 IV, major (4, 6, 1) 17.00 0.077982 7.8 %
5 V6, major (7, 2, 5) 9.00 0.041284 4.13 %
6 I/V, major (5, 7, 2) 8.00 0.036697 3.67 %
7 ii6/V, major (1, 3, 6) 6.00 0.027523 2.75 %
8 V7, major (5, 7, 2, 4) 5.50 0.025229 2.52 %
9 ii6, major (4, 6, 2) 4.50 0.020642 2.06 %
10 V/V, major (2, #4, 6) 4.50 0.020642 2.06 %
11 viio6, major (2, 4, 7) 3.50 0.016055 1.61 %
12 V2, major (4, 5, 7, 2) 3.00 0.013761 1.38 %
13 V65, major (7, 2, 4, 5) 3.00 0.013761 1.38 %
14 ii, major (2, 4, 6) 3.00 0.013761 1.38 %
15 V(64), major (5, 1, 3) 3.00 0.013761 1.38 %
16 I6/V, major (7, 2, 5) 3.00 0.013761 1.38 %
17 vi, major (6, 1, 3) 2.75 0.012615 1.26 %
18 V(4), major (5, 1, 2) 2.50 0.011468 1.15 %
19 ii65/V, major (1, 3, 5, 6) 2.50 0.011468 1.15 %
20 iii6, major (5, 7, 3) 2.50 0.011468 1.15 %
21 vi/V, major (3, 5, 7) 2.00 0.009174 0.92 %
22 IV6, major (6, 1, 4) 1.50 0.006881 0.69 %
23 ii/V, major (6, 1, 3) 1.00 0.004587 0.46 %
24 V(64)/V, major (2, 5, 7) 1.00 0.004587 0.46 %
25 V2/V, major (1, 2, #4, 6) 1.00 0.004587 0.46 %
26 V6(4), major (1, 2, 5) 1.00 0.004587 0.46 %
27 IV(9), major (4, 6, 1) 1.00 0.004587 0.46 %
28 V7/V, major (2, #4, 6, 1) 1.00 0.004587 0.46 %
29 viio, major (7, 2, 4) 1.00 0.004587 0.46 %
30 IV64, major (1, 4, 6) 1.00 0.004587 0.46 %
31 IV6/V, major (3, 5, 1) 1.00 0.004587 0.46 %
32 ii65, major (4, 6, 1, 2) 1.00 0.004587 0.46 %
33 viio6/V, major (6, 1, #4) 1.00 0.004587 0.46 %
34 I6(11), major (3, 5, 1) 1.00 0.004587 0.46 %
35 vii%43, major (4, 6, 7, 2) 1.00 0.004587 0.46 %
36 ii7, major (2, 4, 6, 1) 0.50 0.002294 0.23 %
37 iii%65/V, major (2, 4, 6, 7) 0.50 0.002294 0.23 %
38 iii, major (3, 5, 7) 0.50 0.002294 0.23 %
39 V64/V, major (6, 2, #4) 0.50 0.002294 0.23 %
40 vi6, major (1, 3, 6) 0.50 0.002294 0.23 %
41 vi7, major (6, 1, 3, 5) 0.50 0.002294 0.23 %
42 vii%65, major (2, 4, 6, 7) 0.50 0.002294 0.23 %
43 V43, major (2, 4, 5, 7) 0.50 0.002294 0.23 %
44 viio64, major (4, 7, 2) 0.50 0.002294 0.23 %
unigram_proportions.plot_grouped()