From: Thomas Fillon Date: Thu, 19 Jun 2014 13:51:25 +0000 (+0200) Subject: feat(analyzer/utils.py): add a function to evaluate the next power of two X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=9088f56831e096738be5e41737eb0ed2ddd72774;p=timeside-diadems.git feat(analyzer/utils.py): add a function to evaluate the next power of two --- diff --git a/timeside/analyzer/utils.py b/timeside/analyzer/utils.py index 497126d..b8ad23c 100644 --- a/timeside/analyzer/utils.py +++ b/timeside/analyzer/utils.py @@ -223,3 +223,15 @@ def entropy(serie, nbins=10, base=np.exp(1), approach='unbiased'): nbias = nbias / np.log(base) sigma = sigma / np.log(base) return estimate + + +def nextpow2(value): + """Compute the nearest power of two greater or equal to the input value""" + if value >= 1: + return 2**np.ceil(np.log2(value)).astype(int) + elif value > 0: + return 1 + elif value == 0: + return 0 + else: + raise ValueError('Value must be positive')