You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The bisection search used in this line returns the index closest and smaller than the query rather than the one closest to it.
See example below
timed_data= [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
query_timestamp_ns=2.9# If this is safe we perform the Bisection searchstart=0end=len(timed_data) -1whilestart<end:
mid= (start+end) //2mid_timestamp=timed_data[mid]
ifmid_timestamp==query_timestamp_ns:
start=midbreakifmid_timestamp<query_timestamp_ns:
start=mid+1else:
end=mid-1print(start, timed_data[start]) # it prints 0 1
timed_data= [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
query_timestamp_ns=1.1# If this is safe we perform the Bisection searchstart=0end=len(timed_data) -1whilestart<end:
mid= (start+end) //2mid_timestamp=timed_data[mid]
ifmid_timestamp==query_timestamp_ns:
start=midbreakifmid_timestamp<query_timestamp_ns:
start=mid+1else:
end=mid-1print(start, timed_data[start]) # This also prints 0 1
The text was updated successfully, but these errors were encountered:
This is still buggy - will push another update later.
A proposed fix for the problem:
defbisection_timestamp_search(timed_data, query_timestamp_ns: int) ->int:
""" Binary search helper function, assuming that timed_data is sorted by the field names 'tracking_timestamp' Returns index of the element closest to the query timestamp else returns None if not found (out of time range) """# Deal with border caseiftimed_dataandlen(timed_data) >1:
first_timestamp=timed_data[0].tracking_timestamp.total_seconds() *1e9last_timestamp=timed_data[-1].tracking_timestamp.total_seconds() *1e9ifquery_timestamp_ns<=first_timestamp:
returnNoneelifquery_timestamp_ns>=last_timestamp:
returnNone# If this is safe we perform the Bisection searchstart=0end=len(timed_data) -1whilestart<end:
mid= (start+end) //2mid_timestamp=timed_data[mid].tracking_timestamp.total_seconds() *1e9ifmid_timestamp==query_timestamp_ns:
returnmidifmid_timestamp<query_timestamp_ns:
start=mid+1else:
end=mid-1# Get the value that is actually closest to the query timestampifstart+1<len(timed_data):
start_timestamp=timed_data[start].tracking_timestamp.total_seconds() *1e9start_plus_1_timestamp=timed_data[start+1].tracking_timestamp.total_seconds() *1e9ifabs(start_timestamp-query_timestamp_ns) >abs(start_plus_1_timestamp-query_timestamp_ns):
start=start+1returnstart
The bisection search used in this line returns the index closest and smaller than the query rather than the one closest to it.
See example below
The text was updated successfully, but these errors were encountered: