-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
0.2.3 -> 0.3.0 shims #153
0.2.3 -> 0.3.0 shims #153
Changes from 5 commits
1529c6f
a7303f4
44e768a
5b9fa66
3b315cd
3aba29a
a54a040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ | |
JamsFrame | ||
Sandbox | ||
JObject | ||
|
||
Observation | ||
""" | ||
|
||
import json | ||
|
@@ -45,6 +45,10 @@ | |
import gzip | ||
import copy | ||
import sys | ||
from collections import namedtuple | ||
|
||
from decorator import decorator | ||
|
||
|
||
from .version import version as __VERSION__ | ||
from . import schema | ||
|
@@ -54,7 +58,31 @@ | |
__all__ = ['load', | ||
'JObject', 'Sandbox', 'JamsFrame', | ||
'Annotation', 'Curator', 'AnnotationMetadata', | ||
'FileMetadata', 'AnnotationArray', 'JAMS'] | ||
'FileMetadata', 'AnnotationArray', 'JAMS', | ||
'Observation'] | ||
|
||
|
||
def deprecated(version, version_removed): | ||
'''This is a decorator which can be used to mark functions | ||
as deprecated. | ||
|
||
It will result in a warning being emitted when the function is used.''' | ||
|
||
def __wrapper(func, *args, **kwargs): | ||
'''Warn the user, and then proceed.''' | ||
code = six.get_function_code(func) | ||
warnings.warn_explicit( | ||
"{:s}.{:s}\n\tDeprecated as of JAMS version {:s}." | ||
"\n\tIt will be removed in JAMS version {:s}." | ||
.format(func.__module__, func.__name__, | ||
version, version_removed), | ||
category=DeprecationWarning, | ||
filename=code.co_filename, | ||
lineno=code.co_firstlineno + 1 | ||
) | ||
return func(*args, **kwargs) | ||
|
||
return decorator(__wrapper) | ||
|
||
|
||
@contextlib.contextmanager | ||
|
@@ -489,6 +517,11 @@ def validate(self, strict=True): | |
return valid | ||
|
||
|
||
Observation = namedtuple('Observation', | ||
['time', 'duration', 'value', 'confidence']) | ||
'''Core observation type: (time, duration, value, confidence).''' | ||
|
||
|
||
class Sandbox(JObject): | ||
"""Sandbox (unconstrained) | ||
|
||
|
@@ -711,6 +744,7 @@ def add_observation(self, time=None, duration=None, | |
self.drop(n, inplace=True, errors='ignore') | ||
six.reraise(SchemaError, SchemaError(str(exc)), sys.exc_info()[2]) | ||
|
||
@deprecated('0.2.3', '0.3.0') | ||
def to_interval_values(self): | ||
'''Extract observation data in a `mir_eval`-friendly format. | ||
|
||
|
@@ -1168,6 +1202,34 @@ def slice(self, start_time, end_time, strict=False): | |
|
||
return sliced_ann | ||
|
||
def to_interval_values(self): | ||
'''Extract observation data in a `mir_eval`-friendly format. | ||
|
||
Returns | ||
------- | ||
intervals : np.ndarray [shape=(n, 2), dtype=float] | ||
Start- and end-times of all valued intervals | ||
|
||
`intervals[i, :] = [time[i], time[i] + duration[i]]` | ||
|
||
labels : list | ||
List view of value field. | ||
''' | ||
|
||
times = timedelta_to_float(self.data.time.values) | ||
duration = timedelta_to_float(self.data.duration.values) | ||
|
||
return np.vstack([times, times + duration]).T, list(self.data.value) | ||
|
||
def __iter_obs__(self): | ||
for _, (t, d, v, c) in self.data.iterrows(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find it odd that iterating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... but that's neither here nor there. The point of this change is to add forward support for: for obs in annotation:
# process jams.Observation object the under-the-hood parts of how that iterator works won't matter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I meant that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes, i realize I forgot to contextualize my comment -- this change is fine; however, it occurs to me that the data structure is maybe deeper than necessary? not necessarily for iteration, but mutating it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right -- check the changes I made to #149 last night / this morning. That's all cleaned up now (and either way, irrelevant to this PR). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes, i see now that my understanding of things elsewhere is stale. |
||
yield Observation(time=t.total_seconds(), | ||
duration=d.total_seconds(), | ||
value=v, confidence=c) | ||
|
||
def __iter__(self): | ||
return self.__iter_obs__() | ||
|
||
|
||
class Curator(JObject): | ||
"""Curator | ||
|
@@ -1881,3 +1943,5 @@ def serialize_obj(obj): | |
return [serialize_obj(x) for x in obj] | ||
|
||
return obj | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,8 +104,8 @@ def beat(ref, est, **kwargs): | |
namespace = 'beat' | ||
ref = coerce_annotation(ref, namespace) | ||
est = coerce_annotation(est, namespace) | ||
ref_interval, _ = ref.data.to_interval_values() | ||
est_interval, _ = est.data.to_interval_values() | ||
ref_interval, _ = ref.to_interval_values() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohgeez this is so much cleaner. |
||
est_interval, _ = est.to_interval_values() | ||
|
||
return mir_eval.beat.evaluate(ref_interval[:, 0], est_interval[:, 0], **kwargs) | ||
|
||
|
@@ -145,8 +145,8 @@ def onset(ref, est, **kwargs): | |
namespace = 'onset' | ||
ref = coerce_annotation(ref, namespace) | ||
est = coerce_annotation(est, namespace) | ||
ref_interval, _ = ref.data.to_interval_values() | ||
est_interval, _ = est.data.to_interval_values() | ||
ref_interval, _ = ref.to_interval_values() | ||
est_interval, _ = est.to_interval_values() | ||
|
||
return mir_eval.onset.evaluate(ref_interval[:, 0], est_interval[:, 0], **kwargs) | ||
|
||
|
@@ -187,8 +187,8 @@ def chord(ref, est, **kwargs): | |
namespace = 'chord' | ||
ref = coerce_annotation(ref, namespace) | ||
est = coerce_annotation(est, namespace) | ||
ref_interval, ref_value = ref.data.to_interval_values() | ||
est_interval, est_value = est.data.to_interval_values() | ||
ref_interval, ref_value = ref.to_interval_values() | ||
est_interval, est_value = est.to_interval_values() | ||
|
||
return mir_eval.chord.evaluate(ref_interval, ref_value, | ||
est_interval, est_value, **kwargs) | ||
|
@@ -229,8 +229,8 @@ def segment(ref, est, **kwargs): | |
namespace = 'segment_open' | ||
ref = coerce_annotation(ref, namespace) | ||
est = coerce_annotation(est, namespace) | ||
ref_interval, ref_value = ref.data.to_interval_values() | ||
est_interval, est_value = est.data.to_interval_values() | ||
ref_interval, ref_value = ref.to_interval_values() | ||
est_interval, est_value = est.to_interval_values() | ||
|
||
return mir_eval.segment.evaluate(ref_interval, ref_value, | ||
est_interval, est_value, **kwargs) | ||
|
@@ -253,7 +253,7 @@ def hierarchy_flatten(annotation): | |
A list of lists of labels, ordered by increasing specificity. | ||
''' | ||
|
||
intervals, values = annotation.data.to_interval_values() | ||
intervals, values = annotation.to_interval_values() | ||
|
||
ordering = dict() | ||
|
||
|
@@ -394,8 +394,8 @@ def melody(ref, est, **kwargs): | |
namespace = 'pitch_contour' | ||
ref = coerce_annotation(ref, namespace) | ||
est = coerce_annotation(est, namespace) | ||
ref_interval, ref_p = ref.data.to_interval_values() | ||
est_interval, est_p = est.data.to_interval_values() | ||
ref_interval, ref_p = ref.to_interval_values() | ||
est_interval, est_p = est.to_interval_values() | ||
|
||
ref_freq = np.asarray([p['frequency'] * (-1)**(~p['voiced']) for p in ref_p]) | ||
est_freq = np.asarray([p['frequency'] * (-1)**(~p['voiced']) for p in est_p]) | ||
|
@@ -432,7 +432,7 @@ def pattern_to_mireval(ann): | |
patterns = defaultdict(lambda: defaultdict(list)) | ||
|
||
# Iterate over the data in interval-value format | ||
for interval, observation in zip(*ann.data.to_interval_values()): | ||
for interval, observation in zip(*ann.to_interval_values()): | ||
|
||
pattern_id = observation['pattern_id'] | ||
occurrence_id = observation['occurrence_id'] | ||
|
@@ -525,8 +525,8 @@ def transcription(ref, est, **kwargs): | |
namespace = 'pitch_contour' | ||
ref = coerce_annotation(ref, namespace) | ||
est = coerce_annotation(est, namespace) | ||
ref_intervals, ref_p = ref.data.to_interval_values() | ||
est_intervals, est_p = est.data.to_interval_values() | ||
ref_intervals, ref_p = ref.to_interval_values() | ||
est_intervals, est_p = est.to_interval_values() | ||
|
||
ref_pitches = np.asarray([p['frequency'] * (-1)**(~p['voiced']) for p in ref_p]) | ||
est_pitches = np.asarray([p['frequency'] * (-1)**(~p['voiced']) for p in est_p]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
smkdirs | ||
filebase | ||
find_with_extension | ||
_deprecated | ||
""" | ||
|
||
import os | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,10 +211,16 @@ def test_jamsframe_interval_values(): | |
|
||
jf = jams.JamsFrame.from_dataframe(df) | ||
|
||
intervals, values = jf.to_interval_values() | ||
warnings.resetwarnings() | ||
warnings.simplefilter('always') | ||
with warnings.catch_warnings(record=True) as out: | ||
intervals, values = jf.to_interval_values() | ||
assert len(out) > 0 | ||
assert out[0].category is DeprecationWarning | ||
assert 'deprecated' in str(out[0].message).lower() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
|
||
assert np.allclose(intervals, np.array([[0.0, 1.0], [1.0, 3.0]])) | ||
eq_(values, ['a', 'b']) | ||
assert np.allclose(intervals, np.array([[0.0, 1.0], [1.0, 3.0]])) | ||
eq_(values, ['a', 'b']) | ||
|
||
|
||
def test_jamsframe_serialize(): | ||
|
@@ -369,6 +375,35 @@ def test_annotation_eq(): | |
|
||
assert not (ann1 == ann2) | ||
|
||
|
||
def test_annotation_iterator(): | ||
|
||
data = [dict(time=0, duration=0.5, value='one', confidence=0.2), | ||
dict(time=1, duration=1, value='two', confidence=0.5)] | ||
|
||
namespace = 'tag_open' | ||
|
||
ann = jams.Annotation(namespace, data=data) | ||
|
||
for obs, obs_raw in zip(ann, data): | ||
assert isinstance(obs, jams.Observation) | ||
assert obs._asdict() == obs_raw, (obs, obs_raw) | ||
|
||
|
||
def test_annotation_interval_values(): | ||
|
||
data = dict(time=[0.0, 1.0], | ||
duration=[1., 2.0], | ||
value=['a', 'b'], | ||
confidence=[0.9, 0.9]) | ||
|
||
ann = jams.Annotation(namespace='tag_open', data=data) | ||
|
||
intervals, values = ann.to_interval_values() | ||
|
||
assert np.allclose(intervals, np.array([[0.0, 1.0], [1.0, 3.0]])) | ||
eq_(values, ['a', 'b']) | ||
|
||
# FileMetadata | ||
|
||
def test_filemetadata(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
np.vstack
overnp.array
..?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to concatenate them along the second axis; historical style holdover from the days before arbitary concat support.
but this is getting wiped in #149 also.