-
Notifications
You must be signed in to change notification settings - Fork 119
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
Improving Trip Segmentation by reducing DB calls #958
Open
humbleOldSage
wants to merge
2
commits into
e-mission:master
Choose a base branch
from
humbleOldSage:FastTripSegmentation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−42
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,36 +9,40 @@ | |
import emission.core.wrapper.transition as ecwt | ||
import emission.storage.timeseries.timequery as estt | ||
|
||
def is_tracking_restarted_in_range(start_ts, end_ts, timeseries): | ||
def is_tracking_restarted_in_range(start_ts, end_ts, transition_df): | ||
""" | ||
Check to see if tracing was restarted between the times specified | ||
:param start_ts: the start of the time range to check | ||
:param end_ts: the end of the time range to check | ||
:param timeseries: the timeseries to use for checking | ||
:return: | ||
""" | ||
import emission.storage.timeseries.timequery as estt | ||
transition_df_start_idx=transition_df.ts.searchsorted(start_ts,side='left') | ||
transition_df_end_idx=transition_df.ts.searchsorted(end_ts,side='right') | ||
transition_df_for_current=transition_df.iloc[transition_df_start_idx:transition_df_end_idx] | ||
|
||
tq = estt.TimeQuery(timeType="data.ts", startTs=start_ts, | ||
endTs=end_ts) | ||
transition_df = timeseries.get_data_df("statemachine/transition", tq) | ||
if len(transition_df) == 0: | ||
if len(transition_df_for_current) == 0: | ||
logging.debug("In range %s -> %s found no transitions" % | ||
(tq.startTs, tq.endTs)) | ||
(start_ts, end_ts)) | ||
return False | ||
logging.debug("In range %s -> %s found transitions %s" % | ||
(tq.startTs, tq.endTs, transition_df[["fmt_time", "curr_state", "transition"]])) | ||
return _is_tracking_restarted_android(transition_df) or \ | ||
_is_tracking_restarted_ios(transition_df) | ||
(start_ts, end_ts, transition_df_for_current[["fmt_time", "curr_state", "transition"]])) | ||
return _is_tracking_restarted_android(transition_df_for_current) or \ | ||
_is_tracking_restarted_ios(transition_df_for_current) | ||
|
||
def get_ongoing_motion_in_range(start_ts, end_ts, timeseries): | ||
tq = estt.TimeQuery(timeType = "data.ts", startTs = start_ts, | ||
endTs = end_ts) | ||
motion_list = list(timeseries.find_entries(["background/motion_activity"], tq)) | ||
def get_ongoing_motion_in_range(start_ts, end_ts, motion_df): | ||
## in case when we receive an empty dataframe, there's nothing to | ||
## process | ||
if motion_df.shape == (0,0): | ||
return motion_df | ||
|
||
motion_df_start_idx=motion_df.ts.searchsorted(start_ts,side='left') | ||
motion_df_end_idx=motion_df.ts.searchsorted(end_ts,side='right') | ||
filtered_motion_df=motion_df.iloc[motion_df_start_idx:motion_df_end_idx] | ||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
logging.debug("Found %s motion_activity entries in range %s -> %s" % | ||
(len(motion_list), tq.startTs, tq.endTs)) | ||
logging.debug("sample activities are %s" % motion_list[0:5]) | ||
return motion_list | ||
(len(filtered_motion_df),start_ts, end_ts)) | ||
#logging.debug("sample activities are %s" % filtered_motion_df.head()) | ||
return filtered_motion_df | ||
|
||
def _is_tracking_restarted_android(transition_df): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ def __init__(self, time_threshold, point_threshold, distance_threshold): | |
self.point_threshold = point_threshold | ||
self.distance_threshold = distance_threshold | ||
|
||
def segment_into_trips(self, timeseries, time_query): | ||
def segment_into_trips(self, transition_df, motion_df, timeseries, time_query): | ||
""" | ||
Examines the timeseries database for a specific range and returns the | ||
segmentation points. Note that the input is the entire timeseries and | ||
|
@@ -48,7 +48,8 @@ def segment_into_trips(self, timeseries, time_query): | |
""" | ||
self.filtered_points_df = timeseries.get_data_df("background/filtered_location", time_query) | ||
self.filtered_points_df.loc[:,"valid"] = True | ||
self.transition_df = timeseries.get_data_df("statemachine/transition", time_query) | ||
self.transition_df = transition_df | ||
self.motion_df =motion_df | ||
if len(self.transition_df) > 0: | ||
logging.debug("self.transition_df = %s" % self.transition_df[["fmt_time", "transition"]]) | ||
else: | ||
|
@@ -173,14 +174,14 @@ def has_trip_ended(self, lastPoint, currPoint, timeseries): | |
# for this kind of test | ||
speedThreshold = old_div(float(self.distance_threshold * 2), (old_div(self.time_threshold, 2))) | ||
|
||
if eaisr.is_tracking_restarted_in_range(lastPoint.ts, currPoint.ts, timeseries): | ||
if eaisr.is_tracking_restarted_in_range(lastPoint.ts, currPoint.ts, self.transition_df): | ||
Comment on lines
-176
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is |
||
logging.debug("tracking was restarted, ending trip") | ||
return True | ||
|
||
# In general, we get multiple locations between each motion activity. If we see a bunch of motion activities | ||
# between two location points, and there is a large gap between the last location and the first | ||
# motion activity as well, let us just assume that there was a restart | ||
ongoing_motion_in_range = eaisr.get_ongoing_motion_in_range(lastPoint.ts, currPoint.ts, timeseries) | ||
ongoing_motion_in_range = eaisr.get_ongoing_motion_in_range(lastPoint.ts, currPoint.ts, self.motion_df) | ||
ongoing_motion_check = len(ongoing_motion_in_range) > 0 | ||
if timeDelta > self.time_threshold and not ongoing_motion_check: | ||
logging.debug("lastPoint.ts = %s, currPoint.ts = %s, threshold = %s, large gap = %s, ongoing_motion_in_range = %s, ending trip" % | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a reason you are using this instead of something like
or
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.
First was the performance reason, O(log (n)) here vs O (n) in others.
Second, in case of query(), it creates a boolean series that marks rows to keep or discard, which happens internally, which increases temporary memory usage. For very large DataFrames, this can be an issue.
can use this one ,if log(n) vs O(n) is not an issue :
transition_df_for_current=transition_df[transition_df.ts >= start_ts && transition_df.ts <= end_ts]