Skip to content

Commit

Permalink
Merge pull request #474 from arnaudbore/fix_numpy_warning
Browse files Browse the repository at this point in the history
Fix numpy warning (round 2)
  • Loading branch information
arnaudbore authored Jul 12, 2021
2 parents db91243 + 2da4cb5 commit 7e170d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
25 changes: 14 additions & 11 deletions scilpy/segment/streamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def filter_grid_roi_both(sft, mask_1, mask_2):
# For endpoint filtering, we need to keep 2 separately
# Could be faster for either end, but the code look cleaner like this
line_based_indices = []
voxel_beg = np.asarray([s[0] for s in streamline_vox]).astype(np.int16).transpose(1, 0)
voxel_end = np.asarray([s[-1] for s in streamline_vox]).astype(np.int16).transpose(1, 0)
voxel_beg = np.asarray([s[0] for s in streamline_vox],
dtype=np.int16).transpose(1, 0)
voxel_end = np.asarray([s[-1] for s in streamline_vox],
dtype=np.int16).transpose(1, 0)

map1_beg = map_coordinates(mask_1, voxel_beg, order=0, mode='nearest')
map2_beg = map_coordinates(mask_2, voxel_beg, order=0, mode='nearest')
Expand Down Expand Up @@ -154,7 +156,7 @@ def filter_grid_roi(sft, mask, filter_type, is_exclude):
if is_exclude:
line_based_indices = np.setdiff1d(range(len(sft)),
np.unique(line_based_indices))
line_based_indices = np.asarray(line_based_indices).astype(np.int32)
line_based_indices = np.asarray(line_based_indices, dtype=np.int32)

# From indices to sft
streamlines = sft.streamlines[line_based_indices]
Expand Down Expand Up @@ -196,7 +198,7 @@ def pre_filtering_for_geometrical_shape(sft, size,

# Create relevant info about the ellipsoid in vox/world space
if is_in_vox:
center = np.asarray(apply_affine(transfo, center))
center = np.asarray(apply_affine(transfo, center), dtype=float)
bottom_corner = center - size
top_corner = center + size
x_val = [bottom_corner[0], top_corner[0]]
Expand Down Expand Up @@ -253,7 +255,8 @@ def filter_ellipsoid(sft, ellipsoid_radius, ellipsoid_center,

if is_in_vox:
ellipsoid_center = np.asarray(apply_affine(transfo,
ellipsoid_center))
ellipsoid_center),
dtype=float)
selected_by_ellipsoid = []
line_based_indices_1 = []
line_based_indices_2 = []
Expand All @@ -262,8 +265,8 @@ def filter_ellipsoid(sft, ellipsoid_radius, ellipsoid_center,
# The result won't be identical to MI-Brain since I am not using the
# vtkPolydata. Also it won't be identical to TrackVis either,
# because TrackVis is point-based for Spherical ROI...
ellipsoid_radius = np.asarray(ellipsoid_radius)
ellipsoid_center = np.asarray(ellipsoid_center)
ellipsoid_radius = np.asarray(ellipsoid_radius, dtype=float)
ellipsoid_center = np.asarray(ellipsoid_center, dtype=float)

for i, line in enumerate(pre_filtered_streamlines):
if filter_type in ['any', 'all']:
Expand Down Expand Up @@ -307,7 +310,7 @@ def filter_ellipsoid(sft, ellipsoid_radius, ellipsoid_center,
if is_exclude:
selected_by_ellipsoid = np.setdiff1d(range(len(sft)),
np.unique(selected_by_ellipsoid))
line_based_indices = np.asarray(selected_by_ellipsoid).astype(np.int32)
line_based_indices = np.asarray(selected_by_ellipsoid, dtype=np.int32)

# From indices to sft
streamlines = sft.streamlines[line_based_indices]
Expand Down Expand Up @@ -359,8 +362,8 @@ def filter_cuboid(sft, cuboid_radius, cuboid_center,
line_based_indices_2 = []
# Also here I am not using a mathematical intersection and
# I am not using vtkPolyData like in MI-Brain, so not exactly the same
cuboid_radius = np.asarray(cuboid_radius)
cuboid_center = np.asarray(cuboid_center)
cuboid_radius = np.asarray(cuboid_radius, dtype=float)
cuboid_center = np.asarray(cuboid_center, dtype=float)
for i, line in enumerate(pre_filtered_streamlines):
if filter_type in ['any', 'all']:
# Resample to 1/10 of the voxel size
Expand Down Expand Up @@ -404,7 +407,7 @@ def filter_cuboid(sft, cuboid_radius, cuboid_center,
if is_exclude:
selected_by_cuboid = np.setdiff1d(range(len(sft)),
np.unique(selected_by_cuboid))
line_based_indices = np.asarray(selected_by_cuboid).astype(np.int32)
line_based_indices = np.asarray(selected_by_cuboid, dtype=np.int32)

# From indices to sft
streamlines = sft.streamlines[line_based_indices]
Expand Down
7 changes: 4 additions & 3 deletions scilpy/tracking/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def filter_streamlines_by_length(sft, min_length=0., max_length=np.inf):
else:
filter_stream = []

filtered_streamlines = list(np.asarray(sft.streamlines)[filter_stream])
filtered_streamlines = list(np.asarray(sft.streamlines,
dtype=object)[filter_stream])
filtered_data_per_point = sft.data_per_point[filter_stream]
filtered_data_per_streamline = sft.data_per_streamline[filter_stream]

Expand Down Expand Up @@ -80,7 +81,7 @@ def get_subset_streamlines(sft, max_streamlines, rng_seed=None):
ind = np.arange(len(sft.streamlines))
rng.shuffle(ind)

subset_streamlines = list(np.asarray(sft.streamlines)[
subset_streamlines = list(np.asarray(sft.streamlines, dtype=object)[
ind[:max_streamlines]])
subset_data_per_point = sft.data_per_point[ind[:max_streamlines]]
subset_data_per_streamline = sft.data_per_streamline[ind[:max_streamlines]]
Expand Down Expand Up @@ -205,7 +206,7 @@ def smooth_line_gaussian(streamline, sigma):
x3 = gaussian_filter1d(x, sigma)
y3 = gaussian_filter1d(y, sigma)
z3 = gaussian_filter1d(z, sigma)
smoothed_streamline = np.asarray([x3, y3, z3]).T
smoothed_streamline = np.asarray([x3, y3, z3], dtype=float).T

# Ensure first and last point remain the same
smoothed_streamline[0] = streamline[0]
Expand Down
2 changes: 1 addition & 1 deletion scilpy/utils/bvec_bval_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def identify_shells(bvals, threshold=40.0, roundCentroids=False, sort=False):
# Finding centroids
bval_centroids = [bvals[0]]
for bval in bvals[1:]:
diffs = np.abs(np.asarray(bval_centroids) - bval)
diffs = np.abs(np.asarray(bval_centroids, dtype=float) - bval)
if not len(np.where(diffs < threshold)[0]):
# Found no bval in bval centroids close enough to the current one.
# Create new centroid (i.e. new shell)
Expand Down
2 changes: 1 addition & 1 deletion scilpy/utils/metrics_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _get_profile_one_streamline(streamline, metrics_files):
# Each tuple has S elements, where S is the number of streamlines.
# We then convert each tuple to a numpy array
for metric_values in zip(*metrics_per_strl):
converted.append(np.asarray(metric_values))
converted.append(np.asarray(metric_values, dtype=float))

return converted

Expand Down

0 comments on commit 7e170d4

Please sign in to comment.