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

add consecutive_overlapping_subspans #19

Merged
merged 10 commits into from
Sep 20, 2023

Conversation

ericphanson
Copy link
Member

And add keep_last=false possibility to consecutive_subspans, to have a way to get consistent behavior between the two.

We could also try to add keep_last=true to consecutive_overlapping_subspans but it is much more confusing what that means when you have overlap.

index_groups = Iterators.partition((span.first_index):(span.last_index), n)
if !keep_last
r = rem(n_samples(span), n)
if r != 0
Copy link
Member Author

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!

@ericphanson ericphanson changed the title add consecutive_overlapping_subspans from SuppressionDetection.jl add consecutive_overlapping_subspans Sep 20, 2023
@ericphanson ericphanson force-pushed the eph/consecutive_overlapping_subspans branch from fc0fb36 to 5d3dd03 Compare September 20, 2023 15:05
src/utilities.jl Outdated
Comment on lines 71 to 83
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
Copy link
Member Author

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
Copy link
Member Author

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

Copy link
Member Author

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
Comment on lines 53 to 69
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)`
Copy link
Member Author

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

test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
ericphanson and others added 2 commits September 20, 2023 17:28
test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
test/utilities.jl Outdated Show resolved Hide resolved
Copy link
Contributor

@hannahilea hannahilea left a 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?

test/utilities.jl Show resolved Hide resolved
src/utilities.jl Show resolved Hide resolved
src/utilities.jl Outdated Show resolved Hide resolved
src/utilities.jl Outdated
Comment on lines 62 to 64
!!! 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`.
Copy link
Contributor

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??

Copy link
Member Author

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.

src/utilities.jl Outdated Show resolved Hide resolved
src/utilities.jl Outdated Show resolved Hide resolved
src/utilities.jl Outdated Show resolved Hide resolved
@ericphanson
Copy link
Member Author

Filed #20

src/utilities.jl Outdated Show resolved Hide resolved
@ericphanson ericphanson merged commit 5f9386d into main Sep 20, 2023
8 checks passed
@ericphanson ericphanson deleted the eph/consecutive_overlapping_subspans branch September 20, 2023 16:39
Comment on lines +42 to +43
index_groups = Iterators.partition((span.first_index):(span.last_index),
n_window_samples)
Copy link
Member

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:

Suggested change
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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Member Author

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?

Comment on lines +48 to +49
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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return consecutive_overlapping_subspans(span::AlignedSpan, n_window_samples,
return consecutive_overlapping_subspans(span, n_window_samples,

Was the type assertion there doing something?

Copy link
Member Author

Choose a reason for hiding this comment

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

copy-paste error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants