-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_histogram2d.py
237 lines (199 loc) · 7.68 KB
/
test_histogram2d.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import sys
import os
sys.path = [os.path.join(os.path.dirname(__file__), "..")] + sys.path
import physt
from physt import histogram_nd, h2, binnings
from physt.histogram_nd import Histogram2D
import numpy as np
import pytest
vals = [
[0.1, 2.0],
[-0.1, 0.7],
[0.2, 1.5],
[0.2, -1.5],
[0.2, 1.47],
[1.2, 1.23],
[0.7, 0.5]
]
np.random.seed(42)
class TestCalculateFrequencies:
def test_simple(self):
bins = [[0, 1, 2], [0, 1, 2]]
schemas = [binnings.static_binning(None, np.asarray(bs)) for bs in bins]
frequencies, errors2, missing = histogram_nd.calculate_frequencies(vals, ndim=2, binnings=schemas)
assert np.array_equal([[1, 3], [0, 1]], frequencies)
assert missing == 2
assert np.array_equal(errors2, frequencies)
def test_gap(self):
bins = [
[[-1, 0], [1, 2]],
[[-2, -1], [1, 2]]
]
schemas = [binnings.static_binning(None, np.asarray(bs)) for bs in bins]
frequencies, errors2, missing = histogram_nd.calculate_frequencies(vals, ndim=2, binnings=schemas)
assert np.array_equal([[0, 0], [0, 1]], frequencies)
assert missing == 6
assert np.array_equal(errors2, frequencies)
def test_errors(self):
bins = [
[[-1, 0], [1, 2]],
[[-2, -1], [1, 2]]
]
weights = [2, 1, 1, 1, 1, 2, 1]
schemas = [binnings.static_binning(None, np.asarray(bs)) for bs in bins]
frequencies, errors2, missing = histogram_nd.calculate_frequencies(vals, ndim=2, binnings=schemas, weights=weights)
# frequencies, errors2, missing = histogram_nd.calculate_frequencies(vals, ndim=2, bins=bins, weights=weights)
assert np.array_equal([[0, 0], [0, 2]], frequencies)
assert missing == 7
assert np.array_equal([[0, 0], [0, 4]], errors2)
class TestHistogram2D:
def test_simple_random(self):
x = np.random.normal(100, 1, 1000)
y = np.random.normal(10, 10, 1000)
h2 = physt.h2(x, y, [8, 4], name="Some histogram", axis_names=["x", "y"])
assert h2.frequencies.sum() == 1000
assert h2.shape == (8, 4)
assert h2.name == "Some histogram"
assert h2.axis_names == ("x", "y")
def test_dropna(self):
vals2 = np.array(vals)
vals2[0, 1] = np.nan
with pytest.raises(RuntimeError):
hist = physt.h2(vals2[:,0], vals2[:,1], dropna=False)
hist = physt.h2(vals2[:, 0], vals2[:, 1])
assert hist.frequencies.sum() == 6
# Calculated:
freqs = np.array([[ 1., 0.],
[ 1., 0.],
[ 1., 1.],
[ 1., 0.],
[ 1., 0.]])
class TestArithmetics:
def test_multiply_by_constant(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
assert np.array_equal(h.frequencies, freqs)
i = h * 2
assert np.array_equal(i.frequencies, freqs * 2)
assert np.array_equal(i.errors2, freqs * 4)
i = h * 0.5
assert np.array_equal(i.frequencies, freqs * 0.5)
assert np.array_equal(i.errors2, freqs * 0.25)
def test_multiply_by_other(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
with pytest.raises(RuntimeError):
h * h
def test_divide_by_other(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
with pytest.raises(RuntimeError):
h * h
def test_divide_by_constant(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
i = h / 2
assert np.array_equal(i.frequencies, freqs / 2)
assert np.array_equal(i.errors2, freqs / 4)
def test_addition_by_constant(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
with pytest.raises(RuntimeError):
h + 4
def test_addition_with_another(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
i = h + h
assert np.array_equal(i.frequencies, freqs * 2)
assert np.array_equal(i.errors2, freqs * 2)
def test_addition_with_adaptive(self):
ha = h2([1], [11], "fixed_width", 10, adaptive=True)
hb = h2([10], [5], "fixed_width", 10, adaptive=True)
hha = ha + hb
assert hha == hb + ha
assert hha.shape == (2, 2)
assert hha.total == 2
assert np.array_equal(hha.frequencies, [[0, 1], [1, 0]])
def test_subtraction_with_another(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
i = h * 2 - h
assert np.array_equal(i.frequencies, freqs)
assert np.array_equal(i.errors2, 5 * freqs)
def test_subtraction_by_constant(self):
xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2])
yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2])
h = physt.h2(xx, yy, "fixed_width", 1)
with pytest.raises(RuntimeError):
h - 4
class TestDtype:
def test_simple(self):
from physt import examples
assert examples.normal_h2().dtype == np.dtype(np.int64)
class TestMerging:
def test_2(self):
data1 = np.random.rand(100)
data2 = np.random.rand(100)
hh = h2(data1, data2, 120)
hha = h2(data1, data2, 60)
hhb = hh.merge_bins(2, inplace=False)
assert hha == hhb
class TestPartialNormalizing:
def test_wrong_arguments(self):
freqs = [
[1, 0],
[1, 2]
]
h = Histogram2D(binnings=(range(3), range(3)), frequencies=freqs)
with pytest.raises(ValueError):
h0 = h.partial_normalize(2)
with pytest.raises(ValueError):
h0 = h.partial_normalize(-2)
def test_axis_names(self):
freqs = [
[1, 0],
[1, 2]
]
h = Histogram2D(binnings=(range(3), range(3)), frequencies=freqs, axis_names=["first_axis", "second_axis"])
h1 = h.partial_normalize("second_axis")
assert np.allclose(h1.frequencies, [[1, 0], [.333333333333, .6666666666]])
with pytest.raises(ValueError):
h0 = h.partial_normalize("third_axis")
def test_inplace(self):
freqs = [
[1, 0],
[1, 2]
]
h = Histogram2D(binnings=(range(3), range(3)), frequencies=freqs)
h1 = h.partial_normalize(0, inplace=False)
assert np.allclose(h.frequencies, freqs)
assert not np.allclose(h1.frequencies, h.frequencies)
h.partial_normalize(0, inplace=True)
assert h1 == h
def test_values(self):
freqs = [
[1, 0],
[1, 2]
]
h = Histogram2D(binnings=(range(3), range(3)), frequencies=freqs)
h0 = h.partial_normalize(0)
h1 = h.partial_normalize(1)
assert np.allclose(h0.frequencies, [[.5, 0], [.5, 1.0]])
assert np.allclose(h1.frequencies, [[1, 0], [.333333333333, .6666666666]])
def test_with_zeros(self):
freqs = [
[0, 0],
[0, 2]
]
h = Histogram2D(binnings=(range(3), range(3)), frequencies=freqs)
h1 = h.partial_normalize(1)
assert np.allclose(h1.frequencies, [[0, 0], [0, 1.0]])
if __name__ == "__main__":
pytest.main(__file__)