def posterior_stash_to_jams(stash, penalty_values, output_directory, label_map,
                            model_params):
    """Decode a stash of posteriors to JAMS and write to disk.

    Parameters
    ----------
    stash : biggie.Stash
        Posteriors to decode.
    penalty_values : array_like
        Collection of penalty values with which to run Viterbi.
    output_directory : str
        Base path to write out JAMS files; each collection will be written as
        {output_directory}/{penalty_values[i]}.jamset
    label_map : callable object
        Map from frets to string labels.
    model_params : dict
        Metadata to associate with the annotation.
    """
    # Sweep over the default penalty values.
    for penalty in penalty_values:
        print "[{0}] \tStarting p = {1}".format(time.asctime(), penalty)
        results = decode_stash_parallel(stash, penalty, label_map, NUM_CPUS)

        output_file = os.path.join(output_directory,
                                   "{0}.jamset".format(penalty))

        jamset = dict()
        for key, annot in results.iteritems():
            annot.sandbox.update(timestamp=time.asctime(), **model_params)
            jam = pyjams.JAMS(chord=[annot])
            jam.sandbox.track_id = key
            jamset[key] = jam

        futils.create_directory(output_directory)
        util.save_jamset(jamset, output_file)
示例#2
0
文件: util.py 项目: saifrahmed/dl4mir
def multi_predict(entity, transform, p_vals, vocab, num_cpus=None):
    """Transform a CQT entity and apply Viterbi decoding.

    Parameters
    ----------
    entity : biggie.Entity
        Entity to estimate.
    transform : optimus.Graph
        Consumes 'cqt' fields, returns 'posterior' fields.
    p_vals : list
        Set of self-transition penalties to apply.

    Returns
    -------
    est_jams : pyjams.JAMS
        Populated JAMS object.
    """
    z = convolve(entity, transform, 'cqt')
    pool = Pool(processes=num_cpus)
    threads = [
        pool.apply_async(
            posterior_to_labeled_intervals,
            (biggie.Entity(posterior=z.posterior,
                           chord_labels=z.chord_labels), p, vocab),
        ) for p in p_vals
    ]
    pool.close()
    pool.join()

    jam = pyjams.JAMS()
    for penalty, thread in zip(p_vals, threads):
        annot = jam.chord.create_annotation()
        populate_annotation(*thread.get(), annot=annot)
        annot.sandbox.penalty = penalty
    return jam
def main(args):
    metadata = dict()
    if args.annotation_metadata:
        metadata.update(json.load(open(args.annotation_metadata)))

    labs_dt = glob.glob(os.path.join(args.lab_directory, "*_dt.lab"))
    labs_tdc = glob.glob(os.path.join(args.lab_directory, "*_tdc.lab"))
    jfmt = os.path.join(args.output_directory, "%s.jams")

    for dt, tdc in zip(labs_dt, labs_tdc):
        jam = pyjams.JAMS()
        intervals, labels = mir_eval.io.load_labeled_intervals(dt)
        annot_dt = jam.chord.create_annotation()
        pyjams.util.fill_range_annotation_data(intervals[:, 0], intervals[:,
                                                                          1],
                                               labels, annot_dt)
        annot_tdc = jam.chord.create_annotation()
        intervals, labels = mir_eval.io.load_labeled_intervals(tdc)
        pyjams.util.fill_range_annotation_data(intervals[:, 0], intervals[:,
                                                                          1],
                                               labels, annot_tdc)
        annot_tdc.sandbox.key = "human/RockCorpus/tdeclerqc"
        annot_dt.sandbox.key = "human/RockCorpus/dtemperley"
        jam.sandbox.local_key = futil.filebase(dt).replace("_dt", '')
        pyjams.save(jam, jfmt % jam.sandbox.local_key)
示例#4
0
def create_JAMS(dataset_dir, trackid, out_file):
    """Creates a JAMS file given the Isophonics lab file."""

    metadata_file = os.path.join(dataset_dir, 'Audio', trackid,
                                 '%s_METADATA.yaml' % trackid)

    with open(metadata_file, 'r') as f_in:
        metadata = yaml.load(f_in)

    # New JAMS and annotation
    jam = pyjams.JAMS()

    # Global file metadata
    fill_file_metadata(jam, metadata['artist'], metadata['title'])

    # Create Genre Annotation
    genre_annot = jam.genre.create_annotation()
    U.fill_observation_annotation_data([metadata['genre']], [1], [""],
                                       genre_annot)
    fill_genre_annotation_metadata(genre_annot)

    # Create Melody Annotations
    melody_path = os.path.join(dataset_dir, 'Annotations',
                               'Melody_Annotations')

    melody1_fpath = os.path.join(melody_path, 'MELODY1',
                                 "%s_MELODY1.csv" % trackid)
    if os.path.exists(melody1_fpath):
        melody1_annot = jam.melody.create_annotation()
        fill_melody_annotation(melody1_fpath, melody1_annot, 1)

    melody2_fpath = os.path.join(melody_path, 'MELODY2',
                                 "%s_MELODY2.csv" % trackid)
    if os.path.exists(melody2_fpath):
        melody2_annot = jam.melody.create_annotation()
        fill_melody_annotation(melody2_fpath, melody2_annot, 2)

    # Create SourceID Annotation
    instid_fpath = os.path.join(dataset_dir, 'Annotations',
                                'Instrument_Activations', 'SOURCEID',
                                "%s_SOURCEID.lab" % trackid)
    if os.path.exists(instid_fpath):
        instid_annot = jam.source.create_annotation()
        fill_instid_annotation(instid_fpath, instid_annot)

    # jam.file_metadata.duration = end_times[-1]

    # Save JAMS
    with open(out_file, "w") as fp:
        json.dump(jam, fp, indent=2)
