1# ---------------------------------------------------------------------------- 2# Copyright (c) 2013--, scikit-bio development team. 3# 4# Distributed under the terms of the Modified BSD License. 5# 6# The full license is in the file COPYING.txt, distributed with this software. 7# ---------------------------------------------------------------------------- 8 9import numpy as np 10cimport numpy as cnp 11 12 13def _subsample_counts_without_replacement( 14 cnp.ndarray[cnp.int64_t, ndim=1] counts, n, counts_sum): 15 cdef: 16 cnp.ndarray[cnp.int64_t, ndim=1] result, permuted, unpacked 17 cnp.int64_t cnt 18 Py_ssize_t unpacked_idx, i, j 19 20 unpacked = np.empty(counts_sum, dtype=int) 21 unpacked_idx = 0 22 for i in range(counts.shape[0]): 23 cnt = counts[i] 24 for j in range(cnt): 25 unpacked[unpacked_idx] = i 26 unpacked_idx += 1 27 28 permuted = np.random.permutation(unpacked)[:n] 29 30 result = np.zeros_like(counts) 31 for idx in range(permuted.shape[0]): 32 result[permuted[idx]] += 1 33 34 return result 35