This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_gamma_cube.py
112 lines (89 loc) · 4.29 KB
/
test_gamma_cube.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
# Copyright (C) 2015-2016 Simon Biggs
# 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.
# 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 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/.
"""Tests for npgamma."""
import numpy as np
from npgamma import calc_gamma, _calculate_coordinates_kernel
class TestGamma():
"""Testing class."""
def setUp(self):
"""Run before each test."""
grid_x = np.arange(0, 1, 0.1)
grid_y = np.arange(0, 1.2, 0.1)
grid_z = np.arange(0, 1.4, 0.1)
self.dimensions = (len(grid_x), len(grid_y), len(grid_z))
self.coords = (grid_x, grid_y, grid_z)
self.reference = np.zeros(self.dimensions)
self.reference[2:-2:, 2:-2:, 2:-2:] = 1
self.evaluation = np.zeros(self.dimensions)
self.evaluation[3:-2:, 4:-2:, 5:-2:] = 1.015
self.expected_gamma = np.zeros(self.dimensions)
self.expected_gamma[2:-2:, 2:-2:, 2:-2:] = 0.4
self.expected_gamma[3:-3:, 3:-3:, 3:-3:] = 0.7
self.expected_gamma[4:-4:, 4:-4:, 4:-4:] = 1
self.expected_gamma[3:-2:, 4:-2:, 5:-2:] = 0.5
def test_regression_of_gamma_3d(self):
"""Test for changes in expected 3D gamma."""
self.gamma3d = np.round(calc_gamma(
self.coords, self.reference,
self.coords, self.evaluation,
0.3, 0.03), decimals=3)
assert np.all(self.expected_gamma == self.gamma3d)
def test_regression_of_gamma_2d(self):
"""Test for changes in expected 2D gamma."""
self.gamma2d = np.round(calc_gamma(
self.coords[1::], self.reference[5, :, :],
self.coords[1::], self.evaluation[5, :, :],
0.3, 0.03), decimals=3)
assert np.all(self.expected_gamma[5, :, :] == self.gamma2d)
def test_regression_of_gamma_1d(self):
"""Test for changes in expected 3D gamma."""
self.gamma1d = np.round(calc_gamma(
self.coords[2], self.reference[5, 5, :],
self.coords[2], self.evaluation[5, 5, :],
0.3, 0.03), decimals=3)
assert np.all(self.expected_gamma[5, 5, :] == self.gamma1d)
def test_coords_stepsize(self):
"""Testing correct stepsize implementation.
Confirm that the the largest distance between one point and any other
is less than the defined step size
"""
distance_step_size = 0.03
num_dimensions = 3
distance = 1
x, y, z = _calculate_coordinates_kernel(
distance, num_dimensions, distance_step_size)
distance_between_coords = np.sqrt(
(x[:, None] - x[None, :])**2 +
(y[:, None] - y[None, :])**2 +
(z[:, None] - z[None, :])**2)
distance_between_coords[
distance_between_coords == 0] = np.nan
largest_difference = np.max(np.nanmin(distance_between_coords, axis=0))
assert largest_difference <= distance_step_size
assert largest_difference > distance_step_size * 0.9
def test_calc_by_sections(self):
"""Testing that splitting into sections doesn't change the result."""
self.concurrent_reduction = np.round(calc_gamma(
self.coords, self.reference,
self.coords, self.evaluation,
0.3, 0.03, max_concurrent_calc_points=10000), decimals=3)
# print(self.expected_gamma - self.concurrent_reduction)
assert np.all(self.expected_gamma == self.concurrent_reduction)
def test_multithreading(self):
"""Testing that running multiple threads doesn't change the result."""
self.multithreading = np.round(calc_gamma(
self.coords, self.reference,
self.coords, self.evaluation,
0.3, 0.03, num_threads=4), decimals=3)
# print(self.expected_gamma - self.multithreading)
assert np.all(self.expected_gamma == self.multithreading)