Skip to content

Commit e2b1745

Browse files
committed
ENH test folder structure refactoring and further independent testing in progress
1 parent c701776 commit e2b1745

11 files changed

+230
-104
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ in a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/).
7373
+ Install [ITK-snap](http://www.itksnap.org/pmwiki/pmwiki.php?n=Downloads.SNAP3)
7474

7575

76-
7776
### Documentations
7877

7978
+ [Wiki-pages documentation](https://github.com/SebastianoF/nilabels/wiki)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO

tests/test_auxiliary_methods.py renamed to tests/tools/test_auxiliary_methods.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_set_new_data_simple_modifications():
8484
# see if the infos are the same as in the modified header
8585
assert_array_equal(im_1.get_data()[:], np.ones([3, 3, 3]))
8686
assert_equals(im_1_header['intent_code'], 5)
87-
assert_array_equal(im_1.get_affine(), aff)
87+
assert_array_equal(im_1.affine, aff)
8888

8989

9090
def test_compare_two_nib_equals():
@@ -181,7 +181,7 @@ def test_from_permutation_to_disjoints_cycles_single_cycle():
181181
cauchy_perm = [[1, 2, 3, 4, 5, 6, 7],
182182
[3, 4, 5, 1, 2, 7, 6]]
183183
cycles_perm = permutation_from_cauchy_to_disjoints_cycles(cauchy_perm)
184-
expected_ans = [[1, 3, 5, 2, 4], [6,7]]
184+
expected_ans = [[1, 3, 5, 2, 4], [6, 7]]
185185

186186
print expected_ans
187187
print cycles_perm
@@ -202,11 +202,28 @@ def test_from_disjoint_cycles_to_permutation_single_cycle():
202202
assert_array_equal(c1, c2)
203203

204204

205-
test_from_permutation_to_disjoints_cycles_single_cycle()
206-
test_from_disjoint_cycles_to_permutation_single_cycle()
205+
if __name__ == '__main__':
206+
test_get_morpological_patch()
207+
test_get_patch_values_simple()
208+
test_get_shell_for_given_radius()
209+
test_check_pfi_io()
207210

208-
test_from_disjoint_cycles_to_permutation()
209-
test_from_permutation_to_disjoints_cycles()
211+
test_set_new_data_simple_modifications()
212+
test_compare_two_nib_equals()
213+
test_compare_two_nib_different_nifti_version()
214+
test_compare_two_nib_different_affine()
210215

211-
#
212-
# permutation_from_cauchy_to_disjoints_cycles,
216+
test_eliminates_consecutive_duplicates()
217+
test_lift_list_1()
218+
test_lift_list_2()
219+
test_lift_list_3()
220+
221+
test_labels_query_int_input()
222+
test_labels_query_list_input1()
223+
test_labels_query_list_input2()
224+
test_labels_query_all_or_tot_input()
225+
226+
test_from_permutation_to_disjoints_cycles()
227+
test_from_disjoint_cycles_to_permutation()
228+
test_from_permutation_to_disjoints_cycles_single_cycle()
229+
test_from_disjoint_cycles_to_permutation_single_cycle()

tests/test_caliber_distances.py renamed to tests/tools/test_caliber_distances.py

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
from scipy import ndimage as nd
55

66
from numpy.testing import assert_array_equal, assert_equal, assert_almost_equal
7-
from nilabels.tools.aux_methods.utils_nib import set_new_data
8-
from nilabels.tools.detections.contours import contour_from_segmentation
97

8+
from nilabels.tools.detections.contours import contour_from_segmentation
109

1110
from nilabels.tools.caliber.distances import centroid_array, centroid, dice_score, global_dice_score, \
1211
global_outline_error, covariance_matrices, covariance_distance, hausdorff_distance, \
@@ -17,7 +16,7 @@
1716
# --- Auxiliaries
1817

1918

20-
def test_centroid_array():
19+
def test_centroid_array_1():
2120
test_arr = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
2221
[0, 0, 0, 0, 0, 0, 0, 0, 0],
2322
[0, 1, 1, 1, 0, 0, 0, 0, 0],
@@ -716,96 +715,9 @@ def test_hausdorff_distance():
716715
assert_almost_equal(hd_1_void['label2'], np.nan)
717716

718717

719-
720-
# test symmetric_contour_distance
721-
722-
723-
def test_normalised_symetric_contour_distance(save_data_path='/Users/aaabbbccc/Desktop', verbose=False):
724-
o1 = o_shape(omega=(19, 19, 19), radius=7, background_intensity=0, foreground_intensity=1, dtype=np.uint8)
725-
o2 = 2 * o1
726-
arr1 = np.concatenate([o1, o2], axis=2)
727-
728-
half_o1 = np.zeros_like(o1)
729-
half_o1[:, :10, :] = o1[:, :10, :]
730-
half_o2 = 2 * half_o1
731-
arr2 = np.concatenate([half_o1, half_o2], axis=2)
732-
733-
c1 = cube_shape(omega=(19, 19, 19), center=(9, 9, 9), side_length=13, background_intensity=0, foreground_intensity=1, dtype=np.uint8)
734-
c2 = 2 * c1
735-
arr3 = np.concatenate([c1, c2], axis=2)
736-
737-
arr_void = np.zeros_like(arr1)
738-
739-
im1 = nib.Nifti1Image(arr1, np.eye(4))
740-
im2 = nib.Nifti1Image(arr2, np.eye(4))
741-
im3 = nib.Nifti1Image(arr3, np.eye(4))
742-
743-
im_void = nib.Nifti1Image(arr_void, np.eye(4))
744-
745-
if os.path.exists(save_data_path):
746-
747-
nib.save(im1, os.path.join(save_data_path, 'zzz1.nii.gz'))
748-
nib.save(im2, os.path.join(save_data_path, 'zzz2.nii.gz'))
749-
nib.save(im3, os.path.join(save_data_path, 'zzz3.nii.gz'))
750-
751-
im1_cont = contour_from_segmentation(im1)
752-
im2_cont = contour_from_segmentation(im2)
753-
im3_cont = contour_from_segmentation(im3)
754-
755-
nib.save(im1_cont, os.path.join(save_data_path, 'zzz1_contour.nii.gz'))
756-
nib.save(im2_cont, os.path.join(save_data_path, 'zzz2_contour.nii.gz'))
757-
nib.save(im3_cont, os.path.join(save_data_path, 'zzz3_contour.nii.gz'))
758-
759-
im_dtb1_l1 = set_new_data(im1_cont, nd.distance_transform_edt(1 - (im1_cont.get_data() == 1)))
760-
im_dtb2_l1 = set_new_data(im2_cont, nd.distance_transform_edt(1 - (im2_cont.get_data() == 1)))
761-
im_dtb3_l1 = set_new_data(im3_cont, nd.distance_transform_edt(1 - (im3_cont.get_data() == 1)))
762-
763-
im_dtb1_l2 = set_new_data(im1_cont, nd.distance_transform_edt(1 - (im1_cont.get_data() == 2)))
764-
im_dtb2_l2 = set_new_data(im2_cont, nd.distance_transform_edt(1 - (im2_cont.get_data() == 2)))
765-
im_dtb3_l2 = set_new_data(im3_cont, nd.distance_transform_edt(1 - (im3_cont.get_data() == 2)))
766-
767-
nib.save(im_dtb1_l1, os.path.join(save_data_path, 'zzz1_contour_dist_label1.nii.gz'))
768-
nib.save(im_dtb2_l1, os.path.join(save_data_path, 'zzz2_contour_dist_label1.nii.gz'))
769-
nib.save(im_dtb3_l1, os.path.join(save_data_path, 'zzz3_contour_dist_label1.nii.gz'))
770-
771-
nib.save(im_dtb1_l2, os.path.join(save_data_path, 'zzz1_contour_dist_label2.nii.gz'))
772-
nib.save(im_dtb2_l2, os.path.join(save_data_path, 'zzz2_contour_dist_label2.nii.gz'))
773-
nib.save(im_dtb3_l2, os.path.join(save_data_path, 'zzz3_contour_dist_label2.nii.gz'))
774-
775-
print('Images testing saved in {}'.format(save_data_path))
776-
777-
ascd_1_1 = normalised_symmetric_contour_distance(im1, im1, [1, 2], ['label1', 'label2'])
778-
ascd_1_2 = normalised_symmetric_contour_distance(im1, im2, [1, 2], ['label1', 'label2'])
779-
ascd_1_3 = normalised_symmetric_contour_distance(im1, im3, [1, 2], ['label1', 'label2'])
780-
ascd_1_2_extra = normalised_symmetric_contour_distance(im1, im2, [1, 2, 3], ['label1', 'label2', 'label3'])
781-
ascd_1_void = normalised_symmetric_contour_distance(im1, im_void, [1, 2], ['label1', 'label2'])
782-
783-
if verbose:
784-
print('\n 1 1')
785-
print(ascd_1_1)
786-
print('\n 1 2')
787-
print(ascd_1_2)
788-
print('\n 1 3')
789-
print(ascd_1_3)
790-
print('\n 1 2 extra')
791-
print(ascd_1_2_extra)
792-
print('\n 1 void')
793-
print(ascd_1_void)
794-
795-
assert_almost_equal(ascd_1_1['label1'], 0)
796-
assert_almost_equal(ascd_1_1['label2'], 0)
797-
798-
assert_almost_equal(ascd_1_2['label1'], 1.01226261459, decimal=4)
799-
assert_almost_equal(ascd_1_2['label2'], 1.01226261459, decimal=4)
800-
801-
assert_almost_equal(ascd_1_3['label1'], 0.866793279546 , decimal=4)
802-
assert_almost_equal(ascd_1_3['label2'], 0.866793279546, decimal=4)
803-
804-
assert_almost_equal(ascd_1_2_extra['label3'], np.nan)
805-
806-
assert_almost_equal(ascd_1_void['label1'], np.nan)
807-
assert_almost_equal(ascd_1_void['label2'], np.nan)
808-
718+
def test_normalised_symmetric_contour_distance():
719+
# TODO
720+
pass
809721

810722
# --- extra:
811723

@@ -840,3 +752,25 @@ def test_box_side_lenght():
840752
assert_array_equal(se_answer['2'], [1., 2., 4.])
841753
assert_equal(se_answer['3'], np.nan)
842754

755+
756+
if __name__ == '__main__':
757+
test_centroid_array_1()
758+
test_centroid_array_2()
759+
test_centroid()
760+
test_covariance_matrices()
761+
test_covariance_distance_between_matrices_simple_case()
762+
763+
test_dice_score()
764+
test_global_dice_score()
765+
test_global_outline_error()
766+
767+
test_dice_score_one_label()
768+
test_asymmetric_component_Hausdorff_distance_H_d_and_Hausdorff_distance()
769+
test_dice_score_multiple_labels()
770+
test_covariance_distance()
771+
test_covariance_distance_range()
772+
test_hausdorff_distance()
773+
774+
test_normalised_symmetric_contour_distance()
775+
776+
test_box_side_lenght()
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import os
2+
from os.path import join as jph
3+
4+
import numpy as np
5+
import nibabel as nib
6+
from nose.tools import assert_raises, assert_almost_equal, assert_equal, assert_equals
7+
from numpy.testing import assert_array_equal
8+
9+
from nilabels.tools.caliber.volumes_and_values import get_total_num_nonzero_voxels, get_num_voxels_from_labels_list, \
10+
get_values_below_labels_list
11+
12+
13+
def cube_shape(omega, center, side_length, background_intensity=0, foreground_intensity=100, dtype=np.uint8):
14+
sky = background_intensity * np.ones(omega, dtype=dtype)
15+
half_side_length = int(np.ceil(int(side_length / 2)))
16+
17+
for lx in range(-half_side_length, half_side_length + 1):
18+
for ly in range(-half_side_length, half_side_length + 1):
19+
for lz in range(-half_side_length, half_side_length + 1):
20+
sky[center[0] + lx, center[1] + ly, center[2] + lz] = foreground_intensity
21+
return sky
22+
23+
24+
def test_volumes_and_values_total_num_voxels():
25+
26+
omega = [80, 80, 80]
27+
cube_a = [[10, 60, 55], 11, 1]
28+
cube_b = [[50, 55, 42], 17, 2]
29+
cube_c = [[25, 20, 20], 19, 3]
30+
cube_d = [[55, 16, 9], 9, 4]
31+
32+
sky = cube_shape(omega, center=cube_a[0], side_length=cube_a[1], foreground_intensity=cube_a[2]) + \
33+
cube_shape(omega, center=cube_b[0], side_length=cube_b[1], foreground_intensity=cube_b[2]) + \
34+
cube_shape(omega, center=cube_c[0], side_length=cube_c[1], foreground_intensity=cube_c[2]) + \
35+
cube_shape(omega, center=cube_d[0], side_length=cube_d[1], foreground_intensity=cube_d[2])
36+
im_segm = nib.Nifti1Image(sky, affine=np.eye(4))
37+
38+
num_voxels = get_total_num_nonzero_voxels(im_segm)
39+
assert_equal(num_voxels, 11 ** 3 + 17 ** 3 + 19 ** 3 + 9 **3)
40+
41+
num_voxels = get_total_num_nonzero_voxels(im_segm, list_labels_to_exclude=[2, 4])
42+
assert_equal(num_voxels, 11 ** 3 + 19 ** 3)
43+
44+
45+
def test_volumes_and_values_total_num_voxels_empthy():
46+
47+
omega = [80, 80, 80]
48+
im_segm = nib.Nifti1Image(np.zeros(omega), affine=np.eye(4))
49+
50+
num_voxels = get_total_num_nonzero_voxels(im_segm)
51+
print(num_voxels)
52+
assert_equal(num_voxels, 0)
53+
54+
55+
def test_volumes_and_values_total_num_voxels_full():
56+
57+
omega = [80, 80, 80]
58+
im_segm = nib.Nifti1Image(np.ones(omega), affine=np.eye(4))
59+
60+
num_voxels = get_total_num_nonzero_voxels(im_segm)
61+
print(num_voxels)
62+
assert_equal(num_voxels, 80 ** 3)
63+
64+
65+
def test_get_num_voxels_from_labels_list():
66+
67+
omega = [80, 80, 80]
68+
cube_a = [[10, 60, 55], 11, 1]
69+
cube_b = [[50, 55, 42], 15, 2]
70+
cube_c = [[25, 20, 20], 13, 3]
71+
cube_d = [[55, 16, 9], 7, 4]
72+
73+
sky = cube_shape(omega, center=cube_a[0], side_length=cube_a[1], foreground_intensity=cube_a[2]) + \
74+
cube_shape(omega, center=cube_b[0], side_length=cube_b[1], foreground_intensity=cube_b[2]) + \
75+
cube_shape(omega, center=cube_c[0], side_length=cube_c[1], foreground_intensity=cube_c[2]) + \
76+
cube_shape(omega, center=cube_d[0], side_length=cube_d[1], foreground_intensity=cube_d[2])
77+
im_segm = nib.Nifti1Image(sky, affine=np.eye(4))
78+
79+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[1, 2, 3, 4])
80+
print(num_voxels, [11 **3, 15 **3, 13 **3, 7 ** 3])
81+
assert_array_equal(num_voxels, [11 **3, 15 **3, 13 **3, 7 ** 3])
82+
83+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[1, [2, 3], 4])
84+
print(num_voxels, [11 ** 3, 15 ** 3 + 13 ** 3, 7 ** 3])
85+
assert_array_equal(num_voxels, [11 ** 3, 15 ** 3 + 13 ** 3, 7 ** 3])
86+
87+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[[1, 2, 3], 4])
88+
print(num_voxels, [11 ** 3, 15 ** 3 + 13 ** 3, 7 ** 3])
89+
assert_array_equal(num_voxels, [11 ** 3 + 15 ** 3 + 13 ** 3, 7 ** 3])
90+
91+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[[1, 2, 3, 4]])
92+
print(num_voxels, [11 ** 3, 15 ** 3 + 13 ** 3, 7 ** 3])
93+
assert_array_equal(num_voxels, [11 ** 3 + 15 ** 3 + 13 ** 3 + 7 ** 3])
94+
95+
96+
def test_get_num_voxels_from_labels_list_unexisting_labels():
97+
98+
omega = [80, 80, 80]
99+
cube_a = [[10, 60, 55], 11, 1]
100+
cube_b = [[50, 55, 42], 15, 2]
101+
cube_c = [[25, 20, 20], 13, 3]
102+
cube_d = [[55, 16, 9], 7, 4]
103+
104+
sky = cube_shape(omega, center=cube_a[0], side_length=cube_a[1], foreground_intensity=cube_a[2]) + \
105+
cube_shape(omega, center=cube_b[0], side_length=cube_b[1], foreground_intensity=cube_b[2]) + \
106+
cube_shape(omega, center=cube_c[0], side_length=cube_c[1], foreground_intensity=cube_c[2]) + \
107+
cube_shape(omega, center=cube_d[0], side_length=cube_d[1], foreground_intensity=cube_d[2])
108+
im_segm = nib.Nifti1Image(sky, affine=np.eye(4))
109+
110+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[1, 2, 3, 5])
111+
print(num_voxels, [11 ** 3, 15 ** 3, 13 ** 3, 0])
112+
assert_array_equal(num_voxels, [11 ** 3, 15 ** 3, 13 ** 3, 0])
113+
114+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[1, 2, [3, 5]])
115+
print(num_voxels, [11 ** 3, 15 ** 3, 13 ** 3 + 0])
116+
assert_array_equal(num_voxels, [11 ** 3, 15 ** 3, 13 ** 3 + 0])
117+
118+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[[1, 2], [7, 8]])
119+
print(num_voxels, [11 ** 3 + 15 ** 3, 0])
120+
assert_array_equal(num_voxels, [11 ** 3 + 15 ** 3, 0])
121+
122+
num_voxels = get_num_voxels_from_labels_list(im_segm, labels_list=[[1, 2], [7, -8]])
123+
print(num_voxels, [11 ** 3 + 15 ** 3, 0])
124+
assert_array_equal(num_voxels, [11 ** 3 + 15 ** 3, 0])
125+
126+
127+
def test_get_values_below_labels_list():
128+
omega = [80, 80, 80]
129+
130+
cube_a_seg = [[10, 60, 55], 11, 1]
131+
cube_b_seg = [[50, 55, 42], 15, 2]
132+
cube_c_seg = [[25, 20, 20], 13, 3]
133+
cube_d_seg = [[55, 16, 9], 7, 4]
134+
135+
cube_a_anat = [[10, 60, 55], 11, 1.5]
136+
cube_b_anat = [[50, 55, 42], 15, 2.5]
137+
cube_c_anat = [[25, 20, 20], 13, 3.5]
138+
cube_d_anat = [[55, 16, 9], 7, 4.5]
139+
140+
sky_s = cube_shape(omega, center=cube_a_seg[0], side_length=cube_a_seg[1], foreground_intensity=cube_a_seg[2])
141+
sky_s += cube_shape(omega, center=cube_b_seg[0], side_length=cube_b_seg[1], foreground_intensity=cube_b_seg[2])
142+
sky_s += cube_shape(omega, center=cube_c_seg[0], side_length=cube_c_seg[1], foreground_intensity=cube_c_seg[2])
143+
sky_s += cube_shape(omega, center=cube_d_seg[0], side_length=cube_d_seg[1], foreground_intensity=cube_d_seg[2])
144+
145+
sky_a = cube_shape(omega, center=cube_a_anat[0], side_length=cube_a_anat[1], foreground_intensity=cube_a_anat[2], dtype=np.float32)
146+
sky_a += cube_shape(omega, center=cube_b_anat[0], side_length=cube_b_anat[1], foreground_intensity=cube_b_anat[2], dtype=np.float32)
147+
sky_a += cube_shape(omega, center=cube_c_anat[0], side_length=cube_c_anat[1], foreground_intensity=cube_c_anat[2], dtype=np.float32)
148+
sky_a += cube_shape(omega, center=cube_d_anat[0], side_length=cube_d_anat[1], foreground_intensity=cube_d_anat[2], dtype=np.float32)
149+
150+
im_segm = nib.Nifti1Image(sky_s, affine=np.eye(4))
151+
im_anat = nib.Nifti1Image(sky_a, affine=np.eye(4))
152+
153+
assert im_segm.shape == im_anat.shape
154+
155+
labels_list = [[1, 2], [3, 4], 4]
156+
157+
vals_below = get_values_below_labels_list(im_segm, im_anat, labels_list)
158+
159+
assert_array_equal(vals_below[0], np.array([1.5, ] * (11**3) + [2.5] * (15**3)) )
160+
assert_array_equal(vals_below[1], np.array([3.5, ] * (13**3) + [4.5] * (7**3)) )
161+
assert_array_equal(vals_below[2], np.array([4.5] * (7 ** 3)))
162+

0 commit comments

Comments
 (0)