Skip to content
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

enforce strictly increasing values in old_time and new_time #145

Merged
merged 23 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
988e8a3
enforce strictly increasing values in old_time and new_time
areeves87 Jun 28, 2018
6244c17
update monotonicity check; move warnings out of fxn
areeves87 Jun 28, 2018
0e7e5b9
strictly increase original_times, monotonic incr new_times
areeves87 Jun 28, 2018
ac9936b
remove trailing whitespace for pep8
areeves87 Jun 28, 2018
2dee3db
update new_times indices if original_times fails check
areeves87 Jun 28, 2018
c0c7474
use np.unique to check and enforce original_time strictly increasing
areeves87 Jun 29, 2018
6e8d88d
fix pep8 formatting issues.
areeves87 Jun 29, 2018
c524fa1
fix pep8 formatting issues.
areeves87 Jun 29, 2018
87b0576
fix pep8 formatting issues.
areeves87 Jun 29, 2018
9c8e28a
fix pep8 formatting issues.
areeves87 Jun 29, 2018
ca3a226
fix pep8 formatting issues.
areeves87 Jun 29, 2018
69ad544
convert new_times to array before subsetting with unique_idx
areeves87 Jun 29, 2018
21af894
so that we test adjust_times with appropriate event times
areeves87 Jun 29, 2018
445f9ec
so that test_adjust_times() tests the correct values
areeves87 Jun 29, 2018
80eb650
update tests for adjust_times
areeves87 Jul 2, 2018
d2628d3
comment out some tests in adjust_times
areeves87 Jul 2, 2018
27946a5
PrettyMIDI.adjust_times enforces strict increase in original_times an…
areeves87 Jul 3, 2018
937e7ac
prepare to squash commits
areeves87 Jul 3, 2018
174e2b1
Merge branch 'master' of https://github.com/areeves87/pretty-midi
areeves87 Jul 3, 2018
4bf7dd9
passes all tests
areeves87 Jul 4, 2018
24f584b
passes all tests and pep8
areeves87 Jul 4, 2018
f471ebd
tolerate floating point error in time signature calculation
areeves87 Jul 4, 2018
a20c358
maintain consistent decimal usage
areeves87 Jul 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pretty_midi/pretty_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,20 @@ def adjust_times(self, original_times, new_times):
# Get original downbeat locations (we will use them to determine where
# to put the first time signature change)
original_downbeats = self.get_downbeats()
# Force strict increase in original_times and monotonic in new_times.
# While enforcing, give warning.
original_size = len(original_times)
original_times, unique_idx = np.unique(original_times,
return_index=True)
if ((unique_idx.size != original_size) or
any(unique_idx != np.arange(unique_idx.size))):
warnings.warn('original_times must be strictly increasing; '
'automatically enforcing this.')
new_times = np.asarray(new_times)[unique_idx]
if not np.all(np.diff(new_times) >= 0):
warnings.warn('new_times must be monotonic; '
'automatically enforcing this.')
new_times = np.maximum.accumulate(new_times)
# Only include notes within start/end time of the provided times
for instrument in self.instruments:
instrument.notes = [copy.deepcopy(note)
Expand Down
24 changes: 16 additions & 8 deletions tests/test_pretty_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ def simple():
assert np.allclose(
[n.start for n in pm.instruments[0].notes], expected_starts)
pm = simple()
pm.adjust_times([0, 5, 5, 10], [5, 10, 12, 17])
pm.adjust_times([0, 5, 5, 10], [7, 12, 13, 17])
# Original times [1, 2, 3, 4, 5, 6, 7, 8, 9]
expected_starts = [6, 7, 8, 9, 12, 13, 14, 15, 16]
expected_starts = [8, 9, 10, 11, 12, 13, 14, 15, 16]
assert np.allclose(
[n.start for n in pm.instruments[0].notes], expected_starts)

Expand Down Expand Up @@ -213,7 +213,7 @@ def simple():
# Original tempo change times: [0, 6, 8.1, 8.3, 9.3]
# Plus tempo changes at each of new_times which are not collapsed
# Plus tempo change at 0s by default
expected_times = [0., 5., 6., 8.5,
expected_times = [0., 5., 6, 8.5,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Can you retain the decimal in this 6?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Good catch.

8.5 + (6 - 5.1)*(11 - 8.5)/(10 - 5.1),
8.5 + (8.1 - 5.1)*(11 - 8.5)/(10 - 5.1),
8.5 + (8.3 - 5.1)*(11 - 8.5)/(10 - 5.1),
Expand All @@ -233,11 +233,19 @@ def simple():
assert np.allclose(expected_tempi, tempi, rtol=.002)

# Test that all other events were interpolated as expected
note_starts = [5., 5 + 1/1.1, 7 + .9/(2/1.5), 7 + 1.9/(2/1.5), 8.5 + .5,
note_starts = [5.0,
5 + 1/1.1,
6 + .9/(2/2.5),
6 + 1.9/(2/2.5),
8.5 + .5,
8.5 + 1.5]
note_ends = [5 + .5/1.1, 7 + .4/(2/1.5), 7 + 1.4/(2/1.5), 8.5, 9 + .5,
note_ends = [5 + .5/1.1,
6 + .4/(2/2.5),
6 + 1.4/(2/2.5),
8.5,
8.5 + 1.,
10 + .5]
note_pitches = [101, 102, 103, 104, 107, 108, 109]
note_pitches = [101, 102, 103, 104, 107, 108]
for note, s, e, p in zip(pm.instruments[0].notes, note_starts, note_ends,
note_pitches):
assert note.start == s
Expand All @@ -262,11 +270,11 @@ def simple():
# downbeat location - so, start by computing the location of the first
# downbeat after the start of original_times, then interpolate it
first_downbeat_after = .1 + 2*3*60./100.
first_ts_time = 7 + (first_downbeat_after - 3.1)/(2/1.5)
first_ts_time = 6. + (first_downbeat_after - 3.1)/(2./2.5)
ts_times = [first_ts_time, 8.5, 8.5]
ts_numerators = [3, 4, 6]
for ts, t, n in zip(pm.time_signature_changes, ts_times, ts_numerators):
assert ts.time == t
assert np.isclose(ts.time, t)
assert ts.numerator == n

ks_times = [5., 8.5, 8.5]
Expand Down