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

A basic unflatten pre-processing op to re-create feature hierarchies. #510

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions kauldron/data/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,30 @@ def map(self, features):
return output


@dataclasses.dataclass(kw_only=True, frozen=True, eq=True)
class UnFlattenOneLevel(_ElementWise, grain.MapTransform):
"""Unflattens specified key(s).

For example, using 'a_c_d' as `key`, with:
features = {'a_b': 2, 'a_c_d': 3, 'e': 5, 'f': {'g': 6}}

becomes:
features = {'a_b': 2, 'a_c': {'d': 3}, 'e': 5, 'f': {'g': 6}}
"""

separator: str = "_"

def map(self, features):
output = {}
for key, element, should_transform in self._per_element(features):
if should_transform:
new_key, new_child_key = key.rsplit(self.separator, maxsplit=1)
output[new_key] = {new_child_key: element}
else:
output[key] = element
return output


@dataclasses.dataclass(kw_only=True, frozen=True, eq=True)
class ElementWiseTransform(_ElementWise, grain.MapTransform):
"""Base class for elementwise transforms."""
Expand Down