def test_rsa_temp(self): """Test RSA with a temporal searchlight.""" data = np.array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]) model_dsm = np.array([1, 2, 1]) # One model DSM, no parallelization patches = searchlight(data.shape, temporal_radius=1) rsa_result = rsa_array(data, model_dsm, patches, data_dsm_metric='euclidean') assert rsa_result.shape == (2,) assert_equal(rsa_result, 1) # Multiple model DSMs, no parallelization patches = searchlight(data.shape, temporal_radius=1) rsa_result = rsa_array(data, [model_dsm, model_dsm], patches, data_dsm_metric='euclidean') assert rsa_result.shape == (2, 2) assert_equal(rsa_result, 1) # One model DSM, parallelization across 2 CPUs patches = searchlight(data.shape, temporal_radius=1) rsa_result = rsa_array(data, model_dsm, patches, data_dsm_metric='euclidean', n_jobs=2) assert rsa_result.shape == (2,) assert_equal(rsa_result, 1) # Multiple model DSMs, parallelization across 2 CPUs patches = searchlight(data.shape, temporal_radius=1) rsa_result = rsa_array(data, [model_dsm, model_dsm], patches, data_dsm_metric='euclidean', n_jobs=2) assert rsa_result.shape == (2, 2) assert_equal(rsa_result, 1)
def test_rsa_spat(self): """Test RSA with a spatial searchlight.""" data = np.array([[[1], [2], [3]], [[2], [3], [4]], [[3], [4], [5]]]) model_dsm = np.array([1, 2, 1]) dist = np.array([[0, 1, 2], [1, 0, 1], [2, 1, 0]]) # One model DSM, no paralellization patches = searchlight(data.shape, dist, spatial_radius=1) rsa_result = rsa_array(data, model_dsm, patches, data_dsm_metric='euclidean') assert rsa_result.shape == (3,) assert_equal(rsa_result, 1) # Multiple model DSMs, no paralellization patches = searchlight(data.shape, dist, spatial_radius=1) rsa_result = rsa_array(data, [model_dsm, model_dsm], patches, data_dsm_metric='euclidean') assert rsa_result.shape == (3, 2) assert_equal(rsa_result, 1) # One model DSM, paralellization across 2 CPUs patches = searchlight(data.shape, dist, spatial_radius=1) rsa_result = rsa_array(data, model_dsm, patches, data_dsm_metric='euclidean', n_jobs=2) assert rsa_result.shape == (3,) assert_equal(rsa_result, 1) # Multiple model DSMs, paralellization across 2 CPUs patches = searchlight(data.shape, dist, spatial_radius=1) rsa_result = rsa_array(data, [model_dsm, model_dsm], patches, data_dsm_metric='euclidean', n_jobs=2) assert rsa_result.shape == (3, 2) assert_equal(rsa_result, 1)
def test_iter_spatial(self): """Test generating spatial searchlight patches.""" dist = np.array([[0, 1, 2], [1, 0, 1], [2, 1, 0]]) s = searchlight((5, 10, 3, 4), dist, spatial_radius=2) assert len(s) == 3 assert s.shape == (3, ) assert_equal(list(s), [ (slice(None), slice(None), np.array([0, 1]), slice(None)), (slice(None), slice(None), np.array([0, 1, 2]), slice(None)), (slice(None), slice(None), np.array([1, 2]), slice(None)), ]) s = searchlight((10, 3, 4), dist, spatial_radius=2) assert len(s) == 3 assert s.shape == (3, ) assert_equal(list(s), [ (slice(None), np.array([0, 1]), slice(None)), (slice(None), np.array([0, 1, 2]), slice(None)), (slice(None), np.array([1, 2]), slice(None)), ]) s = searchlight((10, 3), dist, spatial_radius=2) assert len(s) == 3 assert s.shape == (3, ) assert_equal(list(s), [ (slice(None), np.array([0, 1])), (slice(None), np.array([0, 1, 2])), (slice(None), np.array([1, 2])), ])
def test_iter_temporal(self): """Test generating temporal searchlight patches.""" s = searchlight((10, 3, 4), temporal_radius=1) assert len(s) == 2 assert s.shape == (2, ) assert_equal(list(s), [ (slice(None), slice(None), slice(0, 3)), (slice(None), slice(None), slice(1, 4)), ]) s = searchlight((10, 4), temporal_radius=1) assert len(s) == 2 assert s.shape == (2, ) assert_equal(list(s), [ (slice(None), slice(0, 3)), (slice(None), slice(1, 4)), ])
def test_temporal(self): """Test computing DSMs using a temporal searchlight.""" data = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) patches = searchlight(data.shape, temporal_radius=1) dsms = dsm_array(data, patches, dist_metric='euclidean') assert len(dsms) == len(patches) assert dsms.shape == (2, 1) assert_equal(list(dsms), [0, 0])
def test_spatial(self): """Test computing DSMs using a spatial searchlight.""" dist = np.array([[0, 1, 2, 3], [1, 0, 1, 2], [2, 1, 0, 1], [3, 2, 1, 0]]) data = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) patches = searchlight(data.shape, dist, spatial_radius=1) dsms = dsm_array(data, patches, dist_metric='euclidean') assert len(dsms) == len(patches) assert dsms.shape == (4, 1) assert_equal(list(dsms), [0, 0, 0, 0])
def test_iter_single(self): """Test generating a single searchlight patch.""" s = searchlight((5, 10, 3, 4)) assert len(s) == 1 assert s.shape == tuple() assert_equal(next(s), tuple([slice(None)] * 4)) s = searchlight((10, 3, 4)) assert len(s) == 1 assert s.shape == tuple() assert_equal(next(s), tuple([slice(None)] * 3)) s = searchlight((10, 3)) assert len(s) == 1 assert s.shape == tuple() assert_equal(next(s), tuple([slice(None)] * 2)) s = searchlight((10, )) assert len(s) == 1 assert s.shape == tuple() assert_equal(next(s), (slice(None), ))
def test_spatio_temporal(self): """Test computing DSMs using a spatio-temporal searchlight.""" data = np.array([[[1, 2, 3], [2, 3, 4]], [[2, 3, 4], [3, 4, 5]], [[3, 4, 5], [4, 5, 6]]]) dist = np.array([[0, 1, 2], [1, 0, 1], [2, 1, 0]]) patches = searchlight(data.shape, dist, spatial_radius=1, temporal_radius=1) dsms = dsm_array(data, patches, dist_metric='correlation') assert len(dsms) == len(patches) assert dsms.shape == (2, 1, 3) assert_allclose(list(dsms), [[0, 0, 0], [0, 0, 0]], atol=1E-15)
def test_crossvalidation(self): """Test computing DSMs using a searchlight and cross-validation.""" data = np.array([[[1, 2, 3], [2, 3, 4]], [[2, 3, 4], [3, 4, 5]], [[3, 4, 5], [4, 5, 6]], [[1, 2, 3], [2, 3, 4]], [[2, 3, 4], [3, 4, 5]], [[3, 4, 5], [4, 5, 6]]]) dist = np.array([[0, 1, 2], [1, 0, 1], [2, 1, 0]]) patches = searchlight(data.shape, dist, spatial_radius=1, temporal_radius=1) dsms = dsm_array(data, patches, y=[1, 2, 3, 1, 2, 3], n_folds=2) assert len(dsms) == len(patches) assert dsms.shape == (2, 1, 3) assert_allclose(list(dsms), [[0, 0, 0], [0, 0, 0]], atol=1E-15)
def test_invalid_input(self): """Test giving invalid input to searchlight generator.""" dist = np.array([[0, 1, 2], [1, 0, 1], [2, 1, 0]]) # Spatio-temporal case with pytest.raises(ValueError, match='no spatial dimension'): searchlight((10, ), dist=dist, spatial_radius=1, temporal_radius=1) with pytest.raises(ValueError, match='no temporal dimension'): searchlight((10, 3), dist=dist, spatial_radius=1, temporal_radius=1) with pytest.raises(ValueError, match='no distance information'): searchlight((10, 3, 4), spatial_radius=1, temporal_radius=1) # Spatial case with pytest.raises(ValueError, match='no spatial dimension'): searchlight((10, ), dist=dist, spatial_radius=1) with pytest.raises(ValueError, match='no distance information'): searchlight((10, 5), spatial_radius=1) # Temporal case with pytest.raises(ValueError, match='no temporal dimension'): searchlight((10, ), dist=dist, temporal_radius=1)