def test_invalid_threshold_strategies(): maps, _ = generate_maps((6, 8, 10), n_regions=1) extract_strategy_check = RegionExtractor(maps, thresholding_strategy='n_') valid_strategies = ['ratio_n_voxels', 'img_value', 'percentile'] with pytest.raises(ValueError, match="'thresholding_strategy' should be either of " "these".format(valid_strategies)): extract_strategy_check.fit()
def test_region_extractor_fit_and_transform(): n_regions = 9 n_subjects = 5 maps, mask_img = generate_maps((40, 40, 40), n_regions=n_regions) # smoke test to RegionExtractor with thresholding_strategy='ratio_n_voxels' extract_ratio = RegionExtractor(maps, threshold=0.2, thresholding_strategy='ratio_n_voxels') extract_ratio.fit() assert_not_equal(extract_ratio.regions_img_, '') assert_true(extract_ratio.regions_img_.shape[-1] >= 9) # smoke test with threshold=string and strategy=percentile extractor = RegionExtractor(maps, threshold=30, thresholding_strategy='percentile', mask_img=mask_img) extractor.fit() assert_true(extractor.index_, np.ndarray) assert_not_equal(extractor.regions_img_, '') assert_true(extractor.regions_img_.shape[-1] >= 9) n_regions_extracted = extractor.regions_img_.shape[-1] shape = (91, 109, 91, 7) expected_signal_shape = (7, n_regions_extracted) for id_ in range(n_subjects): img, data = _make_random_data(shape) # smoke test NiftiMapsMasker transform inherited in Region Extractor signal = extractor.transform(img) assert_equal(expected_signal_shape, signal.shape)
def test_threshold_as_none_and_string_cases(): maps, _ = generate_maps((6, 8, 10), n_regions=1) extract_thr_none_check = RegionExtractor(maps, threshold=None) assert_raises_regex(ValueError, "The given input to threshold is not valid.", extract_thr_none_check.fit) extract_thr_string_check = RegionExtractor(maps, threshold='30%') assert_raises_regex(ValueError, "The given input to threshold is not valid.", extract_thr_string_check.fit)
def test_threshold_as_none_and_string_cases(): maps, _ = generate_maps((6, 8, 10), n_regions=1) extract_thr_none_check = RegionExtractor(maps, threshold=None) with pytest.raises(ValueError, match="The given input to threshold is not valid."): extract_thr_none_check.fit() extract_thr_string_check = RegionExtractor(maps, threshold='30%') with pytest.raises(ValueError, match="The given input to threshold is not valid."): extract_thr_string_check.fit()
def test_region_extractor_fit_and_transform(): n_regions = 9 n_subjects = 5 maps, mask_img = generate_maps((40, 40, 40), n_regions=n_regions) # Test maps are zero in the mask mask_data = get_data(mask_img) mask_data[1, 1, 1] = 0 extractor_without_mask = RegionExtractor(maps) extractor_without_mask.fit() extractor_with_mask = RegionExtractor(maps, mask_img=mask_img) extractor_with_mask.fit() assert not np.all( get_data(extractor_without_mask.regions_img_)[mask_data == 0] == 0.) assert np.all( get_data(extractor_with_mask.regions_img_)[mask_data == 0] == 0.) # smoke test to RegionExtractor with thresholding_strategy='ratio_n_voxels' extract_ratio = RegionExtractor(maps, threshold=0.2, thresholding_strategy='ratio_n_voxels') extract_ratio.fit() assert extract_ratio.regions_img_ != '' assert extract_ratio.regions_img_.shape[-1] >= 9 # smoke test with threshold=string and strategy=percentile extractor = RegionExtractor(maps, threshold=30, thresholding_strategy='percentile', mask_img=mask_img) extractor.fit() assert extractor.index_, np.ndarray assert extractor.regions_img_ != '' assert extractor.regions_img_.shape[-1] >= 9 n_regions_extracted = extractor.regions_img_.shape[-1] shape = (91, 109, 91, 7) expected_signal_shape = (7, n_regions_extracted) for id_ in range(n_subjects): img, data = _make_random_data(shape) # smoke test NiftiMapsMasker transform inherited in Region Extractor signal = extractor.transform(img) assert expected_signal_shape == signal.shape # smoke test with high resolution image maps, mask_img = generate_maps((20, 20, 20), n_regions=n_regions, affine=.2 * np.eye(4)) extract_ratio = RegionExtractor(maps, thresholding_strategy='ratio_n_voxels', smoothing_fwhm=.6, min_region_size=.4) extract_ratio.fit() assert extract_ratio.regions_img_ != '' assert extract_ratio.regions_img_.shape[-1] >= 9 # smoke test with zeros on the diagonal of the affine affine = np.eye(4) affine[[0, 1]] = affine[[1, 0]] # permutes first and second lines maps, mask_img = generate_maps((40, 40, 40), n_regions=n_regions, affine=affine) extract_ratio = RegionExtractor(maps, threshold=0.2, thresholding_strategy='ratio_n_voxels') extract_ratio.fit() assert extract_ratio.regions_img_ != '' assert extract_ratio.regions_img_.shape[-1] >= 9
def test_region_extractor_fit_and_transform(): n_regions = 9 n_subjects = 5 maps, mask_img = generate_maps((40, 40, 40), n_regions=n_regions) # smoke test to RegionExtractor with thresholding_strategy='ratio_n_voxels' extract_ratio = RegionExtractor(maps, threshold=0.2, thresholding_strategy='ratio_n_voxels') extract_ratio.fit() assert_not_equal(extract_ratio.regions_img_, '') assert_true(extract_ratio.regions_img_.shape[-1] >= 9) # smoke test with threshold=string and strategy=percentile extractor = RegionExtractor(maps, threshold=30, thresholding_strategy='percentile', mask_img=mask_img) extractor.fit() assert_true(extractor.index_, np.ndarray) assert_not_equal(extractor.regions_img_, '') assert_true(extractor.regions_img_.shape[-1] >= 9) n_regions_extracted = extractor.regions_img_.shape[-1] shape = (91, 109, 91, 7) expected_signal_shape = (7, n_regions_extracted) for id_ in range(n_subjects): img, data = _make_random_data(shape) # smoke test NiftiMapsMasker transform inherited in Region Extractor signal = extractor.transform(img) assert_equal(expected_signal_shape, signal.shape) # smoke test with high resolution image maps, mask_img = generate_maps((20, 20, 20), n_regions=n_regions, affine=.2 * np.eye(4)) extract_ratio = RegionExtractor(maps, thresholding_strategy='ratio_n_voxels', smoothing_fwhm=.6, min_region_size=.4) extract_ratio.fit() assert_not_equal(extract_ratio.regions_img_, '') assert_true(extract_ratio.regions_img_.shape[-1] >= 9) # smoke test with zeros on the diagonal of the affine affine = np.eye(4) affine[[0, 1]] = affine[[1, 0]] # permutes first and second lines maps, mask_img = generate_maps((40, 40, 40), n_regions=n_regions, affine=affine) extract_ratio = RegionExtractor(maps, threshold=0.2, thresholding_strategy='ratio_n_voxels') extract_ratio.fit() assert_not_equal(extract_ratio.regions_img_, '') assert_true(extract_ratio.regions_img_.shape[-1] >= 9)
plotting.plot_prob_atlas(components_img, view_type='filled_contours', title='Dictionary Learning maps') ################################################################################ # Extracting regions from networks # Import Region Extractor algorithm from regions module # threshold=0.5 indicates that we keep nominal of amount nonzero voxels across all # maps, less the threshold means that more intense non-voxels will be survived. from nilearn.regions import RegionExtractor extractor = RegionExtractor(components_img, threshold=0.5, thresholding_strategy='ratio_n_voxels', extractor='local_regions', standardize=True, min_region_size=1350) # Just call fit() to process for regions extraction extractor.fit() # Extracted regions are stored in regions_img_ regions_extracted_img = extractor.regions_img_ # Each region index is stored in index_ regions_index = extractor.index_ # Total number of regions extracted n_regions_extracted = regions_extracted_img.shape[-1] # Visualization of region extraction results title = ('%d regions are extracted from %d components.' '\nEach separate color of region indicates extracted region' % (n_regions_extracted, 5))
# control_correlations = [] # for i in subjects_timeseries: # print(labels[i]) # if labels[i] == 1: # abide_correlations.append(correlation_matrices[i]) # else: # control_correlations.append(correlation_matrices[i]) # In[ ]: # region extraction extractor = RegionExtractor(components_img, threshold=2., thresholding_strategy= 'ratio_n_voxels', extractor='local_regions', standardize=True, min_region_size=1350) extractor.fit() regions_extracted_img = extractor.regions_img_ n_regions_extracted = regions_extracted_img.shape[-1] # Visualization of region extraction results title = ('%d regions are extracted from %d components.' % (n_regions_extracted, 20)) plotting.plot_prob_atlas(regions_extracted_img,
using :class:`nilearn.regions.RegionExtractor` from regions module """ ################################################################################ # Fetching the smith ICA 10 RSN by importing datasets utilities from nilearn import datasets smith_atlas = datasets.fetch_atlas_smith_2009() atlas_networks = smith_atlas.rsn10 ################################################################################ # Import region extractor to extract atlas networks from nilearn.regions import RegionExtractor # min_region_size in voxel volume mm^3 extraction = RegionExtractor(atlas_networks, min_region_size=800, threshold=98, thresholding_strategy='percentile') # Just call fit() to execute region extraction procedure extraction.fit() regions_img = extraction.regions_img_ ################################################################################ # Visualization # Show region extraction results by importing image & plotting utilities from nilearn import plotting from nilearn.image import index_img from nilearn.plotting import find_xyz_cut_coords # Showing region extraction results using 4D maps visualization tool plotting.plot_prob_atlas(regions_img, display_mode='z', cut_coords=1, view_type='contours', title="Regions extracted.")
################################################################################# ####################### REGION EXTRACTOR ###################################### ################################################################################ # Extract regions from networks # ------------------------------ # Import Region Extractor algorithm from regions module # threshold=0.5 indicates that we keep nominal of amount nonzero voxels across all # maps, less the threshold means that more intense non-voxels will be survived. from nilearn.regions import RegionExtractor extractor = RegionExtractor(atlas_harvard_oxford.maps, threshold=2, thresholding_strategy='ratio_n_voxels', extractor='local_regions', standardize=True, min_region_size=512) # Just call fit() to process for regions extraction extractor.fit() # Extracted regions are stored in regions_img_ regions_extracted_img = extractor.regions_img_ # Each region index is stored in index_ regions_index = extractor.index_ # Total number of regions extracted n_regions_extracted = regions_extracted_img.shape[-1] # Visualization of region extraction results title = ('%d regions are extracted from %d components.' '\nEach separate color of region indicates extracted region' % (n_regions_extracted, 3))
""" ################################################################################ # Fetching the smith ICA 10 RSN by importing datasets utilities from nilearn import datasets smith_atlas = datasets.fetch_atlas_smith_2009() atlas_networks = smith_atlas.rsn10 ################################################################################ # Import region extractor to extract atlas networks from nilearn.regions import RegionExtractor # min_region_size in voxel volume mm^3 extraction = RegionExtractor(atlas_networks, min_region_size=800, threshold=98, thresholding_strategy='percentile') # Just call fit() to execute region extraction procedure extraction.fit() regions_img = extraction.regions_img_ ################################################################################ # Visualization # Show region extraction results by importing image & plotting utilities from nilearn import plotting from nilearn.image import index_img from nilearn.plotting import find_xyz_cut_coords # Showing region extraction results using 4D maps visualization tool plotting.plot_prob_atlas(regions_img,
from nilearn import plotting plotting.plot_prob_atlas(components_img, view_type='filled_contours', title='Dictionary Learning maps') ################################################################################ # Extract regions from networks # ------------------------------ # Import Region Extractor algorithm from regions module # threshold=0.5 indicates that we keep nominal of amount nonzero voxels across all # maps, less the threshold means that more intense non-voxels will be survived. from nilearn.regions import RegionExtractor extractor = RegionExtractor(components_img, threshold=0.5, thresholding_strategy='ratio_n_voxels', extractor='local_regions', standardize=True, min_region_size=1350) # Just call fit() to process for regions extraction extractor.fit() # Extracted regions are stored in regions_img_ regions_extracted_img = extractor.regions_img_ # Each region index is stored in index_ regions_index = extractor.index_ # Total number of regions extracted n_regions_extracted = regions_extracted_img.shape[-1] # Visualization of region extraction results title = ('%d regions are extracted from %d components.' '\nEach separate color of region indicates extracted region' % (n_regions_extracted, 8)) plotting.plot_prob_atlas(regions_extracted_img, view_type='filled_contours',
for i in range(n_components): img = image.index_img(ica_map, i) display = nilearn.plotting.plot_glass_brain(img, title='comp number ' + str(i)) ### plotting.plot_prob_atlas(ica_map, view_type='filled_contours', title='Dictionary Learning maps') ##### build regions from nilearn.regions import RegionExtractor extractor = RegionExtractor(ica_map, threshold=0.5, thresholding_strategy='ratio_n_voxels', extractor='local_regions', standardize=True, min_region_size=1350) # Just call fit() to process for regions extraction extractor.fit() # Extracted regions are stored in regions_img_ regions_extracted_img = extractor.regions_img_ # Each region index is stored in index_ regions_index = extractor.index_ # Total number of regions extracted n_regions_extracted = regions_extracted_img.shape[-1] plotting.plot_prob_atlas(regions_extracted_img, view_type='filled_contours', title='atlas de ouf')