From 409788da5428d8afc94e7d59b060586dbaa60d9f Mon Sep 17 00:00:00 2001 From: Kashif Rasul Date: Mon, 15 Jul 2019 18:50:17 +0200 Subject: [PATCH] fixes --- pts/__init__.py | 1 + pts/dataset/list_dataset.py | 2 +- pts/dataset/sampler.py | 10 +++++----- pts/exception.py | 4 ++++ pts/feature/transform.py | 3 ++- 5 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 pts/exception.py diff --git a/pts/__init__.py b/pts/__init__.py index e69de29..49dcbbd 100644 --- a/pts/__init__.py +++ b/pts/__init__.py @@ -0,0 +1 @@ +from .exception import assert_pts diff --git a/pts/dataset/list_dataset.py b/pts/dataset/list_dataset.py index 543a2d9..dbb89aa 100644 --- a/pts/dataset/list_dataset.py +++ b/pts/dataset/list_dataset.py @@ -6,7 +6,7 @@ from .process import ProcessDataEntry class ListDataset(Dataset): def __init__( - self, data_iter: Iterable[DataEntry], freq: str, one_dim_target: bool = True + self, data_iter: Iterable[DataEntry], freq: str, one_dim_target: bool = True ) -> None: process = ProcessDataEntry(freq, one_dim_target) self.list_data = [process(data) for data in data_iter] diff --git a/pts/dataset/sampler.py b/pts/dataset/sampler.py index 14716de..2bf245a 100644 --- a/pts/dataset/sampler.py +++ b/pts/dataset/sampler.py @@ -1,11 +1,11 @@ +from abc import ABC, abstractmethod from typing import Union, List import numpy as np -from abc import ABC, abstractmethod - from .stat import ScaleHistogram + class InstanceSampler(ABC): @abstractmethod def __call__(self, ts: np.ndarray, a: int, b: int) -> Union[np.ndarray, List[int]]: @@ -30,7 +30,7 @@ class UniformSplitSampler(InstanceSampler): while ts.shape[-1] >= len(self.lookup): self.lookup = np.arange(2 * len(self.lookup)) mask = np.random.uniform(low=0.0, high=1.0, size=b - a + 1) < self.p - return self.lookup[a : a + len(mask)][mask] + return self.lookup[a: a + len(mask)][mask] class TestSplitSampler(InstanceSampler): @@ -69,7 +69,7 @@ class ExpectedNumInstanceSampler(InstanceSampler): p = self.num_instances / self.avg_length mask = np.random.uniform(low=0.0, high=1.0, size=b - a + 1) < p - indices = self.lookup[a : a + len(mask)][mask] + indices = self.lookup[a: a + len(mask)][mask] return indices @@ -97,5 +97,5 @@ class BucketInstanceSampler(InstanceSampler): self.lookup = np.arange(2 * len(self.lookup)) p = 1.0 / self.scale_histogram.count(ts) mask = np.random.uniform(low=0.0, high=1.0, size=b - a + 1) < p - indices = self.lookup[a : a + len(mask)][mask] + indices = self.lookup[a: a + len(mask)][mask] return indices diff --git a/pts/exception.py b/pts/exception.py new file mode 100644 index 0000000..e3b212b --- /dev/null +++ b/pts/exception.py @@ -0,0 +1,4 @@ + +def assert_pts(condition: bool, message: str, *args, **kwargs) -> None: + if not condition: + raise Exception(message.format(*args, **kwargs)) diff --git a/pts/feature/transform.py b/pts/feature/transform.py index 5220f86..f8c20a8 100644 --- a/pts/feature/transform.py +++ b/pts/feature/transform.py @@ -7,6 +7,7 @@ import numpy as np import pandas as pd from pts.dataset import DataEntry, InstanceSampler +from pts import assert_pts from .time_feature import TimeFeature MAX_IDLE_TRANSFORMS = 100 @@ -237,7 +238,7 @@ class AsNumpyArray(SimpleTransformation): # ugly: required as list conversion will fail in the case of a # float value = np.asarray(value, dtype=self.dtype) - assert_data_error( + assert_pts( value.ndim >= self.expected_ndim, 'Input for field "{self.field}" does not have the required' "dimension (field: {self.field}, ndim observed: {value.ndim}, "