From 3666f079462980e3ac91b8eb40025c41f46278d2 Mon Sep 17 00:00:00 2001 From: Mohammad Etemaddar Date: Tue, 3 Jan 2023 22:35:21 +0100 Subject: [PATCH 1/3] [ADD] quantile_classify() @-> heatmap.py --- app/api/src/core/heatmap.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/api/src/core/heatmap.py b/app/api/src/core/heatmap.py index d35b46ef9..8a8a575aa 100644 --- a/app/api/src/core/heatmap.py +++ b/app/api/src/core/heatmap.py @@ -140,3 +140,20 @@ def modified_gaussian_per_grid(sorted_table, unique, sensitivity, cutoff): else: modified_gaussian_per_grids[i] = sum return modified_gaussian_per_grids + + +def quantile_classify(a, NQ=5): + q = np.arange(1 / NQ, 1, 1 / NQ) + quantiles = np.quantile(a[a > 0], q) + out = np.empty(a.size, np.int8) + out[np.where(a == 0)] = 0 + out[np.where(np.logical_and(np.greater(a, 0), np.less(a, quantiles[0])))] = 1 + out[np.where(a >= quantiles[-1])] = NQ + for i in range(NQ - 2): + out[ + np.where( + np.logical_and(np.greater_equal(a, quantiles[i]), np.less(a, quantiles[i + 1])) + ) + ] = (i + 2) + + return out From 5ec40ccaeab9582f7acdcb01966ee670d761a64b Mon Sep 17 00:00:00 2001 From: Mohammad Etemaddar Date: Tue, 3 Jan 2023 22:49:35 +0100 Subject: [PATCH 2/3] [ADD] Example (test) for quantile_classify --- app/api/src/core/heatmap.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/api/src/core/heatmap.py b/app/api/src/core/heatmap.py index 8a8a575aa..ce54283d8 100644 --- a/app/api/src/core/heatmap.py +++ b/app/api/src/core/heatmap.py @@ -1,4 +1,5 @@ from math import exp +from time import time import numpy as np from numba import njit @@ -157,3 +158,26 @@ def quantile_classify(a, NQ=5): ] = (i + 2) return out + + +def test_quantile(n): + NQ = 5 + a = np.random.random(n) * 120 + a[a < 10] = 0 + + start_time = time() + out = quantile_classify(a, 5) + end_time = time() + print(f"quantile for {a.size} elements is: {int((end_time-start_time)*1000)} ms") + if n <= 100: + print("Example of output:") + print(out) + else: + for i in range(NQ): + print(f"count {i}: {np.where(out==i)[0].size}") + # print(i,np.where(out==i)[0].size) + + +if __name__ == "__main__": + test_quantile(10000) + test_quantile(20) From bea9b560140aa4ee403b0f6ef5d1c86ff227f51b Mon Sep 17 00:00:00 2001 From: Mohammad Etemaddar Date: Tue, 3 Jan 2023 22:56:14 +0100 Subject: [PATCH 3/3] [FIX] Example for quantile_classify() --- app/api/src/core/heatmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/src/core/heatmap.py b/app/api/src/core/heatmap.py index ce54283d8..21f4acc73 100644 --- a/app/api/src/core/heatmap.py +++ b/app/api/src/core/heatmap.py @@ -173,7 +173,7 @@ def test_quantile(n): print("Example of output:") print(out) else: - for i in range(NQ): + for i in range(NQ + 1): print(f"count {i}: {np.where(out==i)[0].size}") # print(i,np.where(out==i)[0].size)