Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] quantile_classify() @-> heatmap.py #1814

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/api/src/core/heatmap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from math import exp
from time import time

import numpy as np
from numba import njit
Expand Down Expand Up @@ -140,3 +141,43 @@ 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


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 + 1):
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)