Ejemplo n.º 1
0
# Local import
from get_data_light import DATA_DIR, get_second_level_dataset

# paths
input_image = path.join(DATA_DIR, 'spmT_0029.nii.gz')
mask_image = path.join(DATA_DIR, 'mask.nii.gz')
if (not path.exists(mask_image)) or (not path.exists(input_image)):
    get_second_level_dataset()

# write directory
write_dir = path.join(getcwd(), 'results')
if not path.exists(write_dir):
    mkdir(write_dir)

# read the data
mask = load(mask_image).get_data() > 0
ijk = np.array(np.where(mask)).T
nvox = ijk.shape[0]
data = load(input_image).get_data()[mask]
image_field = Field(nvox)
image_field.from_3d_grid(ijk, k=6)
image_field.set_field(data)
u, _ = image_field.ward(100)

# write the results
label_image = path.join(write_dir, 'label.nii')
wdata = mask - 1
wdata[mask] = u
save(Nifti1Image(wdata, load(mask_image).get_affine()), label_image)
print("Label image written in %s" % label_image)
Ejemplo n.º 2
0
try:
    import matplotlib.pyplot as plt
except ImportError:
    raise RuntimeError("This script needs the matplotlib library")

from nipy.algorithms.graph.field import Field

dx = 50
dy = 50
dz = 1
nbseeds = 10
data = gaussian_filter(np.random.randn(dx, dy), 2)
F = Field(dx * dy * dz)
xyz = np.reshape(np.indices((dx, dy, dz)), (3, dx * dy * dz)).T.astype(np.int)
F.from_3d_grid(xyz, 6)
F.set_field(data)

seeds = np.argsort(nr.rand(F.V))[:nbseeds]
seeds, label, J0 = F.geodesic_kmeans(seeds)
wlabel, J1 = F.ward(nbseeds)
seeds, label, J2 = F.geodesic_kmeans(seeds, label=wlabel.copy(), eps=1.e-7)

print('Inertia values for the 3 algorithms: ')
print('Geodesic k-means: ', J0, 'Wards: ', J1, 'Wards + gkm: ', J2)

plt.figure(figsize=(8, 4))
plt.subplot(1, 3, 1)
plt.imshow(np.reshape(data, (dx, dy)), interpolation='nearest')
plt.title('Input data')
plt.subplot(1, 3, 2)
Ejemplo n.º 3
0
try:
    import matplotlib.pyplot as plt
except ImportError:
    raise RuntimeError("This script needs the matplotlib library")

from nipy.algorithms.graph.field import Field

dx = 50
dy = 50
dz = 1
nbseeds = 10
data = gaussian_filter(np.random.randn(dx, dy), 2)
F = Field(dx * dy * dz)
xyz = np.reshape(np.indices((dx, dy, dz)), (3, dx * dy * dz)).T.astype(np.int)
F.from_3d_grid(xyz, 6)
F.set_field(data)

seeds = np.argsort(nr.rand(F.V))[:nbseeds]
seeds, label, J0 = F.geodesic_kmeans(seeds)
wlabel, J1 = F.ward(nbseeds)
seeds, label, J2 = F.geodesic_kmeans(seeds, label=wlabel.copy(), eps=1.0e-7)

print "Inertia values for the 3 algorithms: "
print "Geodesic k-means: ", J0, "Wards: ", J1, "Wards + gkm: ", J2

plt.figure(figsize=(8, 4))
plt.subplot(1, 3, 1)
plt.imshow(np.reshape(data, (dx, dy)), interpolation="nearest")
plt.title("Input data")
plt.subplot(1, 3, 2)
Ejemplo n.º 4
0
                [30, 20]])
ampli = np.array([3, 4, 4])

nbvox = dimx * dimy
x = simul.surrogate_2d_dataset(nbsubj=1, dimx=dimx, dimy=dimy,
                               pos=pos, ampli=ampli, width=10.0)

x = np.reshape(x, (dimx, dimy, 1))
beta = np.reshape(x, (nbvox, 1))
xyz = np.array(np.where(x))
nbvox = np.size(xyz, 1)
th = 2.36

# compute the field structure and perform the watershed
Fbeta = Field(nbvox)
Fbeta.from_3d_grid(xyz.T.astype(np.int), 18)
Fbeta.set_field(beta)
idx, label = Fbeta.custom_watershed(0, th)

#compute the region-based signal average
bfm = np.array([np.mean(beta[label == k]) for k in range(label.max() + 1)])
bmap = np.zeros(nbvox)
if label.max() > - 1:
    bmap[label > - 1] = bfm[label[label > - 1]]

label = np.reshape(label, (dimx, dimy))
bmap = np.reshape(bmap, (dimx, dimy))

###############################################################################
# plot the input image
Ejemplo n.º 5
0
from nipy.algorithms.graph.field import Field

# Local import
from get_data_light import DATA_DIR, get_second_level_dataset

# paths
swd = os.getcwd()
input_image = os.path.join(DATA_DIR, 'spmT_0029.nii.gz')
mask_image = os.path.join(DATA_DIR, 'mask.nii.gz')

if (not os.path.exists(mask_image)) or (not os.path.exists(input_image)):
    get_second_level_dataset()

# read the data
mask = load(mask_image).get_data() > 0
ijk = np.array(np.where(mask)).T
nvox = ijk.shape[0]
data = load(input_image).get_data()[mask]
image_field = Field(nvox)
image_field.from_3d_grid(ijk, k=6)
image_field.set_field(data)
u, _ = image_field.ward(100)

# write the results
label_image = os.path.join(swd, 'label.nii')
wdata = mask - 1
wdata[mask] = u
save(Nifti1Image(wdata, load(mask_image).get_affine()), label_image)
print "Label image written in %s" % label_image