Skip to content

Commit 64fa801

Browse files
committed
WIP update after removing phantom generator and ICV estimation
1 parent de8ee04 commit 64fa801

28 files changed

+145
-1487
lines changed

CONTRIBUTE.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# Contributing to nilabel
1+
# Contributing to NiLabels
22

33
Thank you for being there!
44

5-
Nilabel (ex LABelsToolkit) started as a python package containing a range of heterogeneous imaging tools to perform
5+
NiLabels (ex LABelsToolkit) started as a python package containing a range of heterogeneous imaging tools to perform
66
quick manipulations and measurement on segmentations from ipython or jupyter notebook.
77
Initially planned to support several projects undertook by the initial author, after some development and refactoring
8-
it is not intended to be part of the Nipy ecosystem, to provide the community with another hopefully helpful tool
9-
in neuroimaging research.
8+
it is now intended to be part of the Nipy ecosystem, to provide the neuroimaging developer community with another tool.
109

1110
## Code of Conduct
1211

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ nis_app.manipulate_labels.relabel('my_input_folder_path/my_segm.nii.gz', 'my_out
5252
### How to install (in development mode)
5353

5454

55-
+ Install python requirements in requirements.txt with
56-
`pip install -r requirements.txt`
57-
in a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/).
58-
5955
+ Install NiLabels: the current version is not (yet) pip installable. It can be installed in development mode.
6056
To proceede, initialise a virtual environment and execute the following instructions:
6157
```
@@ -67,6 +63,11 @@ pip install -e .
6763
In development mode every change made to your local code will be directly affecting the libray installed in the python distribution
6864
without the need of reinstalling.
6965

66+
67+
+ Install python requirements in requirements.txt with
68+
`pip install -r requirements.txt`
69+
in a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/).
70+
7071
+ For advanced method `symmetrise_wit_registration`, extra examples and quick arrays visualisation with ITK-snap you must
7172
+ Install [NiftySeg](https://github.com/KCL-BMEIS/NiftySeg)
7273
+ Install [NiftyReg](https://github.com/KCL-BMEIS/niftyreg)

examples/a_generate_headlike_phantoms.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/a_generate_phantoms_for_examples.py

Lines changed: 116 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,116 @@
55
import numpy as np
66
import scipy.ndimage.filters as fil
77

8-
from nilabels.tools.defs import root_dir
8+
from nilabels.defs import root_dir
99
from nilabels.tools.detections.get_segmentation import intensity_segmentation
10-
from nilabels.tools.phantoms_generator.shapes_for_phantoms import ellipsoid_shape, o_shape, c_shape, \
11-
cube_shape
1210

1311

14-
def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, foreground=10, noise=5000):
15-
print(creation_list)
16-
print('\n.\n.\n\nGenerating figures for examples, may take some seconds and approx 150MB for the whole '
17-
'dataset.\n.\n.')
18-
pfo_examples = jph(root_dir, 'data_examples')
12+
# ---------- Simple shapes generators ---------------
13+
14+
15+
def o_shape(omega=(250, 250), radius=50,
16+
background_intensity=0, foreground_intensity=20, dtype=np.uint8):
17+
m = background_intensity * np.ones(omega, dtype=dtype)
18+
19+
if len(omega) == 2:
20+
c = [omega[j] / 2 for j in range(len(omega))]
21+
for x in range(omega[0]):
22+
for y in range(omega[1]):
23+
if (x - c[0]) ** 2 + (y - c[1]) ** 2 < radius ** 2:
24+
m[x, y] = foreground_intensity
25+
elif len(omega) == 3:
26+
c = [omega[j] / 2 for j in range(len(omega))]
27+
for x in range(omega[0]):
28+
for y in range(omega[1]):
29+
for z in range(omega[2]):
30+
if (x - c[0]) ** 2 + (y - c[1]) ** 2 + (z - c[2]) ** 2 < radius ** 2:
31+
m[x, y, z] = foreground_intensity
32+
return m
33+
34+
35+
def c_shape(omega=(250, 250), internal_radius=40, external_radius=60, opening_height=50,
36+
background_intensity=0, foreground_intensity=20, dtype=np.uint8, margin=None):
37+
38+
def get_a_2d_c(omega2, internal_radius2d, external_radius2d, opening_height2d, background_intensity2d,
39+
foreground_intensity2d, dtype2d):
40+
41+
m = background_intensity2d * np.ones(omega2[:2], dtype=dtype2d)
42+
43+
c = [omega2[j] / 2 for j in range(len(omega2))]
44+
# create the crown
45+
for x in range(omega2[0]):
46+
for y in range(omega2[1]):
47+
if internal_radius2d ** 2 < (x - c[0]) ** 2 + (y - c[1]) ** 2 < external_radius2d ** 2:
48+
m[x, y] = foreground_intensity2d
49+
50+
# open the c
51+
low_lim = int(omega2[0] / 2) - int(opening_height2d / 2)
52+
high_lim = int(omega2[0] / 2) + int(opening_height2d / 2)
53+
54+
for x in range(omega2[0]):
55+
for y in range(int(omega2[1] / 2), omega2[1]):
56+
if low_lim < x < high_lim and m[x, y] == foreground_intensity2d:
57+
m[x, y] = background_intensity2d
58+
59+
return m
60+
61+
c_2d = get_a_2d_c(omega2=omega[:2], internal_radius2d=internal_radius, external_radius2d=external_radius,
62+
opening_height2d=opening_height, background_intensity2d=background_intensity,
63+
foreground_intensity2d=foreground_intensity, dtype2d=dtype)
64+
65+
if len(omega) == 2:
66+
return c_2d
67+
68+
elif len(omega) == 3:
69+
if margin is None:
70+
return np.repeat(c_2d, omega[2]).reshape(omega)
71+
else:
72+
res = np.zeros(omega, dtype=dtype)
73+
for z in range(margin, omega[2] - 2 * margin):
74+
res[..., z] = c_2d
75+
return res
76+
77+
78+
def ellipsoid_shape(omega, focus_1, focus_2, distance, background_intensity=0, foreground_intensity=100,
79+
dtype=np.uint8):
80+
sky = background_intensity * np.ones(omega, dtype=dtype)
81+
for xi in range(omega[0]):
82+
for yi in range(omega[1]):
83+
for zi in range(omega[2]):
84+
if np.sqrt((focus_1[0] - xi) ** 2 + (focus_1[1] - yi) ** 2 + (focus_1[2] - zi) ** 2) + \
85+
np.sqrt((focus_2[0] - xi) ** 2 + (focus_2[1] - yi) ** 2 + (focus_2[2] - zi) ** 2) <= distance:
86+
sky[xi, yi, zi] = foreground_intensity
87+
return sky
1988

20-
if creation_list['Examples folder']:
2189

90+
def cube_shape(omega, center, side_length, background_intensity=0, foreground_intensity=100, dtype=np.uint8):
91+
sky = background_intensity * np.ones(omega, dtype=dtype)
92+
half_side_length = int(np.ceil(int(side_length / 2)))
93+
94+
for lx in range(-half_side_length, half_side_length + 1):
95+
for ly in range(-half_side_length, half_side_length + 1):
96+
for lz in range(-half_side_length, half_side_length + 1):
97+
sky[center[0] + lx, center[1] + ly, center[2] + lz] = foreground_intensity
98+
return sky
99+
100+
101+
def sphere_shape(omega, centre, radius, foreground_intensity=100, dtype=np.uint8):
102+
sky = np.zeros(omega, dtype=dtype)
103+
for xi in range(omega[0]):
104+
for yi in range(omega[1]):
105+
for zi in range(omega[2]):
106+
if np.sqrt((centre[0] - xi) ** 2 + (centre[1] - yi) ** 2 + (centre[2] - zi) ** 2) <= radius:
107+
sky[xi, yi, zi] = foreground_intensity
108+
return sky
109+
110+
111+
def generate_figures(creation_list, pfo_examples, segmentation_levels=7, sigma_smoothing=6, foreground=10):
112+
print('\n.\n.\n\nGenerate figures for the examples, may take some seconds, and will take approx 150MB.\n.\n.')
113+
114+
if creation_list['Examples folder']:
22115
os.system('mkdir -p ' + pfo_examples)
23116

24117
if creation_list['Punt e mes']:
25-
26118
data_o_punt = o_shape(omega=(256, 256, 256), foreground_intensity=foreground, dtype=np.float64)
27119
data_o_punt = fil.gaussian_filter(data_o_punt, sigma=sigma_smoothing)
28120

@@ -54,7 +146,6 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
54146
print('mes generated')
55147

56148
if creation_list['C']:
57-
58149
data_c_ = c_shape(omega=(256, 256, 256), foreground_intensity=foreground)
59150
data_c_ = fil.gaussian_filter(data_c_, sigma_smoothing)
60151

@@ -76,7 +167,7 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
76167
if creation_list['Planetaruim']:
77168

78169
omega = (256, 256, 256)
79-
centers = [(32, 36, 40), (100, 61, 94), (130, 140, 99), (220, 110, 210),
170+
centers = [(32, 36, 40), (100, 61, 94), (130, 140, 99), (220, 110, 210),
80171
(224, 220, 216), (156, 195, 162), (126, 116, 157), (36, 146, 46)]
81172
radii = [21, 55, 34, 13, 21, 55, 34, 13]
82173
intensities = [100] * 8
@@ -90,7 +181,7 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
90181
if (center[0] - x) ** 2 + (center[1] - y) ** 2 + (center[2] - z) ** 2 <= radius ** 2:
91182
sky[x, y, z] = intensity
92183

93-
planetarium = fil.gaussian_filter(sky, np.min(radii)-np.std(radii))
184+
planetarium = fil.gaussian_filter(sky, np.min(radii) - np.std(radii))
94185

95186
nib_planetarium = nib.Nifti1Image(planetarium, affine=np.eye(4))
96187
nib.save(nib_planetarium, filename=jph(pfo_examples, 'planetarium.nii.gz'))
@@ -103,12 +194,12 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
103194
omega = (120, 140, 160)
104195
epsilon = 5 # perturbation set to 0 to restore the axial symmetry
105196
foci_ellipses_left = [np.array([35, 70, 40]), np.array([50, 70, 120])]
106-
foci_ellipses_right = [np.array([85 + epsilon, 70 - epsilon, 40+epsilon]), np.array([70, 70, 120])]
197+
foci_ellipses_right = [np.array([85 + epsilon, 70 - epsilon, 40 + epsilon]), np.array([70, 70, 120])]
107198
d_left = 95
108199
d_right = 95
109200

110-
ellipsoid_left = ellipsoid_shape(omega, foci_ellipses_left[0], foci_ellipses_left[1], d_left,
111-
foreground_intensity=foreground)
201+
ellipsoid_left = ellipsoid_shape(omega, foci_ellipses_left[0], foci_ellipses_left[1], d_left,
202+
foreground_intensity=foreground)
112203
ellipsoid_right = ellipsoid_shape(omega, foci_ellipses_right[0], foci_ellipses_right[1], d_right,
113204
foreground_intensity=foreground)
114205

@@ -130,12 +221,6 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
130221
nib_ellipsoids_seg_half = nib.Nifti1Image(two_ellipsoids_half_segmentation, affine=np.eye(4))
131222
nib.save(nib_ellipsoids_seg_half, filename=jph(pfo_examples, 'ellipsoids_seg_half.nii.gz'))
132223

133-
salt_and_pepper = np.zeros_like(two_ellipsoids_segmentation)
134-
for x, y, z in zip(*[np.random.randint(1, k, size=noise) for k in salt_and_pepper.shape]):
135-
salt_and_pepper[x, y, z] = np.random.uniform(1, 4)
136-
nib_ellipsoids_seg_salt_and_pepper = nib.Nifti1Image(two_ellipsoids_segmentation + salt_and_pepper, affine=np.eye(4))
137-
nib.save(nib_ellipsoids_seg_salt_and_pepper, filename=jph(pfo_examples, 'ellipsoids_seg_noisy.nii.gz'))
138-
139224
print('Buckle ellipsoids half segmented and whole segmented generated')
140225

141226
if creation_list['Ellipsoids family']:
@@ -159,7 +244,6 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
159244

160245
# --- Generate random ellipsoids in omega: --
161246
for k in range(1, num_ellipsoids + 1):
162-
163247
mid_point = np.array([50, 50, 50])
164248
# Get the first focus in a sphere at (25, 25, 50) with radius 15
165249
center_first_focus = np.array([25, 50, 50])
@@ -168,7 +252,7 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
168252
u, v = np.random.uniform(), np.random.uniform()
169253
# sphere point picking (Wolfram)
170254
theta = 2 * np.pi * u
171-
phi = np.arccos(2*v - 1)
255+
phi = np.arccos(2 * v - 1)
172256
first_focus = np.array([radius_first_focus * np.cos(theta) * np.sin(phi) + center_first_focus[0],
173257
radius_first_focus * np.sin(theta) * np.sin(phi) + center_first_focus[1],
174258
radius_first_focus * np.cos(phi) + center_first_focus[2]])
@@ -212,7 +296,6 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
212296
os.system(cmd)
213297

214298
if creation_list['Cubes in the sky']:
215-
216299
omega = [80, 80, 80]
217300
cube_a = [[10, 60, 55], 11, 1]
218301
cube_b = [[50, 55, 42], 17, 2]
@@ -235,13 +318,12 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
235318
nib.save(im2, filename=jph(pfo_examples, 'cubes_in_space.nii.gz'))
236319

237320
if creation_list['Sandwich']:
238-
239321
omega = [9, 9, 10]
240322
sandwich = np.zeros(omega)
241323

242-
sandwich[:, :2, :] = 2 * np.ones([9, 2, 10])
324+
sandwich[:, :2, :] = 2 * np.ones([9, 2, 10])
243325
sandwich[:, 2:5, :] = 3 * np.ones([9, 3, 10])
244-
sandwich[:, 5:, :] = 4 * np.ones([9, 4, 10])
326+
sandwich[:, 5:, :] = 4 * np.ones([9, 4, 10])
245327
im_sandwich = nib.Nifti1Image(sandwich, affine=np.diag([0.1, 0.2, 0.3, 1]))
246328

247329
nib.save(im_sandwich, filename=jph(pfo_examples, 'sandwich.nii.gz'))
@@ -298,7 +380,7 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
298380
# f = 15
299381
# a = np.sqrt( (f ** 2 + np.sqrt(f**4 + 4*r**4)) / 2. )
300382
a = 22
301-
f = np.sqrt((a**4 - r ** 4) / float(a**2))
383+
f = np.sqrt((a ** 4 - r ** 4) / float(a ** 2))
302384
assert f < a, [f, a]
303385
first_focus_23 = [75 - f, 25, 25] # ellipsoid
304386
second_focus_23 = [75 + f, 25, 25]
@@ -340,16 +422,16 @@ def generate_figures(creation_list, segmentation_levels=7, sigma_smoothing=6, fo
340422

341423

342424
if __name__ == '__main__':
343-
344-
creation_list = {'Examples folder' : True,
425+
creation_steps = {'Examples folder' : True,
345426
'Punt e mes' : True,
346427
'C' : True,
347428
'Planetaruim' : True,
348429
'Buckle ellipsoids' : True,
349430
'Ellipsoids family' : True,
350431
'Cubes in the sky' : True,
351432
'Sandwich' : True,
352-
'Four-folds' : True
353-
}
433+
'Four-folds' : True}
434+
435+
path_folder_examples = jph(root_dir, 'data_examples')
354436

355-
generate_figures(creation_list)
437+
generate_figures(creation_steps, path_folder_examples)

examples/caliber_usage_distances_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import os
21
from os.path import join as jph
32

43
from nilabels.agents.agents_controller import AgentsController as LT
4+
from nilabels.defs import root_dir
55
from nilabels.tools.caliber.distances import dice_score, covariance_distance, hausdorff_distance
6-
from nilabels.tools.defs import root_dir
76

87
if __name__ == '__main__':
98

examples/caliber_usage_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from nilabels.agents.agents_controller import AgentsController as NiL
7-
from nilabels.tools.defs import root_dir
7+
from nilabels.defs import root_dir
88

99
if __name__ == '__main__':
1010

examples/manipulator_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import nilabels.tools.image_colors_manipulations.relabeller as rel
88
import nilabels.tools.image_shape_manipulations.splitter as sp
99
from nilabels.agents.agents_controller import AgentsController
10+
from nilabels.defs import root_dir
1011
from nilabels.tools.aux_methods.utils_nib import set_new_data
11-
from nilabels.tools.defs import root_dir
1212

1313
if __name__ == '__main__':
1414

examples/prototype_check_imperfections.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from os.path import join as jph
33

44
import a_generate_phantoms_for_examples as gen
5-
65
from nilabels.agents.agents_controller import AgentsController as NiL
6+
from nilabels.defs import root_dir
77
from nilabels.tools.aux_methods.label_descriptor_manager import generate_dummy_label_descriptor
8-
from nilabels.tools.defs import root_dir
98

109
# ---- GENERATE DATA ----
1110

examples/prototype_clean_a_segmentation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import os
22
from os.path import join as jph
33

4-
import a_generate_phantoms_for_examples as gen
54
import nibabel as nib
65
import numpy as np
76

7+
import a_generate_phantoms_for_examples as gen
88
from nilabels.agents.agents_controller import AgentsController as NiL
9-
from nilabels.tools.defs import root_dir
9+
from nilabels.defs import root_dir
1010

1111
# ---- GENERATE DATA ----
1212

examples/prototype_segment_an_image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
from os.path import join as jph
33

44
import a_generate_phantoms_for_examples as gen
5-
65
from nilabels.agents.agents_controller import AgentsController as NiL
7-
from nilabels.tools.defs import root_dir
6+
from nilabels.defs import root_dir
87

98
# ---- GENERATE DATA ----
109

0 commit comments

Comments
 (0)