Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease the memory used when clustering #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions whitematteranalysis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ def spectral_atlas_label(input_polydata, atlas, number_of_jobs=2):
# 2) Do Normalized Cuts transform of similarity matrix.
# row sum estimate for current B part of the matrix
row_sum_2 = numpy.sum(B, axis=0) + \
numpy.dot(atlas.row_sum_matrix, B)
numpy.dot(numpy.float32(atlas.row_sum_matrix), B)

# in case of negative row sum estimation (this should not ever happen)
if any(row_sum_2<=0):
Expand All @@ -601,7 +601,7 @@ def spectral_atlas_label(input_polydata, atlas, number_of_jobs=2):


# normalized cuts normalization
row_sum = numpy.concatenate((atlas.row_sum_1, row_sum_2))
row_sum = numpy.concatenate((numpy.float32(atlas.row_sum_1), row_sum_2))
dhat = numpy.sqrt(numpy.divide(1, row_sum))
#dhat = numpy.sqrt(numpy.divide(1, numpy.concatenate((atlas.row_sum_1, row_sum_2))))
B = \
Expand All @@ -611,9 +611,9 @@ def spectral_atlas_label(input_polydata, atlas, number_of_jobs=2):
# <done already in atlas creation>

# 4) Compute embedding using eigenvectors
V = numpy.dot(numpy.dot(B.T, atlas.e_vec), \
numpy.diag(numpy.divide(1.0, atlas.e_val)))
V = numpy.divide(V, atlas.e_vec_norm)
V = numpy.dot(numpy.dot(B.T, numpy.float32(atlas.e_vec)), \
numpy.diag(numpy.divide(1.0, numpy.float32(atlas.e_val))))
V = numpy.divide(V, numpy.float32(atlas.e_vec_norm))
embed = numpy.zeros((number_fibers, atlas.number_of_eigenvectors))
for i in range(0, atlas.number_of_eigenvectors):
embed[:,i] = numpy.divide(V[:,-(i+2)], V[:,-1])
Expand Down Expand Up @@ -665,19 +665,21 @@ def _rectangular_distance_matrix(input_polydata_n, input_polydata_m, threshold,
landmarks_n = numpy.zeros((fiber_array_n.number_of_fibers,3))

# pairwise distance matrix
all_fibers_n = range(0, fiber_array_n.number_of_fibers)

distances = Parallel(n_jobs=number_of_jobs,
verbose=0)(
delayed(similarity.fiber_distance)(
distances = numpy.float32(numpy.zeros([fiber_array_n.number_of_fibers, fiber_array_m.number_of_fibers]))
# all_fibers_n = range(0, fiber_array_n.number_of_fibers)
#
# distances = Parallel(n_jobs=number_of_jobs,
# verbose=0)(
for lidx in xrange(fiber_array_n.number_of_fibers):
distances[lidx,:] = similarity.fiber_distance(
fiber_array_n.get_fiber(lidx),
fiber_array_m,
threshold, distance_method=distance_method,
fiber_landmarks=landmarks_n[lidx,:],
landmarks=landmarks_m, bilateral=bilateral)
for lidx in all_fibers_n)
# for lidx in all_fibers_n)

distances = numpy.array(distances).T
distances = distances.T

return distances

Expand Down
6 changes: 3 additions & 3 deletions whitematteranalysis/similarity.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def _fiber_distance_internal_use(fiber, fiber_array, threshold=0, distance_metho
print "ERROR: Please use distance method Landmarks to compute landmark distances"

# compute the distance from this fiber to the array of other fibers
ddx = fiber_array.fiber_array_r - fiber.r
ddy = fiber_array.fiber_array_a - fiber.a
ddz = fiber_array.fiber_array_s - fiber.s
ddx = numpy.float32(fiber_array.fiber_array_r - fiber.r)
ddy = numpy.float32(fiber_array.fiber_array_a - fiber.a)
ddz = numpy.float32(fiber_array.fiber_array_s - fiber.s)

dx = numpy.square(ddx)
dy = numpy.square(ddy)
Expand Down