<commit_before>#!/usr/bin/env python
'''
CREATED:2014-03-18 by <NAME> <<EMAIL>>

Compute melody extraction evaluation measures

Usage:

./melody_eval.py TRUTH.TXT PREDICTION.TXT
(CSV files also accepted)

For a detailed explanation of the measures please refer to:

<NAME>, <NAME>, <NAME> and <NAME>, "Melody Extraction
from Polyphonic Music Signals: Approaches, Applications and Challenges",
IEEE Signal Processing Magazine, 31(2):118-134, Mar. 2014.
'''

import numpy as np
import argparse
import sys
import os
from collections import OrderedDict
import mir_eval

def evaluate(reference_file, estimated_file, hop=None):
    '''
    Evaluate two melody (predominant f0) transcriptions, where the first is
    treated as the reference (ground truth) and the second as the estimate to
    be evaluated (prediction).

    Input:
        truth_file - path to reference file. File should contain 2 columns:
              column 1 with timestamps and column 2 the corresponding reference
              frequency values in Hz (see *).
        prediction_file - path to estimate file. File should contain with 2 columns:
              column 1 with timestamps and column 2 the corresponding estimate
              frequency values in Hz (see **).
        hop - the desired hop size (in seconds) to compare the reference and
              estimate sequences (see ***)

    Output:
        M - ordered dictionary containing 5 evaluation measures:
        voicing recall rate -       Fraction of voiced frames in ref estimated as voiced in est
        voicing false alarm rate -  Fraction of unvoiced frames in ref estimated as voiced in est
        raw pitch -                 Fraction of voiced frames in ref for which
                                    est gives a correct pitch estimate (within 50 cents)
        raw chroma -                Same as raw pitch, but ignores octave errors
        overall accuracy -          Overall performance measure combining pitch and voicing



    *    Unvoiced frames should be indicated by 0 Hz.
    **   Unvoiced frames can be indicated either by 0 Hz or by a negative Hz
         value - negative values represent the algorithm's pitch estimate for
         frames it has determined as unvoiced, in case they are in fact voiced.
    ***  For a frame-by-frame comparison, both sequences are resampled using
         the provided hop size (in seconds), the default being 10 ms. The
         frequency values of the resampled sequences are obtained via linear
         interpolation of the original frequency values converted to a cent
         scale.
    '''
    # check if a hop size was specified, if not set to default
    if hop is None:
        hop = 0.01
    # load the data
    ref_time, ref_freq = mir_eval.io.load_time_series(reference_file)
    est_time, est_freq = mir_eval.io.load_time_series(estimated_file)
    # Convert to reference/estimated voicing/frequency (cent) arrays
    ref_voicing, est_voicing, ref_cent, est_cent = mir_eval.melody.to_cent_voicing(ref_time,
                                                                                ref_freq,
                                                                                est_time,
                                                                                est_freq,
                                                                                hop=hop)

    # Compute metrics
    M = OrderedDict()

    for name, metric in mir_eval.melody.METRICS.items():
        if metric == mir_eval.melody.voicing_measures:
            M[name] = metric(ref_voicing, est_voicing)
        else:
            M[name] = metric(ref_voicing, est_voicing, ref_cent, est_cent)

    return M

def print_evaluation(prediction_file, M):<commit_msg>dont set hop size to a default value<commit_after>#!/usr/bin/env python
'''
CREATED:2014-03-18 by <NAME> <<EMAIL>>

Compute melody extraction evaluation measures

Usage:

./melody_eval.py TRUTH.TXT PREDICTION.TXT
(CSV files also accepted)

For a detailed explanation of the measures please refer to:

<NAME>, <NAME>, <NAME> and <NAME>, "Melody Extraction
from Polyphonic Music Signals: Approaches, Applications and Challenges",
IEEE Signal Processing Magazine, 31(2):118-134, Mar. 2014.
'''

import numpy as np
import argparse
import sys
import os
from collections import OrderedDict
import mir_eval

def evaluate(reference_file, estimated_file, hop=None):
    '''
    Evaluate two melody (predominant f0) transcriptions, where the first is
    treated as the reference (ground truth) and the second as the estimate to
    be evaluated (prediction).

    Input:
        truth_file - path to reference file. File should contain 2 columns:
              column 1 with timestamps and column 2 the corresponding reference
              frequency values in Hz (see *).
        prediction_file - path to estimate file. File should contain with 2 columns:
              column 1 with timestamps and column 2 the corresponding estimate
              frequency values in Hz (see **).
        hop - the desired hop size (in seconds) to compare the reference and
              estimate sequences (see ***)

    Output:
        M - ordered dictionary containing 5 evaluation measures:
        voicing recall rate -       Fraction of voiced frames in ref estimated as voiced in est
        voicing false alarm rate -  Fraction of unvoiced frames in ref estimated as voiced in est
        raw pitch -                 Fraction of voiced frames in ref for which
                                    est gives a correct pitch estimate (within 50 cents)
        raw chroma -                Same as raw pitch, but ignores octave errors
        overall accuracy -          Overall performance measure combining pitch and voicing



    *    Unvoiced frames should be indicated by 0 Hz.
    **   Unvoiced frames can be indicated either by 0 Hz or by a negative Hz
         value - negative values represent the algorithm's pitch estimate for
         frames it has determined as unvoiced, in case they are in fact voiced.
    ***  For a frame-by-frame comparison, both sequences are resampled using
         the provided hop size (in seconds), the default being 10 ms. The
         frequency values of the resampled sequences are obtained via linear
         interpolation of the original frequency values converted to a cent
         scale.
    '''
    # load the data
    ref_time, ref_freq = mir_eval.io.load_time_series(reference_file)
    est_time, est_freq = mir_eval.io.load_time_series(estimated_file)
    # Convert to reference/estimated voicing/frequency (cent) arrays
    ref_voicing, est_voicing, ref_cent, est_cent = mir_eval.melody.to_cent_voicing(ref_time,
                                                                                ref_freq,
                                                                                est_time,
                                                                                est_freq,
                                                                                hop=hop)

    # Compute metrics
    M = OrderedDict()

    for name, metric in mir_eval.melody.METRICS.items():
        if metric == mir_eval.melody.voicing_measures:
            M[name] = metric(ref_voicing, est_voicing)
        else:
            M[name] = metric(ref_voicing, est_voicing, ref_cent, est_cent)

    return M

def print_evaluation(prediction_file, M):
{'fraction_non_alphanumeric': 0.05355456630244166, 'fraction_numerical': 0.00984756508835829, 'mean_word_length': 2.0623709211069805, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7428368', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1766, 'n_words': 774}