Skip to content

transition_slices_from_group #198

Description

@chaburkland

Using internal ArrayKit utilities, and avoiding the usage of Python objects where possible, lets implement transition_slices_from_group as CPythhon function in ArrayKit.

def _slices_from_transitions(
    transitions: tp.Iterator[int], size: int
) -> tp.Iterator[slice]:
    start = 0
    for t in transitions:
        yield slice(start, t)
        start = t

    if start < size:
        yield slice(start, None)


def transition_slices_from_group(group: np.ndarray) -> tuple[tp.Iterator[slice], bool]:
    if group.ndim == 2:
        group_to_tuple = True
        if group.dtype == DTYPE_OBJECT:
            # NOTE: cannot get view of object; use string
            consolidated = view_2d_as_1d(group.astype(str))
        else:
            consolidated = view_2d_as_1d(group)
        transitions = nonzero_1d(consolidated != roll_1d(consolidated, 1))[1:]
    else:
        group_to_tuple = False
        transitions = nonzero_1d(group != roll_1d(group, 1))[1:]

    return _slices_from_transitions(transitions, len(group)), group_to_tuple  # type: ignor

See comment: static-frame/static-frame#1078 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions