Skip to content

Commit

Permalink
Original edivisive feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikingo authored Nov 20, 2023
2 parents d7fe028 + 5a7a361 commit 7435ad9
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 519 deletions.
13 changes: 13 additions & 0 deletions hunter/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from signal_processing_algorithms.e_divisive.base import SignificanceTester
from signal_processing_algorithms.e_divisive.calculators import cext_calculator
from signal_processing_algorithms.e_divisive.change_points import EDivisiveChangePoint
from signal_processing_algorithms.e_divisive.significance_test import (
QHatPermutationsSignificanceTester,
)


@dataclass
Expand Down Expand Up @@ -221,6 +224,16 @@ def split(series: np.array, window_len: int = 30, max_pvalue: float = 0.001) ->
return [tester.change_point(i, series, window_endpoints) for i in indexes]


def compute_change_points_orig(series: np.array, max_pvalue: float = 0.001) -> List[ChangePoint]:
calculator = cext_calculator
tester = QHatPermutationsSignificanceTester(calculator, pvalue=max_pvalue, permutations=100)
algo = EDivisive(seed=None, calculator=calculator, significance_tester=tester)
pts = algo.get_change_points(series)
indexes = [p.index for p in pts]
window_endpoints = [0] + indexes + [len(series)]
return [tester.change_point(i, series, window_endpoints) for i in indexes]


def compute_change_points(
series: np.array, window_len: int = 50, max_pvalue: float = 0.001, min_magnitude: float = 0.05
) -> List[ChangePoint]:
Expand Down
10 changes: 10 additions & 0 deletions hunter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ def setup_analysis_options_parser(parser: argparse.ArgumentParser):
"as noise so it is best to keep it short enough to include not more "
"than a few change points (optimally at most 1)",
)
parser.add_argument(
"--orig-edivisive",
type=bool,
default=False,
dest="orig_edivisive",
help="use the original edivisive algorithm with no windowing "
"and weak change points analysis improvements",
)


def analysis_options_from_args(args: argparse.Namespace) -> AnalysisOptions:
Expand All @@ -448,6 +456,8 @@ def analysis_options_from_args(args: argparse.Namespace) -> AnalysisOptions:
conf.min_magnitude = args.magnitude
if args.window is not None:
conf.window_len = args.window
if args.orig_edivisive is not None:
conf.orig_edivisive = args.orig_edivisive
return conf


Expand Down
21 changes: 15 additions & 6 deletions hunter/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ComparativeStats,
TTestSignificanceTester,
compute_change_points,
compute_change_points_orig,
fill_missing,
)

Expand All @@ -19,11 +20,13 @@ class AnalysisOptions:
window_len: int
max_pvalue: float
min_magnitude: float
orig_edivisive: bool

def __init__(self):
self.window_len = 50
self.max_pvalue = 0.001
self.min_magnitude = 0.0
self.orig_edivisive = False


@dataclass
Expand Down Expand Up @@ -160,12 +163,18 @@ def __compute_change_points(
for metric in series.data.keys():
values = series.data[metric].copy()
fill_missing(values)
change_points = compute_change_points(
values,
window_len=options.window_len,
max_pvalue=options.max_pvalue,
min_magnitude=options.min_magnitude,
)
if options.orig_edivisive:
change_points = compute_change_points_orig(
values,
max_pvalue=options.max_pvalue,
)
else:
change_points = compute_change_points(
values,
window_len=options.window_len,
max_pvalue=options.max_pvalue,
min_magnitude=options.min_magnitude,
)
result[metric] = []
for c in change_points:
result[metric].append(
Expand Down
Loading

0 comments on commit 7435ad9

Please sign in to comment.