-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add knn interpolation parameter to help with projecting from volume t…
…o surface space
- Loading branch information
1 parent
3fdd57f
commit dc4a9f2
Showing
5 changed files
with
124 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
__all__=["analysis", "extraction"] | ||
|
||
# Version in single place | ||
__version__ = "0.13.1" | ||
__version__ = "0.13.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,75 @@ | ||
"""Internal function for turning CAPs into NifTI Statistical Maps""" | ||
import textwrap, warnings | ||
import nibabel as nib, numpy as np | ||
from nilearn import image | ||
from nilearn import datasets, image | ||
from scipy.spatial import cKDTree | ||
|
||
def _cap2statmap(atlas_file, cap_vector, fwhm): | ||
def _cap2statmap(atlas_file, cap_vector, fwhm, knn_dict): | ||
atlas = nib.load(atlas_file) | ||
atlas_fdata = atlas.get_fdata() | ||
# Get array containing all labels in atlas to avoid issue if atlas labels dont start at 1, like Nilearn's AAL map | ||
# Get array containing all labels in atlas to avoid issue if the first non-zero atlas label is not 1 | ||
target_array = sorted(np.unique(atlas_fdata)) | ||
for indx, value in enumerate(cap_vector): | ||
actual_indx = indx + 1 | ||
atlas_fdata[np.where(atlas_fdata == target_array[actual_indx])] = value | ||
for indx, value in enumerate(cap_vector, start=1): | ||
atlas_fdata[np.where(atlas_fdata == target_array[indx])] = value | ||
stat_map = nib.Nifti1Image(atlas_fdata, atlas.affine, atlas.header) | ||
|
||
# Add smoothing to stat map to help mitigate potential coverage issues | ||
if fwhm is not None: | ||
stat_map = image.smooth_img(stat_map, fwhm=fwhm) | ||
|
||
return stat_map | ||
# Knn implementation to aid in coverage issues | ||
if knn_dict: | ||
# Get target indices | ||
target_indices = _get_target_indices(atlas=atlas, knn_dict=knn_dict) | ||
# Get non-zero indices of the stat map | ||
non_zero_indices = np.array(np.where(stat_map.get_fdata() != 0)).T | ||
if "k" not in knn_dict: | ||
warnings.warn("Defaulting to k=1 since 'k' was not specified in `knn_dict`.") | ||
k = 1 | ||
else: k = knn_dict["k"] | ||
|
||
# Build kdtree for nearest neighbors | ||
kdtree = cKDTree(non_zero_indices) | ||
|
||
for target_indx in target_indices: | ||
# Get the nearest non-zero index | ||
_ , neighbor_indx = kdtree.query(target_indx, k = k) | ||
nearest_neighbors = non_zero_indices[neighbor_indx] | ||
|
||
if k > 1: | ||
# Values of nearest neighbors | ||
neighbor_values = [stat_map.get_fdata()[tuple(nearest_neighbor)] for nearest_neighbor in nearest_neighbors] | ||
# Majority vote | ||
new_value = np.bincount(neighbor_values).argmax() | ||
else: | ||
nearest_neighbor = non_zero_indices[neighbor_indx] | ||
new_value = stat_map.get_fdata()[tuple(nearest_neighbor)] | ||
|
||
# Assign the new value to the current index | ||
stat_map.get_fdata()[tuple(target_indx)] = new_value | ||
|
||
return stat_map | ||
|
||
def _get_target_indices(atlas, knn_dict): | ||
# Get schaefer atlas, which projects well onto cortical surface plots | ||
if "resolution_mm" not in knn_dict: | ||
warnings.warn(textwrap.dedent(""" | ||
Defaulting to 1mm resolution for the Schaefer atlas since 'resolution_knn' was | ||
not specified in `knn_dict`. | ||
""")) | ||
resolution_mm = "1mm" | ||
else: | ||
resolution_mm = knn_dict["resolution_mm"] | ||
|
||
schaefer_atlas = datasets.fetch_atlas_schaefer_2018(resolution_mm=resolution_mm)["maps"] | ||
|
||
# Resample schaefer to atlas file | ||
resampled_schaefer = image.resample_to_img(schaefer_atlas, atlas) | ||
# Get indices that equal zero in schaefer atlas to avoid interpolating background values, will also get the indices for subcortical | ||
background_indices_schaefer = set(zip(*np.where(resampled_schaefer.get_fdata() == 0))) | ||
# Get indices 0 indices for atlas | ||
background_indices_atlas = set(zip(*np.where(atlas.get_fdata() == 0))) | ||
# Get the non-background indices through subtraction | ||
target_indices = list(background_indices_atlas - background_indices_schaefer) | ||
|
||
return target_indices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters