Skip to content

Fix some FieldTimeSeries indexing bugs #4505

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

loisbaker
Copy link

This PR fixes some small indexing bugs for FieldTimeSeries described in #4100

loisbaker added 4 commits May 13, 2025 11:20
Improves element-wise comparison, which returned False for values near zero due to relative error default.
Modifies `find_time_index` to take an extra timescale parameter `dt`, so that an absolute error can be computed to avoid `isapprox` returning `false` for values near zero


# Compute a timescale for comparisons
dt = mean(file_times[2:end] - file_times[1:end-1])
Copy link
Member

@navidcy navidcy May 13, 2025

Choose a reason for hiding this comment

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

Suggested change
dt = mean(file_times[2:end] - file_times[1:end-1])
timescale = mean(diff(file_times))

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, it's really the time increment between time points, so I think Δt is more appropriate. Just suggest using the Δ not dt for readability

Copy link
Member

Choose a reason for hiding this comment

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

Or mean_Δt

Comment on lines 23 to 24
# Compute a timescale for comparisons
dt = mean(file_times[2:end] - file_times[1:end-1])
Copy link
Member

Choose a reason for hiding this comment

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

why don't we compute this within find_time_index? just a thought...
or something like:

function find_time_index(time::Number, file_times; time_scale = mean(diff(file_times)))
    ϵ = 100 * eps(eltype(file_times))
    return findfirst(t -> isapprox(t, time, atol=ϵ*time_scale), file_times)
end

Copy link
Author

Choose a reason for hiding this comment

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

Good point, I guess pre-computing it avoids recalculation at every iteration of the for loop it's used in? But if that's not an issue it would be simpler to compute within the function.

Copy link
Member

Choose a reason for hiding this comment

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

Precomputing serves two purposes: 1) we don't recompute, which could become important for very long time series, and 2) as a point of design, this allows us to change the estimate of the characteristic time increment later. Right now we use the mean of all time increments, but this is not the only choice one might make.

@navidcy navidcy requested a review from glwagner May 13, 2025 15:31
@glwagner
Copy link
Member

@loisbaker should we add tests at all? Ok if that's a todo for the future, but tests will ensure these changes are lasting

@loisbaker
Copy link
Author

@loisbaker should we add tests at all? Ok if that's a todo for the future, but tests will ensure these changes are lasting

Sure, I can work on that - a test to check that find_time_index will pick the right index when the error between the two compared values is sufficiently small, and doesn't otherwise? Would you also make a test to check that the range conversion in field_time_series.jl only happens when the time values are sufficiently close to evenly spaced?

@glwagner
Copy link
Member

@loisbaker should we add tests at all? Ok if that's a todo for the future, but tests will ensure these changes are lasting

Sure, I can work on that - a test to check that find_time_index will pick the right index when the error between the two compared values is sufficiently small, and doesn't otherwise? Would you also make a test to check that the range conversion in field_time_series.jl only happens when the time values are sufficiently close to evenly spaced?

Perhaps just a simple test that resembles the case you presented that illustrated the bug would work (the more fine-grained tests can be nice too, but I don't think necessary in this case and they are more work).

We can also do in a future PR, since I think this is otherwise read to merge -- your call!

@loisbaker
Copy link
Author

@loisbaker should we add tests at all? Ok if that's a todo for the future, but tests will ensure these changes are lasting

Sure, I can work on that - a test to check that find_time_index will pick the right index when the error between the two compared values is sufficiently small, and doesn't otherwise? Would you also make a test to check that the range conversion in field_time_series.jl only happens when the time values are sufficiently close to evenly spaced?

Perhaps just a simple test that resembles the case you presented that illustrated the bug would work (the more fine-grained tests can be nice too, but I don't think necessary in this case and they are more work).

We can also do in a future PR, since I think this is otherwise read to merge -- your call!

Great, I'll put this on the to-do list for a future PR!

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

Successfully merging this pull request may close these issues.

3 participants