Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
mins = np.array(mins)
maxs = np.array(maxs)
diff = (maxs - mins) // 4
bounds_min = mins + diff
bounds_max = maxs - diff
CC_box[bounds_min[0]:bounds_max[0],
bounds_min[1]:bounds_max[1],
bounds_min[2]:bounds_max[2]] = 1
mask_cc_part, cfa = segment_from_cfa(tensorfit, CC_box, threshold,
return_cfa=True)
cfa_img = nib.Nifti1Image((cfa*255).astype(np.uint8), affine)
mask_cc_part_img = nib.Nifti1Image(mask_cc_part.astype(np.uint8), affine)
nib.save(mask_cc_part_img, 'mask_CC_part.nii.gz')
import matplotlib.pyplot as plt
region = 40
fig = plt.figure('Corpus callosum segmentation')
plt.subplot(1, 2, 1)
plt.title("Corpus callosum (CC)")
plt.axis('off')
red = cfa[..., 0]
plt.imshow(np.rot90(red[region, ...]))
plt.subplot(1, 2, 2)
plt.title("CC mask used for SNR computation")
plt.axis('off')
plt.imshow(np.rot90(mask_cc_part[region, ...]))
fig.savefig("CC_segmentation.png", bbox_inches='tight')
gfa = np.where(np.isnan(gfa), 0., gfa)
ttc = ThresholdTissueClassifier(gfa, .2)
# Create around N seeds
seeds = utils.seeds_from_mask(gfa > .25, 2, affine=affine)
seeds = seeds[::len(seeds) // N + 1]
# Create streamline generator
streamlines = LocalTracking(csapeaks, ttc, seeds, affine, .5, max_cross=1)
trk_streamlines = utils.move_streamlines(streamlines,
input_space=affine,
output_space=trackvis_affine)
trk = ((streamline, None, None) for streamline in trk_streamlines)
# Save streamlines
nib.trackvis.write(filename, trk, hdr)
path_im1 = os.path.join(root_dir, 'output/test_im1.nii.gz')
path_im2 = os.path.join(root_dir, 'output/test_im2.nii.gz')
nib.save(im_original, path_im1)
nib.save(im_renewed, path_im2)
if visual_assessment:
os.system('itksnap -g {}'.format(path_im1))
os.system('itksnap -g {}'.format(path_im2))
print 'Check if the second image opened is as the first multip. by 10.'
im1 = nib.load(im_original, path_im1)
im2 = nib.load(im_original, path_im2)
im1_data = im1.get_data()
im2_data = im2.get_data()
# check with a random sampling if the second image is the
# first multiplied by 10
num_samples = 20
i_v = np.random.choice(range(dims[0]), size=(num_samples, 1))
j_v = np.random.choice(range(dims[1]), size=(num_samples, 1))
k_v = np.random.choice(range(dims[2]), size=(num_samples, 1))
points = np.concatenate((i_v, j_v, k_v), axis=1)
assert_array_equal(points.shape, [num_samples, 3])
for m in range(num_samples):
def test_get_volumes_per_label_inconsistent_labels_labels_names():
omega = [10, 10, 3]
data_test = np.zeros(omega)
data_test[:2, :2, :2] = 2
data_test[-3:, -3:, -2:] = 3
im_test = nib.Nifti1Image(data_test, affine=np.eye(4))
with np.testing.assert_raises(IOError):
get_volumes_per_label(im_test, [0, 2, 3, 4], labels_names=['a', 'b'])
def fake_4dimage_nipy():
"""
:return: an empty 4-d nipy Image
"""
nx, ny, nz, nt = 9, 9, 9, 3 # image dimension
data = np.zeros((nx, ny, nz, nt), dtype=np.int8)
data[4, 4, 4, 0] = 1.
affine = np.eye(4)
# Create nibabel object
nii = nib.nifti1.Nifti1Image(data, affine)
# return nipy object
return nifti2nipy(nii)
def fake_3dimage_nipy():
"""
:return: an empty 3-d nipy Image
"""
nx, ny, nz = 9, 9, 9 # image dimension
data = np.zeros((nx, ny, nz), dtype=np.int8)
data[4, 4, 4] = 1.
affine = np.eye(4)
# Create nibabel object
nii = nib.nifti1.Nifti1Image(data, affine)
# return nipy object
return nifti2nipy(nii)
def fake_3dimage_custom(data):
"""
:return: a Nifti1Image (3D) in RAS+ space
"""
affine = np.eye(4)
return nibabel.nifti1.Nifti1Image(data, affine)
def check_dist_to_pial_vertices(elc_name, subject_fol, threshold):
(electrodes, names, hemis, threshold) = utils.load(op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
hemi_indices, close_verts_indices, all_dists, dural_mask = fect.get_t1_voxels_inside_dural(electrodes, subject_fol)
elc_ind = names.index(elc_name)
t1_tkras_coords = np.array([electrodes[elc_ind]])
verts, faces, normals = {}, {}, {}
for hemi in ['lh', 'rh']:
verts[hemi], faces[hemi] = nib.freesurfer.read_geometry(op.join(subject_fol, 'surf', '{}.dural'.format(hemi)))
normals[hemi] = fect.calc_normals(verts[hemi], faces[hemi])
hemi = hemis[elc_ind]
dists = cdist(t1_tkras_coords, verts[hemi])
close_verts = np.argmin(dists, axis=1)
is_inside = fect.point_in_mesh(t1_tkras_coords[0], verts[hemi][close_verts[0]], normals[hemi][close_verts[0]])
# vert_norm = np.linalg.norm(vertices[close_verts][0])
# elc_norm = np.linalg.norm(t1_tkras_coords[0])
print(is_inside)
def make_sct_image(data):
"""
:return: an Image (3D) in RAS+ (aka SCT LPI) space
data: scalar
"""
affine = np.eye(4)
nii = nibabel.nifti1.Nifti1Image(np.array([data, data]), affine)
img = Image(nii.get_data(), hdr=nii.header, orientation="LPI", dim=nii.header.get_data_shape())
return img
def fake_3dimage_nib_big():
"""
:return: an empty 3-d nibabel Image
"""
nx, ny, nz = 29, 39, 19 # image dimension
data = np.zeros((nx, ny, nz), dtype=np.int8)
data[14, 19, 9] = 1.
affine = np.eye(4)
# Create nibabel object
nii = nib.nifti1.Nifti1Image(data, affine)
return nii