Commit 1d6cdde0 authored by Jan André Reuter's avatar Jan André Reuter

Try to fix failture of tests on systems without a GPU

Signed-off-by: default avatarJan André Reuter <jan.andre.reuter@hotmail.de>
parent 0319d2d8
Pipeline #25668 passed with stages
in 4 minutes and 50 seconds
import SLIX
import numpy
if SLIX.toolbox.gpu_available:
class TestToolbox:
def setup_method(self, method):
self.example = SLIX.io.imread('tests/files/demo.nii')
class TestToolbox:
def setup_method(self, method):
self.example = SLIX.io.imread('tests/files/demo.nii')
def test_compare_peaks(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
def test_compare_peaks(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
assert numpy.all(cpu_peaks == gpu_peaks)
assert numpy.all(cpu_peaks == gpu_peaks)
def test_compare_num_peaks(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.num_peaks(self.example)
cpu_peaks = SLIX.toolbox.cpu_toolbox.num_peaks(self.example)
def test_compare_num_peaks(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.num_peaks(self.example)
cpu_peaks = SLIX.toolbox.cpu_toolbox.num_peaks(self.example)
assert numpy.all(cpu_peaks == gpu_peaks)
assert numpy.all(cpu_peaks == gpu_peaks)
def test_compare_prominence(self):
gpu_prominence = SLIX.toolbox.gpu_toolbox.peak_prominence(self.example)
cpu_prominence = SLIX.toolbox.cpu_toolbox.peak_prominence(self.example)
def test_compare_prominence(self):
gpu_prominence = SLIX.toolbox.gpu_toolbox.peak_prominence(self.example)
cpu_prominence = SLIX.toolbox.cpu_toolbox.peak_prominence(self.example)
assert numpy.all(numpy.isclose(cpu_prominence, gpu_prominence))
assert numpy.all(numpy.isclose(cpu_prominence, gpu_prominence))
def test_compare_mean_prominence(self):
gpu_prominence = SLIX.toolbox.gpu_toolbox.mean_peak_prominence(self.example)
cpu_prominence = SLIX.toolbox.cpu_toolbox.mean_peak_prominence(self.example)
def test_compare_mean_prominence(self):
gpu_prominence = SLIX.toolbox.gpu_toolbox.mean_peak_prominence(self.example)
cpu_prominence = SLIX.toolbox.cpu_toolbox.mean_peak_prominence(self.example)
assert numpy.all(numpy.isclose(cpu_prominence, gpu_prominence))
assert numpy.all(numpy.isclose(cpu_prominence, gpu_prominence))
def test_compare_peakwidth(self):
gpu_peakwidth = SLIX.toolbox.gpu_toolbox.peak_width(self.example)
cpu_peakwidth = SLIX.toolbox.cpu_toolbox.peak_width(self.example)
def test_compare_peakwidth(self):
gpu_peakwidth = SLIX.toolbox.gpu_toolbox.peak_width(self.example)
cpu_peakwidth = SLIX.toolbox.cpu_toolbox.peak_width(self.example)
assert numpy.all(numpy.isclose(cpu_peakwidth, gpu_peakwidth))
assert numpy.all(numpy.isclose(cpu_peakwidth, gpu_peakwidth))
def test_compare_mean_peakwidth(self):
gpu_peakwidth = SLIX.toolbox.gpu_toolbox.mean_peak_width(self.example)
cpu_peakwidth = SLIX.toolbox.cpu_toolbox.mean_peak_width(self.example)
def test_compare_mean_peakwidth(self):
gpu_peakwidth = SLIX.toolbox.gpu_toolbox.mean_peak_width(self.example)
cpu_peakwidth = SLIX.toolbox.cpu_toolbox.mean_peak_width(self.example)
assert numpy.all(numpy.isclose(gpu_peakwidth, cpu_peakwidth))
assert numpy.all(numpy.isclose(gpu_peakwidth, cpu_peakwidth))
def test_compare_peakdistance(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks, return_numpy=False)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
def test_compare_peakdistance(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks, return_numpy=False)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
gpu_distance = SLIX.toolbox.gpu_toolbox.peak_distance(gpu_peaks, gpu_centroids)
cpu_distance = SLIX.toolbox.cpu_toolbox.peak_distance(cpu_peaks, cpu_centroids)
gpu_distance = SLIX.toolbox.gpu_toolbox.peak_distance(gpu_peaks, gpu_centroids)
cpu_distance = SLIX.toolbox.cpu_toolbox.peak_distance(cpu_peaks, cpu_centroids)
assert numpy.all(numpy.isclose(cpu_distance, gpu_distance))
assert numpy.all(numpy.isclose(cpu_distance, gpu_distance))
def test_compare_centroids(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
def test_compare_centroids(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
assert numpy.all(numpy.isclose(cpu_centroids, gpu_centroids))
assert numpy.all(numpy.isclose(cpu_centroids, gpu_centroids))
def test_compare_direction(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks, return_numpy=False)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
def test_compare_direction(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks, return_numpy=False)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
gpu_direction = SLIX.toolbox.gpu_toolbox.direction(gpu_peaks, gpu_centroids)
cpu_direction = SLIX.toolbox.cpu_toolbox.direction(cpu_peaks, cpu_centroids)
gpu_direction = SLIX.toolbox.gpu_toolbox.direction(gpu_peaks, gpu_centroids)
cpu_direction = SLIX.toolbox.cpu_toolbox.direction(cpu_peaks, cpu_centroids)
assert numpy.all(numpy.isclose(cpu_direction, gpu_direction))
assert numpy.all(numpy.isclose(cpu_direction, gpu_direction))
def test_compare_unit_vectors(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks, return_numpy=False)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
def test_compare_unit_vectors(self):
gpu_peaks = SLIX.toolbox.gpu_toolbox.peaks(self.example, return_numpy=False)
cpu_peaks = SLIX.toolbox.cpu_toolbox.peaks(self.example)
gpu_centroids = SLIX.toolbox.gpu_toolbox.centroid_correction(self.example, gpu_peaks, return_numpy=False)
cpu_centroids = SLIX.toolbox.cpu_toolbox.centroid_correction(self.example, cpu_peaks)
gpu_direction = SLIX.toolbox.gpu_toolbox.direction(gpu_peaks, gpu_centroids, return_numpy=False)
cpu_direction = SLIX.toolbox.cpu_toolbox.direction(cpu_peaks, cpu_centroids)
gpu_direction = SLIX.toolbox.gpu_toolbox.direction(gpu_peaks, gpu_centroids, return_numpy=False)
cpu_direction = SLIX.toolbox.cpu_toolbox.direction(cpu_peaks, cpu_centroids)
gpu_unit_x, gpu_unit_y = SLIX.toolbox.gpu_toolbox.unit_vectors(gpu_direction)
cpu_unit_x, cpu_unit_y = SLIX.toolbox.cpu_toolbox.unit_vectors(cpu_direction)
gpu_unit_x, gpu_unit_y = SLIX.toolbox.gpu_toolbox.unit_vectors(gpu_direction)
cpu_unit_x, cpu_unit_y = SLIX.toolbox.cpu_toolbox.unit_vectors(cpu_direction)
assert numpy.all(numpy.isclose(cpu_unit_x, gpu_unit_x))
assert numpy.all(numpy.isclose(cpu_unit_y, gpu_unit_y))
assert numpy.all(numpy.isclose(cpu_unit_x, gpu_unit_x))
assert numpy.all(numpy.isclose(cpu_unit_y, gpu_unit_y))
import SLIX
from SLIX.SLIX_GPU import _toolbox as ntoolbox
import cupy
from numba import cuda
threads_per_block = (1, 1)
blocks_per_grid = (1, 1)
class TestNumbaToolboxGPU:
def test_peak_cleanup(self):
test_one_peak = cupy.array([0, 1, 0, 0]).reshape((1, 1, 4))
result = cupy.zeros(test_one_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_one_peak, result)
cuda.synchronize()
assert cupy.all(cupy.array([0, 1, 0, 0]) == result)
test_two_peak = cupy.array([0, 1, 1, 0]).reshape((1, 1, 4))
result = cupy.zeros(test_two_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_two_peak, result)
assert cupy.all(cupy.array([0, 1, 0, 0]) == result)
test_three_peak = cupy.array([0, 1, 1, 1, 0]).reshape((1, 1, 5))
result = cupy.zeros(test_three_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_three_peak, result)
assert cupy.all(cupy.array([0, 0, 1, 0, 0]) == result)
test_double_three_peak = cupy.array([0, 1, 1, 1, 0, 1, 1, 1, 0]).reshape((1, 1, 9))
result = cupy.zeros(test_double_three_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_double_three_peak, result)
assert cupy.all(cupy.array([0, 0, 1, 0, 0, 0, 1, 0, 0]) == result)
def test_prominence(self):
test_array = cupy.array([0, 0.1, 0.2, 0.4, 0.8, 1, 0.5, 0.7, 0.9, 0.5, 0.3, 0.95, 0], dtype='float32')\
.reshape((1, 1, 13))
peaks = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0], dtype='int8').reshape((1, 1, 13))
expected_prominence = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 0.4, 0, 0, 0.65, 0]).reshape((1, 1, 13))
toolbox_prominence = cupy.zeros(expected_prominence.shape, dtype='float32')
ntoolbox._prominence[blocks_per_grid, threads_per_block](test_array, peaks, toolbox_prominence)
print(toolbox_prominence)
assert cupy.all(cupy.isclose(expected_prominence, toolbox_prominence))
def test_peakwidth(self):
test_array = cupy.array([0, 0.1, 0.2, 0.5, 0.8, 1, 0.77, 0.7, 0.66, 0.5, 0.74, 0.98, 0.74], dtype='float32')\
.reshape((1, 1, 13))
peaks = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0], dtype='int8').reshape((1, 1, 13))
prominence = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0.48, 0], dtype='float32').reshape((1, 1, 13))
expected_width = cupy.array([0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 2, 0]).reshape((1, 1, 13))
toolbox_width = cupy.zeros(expected_width.shape, dtype='float32')
ntoolbox._peakwidth[blocks_per_grid, threads_per_block](test_array, peaks, prominence, toolbox_width, 0.5)
assert cupy.all(toolbox_width == expected_width)
def test_peakdistance(self):
test_arr = cupy.array(([False, False, True, False, False, False, False, True, False] +
[False] * 15), dtype='int8')\
.reshape((1, 1, 24))
expected_distance = 75
toolbox_distance = cupy.zeros(test_arr.shape, dtype='float32')
ntoolbox._peakdistance[blocks_per_grid, threads_per_block]\
(test_arr,
cupy.zeros(test_arr.shape, dtype='float32'),
cupy.array([[2]], dtype='int8'),
toolbox_distance)
assert toolbox_distance[0, 0, 2] == expected_distance
assert toolbox_distance[0, 0, 7] == 360 - expected_distance
def test_direction(self):
# Test for one peak
one_peak_arr = cupy.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24)).astype('int8')
expected_direction = cupy.array([45, ntoolbox.BACKGROUND_COLOR, ntoolbox.BACKGROUND_COLOR])
toolbox_direction = cupy.zeros((1, 1, 3), dtype='float32')
ntoolbox._direction[blocks_per_grid, threads_per_block]\
(one_peak_arr,
cupy.zeros(one_peak_arr.shape, dtype='float32'),
cupy.array([[1]], dtype='int8'),
toolbox_direction)
assert cupy.all(expected_direction == toolbox_direction)
# Test for one direction with 180°+-35° distance
two_peak_arr = cupy.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24)).astype('int8')
expected_direction = cupy.array([135, ntoolbox.BACKGROUND_COLOR, ntoolbox.BACKGROUND_COLOR])
ntoolbox._direction[blocks_per_grid, threads_per_block]\
(two_peak_arr,
cupy.zeros(two_peak_arr.shape, dtype='float32'),
cupy.array([[2]], dtype='int8'),
toolbox_direction)
assert cupy.all(expected_direction == toolbox_direction)
def test_centroid_correction_bases(self):
# simple test case: one distinct peak
test_array = cupy.array([0] * 9 + [1] + [0] * 14).reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
left_bases = cupy.zeros(test_array.shape, dtype='uint8')
right_bases = cupy.zeros(test_array.shape, dtype='uint8')
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block]\
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 1
assert cupy.sum(right_bases) == 1
# simple test case: one distinct peak
test_array = cupy.array([0] * 8 + [0.95, 1, 0.5] + [0] * 13, dtype='float32').reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block] \
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 2
assert cupy.sum(right_bases) == 1
# simple test case: centroid is between two measurements
test_array = cupy.array([0] * 8 + [1, 1] + [0] * 14).reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block] \
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 1
assert cupy.sum(right_bases) == 2
# more complicated test case: wide peak plateau
test_array = cupy.array([0] * 8 + [1, 1, 1] + [0] * 13).reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block] \
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 2
assert cupy.sum(right_bases) == 2
def test_centroid(self):
image = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
left = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
right = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
peak = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
result_centroid = cupy.zeros(image.shape, dtype='float32')
ntoolbox._centroid[blocks_per_grid, threads_per_block](image, peak, left, right, result_centroid)
assert cupy.sum(result_centroid) == 0
if SLIX.toolbox.gpu_available:
from SLIX.SLIX_GPU import _toolbox as ntoolbox
import cupy
from numba import cuda
threads_per_block = (1, 1)
blocks_per_grid = (1, 1)
class TestNumbaToolboxGPU:
def test_peak_cleanup(self):
test_one_peak = cupy.array([0, 1, 0, 0]).reshape((1, 1, 4))
result = cupy.zeros(test_one_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_one_peak, result)
cuda.synchronize()
assert cupy.all(cupy.array([0, 1, 0, 0]) == result)
test_two_peak = cupy.array([0, 1, 1, 0]).reshape((1, 1, 4))
result = cupy.zeros(test_two_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_two_peak, result)
assert cupy.all(cupy.array([0, 1, 0, 0]) == result)
test_three_peak = cupy.array([0, 1, 1, 1, 0]).reshape((1, 1, 5))
result = cupy.zeros(test_three_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_three_peak, result)
assert cupy.all(cupy.array([0, 0, 1, 0, 0]) == result)
test_double_three_peak = cupy.array([0, 1, 1, 1, 0, 1, 1, 1, 0]).reshape((1, 1, 9))
result = cupy.zeros(test_double_three_peak.shape, dtype='int8')
ntoolbox._peaks[blocks_per_grid, threads_per_block](test_double_three_peak, result)
assert cupy.all(cupy.array([0, 0, 1, 0, 0, 0, 1, 0, 0]) == result)
def test_prominence(self):
test_array = cupy.array([0, 0.1, 0.2, 0.4, 0.8, 1, 0.5, 0.7, 0.9, 0.5, 0.3, 0.95, 0], dtype='float32')\
.reshape((1, 1, 13))
peaks = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0], dtype='int8').reshape((1, 1, 13))
expected_prominence = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 0.4, 0, 0, 0.65, 0]).reshape((1, 1, 13))
toolbox_prominence = cupy.zeros(expected_prominence.shape, dtype='float32')
ntoolbox._prominence[blocks_per_grid, threads_per_block](test_array, peaks, toolbox_prominence)
print(toolbox_prominence)
assert cupy.all(cupy.isclose(expected_prominence, toolbox_prominence))
def test_peakwidth(self):
test_array = cupy.array([0, 0.1, 0.2, 0.5, 0.8, 1, 0.77, 0.7, 0.66, 0.5, 0.74, 0.98, 0.74], dtype='float32')\
.reshape((1, 1, 13))
peaks = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0], dtype='int8').reshape((1, 1, 13))
prominence = cupy.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0.48, 0], dtype='float32').reshape((1, 1, 13))
expected_width = cupy.array([0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 2, 0]).reshape((1, 1, 13))
toolbox_width = cupy.zeros(expected_width.shape, dtype='float32')
ntoolbox._peakwidth[blocks_per_grid, threads_per_block](test_array, peaks, prominence, toolbox_width, 0.5)
assert cupy.all(toolbox_width == expected_width)
def test_peakdistance(self):
test_arr = cupy.array(([False, False, True, False, False, False, False, True, False] +
[False] * 15), dtype='int8')\
.reshape((1, 1, 24))
expected_distance = 75
toolbox_distance = cupy.zeros(test_arr.shape, dtype='float32')
ntoolbox._peakdistance[blocks_per_grid, threads_per_block]\
(test_arr,
cupy.zeros(test_arr.shape, dtype='float32'),
cupy.array([[2]], dtype='int8'),
toolbox_distance)
assert toolbox_distance[0, 0, 2] == expected_distance
assert toolbox_distance[0, 0, 7] == 360 - expected_distance
def test_direction(self):
# Test for one peak
one_peak_arr = cupy.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24)).astype('int8')
expected_direction = cupy.array([45, ntoolbox.BACKGROUND_COLOR, ntoolbox.BACKGROUND_COLOR])
toolbox_direction = cupy.zeros((1, 1, 3), dtype='float32')
ntoolbox._direction[blocks_per_grid, threads_per_block]\
(one_peak_arr,
cupy.zeros(one_peak_arr.shape, dtype='float32'),
cupy.array([[1]], dtype='int8'),
toolbox_direction)
assert cupy.all(expected_direction == toolbox_direction)
# Test for one direction with 180°+-35° distance
two_peak_arr = cupy.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24)).astype('int8')
expected_direction = cupy.array([135, ntoolbox.BACKGROUND_COLOR, ntoolbox.BACKGROUND_COLOR])
ntoolbox._direction[blocks_per_grid, threads_per_block]\
(two_peak_arr,
cupy.zeros(two_peak_arr.shape, dtype='float32'),
cupy.array([[2]], dtype='int8'),
toolbox_direction)
assert cupy.all(expected_direction == toolbox_direction)
def test_centroid_correction_bases(self):
# simple test case: one distinct peak
test_array = cupy.array([0] * 9 + [1] + [0] * 14).reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
left_bases = cupy.zeros(test_array.shape, dtype='uint8')
right_bases = cupy.zeros(test_array.shape, dtype='uint8')
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block]\
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 1
assert cupy.sum(right_bases) == 1
# simple test case: one distinct peak
test_array = cupy.array([0] * 8 + [0.95, 1, 0.5] + [0] * 13, dtype='float32').reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block] \
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 2
assert cupy.sum(right_bases) == 1
# simple test case: centroid is between two measurements
test_array = cupy.array([0] * 8 + [1, 1] + [0] * 14).reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block] \
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 1
assert cupy.sum(right_bases) == 2
# more complicated test case: wide peak plateau
test_array = cupy.array([0] * 8 + [1, 1, 1] + [0] * 13).reshape((1, 1, 24))
test_high_peaks = SLIX.toolbox.peaks(test_array)
test_reverse_peaks = SLIX.toolbox.peaks(-test_array)
ntoolbox._centroid_correction_bases[blocks_per_grid, threads_per_block] \
(test_array,
test_high_peaks,
test_reverse_peaks,
left_bases,
right_bases)
assert cupy.sum(left_bases) == 2
assert cupy.sum(right_bases) == 2
def test_centroid(self):
image = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
left = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
right = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
peak = cupy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\
.reshape((1, 1, 24))
result_centroid = cupy.zeros(image.shape, dtype='float32')
ntoolbox._centroid[blocks_per_grid, threads_per_block](image, peak, left, right, result_centroid)
assert cupy.sum(result_centroid) == 0
......@@ -2,9 +2,14 @@ from SLIX import toolbox
import numpy
import pytest
if toolbox.gpu_available:
use_gpu_arr = [True, False]
else:
use_gpu_arr = [False]
class TestToolbox:
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_background_mask(self, use_gpu):
array = numpy.empty((256, 3))
array[:, 0] = numpy.zeros(256)
......@@ -15,7 +20,7 @@ class TestToolbox:
assert numpy.all(toolbox_mask[:10] == True)
assert numpy.all(toolbox_mask[10:] == False)
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_all_peaks(self, use_gpu):
# Create an absolute simple peak array
arr = numpy.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0])
......@@ -54,7 +59,7 @@ class TestToolbox:
toolbox_peaks = toolbox.peaks(arr, use_gpu=use_gpu)
assert numpy.all(toolbox_peaks == real_peaks)
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_num_peaks(self, use_gpu):
# Create an absolute simple peak array
test_arr = numpy.array(([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), dtype=bool)
......@@ -65,7 +70,7 @@ class TestToolbox:
expected_value = numpy.count_nonzero(real_peaks[0, 0, :])
assert numpy.all(toolbox_peaks == expected_value)
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_peak_prominence(self, use_gpu):
# Create an absolute simple peak array
arr = numpy.array([0, 1, 0, 0.07, 0, 1, 0, 0.07, 0, 1, 0] * 1)
......@@ -83,7 +88,7 @@ class TestToolbox:
assert numpy.all(high_peaks == toolbox_high_peaks)
assert numpy.all(low_peaks == toolbox_low_peaks)
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_mean_peakprominence(self, use_gpu):
test_arr = numpy.array([0, 1, 0, 0.1, 0, 1, 0, 0.1, 0, 1, 0]).reshape(1, 1, 11)
comparison = toolbox.cpu_toolbox.normalize(test_arr, kind_of_normalization=1)
......@@ -133,7 +138,7 @@ class TestToolbox:
assert toolbox_distance[0, 0, 7] == expected_distance_2
assert toolbox_distance[0, 0, 15] == 360 - expected_distance_2
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_mean_peakdistance(self, use_gpu):
# Test four peaks
test_arr = numpy.array(([False, False, True, False, False, False, False, True, False,
......@@ -146,7 +151,7 @@ class TestToolbox:
dtype=float), use_gpu=use_gpu)
assert numpy.isclose(toolbox_distance[0, 0], expected_distance)
@pytest.mark.parametrize("use_gpu", [True, False])
@pytest.mark.parametrize("use_gpu", use_gpu_arr)
def test_peakwidth(self, use_gpu):
test_arr = numpy.array([0, 0.5, 1, 0.5, 0] + [0] * 19)
test_arr = test_arr.reshape((1, 1, 24))
......@@ -157,7 +162,7 @@ class TestToolbox:
assert toolbox_width[0, 0, 2] == expected_width
assert numpy.sum(toolbox_width) == expected_width