Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor apply_parallel #268

Merged
merged 9 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
fail-fast: false
matrix:
os: [ "ubuntu-latest" ]
python-version: [ "3.9", "3.10", "3.11" ]
python-version: [ "3.8", "3.9", "3.10", "3.11" ]
runs-on: "${{ matrix.os }}"
steps:
- name: Check out repository
Expand Down
46 changes: 24 additions & 22 deletions src/scmdata/groupby.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Functionality for grouping and filtering ScmRun objects
"""
from __future__ import annotations

import warnings
from collections.abc import Iterable
from typing import TYPE_CHECKING, Callable, Generic, Iterator, Optional, Sequence, Union
from typing import TYPE_CHECKING, Callable, Generic, Iterator, Sequence

import numpy as np
import pandas as pd
Expand All @@ -27,7 +29,7 @@ class RunGroupBy(ImplementsArrayReduce, Generic[GenericRun]):
"""

def __init__(
self, run: "GenericRun", groups: "Iterable[str]", na_fill_value: float = -10000
self, run: GenericRun, groups: Iterable[str], na_fill_value: float = -10000
):
self.run = run
self.group_keys = groups
Expand All @@ -45,9 +47,9 @@ def __init__(
else:
m = m.fillna(na_fill_value)

self._grouper: "DataFrameGroupBy" = m.groupby(list(groups), group_keys=True)
self._grouper: DataFrameGroupBy = m.groupby(list(groups), group_keys=True)

def _iter_grouped(self) -> "Iterator[GenericRun]":
def _iter_grouped(self) -> Iterator[GenericRun]:
def _try_fill_value(v: MetadataValue) -> MetadataValue:
try:
if float(v) == float(self.na_fill_value):
Expand All @@ -57,7 +59,7 @@ def _try_fill_value(v: MetadataValue) -> MetadataValue:
return v

groups: Iterable[
Union[MetadataValue, tuple[MetadataValue, ...]]
MetadataValue | tuple[MetadataValue, ...]
] = self._grouper.groups
for indices in groups:
if not isinstance(indices, Iterable) or isinstance(indices, str):
Expand All @@ -82,10 +84,10 @@ def __iter__(self) -> Iterator[GenericRun]:

def apply(
self,
func: "Callable[Concatenate[GenericRun, P], Union[GenericRun, pd.DataFrame, None]]",
*args: "P.args",
**kwargs: "P.kwargs",
) -> "GenericRun":
func: Callable[Concatenate[GenericRun, P], GenericRun | (pd.DataFrame | None)],
*args: P.args,
**kwargs: P.kwargs,
) -> GenericRun:
"""
Apply a function to each group and append the results

Expand Down Expand Up @@ -128,12 +130,12 @@ def apply(

def apply_parallel(
self,
func: "Callable[Concatenate[GenericRun, P], Union[GenericRun, pd.DataFrame, None]]",
func: Callable[Concatenate[GenericRun, P], GenericRun | (pd.DataFrame | None)],
n_jobs: int = 1,
backend: str = "loky",
*args: "P.args",
**kwargs: "P.kwargs",
) -> "GenericRun":
*args: P.args,
**kwargs: P.kwargs,
) -> GenericRun:
"""
Apply a function to each group in parallel and append the results

Expand Down Expand Up @@ -181,7 +183,7 @@ def apply_parallel(
) from e

grouped = self._iter_grouped()
applied: "list[Union[GenericRun, pd.DataFrame, None]]" = joblib.Parallel(
applied: list[GenericRun | (pd.DataFrame | None)] = joblib.Parallel(
n_jobs=n_jobs, backend=backend
)(joblib.delayed(func)(arr, *args, **kwargs) for arr in grouped)
return self._combine(applied)
Expand All @@ -202,8 +204,8 @@ def map(self, func, *args, **kwargs):
return self.apply(func, *args, **kwargs)

def _combine(
self, applied: "Sequence[Union[GenericRun, pd.DataFrame, None]]"
) -> "GenericRun":
self, applied: Sequence[GenericRun | (pd.DataFrame | None)]
) -> GenericRun:
"""
Recombine the applied objects like the original.
"""
Expand All @@ -219,12 +221,12 @@ def _combine(

def reduce(
self,
func: "Callable[Concatenate[NDArray[np.float_], P], NDArray[np.float_]]",
dim: "Optional[Union[str, Iterable[str]]]" = None,
axis: "Optional[Union[str, Iterable[int]]]" = None,
*args: "P.args",
**kwargs: "P.kwargs",
) -> "GenericRun":
func: Callable[Concatenate[NDArray[np.float_], P], NDArray[np.float_]],
dim: str | Iterable[str] | None = None,
axis: str | Iterable[int] | None = None,
*args: P.args,
**kwargs: P.kwargs,
) -> GenericRun:
"""
Reduce the items in this group by applying `func` along some dimension(s).

Expand Down