-
Notifications
You must be signed in to change notification settings - Fork 2
/
prof_test.py
340 lines (275 loc) · 12.4 KB
/
prof_test.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Copyright (C) 2019 Paul King
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version (the "AGPL-3.0+").
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License and the additional terms for more
# details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ADDITIONAL TERMS are also included as allowed by Section 7 of the GNU
# Affero General Public License. These additional terms are Sections 1, 5,
# 6, 7, 8, and 9 from the Apache License, Version 2.0 (the "Apache-2.0")
# where all references to the definition "License" are instead defined to
# mean the AGPL-3.0+.
# You should have received a copy of the Apache-2.0 along with this
# program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
""" Test profile. """
import os
import numpy as np
import sys
from prof_funct import Profile
import profile_from
import cross_calibrate
# pylint: disable = E1102, C0111
PROFILER = [(-16.4, 0.22), (-16, 0.3), (-15.6, 0.28), (-15.2, 0.3),
(-14.8, 0.36), (-14.4, 0.38), (-14, 0.41), (-13.6, 0.45),
(-13.2, 0.47), (-12.8, 0.51), (-12.4, 0.55), (-12, 0.62),
(-11.6, 0.67), (-11.2, 0.74), (-10.8, 0.81), (-10.4, 0.91),
(-10, 0.97), (-9.6, 1.12), (-9.2, 1.24), (-8.8, 1.4),
(-8.4, 1.56), (-8, 1.75), (-7.6, 2.06), (-7.2, 2.31),
(-6.8, 2.56), (-6.4, 3.14), (-6, 3.83), (-5.6, 4.98),
(-5.2, 8.17), (-4.8, 40.6), (-4.4, 43.34), (-4, 44.17),
(-3.6, 44.44), (-3.2, 44.96), (-2.8, 44.18), (-2.4, 45.16),
(-2, 45.54), (-1.6, 45.07), (-1.2, 45.28), (-0.8, 45.27),
(-0.4, 44.57), (0, 45.23), (0.4, 45.19), (0.8, 45.18),
(1.2, 45.37), (1.6, 45.34), (2, 45.39), (2.4, 45.32),
(2.8, 45.25), (3.2, 44.84), (3.6, 44.76), (4, 44.23),
(4.4, 43.22), (4.8, 39.14), (5.2, 7.98), (5.6, 4.89),
(6, 3.71), (6.4, 3.11), (6.8, 2.59), (7.2, 2.27),
(7.6, 1.95), (8, 1.71), (8.4, 1.46), (8.8, 1.35),
(9.2, 1.18), (9.6, 1.11), (10, 0.93), (10.4, 0.87),
(10.8, 0.78), (11.2, 0.7), (11.6, 0.64), (12, 0.6),
(12.4, 0.54), (12.8, 0.49), (13.2, 0.47), (13.6, 0.43),
(14, 0.4), (14.4, 0.39), (14.8, 0.34), (15.2, 0.33),
(15.6, 0.32), (16, 0.3), (16.4, 0.3)]
WEDGED = [(-16.4, 0.27), (-16, 0.31), (-15.6, 0.29), (-15.2, 0.29),
(-14.8, 0.32), (-14.4, 0.33), (-14, 0.35), (-13.6, 0.38),
(-13.2, 0.4), (-12.8, 0.44), (-12.4, 0.46), (-12, 0.51),
(-11.6, 0.55), (-11.2, 0.6), (-10.8, 0.65), (-10.4, 0.7),
(-10, 0.74), (-9.6, 0.84), (-9.2, 0.94), (-8.8, 1.04), (-8.4, 1.14),
(-8, 1.25), (-7.6, 1.45), (-7.2, 1.6), (-6.8, 1.78), (-6.4, 2.14),
(-6, 2.66), (-5.6, 3.62), (-5.2, 6.54), (-4.8, 17.55), (-4.4, 20.07),
(-4, 21.37), (-3.6, 22.19), (-3.2, 23.1), (-2.8, 23.74), (-2.4, 24.56),
(-2, 25.49), (-1.6, 26.35), (-1.2, 27), (-0.8, 28.06), (-0.4, 28.89),
(0, 29.8), (0.4, 30.61), (0.8, 31.4), (1.2, 32.53), (1.6, 33.06),
(2, 34.15), (2.4, 34.85), (2.8, 35.65), (3.2, 36.6), (3.6, 37.04),
(4, 37.45), (4.4, 36.72), (4.8, 30.93), (5.2, 10.06), (5.6, 5.43),
(6, 3.71), (6.4, 3.01), (6.8, 2.52), (7.2, 2.19), (7.6, 1.9), (8, 1.7),
(8.4, 1.48), (8.8, 1.35), (9.2, 1.19), (9.6, 1.09), (10, 0.93),
(10.4, 0.89), (10.8, 0.78), (11.2, 0.72), (11.6, 0.65), (12, 0.6),
(12.4, 0.55), (12.8, 0.5), (13.2, 0.48), (13.6, 0.45), (14, 0.41),
(14.4, 0.4), (14.8, 0.35), (15.2, 0.33), (15.6, 0.32),
(16, 0.31), (16.4, 0.3)]
DATA_DIR = (os.path.abspath(os.path.join(os.path.dirname(__file__), 'data')))
assert os.path.isdir(DATA_DIR)
def test_init():
assert np.allclose(Profile(x=np.array([0]), y=np.array([0])).x, [0])
def test_interp():
assert Profile().interp is None
assert np.isclose(Profile(x=[0, 1], y=[0, 1]).interp(0.5), 0.5)
def test_magic_methods():
assert not Profile()
# __len__
assert len(profile_from.tuples(PROFILER)) == 83
# __eq__
assert Profile() == Profile()
assert Profile(x=[], y=[]) == Profile()
assert Profile(x=[0], y=[0]) != Profile()
# __copy__
original = Profile()
same = original
assert same == original
# __str__
empty_profile = Profile()
print(empty_profile)
profiler = profile_from.tuples(PROFILER)
assert profiler.__str__()
# __add__, __radd__, __iadd__
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.get_y(0),
(profiler+2).get_y(2))
# __sub__, __rsub__, __isub__
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.get_y(0),
(profiler-2).get_y(-2))
# __mul__, __rmul__, __imul__
profiler = profile_from.tuples(PROFILER)
assert np.isclose(4*sum(profiler.y), sum((4*profiler).y))
assert np.isclose(4*sum(profiler.y), sum((profiler*4).y))
ref = 4*sum(profiler.y)
profiler *= 4
assert np.isclose(sum(profiler.y), ref)
def test_from_lists():
empty = Profile()
also_empty = profile_from.lists([], [])
assert empty == also_empty
def test_fromtuples():
profiler = profile_from.tuples(PROFILER)
assert len(profiler.x) == len(PROFILER)
assert profiler.x[0] == PROFILER[0][0]
def test_from_pulse():
pulse = 4 * profile_from.pulse(0.0, 1, (-5, 5), 0.1)
assert np.isclose(sum(pulse.y), 40)
def test_from_snc_profiler():
file_name = os.path.join(DATA_DIR, '2018_12_03 clinac 10x10 open.prs')
x_profile = profile_from.snc_profiler(file_name, 'tvs')
y_profile = profile_from.snc_profiler(file_name, 'rad')
assert np.isclose(x_profile.get_y(0), 45.50562901780488)
assert np.isclose(y_profile.get_y(0), 45.50562901780488)
assert x_profile.meta['SSD'] == y_profile.meta['SSD']
def test_from_narrow_png():
file_name = os.path.join(DATA_DIR, 'film', '2017_12_04 FilmCalib_EBT_vert_strip.png')
png = profile_from.narrow_png(file_name)
assert np.isclose(png.get_y(0), 0.609074819347117)
def test_from_raystation_line():
file_name = os.path.join(DATA_DIR, '2018_02_08_raystation_line_dose.csv')
ray = profile_from.raystation_line(file_name)
assert np.isclose(min(ray.x), ray.x[0])
assert np.isclose(max(ray.x), ray.x[-1])
assert np.isclose(max(ray.y), 0.41)
def test_from_rfa_ascii():
file_name = os.path.join(DATA_DIR, '2018_02_01 RFA300 ASCII Measurement.asc')
rfa = profile_from.rfa_ascii(file_name)
assert len(rfa)==2
assert np.isclose(rfa[0].x[0], -11.2)
def test_from_pinnacle_ascii():
file_name = os.path.join(DATA_DIR, '2007_11_20 - Pinnacle ASCII 40x40.dat')
pinn = profile_from.pinnacle_ascii(file_name)
assert type(pinn) == list
assert len(pinn) == 5
assert np.isclose(pinn[0].x[0], -24)
def test_get_y():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.get_y(0), 45.23)
def test_get_x():
profiler = profile_from.tuples(PROFILER)
assert np.allclose(profiler.get_x(10), (-5.17742830712, 5.1740693196))
def test_get_increment():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.get_increment(), 0.4)
def test_slice_segment():
profiler = profile_from.tuples(PROFILER)
# NO POINTS
no_points = profiler.slice_segment(start=1, stop=0)
assert np.array_equal(no_points.x, [])
assert np.array_equal(no_points.y, [])
# ONE POINT
profiler = profile_from.tuples(PROFILER)
one_point = profiler.slice_segment(start=0, stop=0)
assert np.array_equal(one_point.x, [0])
assert np.array_equal(one_point.y, [45.23])
# ALL POINTS
profiler = profile_from.tuples(PROFILER)
all_points = profiler.slice_segment()
assert np.array_equal(all_points.x, profiler.x)
assert np.array_equal(all_points.y, profiler.y)
def test_resample_x():
profiler = profile_from.tuples(PROFILER, meta={'depth': 10})
assert profiler.meta['depth'] == 10
assert np.isclose(profiler.interp(0), profiler.resample_x(0.1).interp(0))
assert np.isclose(profiler.interp(6.372),
profiler.resample_x(0.1).interp(6.372))
resampled = profiler.resample_x(0.1)
increments = np.diff([i for i in resampled.x])
assert np.allclose(increments, 0.1)
assert np.isclose(resampled.y[0], profiler.y[0])
def test_resample_y():
profiler = profile_from.tuples(PROFILER)
assert len(profiler.resample_y(0.5)) > len(profiler.resample_y(1))
def test_make_normal_y():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.make_normal_y(x=0).get_y(0), 1.0)
def test_get_edges():
profiler = profile_from.tuples(PROFILER)
assert np.allclose(profiler.get_edges(), (-5.2, 4.8))
assert len(profiler) == len(PROFILER)
def test_make_normal_x():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.make_normal_x().x[0], -3.1538461538461533)
profiler = profile_from.tuples(PROFILER)
assert len(PROFILER) == len(profiler.make_normal_x().x)
def test_slice_umbra():
profiler = profile_from.tuples(PROFILER).resample_x(0.1)
profiler_length = len(profiler)
umbra = profiler.slice_umbra()
assert len(umbra) < profiler_length
def test_slice_penumbra():
profiler = profile_from.tuples(PROFILER).resample_x(0.1)
lt_penum, rt_penum = profiler.slice_penumbra()
assert np.all(lt_penum.x < 0)
assert np.all(rt_penum.x > 0)
assert np.all(lt_penum.y < profiler.get_y(0))
assert np.all(rt_penum.y < profiler.get_y(0))
def test_slice_shoulders():
profiler = profile_from.tuples(PROFILER).resample_x(0.1)
lt_should, rt_should = profiler.slice_shoulders()
assert np.all(lt_should.x < min(rt_should.x))
assert np.all(rt_should.x > max(lt_should.x))
def test_slice_tails():
profiler = profile_from.tuples(PROFILER).resample_x(0.1)
lt_tail, rt_tail = profiler.slice_tails()
assert np.all(lt_tail.x < min(rt_tail.x))
assert np.all(rt_tail.x > max(lt_tail.x))
def test_get_flatness():
profiler = profile_from.tuples(PROFILER)
profiler = profiler.resample_x(0.1)
assert np.isclose(profiler.get_flatness(), 0.03042644213284108)
def test_get_symmetry():
profiler = profile_from.tuples(PROFILER)
profiler = profiler.resample_x(0.1)
symmetry = profiler.get_symmetry()
assert np.isclose(symmetry, 0.024152376510553037)
def test_make_symmetric():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.make_symmetric().get_symmetry(), 0.0)
def test_make_centered():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(np.sum(profiler.make_centered().get_edges()), 0.0)
def test_make_flipped():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.get_y(3), profiler.make_flipped().get_y(-3))
def test_align_to():
profiler = profile_from.tuples(PROFILER)
assert np.isclose(profiler.align_to(profiler+(2)).x[0], profiler.x[0] + 2)
def test_cross_calibrate():
reference_file_name = os.path.join(DATA_DIR, 'film', '2017_12_04 FilmCalib.prs')
measured_file_name = os.path.join(DATA_DIR, 'film', '2017_12_04 FilmCalib_EBT_vert_strip.png')
cal_curve = cross_calibrate.cross_calibrate(reference_file_name, measured_file_name)
assert np.allclose(cal_curve([0.3, 0.5, 0.65]), [22, 135, 325], rtol=0.2)
### in order for this to work, the PNG image must be a "negative if RGB"
if __name__ == "__main__":
test_init()
test_interp()
test_magic_methods()
test_from_lists()
test_fromtuples()
test_from_pulse()
test_from_snc_profiler()
test_from_narrow_png()
test_from_raystation_line()
test_from_rfa_ascii()
test_from_pinnacle_ascii()
test_get_y()
test_get_x()
test_get_increment()
test_slice_segment()
test_resample_x()
test_resample_y()
test_make_normal_y()
test_get_edges()
test_make_normal_x()
test_slice_umbra()
test_slice_penumbra()
test_slice_shoulders()
test_slice_tails()
test_get_flatness()
test_get_symmetry()
test_make_symmetric()
test_make_centered()
test_make_flipped()
test_align_to()
test_cross_calibrate()