#!/usr/bin/env perl
use strict;
use warnings;

# Plot the Shannon diversity index for each song.

#use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(Music-BachChoralHarmony Statistics-Diversity-Shannon); # local author libs
use Music::BachChoralHarmony;
use Statistics::Diversity::Shannon;

#my $data_file = 'share/jsbach_chorals_harmony.data'; # local author files
#my $key_title = 'share/jsbach_BWV_keys_titles.txt';  # "
my $bach = Music::BachChoralHarmony->new(
#    data_file => $data_file,
#    key_title => $key_title,
);
my $songs = $bach->parse();

# Get the usage number of each songs note sets
my %score;
for my $id ( keys %$songs ) {
    for my $event ( @{ $songs->{$id}{events} } ) {
        $score{$id}->{ $event->{notes} }++;
    }
}

my $i = 0;

# Find the Shannon diversity of each song note set
my %diversity;
for my $id ( sort keys %score ) {
    $i++;
    my $d = Statistics::Diversity::Shannon->new( data => [ values %{ $score{$id} } ] );
    warn sprintf "%02d. %s: %.4f, %.4f\n", $i, $id, $d->index, $d->evenness;
    $diversity{$id} = [ $d->index, $d->evenness ];
}

# Build chart
use Chart::Points;
my $chart = Chart::Points->new( 1000, 400 );

$chart->set(
    title         => 'Shannon Diversity',
    pt_size       => 10,
    legend_labels => ['Index'],
    x_label       => 'Chorale BWV',
    y_label       => 'Index',
    x_ticks       => 'vertical',
    grid_lines    => 1,
    colors        => {
        grid_lines => 'gray',
    },
);

my @sorted = sort { $diversity{$a}->[0] <=> $diversity{$b}->[0] } keys %diversity;
$chart->add_dataset( map { $songs->{$_}{bwv} } @sorted );
$chart->add_dataset( map { $diversity{$_}->[0] } @sorted );

$chart->png("$0.png");
