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

Ev: Add map-reduce style interface. #3003

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions src/gluonts/ev/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

from __future__ import annotations


from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np

from gluonts import maybe


@dataclass
class Aggregation:
Expand All @@ -31,6 +36,9 @@ def step(self, values: np.ndarray) -> None:
def get(self) -> np.ndarray:
raise NotImplementedError

def reduce(self, other) -> Aggregation:
raise NotImplementedError


@dataclass
class Sum(Aggregation):
Expand Down Expand Up @@ -65,6 +73,15 @@ def get(self) -> np.ndarray:

return np.ma.concatenate(self.partial_result)

def reduce(self, other) -> Sum:
if self.axis is None or 0 in self.axis:
return Sum(self.axis, self.partial_result + other.partial_result)

return Sum(
self.axis,
np.concatenate([self.partial_result, other.partial_result]),
)


@dataclass
class Mean(Aggregation):
Expand Down Expand Up @@ -108,3 +125,21 @@ def get(self) -> np.ndarray:
return self.partial_result / self.n

return np.ma.concatenate(self.partial_result)

def reduce(self, other) -> Sum:
if self.axis is None or 0 in self.axis:
return Mean(
self.axis,
self.partial_result + other.partial_result,
self.n + other.n,
)

return Mean(
self.axis,
np.concatenate(
[
maybe.unwrap_or(self.partial_result, []),
maybe.unwrap_or(other.partial_result, []),
]
),
)
24 changes: 24 additions & 0 deletions src/gluonts/ev/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import numpy as np

from gluonts.itertools import join_items

from .aggregations import Aggregation, Mean, Sum
from .stats import (
error,
Expand Down Expand Up @@ -89,6 +91,9 @@ def update_all(self, stream: Iterator[Mapping[str, np.ndarray]]) -> Self:
def get(self) -> np.ndarray:
raise NotImplementedError

def reduce(self, other):
raise NotImplementedError


@dataclass
class DirectMetric(Metric):
Expand All @@ -105,6 +110,11 @@ def update(self, data: Mapping[str, np.ndarray]) -> Self:
def get(self) -> np.ndarray:
return self.aggregate.get()

def reduce(self, other):
return DirectMetric(
self.name, self.stat, self.aggregate.reduce(other.aggregate)
)


@dataclass
class DerivedMetric(Metric):
Expand All @@ -131,6 +141,20 @@ def get(self) -> np.ndarray:
}
)

def reduce(self, other):
metrics = {
key: left.reduce(right)
for key, left, right in join_items(
self.metrics, other.metrics, "strict"
)
}

return DerivedMetric(
self.name,
metrics,
self.post_process,
)


@runtime_checkable
class MetricDefinition(Protocol):
Expand Down
Loading