-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rh-pre-commit.version: 2.2.0 rh-pre-commit.check-secrets: ENABLED Signed-off-by: Auto User <[email protected]>
- Loading branch information
1 parent
224bc3e
commit 7cfd97e
Showing
9 changed files
with
172 additions
and
5 deletions.
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Init for CMR Algorithm | ||
""" | ||
from .cmr import CMR |
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"""CMR - Comparing Mean Responses Algorithm""" | ||
|
||
# pylint: disable = line-too-long | ||
from typing import List | ||
import pandas as pd | ||
import numpy | ||
|
||
from fmatch.logrus import SingletonLogger | ||
from hunter.series import ChangePoint, ComparativeStats | ||
from pkg.algorithms.algorithm import Algorithm | ||
|
||
|
||
class CMR(Algorithm): | ||
"""Implementation of the CMR algorithm | ||
Will Combine metrics into 2 lines and compare with a tolerancy to set pass fail | ||
Args: | ||
Algorithm (Algorithm): Inherits | ||
""" | ||
|
||
|
||
def _analyze(self): | ||
"""Analyze the dataframe with meaning any previous data and generate percent change with a current uuid | ||
Returns: | ||
series: data series that contains attributes and full dataframe | ||
change_points_by_metric: list of ChangePoints | ||
""" | ||
logger_instance = SingletonLogger.getLogger("Orion") | ||
logger_instance.info("Starting analysis using CMR") | ||
self.dataframe["timestamp"] = pd.to_datetime(self.dataframe["timestamp"]) | ||
self.dataframe["timestamp"] = self.dataframe["timestamp"].astype(int) // 10**9 | ||
|
||
if len(self.dataframe.index) == 1: | ||
series= self.setup_series() | ||
series.data = self.dataframe | ||
return series, {} | ||
# if larger than 2 rows, need to get the mean of 0 through -2 | ||
self.dataframe = self.combine_and_average_runs( self.dataframe) | ||
|
||
series= self.setup_series() | ||
tolerancy = 20 | ||
|
||
df, change_points_by_metric = self.run_cmr(tolerancy, self.dataframe) | ||
series.data= df | ||
return series, change_points_by_metric | ||
|
||
|
||
def run_cmr(self, tolerancy: int, dataframe_list: pd.DataFrame): | ||
""" | ||
Generate the percent difference in a 2 row dataframe | ||
Args: | ||
tolerancy (int): tolerancy to compare on | ||
metric_columns (List[str]): string list of metric column names | ||
dataframe_list (pd.DataFrame): data frame of all data to compare on | ||
Returns: | ||
pd.Dataframe, dict[metric_name, ChangePoint]: Returned data frame and change points | ||
""" | ||
metric_columns = self.metrics_config.keys() | ||
change_points_by_metric={ k:[] for k in metric_columns } | ||
max_date_time = pd.Timestamp.max.to_pydatetime() | ||
max_time = max_date_time.timestamp() | ||
# difference = ["difference", max_time] | ||
# pass_fail_list = ["Pass/Fail", max_time] | ||
for column in metric_columns: | ||
pct_change_result = dataframe_list[column].pct_change() | ||
single_pct_diff = round(pct_change_result.iloc[[-1]].values[0] * 100) | ||
pass_fail = "Pass" | ||
if single_pct_diff > tolerancy: | ||
pass_fail = "Fail" | ||
|
||
change_point = ChangePoint(metric=column, | ||
index=1, | ||
time=max_time, | ||
stats=ComparativeStats( | ||
mean_1=dataframe_list[column][0], | ||
mean_2=dataframe_list[column][1], | ||
std_1=0, | ||
std_2=0, | ||
pvalue=1 | ||
)) | ||
change_points_by_metric[column].append(change_point) | ||
# difference.append(single_pct_diff) | ||
# pass_fail_list.append(pass_fail) | ||
# difference.append("none") | ||
# pass_fail_list.append("none") | ||
# dataframe_list.loc[len(dataframe_list.index)] = difference | ||
#dataframe_list.loc[len(dataframe_list.index)] = pass_fail_list | ||
# logger_instance.info("final data frame " + str(dataframe_list)) | ||
|
||
# based on change point generate pass/fail | ||
return dataframe_list, change_points_by_metric | ||
|
||
def combine_and_average_runs(self, dataFrame: pd.DataFrame): | ||
""" | ||
If more than 1 previous run, mean data together into 1 single row | ||
Combine with current run into 1 data frame (current run being -1 index) | ||
Args: | ||
dataFrame (pd.DataFrame): data to combine into 2 rows | ||
Returns: | ||
pd.Dataframe: data frame of most recent run and averaged previous runs | ||
""" | ||
i = 0 | ||
|
||
last_row = dataFrame.tail(1) | ||
dF = dataFrame[:-1] | ||
data2 = {} | ||
|
||
metric_columns = list(dataFrame.columns) | ||
for column in metric_columns: | ||
|
||
if isinstance(dF.loc[0, column], (numpy.float64, numpy.int64)): | ||
mean = dF[column].mean() | ||
data2[column] = [mean] | ||
else: | ||
column_list = dF[column].tolist() | ||
non_numeric_joined_list = ','.join(column_list) | ||
data2[column] = [non_numeric_joined_list] | ||
i += 1 | ||
df2 = pd.DataFrame(data2) | ||
|
||
result = pd.concat([df2, last_row], ignore_index=True) | ||
return result |
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
JSON="json" | ||
TEXT="text" | ||
JUNIT="junit" | ||
CMR="cmr" |
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