def create_JAMS(lab_file, out_file, index_data=None):
    """Creates a JAMS file given the Isophonics lab file."""
    jam = pyjams.JAMS()

    # Global file metadata
    if index_data:
        fill_file_metadata(jam, index_data)

    # Create Chord annotation
    start_times, end_times, chord_labels = pyjams.util.read_lab(lab_file, 3)
    chord_annot = jam.chord.create_annotation()
    pyjams.util.fill_range_annotation_data(start_times, end_times,
                                           chord_labels, chord_annot)
    fill_annotation_metadata(chord_annot)
    jam.file_metadata.duration = end_times[-1]

    # Save JAMS
    with open(out_file, "w") as fp:
        json.dump(jam, fp, indent=2)
def main(args):
    metadata = dict()
    if args.annotation_metadata:
        metadata.update(json.load(open(args.annotation_metadata)))

    jamset = dict()
    for key, lab_files in json.load(open(args.annotation_set)).items():
        jamset[key] = pyjams.JAMS()
        for f in [lab_files]:
            intervals, labels = mir_eval.io.load_labeled_intervals(str(f))
            annot = jamset[key].chord.create_annotation()
            pyjams.util.fill_range_annotation_data(
                intervals[:, 0], intervals[:, 1], labels, annot)

            annot.annotation_metadata.update(**metadata.get(key, {}))
            annot.sandbox.source_file = f
            annot.sandbox.timestamp = time.asctime()

    futil.create_directory(os.path.split(args.output_file)[0])
    util.save_jamset(jamset, args.output_file)
def create_JAMS(lab_file, out_file):
    """
    Creates a JAMS file given the mirex05 annotation file (*.REF.txt).
    """

    # New JAMS and annotation
    jam = pyjams.JAMS()

    # Global file metadata
    fill_file_metadata(jam, lab_file)

    # Create Melody annotation
    times, values = pyjams.util.read_lab(lab_file, 2)
    melody_annot = jam.melody.create_annotation()
    pyjams.util.fill_timeseries_annotation_data(times, values, None,
                                                melody_annot)
    fill_annotation_metadata(melody_annot)

    # Save JAMS
    with open(out_file, "w") as fp:
        json.dump(jam, fp, indent=2)
示例#8
0
def create_JAMS(in_dir, out_dir, filebase, artist, album, timing_added=True):
    """Add all annotations for given song to a JAMS object and write it out."""

    jam = pyjams.JAMS()

    # add file level metadata
    jam.file_metadata.artist = artist
    jam.file_metadata.title = filebase.replace('_', ' ').title()
    jam.file_metadata.release = album.replace('\t', ' ')

    for a in ANNOTATORS.keys():
        # two harmony annotations
        harmony_file = os.path.join(in_dir, HARMONY_DIR,
                                    "%s_%s.clt" % (filebase, a))
        if not os.path.exists(harmony_file):
            logging.error('file: %s not found. Skipping!' % harmony_file)
            return
        else:
            parse_harmony_clt_file(harmony_file=harmony_file,
                                   jam=jam,
                                   annotator=ANNOTATORS[a],
                                   timing_added=timing_added)

        # one melody annotation (with annotator indicated in file name)
        melody_file = os.path.join(in_dir, MELODY_DIR,
                                   "%s_%s.nlt" % (filebase, a))
        if os.path.exists(melody_file):
            parse_melody_nlt_file(melody_file=melody_file,
                                  jam=jam,
                                  annotator=ANNOTATORS[a],
                                  timing_added=timing_added)

    # one timing file (no indication of annotator)
    timing_file = os.path.join(in_dir, TIMING_DATA_DIR, '%s.tim' % filebase)
    parse_timing_file(timing_file=timing_file, jam=jam)

    # Save JAMS
    out_file = os.path.join(out_dir, '%s.jams' % filebase)
    with open(out_file, "w") as fp:
        json.dump(jam, fp, indent=2)
def create_JAMS(lab_file, out_file):
    """
    Creates a JAMS file given the adc2004 annotation file (*.REF.txt).
    Note: the melody f0 annotations are provided in the *REF.txt files,
    not the .lab files (which contain quantized note annotations).
    """

    # New JAMS and annotation
    jam = pyjams.JAMS()

    # Global file metadata
    fill_file_metadata(jam, lab_file)

    # Create Melody annotation
    times, values = pyjams.util.read_lab(lab_file, 2)
    melody_annot = jam.melody.create_annotation()
    pyjams.util.fill_timeseries_annotation_data(times, values, None,
                                                melody_annot)
    fill_annotation_metadata(melody_annot)

    # Save JAMS
    with open(out_file, "w") as fp:
        json.dump(jam, fp, indent=2)