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

Initial version of RollingInputFixedReference #197

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
101 changes: 98 additions & 3 deletions popmon/analysis/comparison/hist_comparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def __init__(
store_key,
assign_to_key=None,
hist_col="histogram",
suffix="comp",
suffix="prev",
prefix="prev",
max_res_bound=7.0,
*args,
**kwargs,
Expand All @@ -163,7 +164,8 @@ def __init__(
:param str store_key: key of output data to store in data store
:param str assign_to_key: key of the input data to assign function applied-output to. (optional)
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param str suffix: column/key of rolling histogram. default is 'roll' -> column = 'histogram_roll'
:param str suffix: column/key of rolling histogram. default is 'ref' -> column = 'histogram_ref'
:param str prefix: column/key of comparisons. default is 'comp' -> column = 'comp_pearson'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
:param args: (tuple, optional): residual args passed on to func_mean and func_std
Expand All @@ -189,7 +191,7 @@ def __init__(
"func": hist_compare,
"hist_name1": hist_col,
"hist_name2": hist_col + "_" + suffix,
"prefix": suffix,
"prefix": prefix,
"axis": 1,
"max_res_bound": max_res_bound,
}
Expand All @@ -210,6 +212,7 @@ def __init__(
shift=1,
hist_col="histogram",
suffix="roll",
prefix="rolling",
max_res_bound=7.0,
):
"""Initialize an instance of RollingHistComparer.
Expand All @@ -230,6 +233,7 @@ def __init__(
read_key,
hist_col,
suffix,
prefix,
max_res_bound,
window=window,
shift=shift,
Expand Down Expand Up @@ -286,6 +290,7 @@ def __init__(
shift=1,
hist_col="histogram",
suffix="expanding",
prefix="expanding",
max_res_bound=7.0,
):
"""Initialize an instance of ExpandingHistComparer.
Expand All @@ -305,6 +310,7 @@ def __init__(
read_key,
hist_col,
suffix,
prefix,
max_res_bound,
shift=shift,
hist_name=hist_col,
Expand All @@ -328,6 +334,7 @@ def __init__(
store_key,
hist_col="histogram",
suffix="ref",
prefix="reference",
max_res_bound=7.0,
):
"""Initialize an instance of ReferenceHistComparer.
Expand All @@ -347,6 +354,7 @@ def __init__(
assign_to_key,
hist_col,
suffix,
prefix,
max_res_bound,
metrics=[hist_col],
)
Expand All @@ -360,6 +368,93 @@ def transform(self, datastore):
return super().transform(datastore)


class RollingInputFixedReference(Pipeline):
"""Base pipeline to compare rolling input histograms to fixed (external) histograms"""

def __init__(
self,
read_key,
reference_key,
store_key,
assign_to_key=None,
window=1,
shift=1,
hist_col="histogram",
suffix1="roll",
suffix2="ref",
prefix="rollref",
max_res_bound=7.0,
):
"""Initialize an instance of RollingInputFixedReference.

:param str read_key: key of input data to read from data store
:param str reference_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param str assign_to_key: key of the input data to assign function applied-output to. (optional)
:param int window: size of rolling window, default is 1.
:param int shift: shift of rolling window. default is 1.
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'.
:param str suffix1: column/key of rolling histogram. default is 'roll' -> column = 'histogram_roll'
:param str suffix2: column/key of external histogram. default is 'ref' -> column = 'histogram_ref'
:param str prefix: prefix of comparisons metrics. default is 'rollref'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
"""
if assign_to_key is None:
assign_to_key = read_key

# make rolling reference histograms
hist_collector1 = ApplyFunc(
apply_to_key=read_key,
assign_to_key=assign_to_key,
)
hist_collector1.add_apply_func(
func=rolling_hist,
entire=True,
suffix=suffix1,
window=window,
shift=shift,
hist_name=hist_col,
)

# make fixed (external) reference histograms
hist_collector2 = ApplyFunc(
apply_to_key=reference_key,
assign_to_key=assign_to_key,
)
hist_collector2.add_apply_func(
func=hist_sum, entire=True, suffix=suffix2, metrics=[hist_col]
)

# do histogram comparison
hist_comparer = ApplyFunc(
apply_to_key=assign_to_key,
assign_to_key=store_key,
apply_funcs=[
{
"func": hist_compare,
"hist_name1": hist_col + "_" + suffix1,
"hist_name2": hist_col + "_" + suffix2,
"prefix": prefix,
"axis": 1,
"max_res_bound": max_res_bound,
}
],
)

super().__init__(modules=[hist_collector1, hist_collector2, hist_comparer])

self.read_key = read_key
self.reference_key = reference_key
self.window = window

def transform(self, datastore):
self.logger.info(
f'Comparing "{self.read_key}" with rolling sum of {self.window} slots to {self.reference_key}.'
)
return super().transform(datastore)


class NormHistComparer(Pipeline):
"""Base pipeline to compare histogram to normalized histograms"""

Expand Down
Loading