diff --git a/src/wela/sorting.py b/src/wela/sorting.py new file mode 100644 index 0000000000000000000000000000000000000000..31616c1dcee42f32e932a7fe7dca7a0a729d9a7c --- /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