cosanlab / nltools

Python toolbox for analyzing imaging data
https://nltools.org
MIT License
121 stars 44 forks source link

Issue with Brain_Data.regions() method #407

Closed ljchang closed 1 year ago

ljchang commented 2 years ago

I'm running into issues using the regions method.

(post - pre).ttest(threshold_dict={'unc':.001})['thr_t'].regions(min_region_size=27)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-19-f7ecab4d5566> in <module>
----> 1 (post - pre).ttest(threshold_dict={'unc':.001})['thr_t'].regions(min_region_size=27)

~/anaconda3/lib/python3.8/site-packages/nltools/data/brain_data.py in regions(self, min_region_size, extract_type, smoothing_fwhm, is_mask)
   1928             regions, _ = connected_label_regions(self.to_nifti())
   1929         else:
-> 1930             regions, _ = connected_regions(
   1931                 self.to_nifti(), min_region_size, extract_type, smoothing_fwhm
   1932             )

~/anaconda3/lib/python3.8/site-packages/nilearn/regions/region_extractor.py in connected_regions(maps_img, min_region_size, extract_type, smoothing_fwhm, mask_img)
    213             # Assign -1 to values which are 0. to indicate to ignore
    214             seeds_label[map_3d == 0.] = -1
--> 215             rw_maps = _random_walker(map_3d, seeds_label)
    216             # Now simply replace "-1" with "0" for regions separation
    217             rw_maps[rw_maps == -1] = 0.

~/anaconda3/lib/python3.8/site-packages/nilearn/_utils/segmentation.py in _random_walker(data, labels, beta, tol, copy, spacing)
    284         lap_sparse = _build_laplacian(data, spacing, beta=beta)
    285 
--> 286     lap_sparse, B = _buildAB(lap_sparse, labels)
    287 
    288     # We solve the linear system

~/anaconda3/lib/python3.8/site-packages/nilearn/_utils/segmentation.py in _buildAB(lap_sparse, labels)
    110     seeds_indices = indices[labels > 0]
    111     # The following two lines take most of the time in this function
--> 112     B = lap_sparse[unlabeled_indices][:, seeds_indices]
    113     lap_sparse = lap_sparse[unlabeled_indices][:, unlabeled_indices]
    114     nlabels = labels.max()

~/anaconda3/lib/python3.8/site-packages/scipy/sparse/_index.py in __getitem__(self, key)
     31     """
     32     def __getitem__(self, key):
---> 33         row, col = self._validate_indices(key)
     34         # Dispatch to specialized methods.
     35         if isinstance(row, INT_TYPES):

~/anaconda3/lib/python3.8/site-packages/scipy/sparse/_index.py in _validate_indices(self, key)
    136                 row += M
    137         elif not isinstance(row, slice):
--> 138             row = self._asindices(row, M)
    139 
    140         if isintlike(col):

~/anaconda3/lib/python3.8/site-packages/scipy/sparse/_index.py in _asindices(self, idx, length)
    168         max_indx = x.max()
    169         if max_indx >= length:
--> 170             raise IndexError('index (%d) out of range' % max_indx)
    171 
    172         min_indx = x.min()

IndexError: index (29) out of range

I think there are two possibilities producing the error:

1) we need to re-evaluate using nilearn's connected_label_regions vs connected_regions 2) we need to make sure we can pass arguments into nilearn's functions such as min_region_size

ljchang commented 1 year ago

I can't replicate this error. Closing for now.