| 1 |
# cython: language_level=3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Licensed under the terms of the BSD 3-Clause |
| 5 |
# (see plotpy/__init__.py for details) |
| 6 |
|
| 7 |
"""2D-Histogram algorithm""" |
| 8 |
|
| 9 |
cimport cython |
| 10 |
cimport numpy as cnp |
| 11 |
from libc.math cimport log |
| 12 |
|
| 13 |
cnp.import_array() |
| 14 |
|
| 15 |
cdef inline double double_max(double a, double b): return a if a >= b else b |
| 16 |
cdef inline double double_min(double a, double b): return a if a <= b else b |
| 17 |
|
| 18 |
@cython.profile(False) |
| 19 |
@cython.boundscheck(False) |
| 20 |
def histogram2d(cnp.ndarray[double, ndim=1] X, cnp.ndarray[double, ndim=1] Y, |
| 21 |
double i0, double i1, double j0, double j1, |
| 22 |
cnp.ndarray[double, ndim=2] data, logscale): |
| 23 |
"""Compute 2-D Histogram from data X, Y""" |
| 24 |
cdef double cx, cy, nmax, ix, iy |
| 25 |
cdef unsigned int i |
| 26 |
cdef Py_ssize_t n = X.shape[0] |
| 27 |
cdef Py_ssize_t nx = data.shape[1] |
| 28 |
cdef Py_ssize_t ny = data.shape[0] |
| 29 |
|
| 30 |
cx = nx/(i1-i0) |
| 31 |
cy = ny/(j1-j0) |
| 32 |
|
| 33 |
for i in range(n): |
| 34 |
# Centered bins => - .5 |
| 35 |
ix = (X[i] - i0) * cx - .5 |
| 36 |
iy = (Y[i] - j0) * cy - .5 |
| 37 |
if ix >= 0 and ix <= nx-1 and iy >= 0 and iy <= ny-1: |
| 38 |
data[<int> iy, <int> ix] += 1 |
| 39 |
|
| 40 |
nmax = 0. |
| 41 |
if logscale: |
| 42 |
for j in range(ny): |
| 43 |
for i in range(nx): |
| 44 |
data[j, i] = log(1+data[j, i]) |
| 45 |
nmax = double_max(nmax, data[j, i]) |
| 46 |
else: |
| 47 |
for j in range(ny): |
| 48 |
for i in range(nx): |
| 49 |
nmax = double_max(nmax, data[j, i]) |
| 50 |
return nmax |
| 51 |
|
| 52 |
@cython.profile(False) |
| 53 |
@cython.boundscheck(False) |
| 54 |
def histogram2d_func(cnp.ndarray[double, ndim=1] X, |
| 55 |
cnp.ndarray[double, ndim=1] Y, |
| 56 |
cnp.ndarray[double, ndim=1] Z, |
| 57 |
double i0, double i1, double j0, double j1, |
| 58 |
cnp.ndarray[double, ndim=2] data_tmp, |
| 59 |
cnp.ndarray[double, ndim=2] data, int computation): |
| 60 |
"""Compute 2-D Histogram from data X, Y""" |
| 61 |
cdef double cx, cy, nmax, ix, iy |
| 62 |
cdef unsigned int i, u, v |
| 63 |
cdef Py_ssize_t n = X.shape[0] |
| 64 |
cdef Py_ssize_t nx = data.shape[1] |
| 65 |
cdef Py_ssize_t ny = data.shape[0] |
| 66 |
|
| 67 |
cx = nx/(i1-i0) |
| 68 |
cy = ny/(j1-j0) |
| 69 |
|
| 70 |
for i in range(n): |
| 71 |
# Centered bins => - .5 |
| 72 |
ix = (X[i] - i0) * cx - .5 |
| 73 |
iy = (Y[i] - j0) * cy - .5 |
| 74 |
if ix >= 0 and ix <= nx-1 and iy >= 0 and iy <= ny-1: |
| 75 |
u, v = <int> iy, <int> ix |
| 76 |
if computation == 0: # max |
| 77 |
data_tmp[u, v] += 1 |
| 78 |
data[u, v] = double_max(data[u, v], Z[i]) |
| 79 |
elif computation == 1: # min |
| 80 |
data_tmp[u, v] += 1 |
| 81 |
data[u, v] = double_min(data[u, v], Z[i]) |
| 82 |
elif computation == 2: # sum |
| 83 |
data_tmp[u, v] += 1 |
| 84 |
data[u, v] += Z[i] |
| 85 |
elif computation == 3: # prod |
| 86 |
data_tmp[u, v] += 1 |
| 87 |
data[u, v] *= Z[i] |
| 88 |
elif computation == 4: # avg |
| 89 |
data_tmp[u, v] += 1 |
| 90 |
data[u, v] += (Z[i]-data[u, v])/data_tmp[u, v] |
| 91 |
elif computation == 5: # argmin |
| 92 |
if data[u, v] > Z[i]: |
| 93 |
data_tmp[u, v] = i |
| 94 |
data[u, v] = Z[i] |
| 95 |
elif computation == 6: # argmax |
| 96 |
if data[u, v] < Z[i]: |
| 97 |
data_tmp[u, v] = i |
| 98 |
data[u, v] = Z[i] |