forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bin_grid.py
204 lines (169 loc) · 5.92 KB
/
test_bin_grid.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
####################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
####################################################################################################
import platform
import numpy as np
import pytest
import PyPartMC as ppmc
class TestBinGrid:
@staticmethod
def test_ctor():
# arrange
pass
# act
sut = ppmc.BinGrid(123, "log", 1, 100)
# assert
assert sut is not None
@staticmethod
def test_len():
# arrange
grid_size = 666
sut = ppmc.BinGrid(grid_size, "log", 1, 100)
# act
size = len(sut)
# assert
assert size == grid_size
@staticmethod
def test_bin_edges_len():
# arrange
grid_size = 100
sut = ppmc.BinGrid(grid_size, "log", 1, 100)
# act
edges = sut.edges
# assert
assert grid_size + 1 == len(edges)
@staticmethod
def test_bin_edges_values():
# arrange
n_bins = 10
left_edge = 1
right_edge = 100
sut = ppmc.BinGrid(n_bins, "log", left_edge, right_edge)
# act
edges = sut.edges
# assert
np.testing.assert_array_almost_equal(
np.logspace(np.log10(left_edge), np.log10(right_edge), n_bins + 1), edges
)
@staticmethod
def test_bin_centers_len():
# arrange
grid_size = 44
sut = ppmc.BinGrid(grid_size, "log", 1, 100)
# act
centers = sut.centers
# assert
assert grid_size == len(centers)
@staticmethod
def test_bin_centers_values():
# arrange
n_bins = 10
left_edge = 1
right_edge = 100
sut = ppmc.BinGrid(n_bins, "log", left_edge, right_edge)
# act
centers = sut.centers
# assert
np.testing.assert_array_almost_equal(
np.logspace(np.log10(left_edge), np.log10(right_edge), 2 * n_bins + 1)[
1:-1:2
],
centers,
)
@staticmethod
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_invalid_grid():
grid_size = 100
try:
_ = ppmc.BinGrid(grid_size, "X", 1, 100)
except ValueError as error:
assert str(error) == "Invalid grid spacing."
@staticmethod
def test_histogram_1d():
# arrange
n_data = 1000
grid = ppmc.BinGrid(100, "linear", 0, 1000)
vals = np.random.random(n_data) * 1000
weights = np.ones(n_data)
hist, bin_edges = np.histogram(vals, bins=grid.edges)
# act
data = ppmc.histogram_1d(grid, vals, weights)
# assert
np.testing.assert_array_almost_equal(data, hist / (bin_edges[1] - bin_edges[0]))
@staticmethod
def test_histogram_2d_linear_linear():
# arrange
n_data = 1000
x_grid = ppmc.BinGrid(15, "linear", 0, 1000)
y_grid = ppmc.BinGrid(12, "linear", 0, 500)
x_vals = np.random.random(n_data) * 1000
y_vals = np.random.random(n_data) * 500
weights = np.random.random(n_data)
data_numpy, bin_edges_x, bin_edges_y = np.histogram2d(
x_vals, y_vals, bins=[x_grid.edges, y_grid.edges], weights=weights
)
cell_size = (bin_edges_x[1] - bin_edges_x[0]) * (
bin_edges_y[1] - bin_edges_y[0]
)
# act
data = ppmc.histogram_2d(x_grid, x_vals, y_grid, y_vals, weights)
# assert
np.testing.assert_array_almost_equal(
np.array(data), data_numpy / cell_size, decimal=15
)
@staticmethod
def test_histogram_2d_linear_log():
# arrange
n_data = 100
y_data_min = 0.1
y_data_max = 10.0
x_grid = ppmc.BinGrid(15, "linear", 0, 1000)
y_grid = ppmc.BinGrid(12, "log", y_data_min, y_data_max)
x_vals = np.random.random(n_data) * 1000
y_vals = y_data_min * 10 ** (
np.log10(y_data_max / y_data_min) * np.random.random(n_data)
)
weights = np.random.random(n_data)
data_numpy, bin_edges_x, bin_edges_y = np.histogram2d(
x_vals, y_vals, bins=[x_grid.edges, y_grid.edges], weights=weights
)
cell_size = (bin_edges_x[1] - bin_edges_x[0]) * np.log(
bin_edges_y[1] / bin_edges_y[0]
)
# act
data = ppmc.histogram_2d(x_grid, x_vals, y_grid, y_vals, weights)
# assert
np.testing.assert_array_almost_equal(
np.array(data), data_numpy / cell_size, decimal=15
)
@staticmethod
def test_histogram_2d_log_log():
# arrange
n_data = 100
x_data_min = 1
x_data_max = 1000
y_data_min = 0.1
y_data_max = 10.0
x_grid = ppmc.BinGrid(15, "log", x_data_min, x_data_max)
y_grid = ppmc.BinGrid(12, "log", y_data_min, y_data_max)
x_vals = x_data_min * 10 ** (
np.log10(x_data_max / x_data_min) * np.random.random(n_data)
)
y_vals = y_data_min * 10 ** (
np.log10(y_data_max / y_data_min) * np.random.random(n_data)
)
weights = np.random.random(n_data)
data_numpy, bin_edges_x, bin_edges_y = np.histogram2d(
x_vals, y_vals, bins=[x_grid.edges, y_grid.edges], weights=weights
)
cell_size = np.log(bin_edges_x[1] / bin_edges_x[0]) * np.log(
bin_edges_y[1] / bin_edges_y[0]
)
# act
data = ppmc.histogram_2d(x_grid, x_vals, y_grid, y_vals, weights)
# assert
np.testing.assert_array_almost_equal(
np.array(data), data_numpy / cell_size, decimal=13
)