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

More visualization tools #84

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 21 additions & 1 deletion flox/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def visualize_groups_1d(array, labels, axis=-1, colors=None, cmap=None):
assert labels.ndim == 1
factorized, unique_labels = pd.factorize(labels)
assert np.array(labels).ndim == 1
assert len(labels) == array.shape[axis]
chunks = array.chunks[axis]

if colors is None:
Expand All @@ -96,7 +97,7 @@ def visualize_groups_1d(array, labels, axis=-1, colors=None, cmap=None):
plt.figure()
i0 = 0
for i in chunks:
lab = labels[i0 : i0 + i]
lab = factorized[i0 : i0 + i]
col = [colors[label] for label in lab] + [(1, 1, 1)]
draw_mesh(
1,
Expand Down Expand Up @@ -170,3 +171,22 @@ def visualize_cohorts_2d(by, array, method="cohorts"):
ax[1].set_title(f"{len(before_merged)} cohorts")
ax[2].set_title(f"{len(merged)} merged cohorts")
f.set_size_inches((6, 6))


def visualize_groups_1d_long(array, labels, axis):
labels = np.asarray(labels)
assert labels.ndim == 1
factorized, unique_labels = pd.factorize(labels)
assert np.array(labels).ndim == 1
assert len(labels) == array.shape[axis]
chunks = array.chunks[axis]

idx = np.concatenate([np.concatenate([np.ones((c,)), np.array([np.nan])]) for c in chunks])[:-1]
labels_ = labels[np.nancumsum(idx).astype(int) - 1].astype(float)
labels_[np.isnan(idx)] = np.nan
ncol = 5 * (20 + 1)
extra = ncol - len(idx) % ncol
idx2d = np.pad(labels_, (0, extra), constant_values=np.nan).reshape(-1, ncol)

plt.figure()
plt.pcolormesh(idx2d, cmap=mpl.cm.Reds)