Skip to content

Commit

Permalink
💪 adding code written by @jvdd
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasvdd committed Apr 18, 2024
1 parent e3131a7 commit c9f0b5a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions tsflex/processing/series_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__()
16 changes: 9 additions & 7 deletions tsflex/processing/series_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}

Expand All @@ -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__()

Expand Down
6 changes: 4 additions & 2 deletions tsflex/processing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions tsflex/utils/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tsflex/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9f0b5a

Please sign in to comment.