Skip to content

Commit

Permalink
feat: implement basic get_row_reorder_df
Browse files Browse the repository at this point in the history
  • Loading branch information
machow committed Oct 26, 2023
1 parent f273bb9 commit 2233917
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gt/utils_render_common.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Tuple

if TYPE_CHECKING:
from ._gt_data import RowGroups, Stub


TupleStartFinal = Tuple[int, int]


def get_row_reorder_df(groups: RowGroups, stub_df: Stub) -> list[TupleStartFinal]:
if not len(groups):
indices = range(len(stub_df))

# TODO: is this used in indexing? If so, we may need to use
# ii + 1 for the final part?
return [(ii, ii) for ii in indices]

# where in the group each element is
groups_pos = [groups.index(row.group_id) for row in stub_df]
# the index that when used on the rows will sort them by the order in groups
start_pos = list(range(len(groups_pos)))
sort_indx = sorted(start_pos, key=lambda ii: groups_pos[ii])

return list(zip(start_pos, sort_indx))
19 changes: 19 additions & 0 deletions tests/test_utils_render_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from gt._gt_data import RowGroups, Stub, RowInfo
from gt.utils_render_common import get_row_reorder_df


def test_get_row_reorder_df_simple():
groups = RowGroups(["b", "a"])
stub = Stub([RowInfo(0, "a"), RowInfo(1, "b"), RowInfo(2, "a")])

start_end = get_row_reorder_df(groups, stub)

assert start_end == [(0, 1), (1, 0), (2, 2)]


def test_get_row_reorder_df_no_groups():
groups = RowGroups()
stub = Stub([RowInfo(0, "a"), RowInfo(1, "b")])

start_end = get_row_reorder_df(groups, stub)
assert start_end == [(0,0), (1,1)]

0 comments on commit 2233917

Please sign in to comment.