def test_invalid_thresholds_in_threshold_maps_ratio(): maps, _ = generate_maps((10, 11, 12), n_regions=2) for invalid_threshold in ['80%', 'auto', -1.0]: with pytest.raises( ValueError, match="threshold given as ratio to the number of voxels must " "be Real number and should be positive " "and between 0 and total number of maps " "i.e. n_maps={0}. " "You provided {1}".format(maps.shape[-1], invalid_threshold)): _threshold_maps_ratio(maps, threshold=invalid_threshold)
def test_nans_threshold_maps_ratio(): maps, _ = generate_maps((10, 10, 10), n_regions=2) data = get_data(maps) data[:, :, 0] = np.nan maps_img = nibabel.Nifti1Image(data, np.eye(4)) thr_maps = _threshold_maps_ratio(maps_img, threshold=0.8)
def test_threshold_maps_ratio(): # smoke test for function _threshold_maps_ratio with randomly # generated maps # make sure that n_regions (4th dimension) are kept same even # in thresholded image maps, _ = generate_maps((6, 8, 10), n_regions=3) thr_maps = _threshold_maps_ratio(maps, threshold=1.0) assert_true(thr_maps.shape[-1] == maps.shape[-1]) # check that the size should be same for 3D image # before and after thresholding img = np.zeros((30, 30, 30)) + 0.1 * np.random.randn(30, 30, 30) img = nibabel.Nifti1Image(img, affine=np.eye(4)) thr_maps_3d = _threshold_maps_ratio(img, threshold=0.5) assert_true(img.shape == thr_maps_3d.shape)
def test_nans_threshold_maps_ratio(): maps, _ = generate_maps((10, 10, 10), n_regions=2) data = maps.get_data() data[:, :, 0] = np.nan maps_img = nibabel.Nifti1Image(data, np.eye(4)) thr_maps = _threshold_maps_ratio(maps_img, threshold=0.8)
def _region_extractor_cache(maps_img, mask_img=None, min_region_size=2500, threshold=1., thresholding_strategy='ratio_n_voxels', extractor='local_regions', memory=Memory(cachedir=None), memory_level=0): """Region Extraction with caching built upon helper functions See nilearn.regions.RegionExtractor for documentation and related """ if thresholding_strategy == 'ratio_n_voxels': print("[Thresholding] using maps ratio with threshold={0}" " ".format(threshold)) threshold_maps = _threshold_maps_ratio(maps_img, threshold) else: if thresholding_strategy == 'percentile': threshold = "{0}%".format(threshold) print("[Thresholding] using threshold_img with threshold={0}" " ".format(threshold)) threshold_maps = threshold_img(maps_img, mask_img=mask_img, threshold=threshold) print("Done thresholding") print("[Region Extraction] with {0}".format(extractor)) regions_img, _ = cache(connected_regions, memory, memory_level=memory_level, func_memory_level=1)(threshold_maps, min_region_size=min_region_size, extract_type=extractor) return regions_img
def test_threshold_maps_ratio(): # smoke test for function _threshold_maps_ratio with randomly # generated maps maps, _ = generate_maps((6, 8, 10), n_regions=3) # test that there is no side effect get_data(maps)[:3] = 100 maps_data = get_data(maps).copy() thr_maps = _threshold_maps_ratio(maps, threshold=1.0) np.testing.assert_array_equal(get_data(maps), maps_data) # make sure that n_regions (4th dimension) are kept same even # in thresholded image assert thr_maps.shape[-1] == maps.shape[-1] # check that the size should be same for 3D image # before and after thresholding img = np.zeros((30, 30, 30)) + 0.1 * np.random.randn(30, 30, 30) img = nibabel.Nifti1Image(img, affine=np.eye(4)) thr_maps_3d = _threshold_maps_ratio(img, threshold=0.5) assert img.shape == thr_maps_3d.shape
def test_threshold_maps_ratio(): # smoke test for function _threshold_maps_ratio with randomly # generated maps maps, _ = generate_maps((6, 8, 10), n_regions=3) # test that there is no side effect maps.get_data()[:3] = 100 maps_data = maps.get_data().copy() thr_maps = _threshold_maps_ratio(maps, threshold=1.0) np.testing.assert_array_equal(maps.get_data(), maps_data) # make sure that n_regions (4th dimension) are kept same even # in thresholded image assert_true(thr_maps.shape[-1] == maps.shape[-1]) # check that the size should be same for 3D image # before and after thresholding img = np.zeros((30, 30, 30)) + 0.1 * np.random.randn(30, 30, 30) img = nibabel.Nifti1Image(img, affine=np.eye(4)) thr_maps_3d = _threshold_maps_ratio(img, threshold=0.5) assert_true(img.shape == thr_maps_3d.shape)