Skip to content

Commit a8b9432

Browse files
committed
Rename normalize_by_neighborhood() to rescale_by_neighborhood().
Add Boolean option to set all rescaled values greater than 1 to 1.0. Former-commit-id: f993ece
1 parent 23eb8a5 commit a8b9432

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

mindboggle/shapes/measure.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,11 @@ def volume_per_label(labels, input_file):
401401

402402
return volumes.tolist(), labels
403403

404-
def normalize_by_neighborhood(scalars, indices, neighbor_lists, nedges=10, p=99):
404+
def rescale_by_neighborhood(scalars, indices, neighbor_lists, nedges=10, p=99,
405+
set_max_to_1=True):
405406
"""
406-
Normalize the scalar values of a VTK file by a percentile value in each
407-
vertex's surface mesh neighborhood.
407+
Rescale the scalar values of a VTK file by a percentile value
408+
in each vertex's surface mesh neighborhood.
408409
409410
Parameters
410411
----------
@@ -418,18 +419,20 @@ def normalize_by_neighborhood(scalars, indices, neighbor_lists, nedges=10, p=99)
418419
number or edges from vertex, defining the size of its neighborhood
419420
p : float in range of [0,100]
420421
percentile used to normalize each scalar
422+
set_max_to_1 : Boolean
423+
set all rescaled values greater than 1 to 1.0?
421424
422425
Returns
423426
-------
424-
normalized_scalars : list of floats
425-
normalized scalar values
427+
rescaled_scalars : list of floats
428+
rescaled scalar values
426429
427430
Examples
428431
--------
429432
>>> import os
430433
>>> from mindboggle.utils.io_vtk import read_scalars
431434
>>> from mindboggle.utils.mesh import find_neighbors_from_file
432-
>>> from mindboggle.shapes.measure import normalize_by_neighborhood
435+
>>> from mindboggle.shapes.measure import rescale_by_neighborhood
433436
>>> path = os.environ['MINDBOGGLE_DATA']
434437
>>> vtk_file = os.path.join(path, 'arno', 'measures', 'lh.pial.depth.vtk')
435438
>>> scalars, name = read_scalars(vtk_file, return_first=True, return_array=True)
@@ -439,16 +442,17 @@ def normalize_by_neighborhood(scalars, indices, neighbor_lists, nedges=10, p=99)
439442
>>> neighbor_lists = find_neighbors_from_file(vtk_file)
440443
>>> nedges = 10
441444
>>> p = 99
445+
>>> set_max_to_1 = True
442446
>>> #
443-
>>> normalized_scalars = normalize_by_neighborhood(scalars, indices,
447+
>>> rescaled_scalars = rescale_by_neighborhood(scalars, indices,
444448
>>> neighbor_lists, nedges, p)
445449
>>> #
446-
>>> # View normalized scalar values on folds:
450+
>>> # View rescaled scalar values on folds:
447451
>>> from mindboggle.utils.io_vtk import rewrite_scalars
448452
>>> from mindboggle.utils.mesh import plot_vtk
449-
>>> rewrite_scalars(vtk_file, 'test_normalize_by_neighborhood.vtk',
450-
>>> normalized_scalars, 'normalized_scalars', subfolds)
451-
>>> plot_vtk('test_normalize_by_neighborhood.vtk')
453+
>>> rewrite_scalars(vtk_file, 'test_rescale_by_neighborhood.vtk',
454+
>>> rescaled_scalars, 'rescaled_scalars', subfolds)
455+
>>> plot_vtk('test_rescale_by_neighborhood.vtk')
452456
453457
"""
454458
import numpy as np
@@ -459,7 +463,7 @@ def normalize_by_neighborhood(scalars, indices, neighbor_lists, nedges=10, p=99)
459463
scalars = np.asarray(scalars)
460464

461465
# Loop through all vertices:
462-
normalized_scalars = -1 * np.ones(len(scalars))
466+
rescaled_scalars = -1 * np.ones(len(scalars))
463467
for index in indices:
464468
print('{0} of {1})'.format(index, len(indices)))
465469

@@ -468,12 +472,13 @@ def normalize_by_neighborhood(scalars, indices, neighbor_lists, nedges=10, p=99)
468472

469473
# Compute a high neighborhood percentile to normalize the vertex's value:
470474
normalization_factor = np.percentile(scalars[neighborhood], p)
471-
normalized_scalar = scalars[index] / normalization_factor
472-
normalized_scalars[index] = normalized_scalar
475+
rescaled_scalar = scalars[index] / normalization_factor
476+
rescaled_scalars[index] = rescaled_scalar
473477

474-
# Make any normalized value greater than 1 equal to 1:
475-
for index in indices:
476-
if normalized_scalars[index] > 1:
477-
normalized_scalars[index] = 1.0
478+
# Make any rescaled value greater than 1 equal to 1:
479+
if set_max_to_1:
480+
for index in indices:
481+
if rescaled_scalars[index] > 1:
482+
rescaled_scalars[index] = 1.0
478483

479-
return normalized_scalars.tolist()
484+
return rescaled_scalars.tolist()

0 commit comments

Comments
 (0)