def select_groups(labels, groups_order_subset='all'): adata_obs_key = labels groups_order = labels.cat.categories groups_masks = cp.zeros( (len(labels.cat.categories), len(labels.cat.codes)), dtype=bool) for iname, name in enumerate(labels.cat.categories.to_pandas()): # if the name is not found, fallback to index retrieval if labels.cat.categories[iname] in labels.cat.codes: mask = labels.cat.categories[iname] == labels.cat.codes else: mask = iname == labels.cat.codes groups_masks[iname] = mask.values groups_ids = list(range(len(groups_order))) if groups_order_subset != 'all': groups_ids = [] for name in groups_order_subset: groups_ids.append( cp.where( cp.array(labels.cat.categories.to_array().astype("int32")) == int(name))[0][0]) if len(groups_ids) == 0: # fallback to index retrieval groups_ids = cp.where( cp.in1d( cp.arange(len(labels.cat.categories)).astype(str), cp.array(groups_order_subset), ))[0] groups_ids = [groups_id.item() for groups_id in groups_ids] groups_masks = groups_masks[groups_ids] groups_order_subset = labels.cat.categories[groups_ids].to_array( ).astype(int) else: groups_order_subset = groups_order.to_array() return groups_order_subset, groups_masks
def ECP(error_weight, orig_weight, set_map): orig_shape = error_weight.shape error_weight, orig_weight = cp.ravel(error_weight.view( cp.uint32)), cp.ravel(orig_weight.view(cp.uint32)) shape = (int(error_weight.__len__() / 16), 16) # Reshape 64 bit in one row error_weight, orig_weight = cp.reshape(error_weight, shape), cp.reshape( orig_weight, shape) # Calculate stuck bits stuck_bits = cp.bitwise_xor(error_weight, orig_weight) stuck_bits_sum = cp.sum(stuck_bits, axis=1) error = cp.concatenate(cp.in1d(stuck_bits_sum, set_map).nonzero()) if error.__len__() == 0: return cp.reshape(error_weight, orig_shape).view(cp.float32) else: error_weight[error, :] = orig_weight[error, :] return cp.reshape(error_weight, orig_shape).view(cp.float32)
def auc(vector_predict, vector_true, gpu=False): if gpu: vector_predict = cp.array(vector_predict) vector_true = cp.array(vector_true) pos_indexes = cp.where(vector_true == 1)[0] sort_indexes = cp.argsort(vector_predict) rank = cp.nonzero(cp.in1d(sort_indexes, pos_indexes))[0] return (cp.sum(rank) - len(pos_indexes) * (len(pos_indexes) + 1) / 2) / ( len(pos_indexes) * (len(vector_predict) - len(pos_indexes))) else: pos_indexes = np.where(vector_true == 1)[0] sort_indexes = np.argsort(vector_predict) rank = np.nonzero(np.in1d(sort_indexes, pos_indexes))[0] return (np.sum(rank) - len(pos_indexes) * (len(pos_indexes) + 1) / 2) / ( len(pos_indexes) * (len(vector_predict) - len(pos_indexes)))
def _insert_zeros(ary, zero_indices): """ Create a new array of len(ary + zero_indices) where zero_indices indicates indexes of 0s in the new array. Ary is used to fill the rest. Examples -------- _insert_zeros([1, 2, 3], [1, 3]) => [1, 0, 2, 0, 3] """ if len(zero_indices) == 0: return ary.values new_ary = cp.zeros((len(ary) + len(zero_indices)), dtype=cp.int32) # getting mask of non-zeros data_mask = ~cp.in1d(cp.arange(0, len(new_ary), dtype=cp.int32), zero_indices) new_ary[data_mask] = ary return new_ary