-
Notifications
You must be signed in to change notification settings - Fork 15
/
bin_utils.py
181 lines (149 loc) · 4.43 KB
/
bin_utils.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Methods for investigation and manipulation of bin arrays."""
from __future__ import absolute_import
import numpy as np
def make_bin_array(bins):
"""Turn bin data into array understood by HistogramXX classes.
Parameters
----------
bins: array_like
Array of edges or array of edge tuples
Returns
-------
numpy.ndarray
Examples
--------
>>> make_bin_array([0, 1, 2])
array([[0, 1],
[1, 2]])
>>> make_bin_array([[0, 1], [2, 3]])
array([[0, 1],
[2, 3]])
"""
bins = np.asarray(bins)
if bins.ndim == 1:
# if bins.shape[0] == 0:
# raise RuntimeError("Needs at least one bin")
return np.hstack((bins[:-1, np.newaxis], bins[1:, np.newaxis]))
elif bins.ndim == 2:
if bins.shape[1] != 2:
raise RuntimeError("Binning schema with ndim==2 must have 2 columns")
# if bins.shape[0] == 0:
# raise RuntimeError("Needs at least one bin")
return bins # Already correct, just pass
else:
raise RuntimeError("Binning schema must have ndim==1 or ndim==2")
def to_numpy_bins(bins):
"""Convert physt bin format to numpy edges.
Parameters
----------
bins: array_like
1-D (n) or 2-D (n, 2) array of edges
Returns
-------
edges: np.ndarray
all edges
"""
bins = np.asarray(bins)
if bins.ndim == 1: # Already in the proper format
return bins
if not is_consecutive(bins):
raise RuntimeError("Cannot create numpy bins from inconsecutive edges")
return np.concatenate([bins[:1, 0], bins[:, 1]])
def to_numpy_bins_with_mask(bins):
"""Numpy binning edges including gaps.
Parameters
----------
bins: array_like
1-D (n) or 2-D (n, 2) array of edges
Returns
-------
edges: np.ndarray
all edges
mask: np.ndarray
List of indices that correspond to bins that have to be included
Examples
--------
>>> to_numpy_bins_with_mask([0, 1, 2])
(array([0., 1., 2.]), array([0, 1]))
>>> to_numpy_bins_with_mask([[0, 1], [2, 3]])
(array([0, 1, 2, 3]), array([0, 2])
"""
bins = np.asarray(bins)
if bins.ndim == 1:
edges = bins
if bins.shape[0] > 1:
mask = np.arange(bins.shape[0] - 1)
else:
mask = []
elif bins.ndim == 2:
edges = []
mask = []
j = 0
if bins.shape[0] > 0:
edges.append(bins[0, 0])
for i in range(bins.shape[0] - 1):
mask.append(j)
edges.append(bins[i, 1])
if bins[i, 1] != bins[i+1, 0]:
edges.append(bins[i+1, 0])
j += 1
j += 1
mask.append(j)
edges.append(bins[-1, 1])
else:
raise RuntimeError("to_numpy_bins_with_mask: array with dim=1 or 2 expected")
if not np.all(np.diff(edges) > 0):
raise RuntimeError("to_numpy_bins_with_mask: edges array not monotone.")
return edges, mask
def is_rising(bins):
"""Check whether the bins are in raising order.
Does not check if the bins are consecutive.
Parameters
----------
bins: array_like
Returns
-------
bool
"""
# TODO: Optimize for numpy bins
bins = make_bin_array(bins)
if np.any(bins[:, 0] >= bins[:, 1]):
return False
if np.any(bins[1:, 0] < bins[:-1, 1]):
return False
return True
def is_consecutive(bins, rtol=1.e-5, atol=1.e-8):
"""Check whether the bins are consecutive (edges match).
Does not check if the bins are in rising order.
Returns
-------
bool
"""
bins = np.asarray(bins)
if bins.ndim == 1:
return True
else:
bins = make_bin_array(bins)
return np.allclose(bins[1:, 0], bins[:-1, 1], rtol, atol)
def is_bin_subset(sub, sup):
"""Check whether all bins in one binning are present also in another:
Parameters
----------
sub: array_like
Candidate for the bin subset
sup: array_like
Candidate for the bin superset
Returns
-------
bool
"""
sub = make_bin_array(sub)
sup = make_bin_array(sup)
for row in sub:
if not (row == sup).all(axis=1).any():
# TODO: Enable also approximate equality
return False
return True
def is_bin_superset(sup, sub):
"""Inverse of is_bin_subset"""
return is_bin_subset(sub=sub, sup=sup)