From 8178f935b1b23c572f8644674c087cdaf0ed55f0 Mon Sep 17 00:00:00 2001 From: pswain <peter.swain@ed.ac.uk> Date: Thu, 16 May 2024 22:03:11 +0100 Subject: [PATCH] added sorting2 --- src/wela/sorting.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/wela/sorting.py diff --git a/src/wela/sorting.py b/src/wela/sorting.py new file mode 100644 index 0000000..31616c1 --- /dev/null +++ b/src/wela/sorting.py @@ -0,0 +1,33 @@ +import numpy as np + + +def sort_by_budding(buddings, bud_number=0): + """Return indices for each cell when a particular budding occurs.""" + bud_indices = np.transpose(np.nonzero(buddings)) + sort_order = [] + for cell_i in range(buddings.shape[0]): + cell_buds = bud_indices[bud_indices[:, 0] == cell_i, :] + if bud_number < cell_buds.shape[0]: + sort_order.append(cell_buds[bud_number][1]) + else: + sort_order.append(np.nan) + sort_order = np.array(sort_order) + return sort_order + + +def sort_by_maximum(data, byfunction=None): + """Return indices of cells sorted by largest value.""" + sort_order = np.nan * np.ones(data.shape[0]) + sdata = data[not_all_nan(data), :] + if byfunction is not None: + sorted_indices = np.argsort(byfunction(sdata, axis=1)) + else: + sorted_indices = np.nanargmax(sdata, axis=1) + sort_order[not_all_nan(data)] = sorted_indices + return sort_order + + +def not_all_nan(data): + """Find rows that are not all NaN.""" + indices = ~np.all(np.isnan(data), axis=1) + return indices -- GitLab