Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_sphere():
"""
Test get_sphere functionality
"""
base_dir = str(Path(__file__).parent/"examples")
dir_path = base_dir + '/002/dmri'
img_file = dir_path + '/nodif_b0_bet.nii.gz'
img = nib.load(img_file)
r = 4
vox_dims = (2.0, 2.0, 2.0)
coords_file = dir_path + '/DesikanKlein2012/Default_coords_rsn.pkl'
with open(coords_file, 'rb') as file_:
coords = pickle.load(file_)
neighbors = []
for coord in coords:
neighbors.append(nodemaker.get_sphere(coord, r, vox_dims, img.shape[0:3]))
neighbors = [i for i in neighbors if len(i)>0]
assert len(neighbors) == 4
mx = len(np.unique(atlas_data.astype('uint16'))) - 1
g = nx.Graph(ecount=0, vcount=mx)
edge_dict = defaultdict(int)
node_dict = dict(zip(np.unique(atlas_data.astype('uint16')) + 1, np.arange(mx) + 1))
# Add empty vertices
for node in range(1, mx + 1):
g.add_node(node)
# Build graph
start_time = time.time()
ix = 0
for s in streamlines:
# Map the streamlines coordinates to voxel coordinates and get labels for label_volume
i, j, k = np.vstack(np.array([nodemaker.get_sphere(coord, error_margin, roi_zooms,
roi_shape) for coord in
_to_voxel_coordinates(s, lin_T, offset)])).T
# get labels for label_volume
lab_arr = atlas_data[i, j, k]
endlabels = []
for lab in np.unique(lab_arr).astype('uint32'):
if (lab > 0) and (np.sum(lab_arr == lab) >= overlap_thr):
try:
endlabels.append(node_dict[lab])
except UserWarning:
print("%s%s%s" % ('Label ', lab, ' missing from parcellation. Check registration and ensure valid '
'input parcellation file.'))
edges = combinations(endlabels, 2)
for edge in edges:
if parc is False:
i = -1
RSN_parcels = None
RSN_coords_vox = []
net_labels = []
for coords in coords_vox:
sphere_vol = np.zeros(RSNmask.shape, dtype=bool)
sphere_vol[tuple(coords)] = 1
i = i + 1
if (RSNmask.astype('bool') & sphere_vol).any():
print("%s%s%s%s" % (coords, ' coords falls within ', network, '...'))
RSN_coords_vox.append(coords)
net_labels.append(labels[i])
continue
else:
inds = get_sphere(coords, error, (np.abs(x_vox), y_vox, z_vox), RSNmask.shape)
sphere_vol[tuple(inds.T)] = 1
if (RSNmask.astype('bool') & sphere_vol).any():
print("%s%s%.2f%s%s%s" % (coords, ' coords is within a + or - ', float(error),
' mm neighborhood of ',
network, '...'))
RSN_coords_vox.append(coords)
net_labels.append(labels[i])
coords_mm = []
for i in RSN_coords_vox:
coords_mm.append(VoxTomm(bna_aff, i))
coords_mm = list(set(list(tuple(x) for x in coords_mm)))
else:
i = 0
RSN_parcels = []
coords_with_parc = []
print("%s%s" % ('Creating spherical ROI atlas with radius: ', node_size))
coords_vox = []
for i in coords:
coords_vox.append(mmToVox(mask_aff, i))
coords_vox = list(set(list(tuple(x) for x in coords_vox)))
x_vox = np.diagonal(mask_img.affine[:3, 0:3])[0]
y_vox = np.diagonal(mask_img.affine[:3, 0:3])[1]
z_vox = np.diagonal(mask_img.affine[:3, 0:3])[2]
sphere_vol = np.zeros(mask_img.shape, dtype=bool)
parcel_list = []
i = 0
for coord in coords_vox:
inds = get_sphere(coord, node_size, (np.abs(x_vox), y_vox, z_vox), mask_img.shape)
sphere_vol[tuple(inds.T)] = i * 1
parcel_list.append(nib.Nifti1Image(sphere_vol.astype('int'), affine=mask_aff))
i = i + 1
par_max = len(coords)
parc = True
return parcel_list, par_max, node_size, parc
print("%s%s" % ('Creating spherical ROI atlas with radius: ', node_size))
coords_vox = []
for i in coords:
coords_vox.append(mmToVox(mask_aff, i))
coords_vox = list(set(list(tuple(x) for x in coords_vox)))
x_vox = np.diagonal(mask_aff[:3, 0:3])[0]
y_vox = np.diagonal(mask_aff[:3, 0:3])[1]
z_vox = np.diagonal(mask_aff[:3, 0:3])[2]
sphere_vol = np.zeros(mask_shape, dtype=bool)
parcel_list = []
i = 0
for coord in coords_vox:
sphere_vol[tuple(get_sphere(coord, node_size, (np.abs(x_vox), y_vox, z_vox), mask_shape).T)] = i * 1
parcel_list.append(nib.Nifti1Image(sphere_vol.astype('uint16'), affine=mask_aff))
i = i + 1
par_max = len(coords)
if par_max > 0:
parc = True
else:
raise ValueError('Number of nodes is zero.')
return parcel_list, par_max, node_size, parc
if parc is False:
i = -1
RSN_parcels = None
RSN_coords_vox = []
net_labels = []
for coords in coords_vox:
sphere_vol = np.zeros(RSNmask.shape, dtype=bool)
sphere_vol[tuple(coords)] = 1
i = i + 1
if (RSNmask.astype('bool') & sphere_vol).any():
print("%s%s%s%s" % (coords, ' coords falls within ', network, '...'))
RSN_coords_vox.append(coords)
net_labels.append(labels[i])
continue
else:
inds = get_sphere(coords, error, (np.abs(x_vox), y_vox, z_vox), RSNmask.shape)
sphere_vol[tuple(inds.T)] = 1
if (RSNmask.astype('bool') & sphere_vol).any():
print("%s%s%.2f%s%s%s" % (coords, ' coords is within a + or - ', float(error),
' mm neighborhood of ',
network, '...'))
RSN_coords_vox.append(coords)
net_labels.append(labels[i])
coords_mm = []
for i in RSN_coords_vox:
coords_mm.append(VoxTomm(bna_aff, i))
coords_mm = list(set(list(tuple(x) for x in coords_mm)))
else:
i = 0
RSN_parcels = []
coords_with_parc = []