Skip to content

Commit

Permalink
Add examples for presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Domi020 committed Mar 13, 2024
1 parent 383f2d7 commit 7b73234
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
src/cpdbench-log.txt
src/cpdbench-result.json

*.txt
*.json

venv
venv-new
23 changes: 23 additions & 0 deletions src/cpdbench/examples/ExampleAlgorithms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
from changepoynt.algorithms.sst import SST


def numpy_array_accesses(dataset, array_indexes):
indexes = []
for i in array_indexes:
indexes.append(dataset[i])
confidences = [1 for _ in range(len(indexes))]
return indexes, confidences


def algorithm_execute_single_esst(signal):
"""Uses SST as implemented in the changepoynt library as algorithm."""
detector = SST(90, method='rsvd')
sig = signal[0]
res = detector.transform(sig)
indexes = [res.argmax()]
confidences = [1.0]
return indexes, confidences


def algorithm_execute_single_esst(signal, window_length):
"""Uses SST as implemented in the changepoynt library as algorithm."""
detector = SST(window_length, method='rsvd')
sig = signal[0]
res = detector.transform(sig)
indexes = [res.argmax()]
confidences = [1.0]
return indexes, confidences
16 changes: 16 additions & 0 deletions src/cpdbench/examples/ExampleDatasets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import pathlib

import numpy as np

from cpdbench.dataset.CPD2DFromFileDataset import CPD2DFromFileDataset
from cpdbench.dataset.CPD2DNdarrayDataset import CPD2DNdarrayDataset


def get_extreme_large_dataset_from_file():
path = pathlib.Path(__file__).parent.resolve()
path = path.joinpath("data", "very_big_numpy_file.dat")
dataset = CPD2DFromFileDataset(str(path), "float32", [5, 245, 255, 256, 25])
return dataset

def dataset_get_apple_dataset():
raw_data = np.load("../../../data/apple.npy")
timeseries = raw_data[:, 0]
reshaped_ts = np.reshape(timeseries, [1, timeseries.size])
return CPD2DNdarrayDataset(reshaped_ts, [337])


def dataset_get_bitcoin_dataset():
raw_data = np.load("../../../data/bitcoin.npy")
timeseries = raw_data[:, 0]
reshaped_ts = np.reshape(timeseries, [1, timeseries.size])
return CPD2DNdarrayDataset(reshaped_ts, [569])
30 changes: 30 additions & 0 deletions src/cpdbench/examples/Example_Parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from cpdbench.CPDBench import CPDBench
import cpdbench.examples.ExampleDatasets as example_datasets
import cpdbench.examples.ExampleAlgorithms as example_algorithms
import cpdbench.examples.ExampleMetrics as example_metrics

cpdb = CPDBench()


@cpdb.dataset
def get_apple_dataset():
return example_datasets.dataset_get_apple_dataset()


@cpdb.dataset
def get_bitcoin_dataset():
return example_datasets.dataset_get_bitcoin_dataset()


@cpdb.algorithm
def execute_esst_test(signal, *, window_length):
return example_algorithms.algorithm_execute_single_esst(signal, window_length)


@cpdb.metric
def calc_accuracy(indexes, scores, ground_truth, *, window_size):
return example_metrics.metric_accuracy_in_allowed_windows(indexes, scores, ground_truth, window_size=window_size)


if __name__ == '__main__':
cpdb.start("configs/parametersConfig.yml")
30 changes: 30 additions & 0 deletions src/cpdbench/examples/Example_Simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from cpdbench.CPDBench import CPDBench
import cpdbench.examples.ExampleDatasets as example_datasets
import cpdbench.examples.ExampleAlgorithms as example_algorithms
import cpdbench.examples.ExampleMetrics as example_metrics

cpdb = CPDBench()


@cpdb.dataset
def get_apple_dataset():
return example_datasets.dataset_get_apple_dataset()


@cpdb.dataset
def get_bitcoin_dataset():
return example_datasets.dataset_get_bitcoin_dataset()


@cpdb.algorithm
def execute_esst_test(signal):
return example_algorithms.algorithm_execute_single_esst(signal)


@cpdb.metric
def calc_accuracy(indexes, scores, ground_truth):
return example_metrics.metric_accuracy_in_allowed_windows(indexes, scores, ground_truth, window_size=25)


if __name__ == '__main__':
cpdb.start()
45 changes: 0 additions & 45 deletions src/cpdbench/examples/Functions.py

This file was deleted.

19 changes: 19 additions & 0 deletions src/cpdbench/examples/configs/parametersConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
logging:
filename: cpdbench-log-parameters.txt
log-level: DEBUG
log-level-console: WARNING
multiprocessing: True

result:
filename: cpdbench-result-parameters.json


user:
algorithm-executions:
- window_length: 90
- window_length: 45
- window_length: 70

metric-executions:
- window_size: 50
- window_size: 20
2 changes: 1 addition & 1 deletion src/cpdbench/task/AlgorithmExecutionTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def execute(self, data: ndarray) -> tuple[Iterable, Iterable]:
return alg_res_index, alg_res_scores

def get_task_name(self) -> str:
return f"algorithm: {self._task_name}"
return f"algorithm:{self._task_name}"
2 changes: 1 addition & 1 deletion src/cpdbench/task/DatasetFetchTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def execute(self) -> CPDDataset:
return dataset

def get_task_name(self) -> str:
return f"dataset: {self._task_name}"
return f"dataset:{self._task_name}"
2 changes: 1 addition & 1 deletion src/cpdbench/task/MetricExecutionTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def validate_input(self, indexes: Iterable, scores: Iterable, ground_truths: Ite
return res

def get_task_name(self) -> str:
return f"metric: {self._task_name}"
return f"metric:{self._task_name}"

0 comments on commit 7b73234

Please sign in to comment.