From c9f0b5aeb41a38569bd5ed19494b13d17283c465 Mon Sep 17 00:00:00 2001 From: jonasvdd Date: Thu, 18 Apr 2024 14:15:20 +0200 Subject: [PATCH] :muscle: adding code written by @jvdd --- tsflex/processing/series_pipeline.py | 6 +++--- tsflex/processing/series_processor.py | 16 +++++++++------- tsflex/processing/utils.py | 6 ++++-- tsflex/utils/classes.py | 6 ++++-- tsflex/utils/logging.py | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tsflex/processing/series_pipeline.py b/tsflex/processing/series_pipeline.py index 11a30177..c67a3593 100644 --- a/tsflex/processing/series_pipeline.py +++ b/tsflex/processing/series_pipeline.py @@ -244,7 +244,7 @@ def process( else: return [s for s in series_dict.values()] - def serialize(self, file_path: Union[str, Path]): + def serialize(self, file_path: Union[str, Path]) -> None: """Serialize this ``SeriesPipeline`` instance. Notes @@ -262,10 +262,10 @@ def serialize(self, file_path: Union[str, Path]): with open(file_path, "wb") as f: dill.dump(self, f, recurse=True) - def __repr__(self): + def __repr__(self) -> str: """Return formal representation of object.""" return "[\n" + "".join([f"\t{str(p)}\n" for p in self.processing_steps]) + "]" - def __str__(self): + def __str__(self) -> str: """Return informal representation of object.""" return self.__repr__() diff --git a/tsflex/processing/series_processor.py b/tsflex/processing/series_processor.py index c1d0f3c7..25c23e1e 100644 --- a/tsflex/processing/series_processor.py +++ b/tsflex/processing/series_processor.py @@ -16,7 +16,7 @@ __pdoc__["SeriesProcessor.__call__"] = True -def dataframe_func(func: Callable): +def dataframe_func(func: Callable) -> Callable: """Decorate function to use a DataFrame instead of multiple series (as argument). This decorator can be used for functions that need to work on a whole @@ -41,7 +41,9 @@ def dataframe_func(func: Callable): """ - def wrapper(*series: pd.Series, **kwargs): + def wrapper( + *series: pd.Series, **kwargs # type: ignore[no-untyped-def] + ) -> Union[np.ndarray, pd.Series, pd.DataFrame, List[pd.Series]]: series_dict = {s.name: s for s in series} df = series_dict_to_df(series_dict) res = func(df, **kwargs) @@ -114,7 +116,7 @@ def __init__( self, function: Callable, series_names: Union[str, Tuple[str, ...], List[str], List[Tuple[str, ...]]], - **kwargs, + **kwargs, # type: ignore[no-untyped-def] ): series_names = [to_tuple(names) for names in to_list(series_names)] # Assert that function inputs (series) all have the same length @@ -192,11 +194,11 @@ def __call__(self, series_dict: Dict[str, pd.Series]) -> Dict[str, pd.Series]: # Variable that will contain the final output of this method processed_output: Dict[str, pd.Series] = {} - def get_series_list(keys: Tuple[str, ...]): + def get_series_list(keys: Tuple[str, ...]) -> List[pd.Series]: """Get an ordered series list view for the given keys.""" return [series_dict[key] for key in keys] - def get_series_dict(keys: Tuple[str, ...]): + def get_series_dict(keys: Tuple[str, ...]) -> Dict[str, pd.Series]: """Get a series dict view for the given keys.""" return {key: series_dict[key] for key in keys} @@ -223,13 +225,13 @@ def get_series_dict(keys: Tuple[str, ...]): return processed_output - def __repr__(self): + def __repr__(self) -> str: """Return formal representation of object.""" repr_str = self.name + (" " + str(self.kwargs)) repr_str += " : " + " ".join([str(s) for s in self.series_names]) return repr_str - def __str__(self): + def __str__(self) -> str: """Return informal representation of object.""" return self.__repr__() diff --git a/tsflex/processing/utils.py b/tsflex/processing/utils.py index 00d3af86..dd378899 100644 --- a/tsflex/processing/utils.py +++ b/tsflex/processing/utils.py @@ -19,7 +19,7 @@ def process_chunks_multithreaded( series_pipeline: SeriesPipeline, show_progress: Optional[bool] = True, n_jobs: Optional[int] = None, - **processing_kwargs, + **processing_kwargs, # type: ignore[no-untyped-def] ) -> Optional[List[Any]]: """Process `same_range_chunks_list` in a multithreaded manner, order is preserved. @@ -54,7 +54,9 @@ def process_chunks_multithreaded( """ n_jobs = parse_n_jobs(n_jobs) - def _executor(same_range_chunks: List[Union[pd.Series, pd.DataFrame]]): + def _executor( + same_range_chunks: List[Union[pd.Series, pd.DataFrame]] + ) -> Union[List[pd.Series], pd.DataFrame]: try: return series_pipeline.process(same_range_chunks, **processing_kwargs) except Exception: diff --git a/tsflex/utils/classes.py b/tsflex/utils/classes.py index 909d1da9..6d305359 100644 --- a/tsflex/utils/classes.py +++ b/tsflex/utils/classes.py @@ -2,16 +2,18 @@ __author__ = "Jonas Van Der Donckt" +from typing import Any + class FrozenClass(object): """Superclass which allows subclasses to freeze at any time.""" __is_frozen = False - def __setattr__(self, key, value): + def __setattr__(self, key: Any, value: Any) -> None: if self.__is_frozen and not hasattr(self, key): raise TypeError("%r is a frozen class" % self) object.__setattr__(self, key, value) - def _freeze(self): + def _freeze(self) -> None: self.__is_frozen = True diff --git a/tsflex/utils/logging.py b/tsflex/utils/logging.py index a60544b9..d820cb3e 100644 --- a/tsflex/utils/logging.py +++ b/tsflex/utils/logging.py @@ -41,7 +41,7 @@ def remove_inner_brackets(message: str) -> str: return new_message -def delete_logging_handlers(logger: logging.Logger): +def delete_logging_handlers(logger: logging.Logger) -> None: """Delete all logging handlers that are not stream-handlers. Parameters