craffel / pretty-midi

Utility functions for handling MIDI data in a nice/intuitive way.
MIT License
856 stars 151 forks source link

one-line data preparation for mir_eval #222

Closed almostimplemented closed 1 year ago

almostimplemented commented 1 year ago

Hi @craffel , I use pretty_midi, mir_eval, and mido almost every day in my research. Thank you so much!

This feature feels like it should exist in the field of MIR:

At first I thought, "I should open a PR in mir_eval for this", but I realized it would introduce a dependency or a custom rolled MIDI parser :-1: ... but it would be slick if pretty_midi could prepare the inputs for mir_eval.transcription (especially considering the projects have the same author... 😇 )

With this change you can do:

import pretty_midi as pm
import mir_eval

ref_intervals, ref_pitches = pm.PrettyMidi("ground_truth.mid").get_intervals_and_pitches()
est_intervals, est_pitches = pm.PrettyMidi("inference.mid").get_intervals_and_pitches()

(p, r, f, _) = mir_eval.transcription.precision_recall_f1_overlap(
         ref_intervals, ref_pitches,
         est_intervals, est_pitches, 
         offset_ratio=None)

and I think that's pretty cool.

craffel commented 1 year ago

Hey, thanks a lot for this! I have to say I'm on the fence a bit as to whether this makes sense as a function in the core PrettyMIDI class. However, there are lots of things that should probably be external functions (basically anything that computes some features/alternative representation), so this is certainly within scope in that respect and I'll merge it. Thanks again.

almostimplemented commented 1 year ago

Ah that is a fair point. When I looked at utilities.py, I didn't see any existing methods that operate on PrettyMidi objects directly. Plus I thought this is a bit similar to get_piano_roll.

But say the word if you would like me to move it to utilities.py! More than happy to. Thanks!