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

Make it easier to chain data transformers #464

Closed
redeboer opened this issue Oct 17, 2022 · 0 comments · Fixed by #470
Closed

Make it easier to chain data transformers #464

redeboer opened this issue Oct 17, 2022 · 0 comments · Fixed by #470
Assignees
Milestone

Comments

@redeboer
Copy link
Member

From another project:

from __future__ import annotations

from attrs import frozen, field
from tensorwaves.interface import DataSample, DataTransformer


@frozen
class ChainedDataTransformer(DataTransformer):
    transformers: tuple[DataTransformer] = field(converter=tuple)
    extend: bool = True

    def __call__(self, data: DataSample) -> DataSample:
        new_data = dict(data)
        weights = new_data.get("weights")
        for transformer in self.transformers:
            if self.extend:
                new_data.update(transformer(new_data))
            else:
                new_data = transformer(new_data)
        if weights is not None:
            new_data["weights"] = weights
        return new_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant