Files
cellxgene/server/data_cxg/cxg_util.py
bmccandless d0577b94af Return an empty matrix if no rows or columns are selected (#1501)
Return an empty matrix if no rows or columns are selected

Fixes #1499
2020-05-29 14:47:46 -07:00

38 lines
970 B
Python

import numpy as np
def pack_selector_from_mask(boolarray):
"""
pack all contiguous selectors into slices. Remember that
tiledb multi_index requires INCLUSIVE indices.
"""
if boolarray is None:
return slice(None)
assert type(boolarray) == np.ndarray
assert boolarray.dtype == bool
selector = np.nonzero(boolarray)[0]
return pack_selector_from_indices(selector)
def pack_selector_from_indices(selector):
if len(selector) == 0:
return None
result = []
current = slice(selector[0], selector[0])
for sel in selector[1:]:
if sel == current.stop + 1:
current = slice(current.start, sel)
else:
result.append(current if current.start != current.stop else current.start)
current = slice(sel, sel)
if len(result) == 0 or result[-1] != current:
result.append(current if current.start != current.stop else current.start)
return result