Skip to content

Commit

Permalink
[MRG] Add MultipitchData (#182)
Browse files Browse the repository at this point in the history
* fixes #101

* run black
  • Loading branch information
Vincent Lostanlen authored Mar 6, 2020
1 parent ab629f3 commit 1baecfb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
19 changes: 8 additions & 11 deletions mirdata/medleydb_melody.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __repr__(self):
+ "is_instrumental={}, n_sources={}, "
+ "melody1=F0Data('times', 'frequencies', confidence'), "
+ "melody2=F0Data('times', 'frequencies', confidence'), "
+ "melody3=F0Data('times', 'frequencies', confidence'))"
+ "melody3=MultipitchData('times', 'frequencies', confidence'))"
)
return repr_string.format(
self.track_id,
Expand Down Expand Up @@ -156,12 +156,9 @@ def audio(self):
return librosa.load(self.audio_path, sr=None, mono=True)

def to_jams(self):
# jams does not support multipitch, so we skip melody3
return jams_utils.jams_converter(
f0_data=[
(self.melody1, 'melody1'),
(self.melody2, 'melody2'),
(self.melody3, 'melody3'),
],
f0_data=[(self.melody1, 'melody1'), (self.melody2, 'melody2')],
metadata=self._track_metadata,
)

Expand Down Expand Up @@ -268,17 +265,17 @@ def _load_melody3(melody_path):
if not os.path.exists(melody_path):
return None
times = []
freqs = []
freqs_list = []
conf_list = []
with open(melody_path, 'r') as fhandle:
reader = csv.reader(fhandle, delimiter=',')
for line in reader:
times.append(float(line[0]))
freqs.append([float(v) for v in line[1:]])
freqs_list.append([float(v) for v in line[1:]])
conf_list.append([float(float(v) > 0) for v in line[1:]])

times = np.array(times)
freqs = np.array(freqs)
confidence = (freqs > 0).astype(float)
melody_data = utils.F0Data(times, freqs, confidence)
melody_data = utils.MultipitchData(times, freqs_list, conf_list)
return melody_data


Expand Down
4 changes: 4 additions & 0 deletions mirdata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def validator(dataset_index, data_home, silence=False):

F0Data = namedtuple('F0Data', ['times', 'frequencies', 'confidence'])

MultipitchData = namedtuple(
'MultipitchData', ['times', 'frequency_list', 'confidence_list']
)

LyricData = namedtuple(
'LyricData', ['start_times', 'end_times', 'lyrics', 'pronunciations']
)
Expand Down
53 changes: 22 additions & 31 deletions tests/test_medleydb_melody.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_track():

assert type(track.melody1) is utils.F0Data
assert type(track.melody2) is utils.F0Data
assert type(track.melody3) is utils.F0Data
assert type(track.melody3) is utils.MultipitchData

y, sr = track.audio
assert sr == 44100
Expand All @@ -74,7 +74,7 @@ def test_track():
+ " genre=Classical, is_excerpt=True, is_instrumental=True, "
+ "n_sources=18, melody1=F0Data('times', 'frequencies', confidence'),"
+ " melody2=F0Data('times', 'frequencies', confidence'), "
+ "melody3=F0Data('times', 'frequencies', confidence'))"
+ "melody3=MultipitchData('times', 'frequencies', confidence'))"
)
assert track.__repr__() == repr_string

Expand Down Expand Up @@ -147,42 +147,33 @@ def test_load_melody3():
melody_data = medleydb_melody._load_melody3(melody_path)

# check types
assert type(melody_data) == utils.F0Data
assert type(melody_data) == utils.MultipitchData
assert type(melody_data.times) is np.ndarray
assert type(melody_data.frequencies) is np.ndarray
assert type(melody_data.confidence) is np.ndarray
assert type(melody_data.frequency_list) is list
assert type(melody_data.confidence_list) is list

# check values
assert np.array_equal(
melody_data.times,
np.array([0.046439909297052155, 0.052244897959183675, 0.1219047619047619]),
)
assert np.array_equal(
melody_data.frequencies,
np.array(
[
[0.0, 0.0, 497.01600000000002, 0.0, 0.0],
[965.99199999999996, 996.46799999999996, 497.10599999999999, 0.0, 0.0],
[
987.32000000000005,
987.93200000000002,
495.46800000000002,
495.29899999999998,
242.98699999999999,
],
]
),
)
assert np.array_equal(
melody_data.confidence,
np.array(
[
[0.0, 0.0, 1.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0],
]
),
)
assert melody_data.frequency_list == [
[0.0, 0.0, 497.01600000000002, 0.0, 0.0],
[965.99199999999996, 996.46799999999996, 497.10599999999999, 0.0, 0.0],
[
987.32000000000005,
987.93200000000002,
495.46800000000002,
495.29899999999998,
242.98699999999999,
],
]

assert melody_data.confidence_list == [
[0.0, 0.0, 1.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0],
]

# load a file which doesn't exist
melody_data_none = medleydb_melody._load_melody3('fake/file/path')
Expand Down

0 comments on commit 1baecfb

Please sign in to comment.