Package jazzparser :: Package misc :: Package chordlabel :: Module hmm :: Class HPChordLabeler
[hide private]
[frames] | no frames]

Class HPChordLabeler

source code

                                object --+        
                                         |        
         utils.nltk.ngram.model.NgramModel --+    
                                             |    
utils.nltk.ngram.dictionary.DictionaryHmmModel --+
                                                 |
                                                HPChordLabeler

HMM based on that of Harmony Progression Analyzer for MIREX 2011, by Ni, Mcvicar, Santos-Rodriguez, De Bie. Their audio labeling model is here adapted to MIDI labeling.

The state labels are triples: (key,root,label). key is the ET key (C=0, C#=1, etc). root is the chord root: e.g. root=7 denotes chord root G. The chord root is not relative to the key, though the probabilities stored in the distributions are.

Instance Methods [hide private]
 
__init__(self, initial_key_dist, initial_chord_dist, key_transition_dist, chord_transition_dist, emission_dist, note_number_dist, chord_vocab, max_notes, chord_corpus_mapping, history='', description='', name='default')
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
copy(self, mutable=False)
Creates a complete copy of the model, optionally making the distributions mutable.
source code
 
clear_cache(self)
Initializes or empties probability distribution caches.
source code
 
add_history(self, string)
Adds a line to the end of this model's history string.
source code
 
get_mapping_to_corpus(self)
Returns a dict mapping the labels of this model's vocabulary to the chord types of the chord corpus.
source code
 
get_mapping_from_corpus(self)
Returns a dict mapping the chord types of the chord corpus to this model's chord labels.
source code
 
map_to_corpus(self, sequence)
Postprocesses the sequence, which should be a sequence in the form it comes out from the labeler, to apply the corpus mapping associated with this labeler.
source code
 
train_transition_distribution(self, inputs, input_keys, chord_mapping=None)
Train the transition distribution parameters in a supervised manner, using chord corpus input.
source code
 
train_emission_number_distribution(self, inputs)
Trains the distribution over the number of notes emitted from a chord class.
source code
 
transition_log_probability(self, state, previous_state)
Gives the probability P(label_i | label_(i-1), ..., label_(i-n)), where the previous labels are given in the sequence label_context.
source code
 
emission_log_probability(self, emission, state)
Gives the probability P(emission | label).
source code
 
get_emission_matrix(self, sequence)
We override this to make it faster by taking advantage of where we know states share probabilities
source code
 
get_small_emission_matrix(self, sequence)
Instead of building the emission matrix for every state, just includes probabilities for (root,label) pairs, decomposed into 2 dimensions.
source code
 
get_small_transition_matrix(self, transpose=False)
Decomposed version of just the chord part of the transition probabilities, for forward-backward calculations.
source code
 
normal_forward_probabilities(self, sequence, seq_prob=False, decomposed=False)
Specialized version of this to make it faster.
source code
 
normal_backward_probabilities(self, sequence, decomposed=False)
Specialized version of this to make it faster.
source code
 
compute_decomposed_xi(self, sequence, forward=None, backward=None, emission_matrix=None, transition_matrix=None) source code
 
label(self, midi, options={}, corpus=False)
Decodes the model with the given midi data to produce a chord labeling.
source code
 
label_lattice(self, *args, **kwargs)
Decodes the model and produces a lattice of the top probability chord labels.
source code
 
to_picklable_dict(self)
Produces a picklable representation of model as a dict.
source code
 
__get_my_filename(self) source code
 
save(self)
Saves the model data to a file.
source code
 
delete(self)
Removes all the model's data.
source code
 
_get_readable_params(self) source code

Inherited from utils.nltk.ngram.dictionary.DictionaryHmmModel: compute_gamma, compute_xi, viterbi_decode

Inherited from utils.nltk.ngram.model.NgramModel: __repr__, backward_log_probabilities, backward_probabilities, decode_forward, decode_gamma, emission_probability, forward_backward_log_probabilities, forward_backward_probabilities, forward_log_probabilities, forward_probabilities, gamma_probabilities, generalized_viterbi, generate, get_all_ngrams, get_backoff_models, get_transition_matrix, labeled_sequence_log_probability, normal_forward_backward_probabilities, precompute, transition_log_probability_debug, transition_probability, transition_probability_debug, viterbi_selector_probabilities

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
 
initialize_chords(cls, chord_prob, max_notes, chord_vocab, chord_mapping, name='default')
Creates a new model with the distributions initialized naively to favour simple chord-types, in a similar way to what R&S do in their paper.
source code
 
from_picklable_dict(cls, data, name)
Reproduces an model that was converted to a picklable form using to_picklable_dict.
source code
 
_get_model_dir(cls) source code
 
__get_filename(cls, model_name) source code
 
list_models(cls)
Returns a list of the names of available models.
source code
 
load_model(cls, model_name) source code
Static Methods [hide private]
 
train(data, name, logger=None, options={}, chord_data=None)
Initializes and trains an HMM in a supervised fashion using the given training data.
source code
 
process_training_options(opts)
Verifies and processes the training option values.
source code
 
process_labeling_options(opts)
Verifies and processes the labeling option values (dict).
source code

Inherited from utils.nltk.ngram.dictionary.DictionaryHmmModel: from_ngram_model

Class Variables [hide private]
  TRAINING_OPTIONS = [ModuleOption('chordprob', filter= float, h...
  LABELING_OPTIONS = [ModuleOption('n', filter= int, help_text= ...
Properties [hide private]
  _filename
  readable_parameters

Inherited from utils.nltk.ngram.model.NgramModel: model_type

Inherited from object: __class__

Method Details [hide private]

__init__(self, initial_key_dist, initial_chord_dist, key_transition_dist, chord_transition_dist, emission_dist, note_number_dist, chord_vocab, max_notes, chord_corpus_mapping, history='', description='', name='default')
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Parameters:
  • label_dist - transition distribution
  • emission_dist - emission distribution
  • label_dom - state domain
  • emission_dom - emission domain
  • mutable - if true, the distributions stored will be mutable dictionary distributions, so the model can be updated
Overrides: object.__init__
(inherited documentation)

clear_cache(self)

source code 

Initializes or empties probability distribution caches.

Make sure to call this if you change or update the distributions.

No caches used thus far, so this does nothing for now, except call the super method.

Overrides: utils.nltk.ngram.model.NgramModel.clear_cache

map_to_corpus(self, sequence)

source code 

Postprocesses the sequence, which should be a sequence in the form it comes out from the labeler, to apply the corpus mapping associated with this labeler. The result should be a chord sequence that uses the chord corpus chord types.

initialize_chords(cls, chord_prob, max_notes, chord_vocab, chord_mapping, name='default')
Class Method

source code 

Creates a new model with the distributions initialized naively to favour simple chord-types, in a similar way to what R&S do in their paper.

The transition distributions are initialized so that everything is equiprobable.

Parameters:
  • chord_prob (float) - prob of a note in the chord. This prob is distributed over the notes of the chord. The remaining prob mass is distributed over the remaining notes. You'll want this to be big enough that chord notes are more probable than others.
  • max_notes (int) - maximum number of notes that can be generated in each emission. Usually best to set to something high, like 100 - it's just to make the distribution finite.
  • chord_vocab (dict) - a vocabularly of chord types. Mapping from string chord labels to a list of ints representing the notes, relative to the root that are contained in the chord.

train_transition_distribution(self, inputs, input_keys, chord_mapping=None)

source code 

Train the transition distribution parameters in a supervised manner, using chord corpus input.

This is used as an initialization step to set transition parameters before running EM on unannotated data.

Parameters:
  • inputs (jazzparser.data.input.AnnotatedDbBulkInput) - annotated chord training data
  • input_keys (list of lists of ints) - the key associated with each chord. Should contain a key list for each input sequence and each should be the length of the chord sequence
  • chord_mapping (dict) - a mapping from the chord labels of the corpus to those we will use for this model, so that we can use the training data. See jazzparser.misc.chordlabel.chord_vocabs for mappings and use get_mapping to prepare a dict from them. This doesn't have to be the same as the mapping stored in the model (model.chord_corpus_mapping) and won't overwrite it. If not given, the model's corpus mapping will be used

train_emission_number_distribution(self, inputs)

source code 

Trains the distribution over the number of notes emitted from a chord class. It's not conditioned on the chord class, so the only training data needed is a segmented MIDI corpus.

Parameters:

train(data, name, logger=None, options={}, chord_data=None)
Static Method

source code 

Initializes and trains an HMM in a supervised fashion using the given training data.

Overrides: utils.nltk.ngram.model.NgramModel.train

transition_log_probability(self, state, previous_state)

source code 

Gives the probability P(label_i | label_(i-1), ..., label_(i-n)), where the previous labels are given in the sequence label_context. The context should be in reverse order, i.e. with the most recent label at the start.

Note that this is the probability of a label given the previous n-1 labels, which is the same as the probability of the n-gram [label_i, ..., label_(i-n+1)] given the ngram [label_(i-1), ..., label_(i-n)], since all but the last element of the ngram overlaps with the condition, so has probability 1.

Caches all computed transition probabilities. This is particularly important for backoff models. Many n-grams will back off to the same (n-1)-gram and we don't want to recompute the transition probability for that each time.

Overrides: utils.nltk.ngram.model.NgramModel.transition_log_probability
(inherited documentation)

emission_log_probability(self, emission, state)

source code 

Gives the probability P(emission | label). Returned as a base 2 log.

The emission should be a list of emitted notes as integer pitch classes.

Overrides: utils.nltk.ngram.model.NgramModel.emission_log_probability

get_emission_matrix(self, sequence)

source code 

We override this to make it faster by taking advantage of where we know states share probabilities

Overrides: utils.nltk.ngram.model.NgramModel.get_emission_matrix

get_small_emission_matrix(self, sequence)

source code 

Instead of building the emission matrix for every state, just includes probabilities for (root,label) pairs, decomposed into 2 dimensions. This is useful for the forward-backward calculations.

normal_forward_probabilities(self, sequence, seq_prob=False, decomposed=False)

source code 

Specialized version of this to make it faster.

Parameters:
  • seq_prob - return the log probability of the whole sequence as well as the array (tuple of (array,logprob)).
Returns:
2D Numpy array. The first dimension represents timesteps, the second the states.
Overrides: utils.nltk.ngram.model.NgramModel.normal_forward_probabilities

Note: verified that this gets identical results to the superclass

normal_backward_probabilities(self, sequence, decomposed=False)

source code 

Specialized version of this to make it faster.

Parameters:
  • seq_prob - return the log probability of the whole sequence as well as the array (tuple of (array,logprob)).
Returns:
2D Numpy array. The first dimension represents timesteps, the second the states.
Overrides: utils.nltk.ngram.model.NgramModel.normal_backward_probabilities

Note: verified that this gets identical results to the superclass

label(self, midi, options={}, corpus=False)

source code 

Decodes the model with the given midi data to produce a chord labeling. This is in the form of a set of possible chord labels for each midi segment, each with a probability.

Parameters:

label_lattice(self, *args, **kwargs)

source code 

Decodes the model and produces a lattice of the top probability chord labels.

This is just label with some extra wrapping to produce a jazzparser.data.input.WeightedChordLabelInput for the lattice.

to_picklable_dict(self)

source code 

Produces a picklable representation of model as a dict.

Overrides: utils.nltk.ngram.model.NgramModel.to_picklable_dict

from_picklable_dict(cls, data, name)
Class Method

source code 

Reproduces an model that was converted to a picklable form using to_picklable_dict.

Overrides: utils.nltk.ngram.model.NgramModel.from_picklable_dict

delete(self)

source code 

Removes all the model's data. It is assumed that the model will not be used at all after this has been called.


Class Variable Details [hide private]

TRAINING_OPTIONS

Value:
[ModuleOption('chordprob', filter= float, help_text= "Initialization o\
f the emission distribution.", usage= "ccprob=P, P is a probability. P\
rob P is distributed " "over the pitch classes that are in the chord."\
, required= True), ModuleOption('maxnotes', filter= int, help_text= "M\
aximum number of notes that can be generated from a " "a state. Limit \
is required to make the distribution finite.", usage= "maxnotes=N, N i\
s an integer", default= 100), ModuleOption('vocab', filter= choose_fro\
m_list(CHORD_VOCABS.keys()), help_text= "Chord vocabulary for the mode\
...

LABELING_OPTIONS

Value:
[ModuleOption('n', filter= int, help_text= "Number of best labels to c\
onsider for each timestep", usage= "decoden=N, where N is an integer",\
 default= 10), ModuleOption('nokey', filter= str_to_bool, help_text= "\
Don't distinguish between keys: removed labels that  " "are duplicate \
if you ignore their key and return all chords " "with a key of 0. Prob\
abilities are not summed: only the " "highest is returned", usage= "no\
key=B, where B is 'true' or 'false'", default= False), ModuleOption('v\
iterbi', filter= str_to_bool, help_text= "Use Viterbi decoding, instea\
...

Property Details [hide private]

_filename

Get Method:
__get_my_filename(self)

readable_parameters

Get Method:
_get_readable_params(self)