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

feature: add pattern detection #93

Merged
merged 9 commits into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions pipit/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,62 @@ def multirun_analysis(
combined_df = combined_df[function_sums.sort_values(ascending=False).index]

return combined_df

def detect_pattern(
self,
start_event,
iterations=None,
window_size=None,
process=0,
metric="time.exc",
):
import stumpy

enter_events = self.events[
(self.events["Name"] == start_event)
& (self.events["Event Type"] == "Enter")
& (self.events["Process"] == process)
]

leave_events = self.events[
(self.events["Name"] == start_event)
& (self.events["Event Type"] == "Leave")
& (self.events["Process"] == process)
]

# count the number of enter events to
# determine the number of iterations if it's not
# given by the user.
if iterations is None:
iterations = len(enter_events)

# get the first enter and last leave of
# the given event. we will only investigate
# this portion of the data.
first_loop_enter = enter_events.index[0]
last_loop_leave = leave_events.index[-1]

df = self.events.iloc[first_loop_enter + 1 : last_loop_leave]
Copy link
Collaborator

Choose a reason for hiding this comment

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

any reason why we don't include first_loop_enter and last_loop_leave themselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't remember but I will think about it and add comments

filtered_df = df.loc[(df[metric].notnull()) & (df["Process"] == process)]
y = filtered_df[metric].values[:]

if window_size is None:
window_size = int(len(y) / iterations)

matrix_profile = stumpy.stump(y, window_size)
dists, indices = stumpy.motifs(y, matrix_profile[:, 0], max_matches=iterations)

# Gets the corresponding portion from the original
# dataframe for each pattern.
patterns = []
for idx in indices[0]:
end_idx = idx + window_size

match_original = self.events.loc[
self.events["Timestamp (ns)"].isin(
filtered_df.iloc[idx:end_idx]["Timestamp (ns)"].values
)
]
patterns.append(match_original)

return patterns
Loading