-
Notifications
You must be signed in to change notification settings - Fork 0
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
add consecutive_overlapping_subspans
#19
Conversation
index_groups = Iterators.partition((span.first_index):(span.last_index), n) | ||
if !keep_last | ||
r = rem(n_samples(span), n) | ||
if r != 0 |
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.
I know the tests test this branch, bc originally I accidentally had r==0
and they failed!
consecutive_overlapping_subspans
from SuppressionDetection.jlconsecutive_overlapping_subspans
fc0fb36
to
5d3dd03
Compare
src/utilities.jl
Outdated
function consecutive_overlapping_subspans(span::AlignedSpan, duration::Period, | ||
hop_duration::Period) | ||
n = n_samples(span.sample_rate, duration) | ||
m = n_samples(span.sample_rate, hop_duration) | ||
return consecutive_overlapping_subspans(span::AlignedSpan, n, m) | ||
end | ||
|
||
function consecutive_overlapping_subspans(span::AlignedSpan, n::Int, m::Int) | ||
index_groups = Iterators.partition((span.first_index):(span.last_index - n + 1), | ||
m) | ||
return (AlignedSpan(span.sample_rate, first(I), first(I) + n - 1) | ||
for I in index_groups) | ||
end |
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.
this code is untouched from the private package in which it was copied from
@@ -37,3 +57,47 @@ end | |||
|
|||
@test_throws ArgumentError consecutive_subspans(aligned, Millisecond(1)) | |||
end | |||
|
|||
@testset "consecutive_overlapping_subspans" begin |
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.
this whole testset is untouched from the private package in which it was copied from
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.
(new stuff added in 8eabc1e)
src/utilities.jl
Outdated
consecutive_overlapping_subspans(span::AlignedSpan, duration::Period, | ||
hop_duration::Period) | ||
consecutive_overlapping_subspans(span::AlignedSpan, n::Int, m::Int) | ||
|
||
Create an iterator of `AlignedSpan` such that each `AlignedSpan` has | ||
`n` (calculated as `n_samples(span.sample_rate, duration)` if `duration::Period` is supplied) samples, shifted by | ||
`m` (calculated as `n_samples(span.sample_rate, hop_duration)` if `hop_duration::Period` is supplied) samples between | ||
consecutive spans. | ||
|
||
!!! warning | ||
When `n_samples(span)` is not an integer multiple of `n`, only AlignedSpans with `n` | ||
samples will be returned. This is analgous to `consecutive_subspans` with `keep_last=false`, which is not the default behavior for `consecutive_subspans`. | ||
|
||
Note: If `hop_duration` cannot be represented as an integer number of samples, | ||
rounding will occur to ensure that all output AlignedSpans will have the | ||
same number of samples. When rounding occurs, the output hop_duration will be: | ||
`Nanosecond(n_samples(samp_rate, hop_duration) / samp_rate * 1e9)` |
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.
this docstring is lightly edited from the private package it was taken from
Co-authored-by: Hannah Robertson <[email protected]>
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.
LGTM, contingent on renaming m
variable.
Consider adding keep_last
to consecutive_overlapping_subspans
, or at least filing it as a feature request?
src/utilities.jl
Outdated
!!! warning | ||
When `n_samples(span)` is not an integer multiple of `n`, only AlignedSpans with `n` | ||
samples will be returned. This is analgous to `consecutive_subspans` with `keep_last=false`, which is not the default behavior for `consecutive_subspans`. |
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.
huh. i wonder if we should add keep_last
as a kwarg, for parity??
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.
Yeah, I thought about that (see OP). I think it is a bit hard to know exactly what those semantics should be when there's overlap. I guess something like: if there's 8 fold overlap, the last 7 will all be shorter than usual, and the last one will be 1 sample long. But it's a bit weird. I'd rather leave that until someone needs it.
Filed #20 |
index_groups = Iterators.partition((span.first_index):(span.last_index), | ||
n_window_samples) |
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.
You can use the existing utility function here for clarity/brevity:
index_groups = Iterators.partition((span.first_index):(span.last_index), | |
n_window_samples) | |
index_groups = Iterators.partition(indices(span), n_window_samples) |
index_groups = Iterators.partition((span.first_index):(span.last_index), | ||
n_window_samples) | ||
if !keep_last | ||
r = rem(n_samples(span), n_window_samples) |
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.
r = rem(n_samples(span), n_window_samples) | |
n, r = fldmod(n_samples(span), n_window_samples) |
Note that currently you're using rem
with fld
, which have different rounding semantics. You probably want them to match, so to get fld
behavior you can use fldmod
or equivalently divrem
with RoundDown
.
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.
does this matter if all quantities are >0
?
grps = Iterators.take(index_groups, fld(n_samples(span), n_window_samples)) | ||
return (AlignedSpan(span.sample_rate, first(I), last(I)) for I in grps) |
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.
grps = Iterators.take(index_groups, fld(n_samples(span), n_window_samples)) | |
return (AlignedSpan(span.sample_rate, first(I), last(I)) for I in grps) | |
index_groups = Iterators.take(index_groups, n) |
(Uses the definition of n
in the previous suggestion.) If you redefine the binding then you don't have to rewrite the comprehension which is otherwise the same as the one outside of this block.
hop_duration::Period) | ||
n_window_samples = n_samples(span.sample_rate, duration) | ||
n_hop_samples = n_samples(span.sample_rate, hop_duration) | ||
return consecutive_overlapping_subspans(span::AlignedSpan, n_window_samples, |
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.
return consecutive_overlapping_subspans(span::AlignedSpan, n_window_samples, | |
return consecutive_overlapping_subspans(span, n_window_samples, |
Was the type assertion there doing something?
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.
copy-paste error
And add
keep_last=false
possibility toconsecutive_subspans
, to have a way to get consistent behavior between the two.We could also try to add
keep_last=true
toconsecutive_overlapping_subspans
but it is much more confusing what that means when you have overlap.