-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlutrange.py
More file actions
67 lines (49 loc) · 1.81 KB
/
lutrange.py
File metadata and controls
67 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
"""
LUT range
---------
The :mod:`plotpy.lutrange` module provides functions to compute and manipulate
the LUT range of plot items.
.. autofunction:: hist_range_threshold
.. autofunction:: lut_range_threshold
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
if TYPE_CHECKING:
from plotpy.items import BaseImageItem, Histogram2DItem
from plotpy.items.histogram import HistDataSource
def hist_range_threshold(
hist: np.ndarray, bin_edges: np.ndarray, percent: float
) -> tuple[float, float]:
"""Return the range corresponding to the given percent of the histogram
Args:
hist (numpy.ndarray): The histogram
bin_edges (numpy.ndarray): The bin edges
percent (float): The percent of the histogram
Returns:
tuple[float, float]: The range
"""
hist = np.concatenate((hist, [0]))
hist = hist[1:]
bin_edges = bin_edges[1:]
threshold = 0.5 * percent / 100 * hist.sum()
i_bin_min = np.cumsum(hist).searchsorted(threshold)
i_bin_max = -1 - np.cumsum(np.flipud(hist)).searchsorted(threshold)
return bin_edges[i_bin_min], bin_edges[i_bin_max]
def lut_range_threshold(
item: BaseImageItem | Histogram2DItem | HistDataSource, bins: int, percent: float
) -> tuple[float, float]:
"""Return the lut range corresponding to the given percent of the histogram
Args:
item (BaseImageItem | Histogram2DItem | HistDataSource): The plot item
bins (int): The number of bins
percent (float): The percent of the histogram
Returns:
tuple[float, float]: The lut range
"""
hist, bin_edges = item.get_histogram(bins)
return hist_range_threshold(hist, bin_edges, percent)