def test_data(tmpdir): sim = Simulator() r = 10 sigma = 1 y = [0, 1] n_reps = 3 output_dir = str(tmpdir) sim.create_data(y, sigma, reps=n_reps, output_dir=output_dir) shape_3d = (91, 109, 91) shape_2d = (6, 238955) y = pd.read_csv(os.path.join(str(tmpdir.join('y.csv'))), header=None, index_col=None).T flist = glob.glob(str(tmpdir.join('centered*.nii.gz'))) # Test load list dat = Brain_Data(data=flist, Y=y) # Test load file assert Brain_Data(flist[0]) # Test to_nifti d = dat.to_nifti() assert d.shape[0:3] == shape_3d # Test load nibabel assert Brain_Data(d) # Test shape assert dat.shape() == shape_2d # Test Mean assert dat.mean().shape()[0] == shape_2d[1] # Test Std assert dat.std().shape()[0] == shape_2d[1] # Test add new = dat + dat assert new.shape() == shape_2d # Test subtract new = dat - dat assert new.shape() == shape_2d # Test multiply new = dat * dat assert new.shape() == shape_2d # Test Iterator x = [x for x in dat] assert len(x) == len(dat) assert len(x[0].data.shape) == 1 # # Test T-test out = dat.ttest() assert out['t'].shape()[0] == shape_2d[1] # # # Test T-test - permutation method # out = dat.ttest(threshold_dict={'permutation':'tfce','n_permutations':50,'n_jobs':1}) # assert out['t'].shape()[0]==shape_2d[1] # Test Regress dat.X = pd.DataFrame( { 'Intercept': np.ones(len(dat.Y)), 'X1': np.array(dat.Y).flatten() }, index=None) out = dat.regress() assert out['beta'].shape() == (2, shape_2d[1]) # Test indexing assert out['t'][1].shape()[0] == shape_2d[1] # Test threshold i = 1 tt = threshold(out['t'][i], out['p'][i], .05) assert isinstance(tt, Brain_Data) # Test write dat.write(os.path.join(str(tmpdir.join('test_write.nii')))) assert Brain_Data(os.path.join(str(tmpdir.join('test_write.nii')))) # Test append assert dat.append(dat).shape()[0] == shape_2d[0] * 2 # Test distance distance = dat.distance(method='euclidean') assert distance.shape == (shape_2d[0], shape_2d[0]) # Test predict stats = dat.predict(algorithm='svm', cv_dict={ 'type': 'kfolds', 'n_folds': 2, 'n': len(dat.Y) }, plot=False, **{'kernel': "linear"}) # Support Vector Regression, with 5 fold cross-validation with Platt Scaling # This will output probabilities of each class stats = dat.predict(algorithm='svm', cv_dict=None, plot=False, **{ 'kernel': 'linear', 'probability': True }) assert isinstance(stats['weight_map'], Brain_Data) # Logistic classificiation, with 5 fold stratified cross-validation. stats = dat.predict(algorithm='logistic', cv_dict={ 'type': 'kfolds', 'n_folds': 5, 'n': len(dat.Y) }, plot=False) assert isinstance(stats['weight_map'], Brain_Data) # Ridge classificiation, with 5 fold between-subject cross-validation, where data for each subject is held out together. stats = dat.predict(algorithm='ridgeClassifier', cv_dict=None, plot=False) assert isinstance(stats['weight_map'], Brain_Data) # Test Similarity r = dat.similarity(stats['weight_map']) assert len(r) == shape_2d[0] r2 = dat.similarity(stats['weight_map'].to_nifti()) assert len(r2) == shape_2d[0] # Test apply_mask - might move part of this to test mask suite s1 = create_sphere([41, 64, 55], radius=10) assert isinstance(s1, nb.Nifti1Image) s2 = Brain_Data(s1) masked_dat = dat.apply_mask(s1) assert masked_dat.shape()[1] == np.sum(s2.data != 0) # Test extract_roi mask = create_sphere([41, 64, 55], radius=10) assert len(dat.extract_roi(mask)) == shape_2d[0] # Test r_to_z z = dat.r_to_z() assert z.shape() == dat.shape() # Test copy d_copy = dat.copy() assert d_copy.shape() == dat.shape() # Test detrend detrend = dat.detrend() assert detrend.shape() == dat.shape()
def create_sphere(coordinates, radius=5, mask=None): """Generate a set of spheres in the brain mask space Args: radius: vector of radius. Will create multiple spheres if len(radius) > 1 centers: a vector of sphere centers of the form [px, py, pz] or [[px1, py1, pz1], ..., [pxn, pyn, pzn]] """ from nltools.data import Brain_Data if mask is not None: if not isinstance(mask, nib.Nifti1Image): if isinstance(mask, str): if os.path.isfile(mask): mask = nib.load(mask) else: raise ValueError("mask is not a nibabel instance or a valid " "file name") else: mask = nib.load(resolve_mni_path(MNI_Template)["mask"]) def sphere(r, p, mask): """create a sphere of given radius at some point p in the brain mask Args: r: radius of the sphere p: point (in coordinates of the brain mask) of the center of the sphere """ dims = mask.shape m = [dims[0] / 2, dims[1] / 2, dims[2] / 2] x, y, z = np.ogrid[-m[0]:dims[0] - m[0], -m[1]:dims[1] - m[1], -m[2]:dims[2] - m[2]] mask_r = x * x + y * y + z * z <= r * r activation = np.zeros(dims) activation[mask_r] = 1 translation_affine = np.array([ [1, 0, 0, p[0] - m[0]], [0, 1, 0, p[1] - m[1]], [0, 0, 1, p[2] - m[2]], [0, 0, 0, 1], ]) return nib.Nifti1Image(activation, affine=translation_affine) if any(isinstance(i, list) for i in coordinates): if isinstance(radius, list): if len(radius) != len(coordinates): raise ValueError("Make sure length of radius list matches" "length of coordinate list.") elif isinstance(radius, int): radius = [radius] * len(coordinates) out = Brain_Data( nib.Nifti1Image(np.zeros_like(mask.get_data()), affine=mask.affine), mask=mask, ) for r, c in zip(radius, coordinates): out = out + Brain_Data(sphere(r, c, mask), mask=mask) else: out = Brain_Data(sphere(radius, coordinates, mask), mask=mask) out = out.to_nifti() out.get_data()[out.get_data() > 0.5] = 1 out.get_data()[out.get_data() < 0.5] = 0 return out
def create_sphere(coordinates, radius=5, mask=None): """ Generate a set of spheres in the brain mask space Args: radius: vector of radius. Will create multiple spheres if len(radius) > 1 centers: a vector of sphere centers of the form [px, py, pz] or [[px1, py1, pz1], ..., [pxn, pyn, pzn]] """ from nltools.data import Brain_Data if mask is not None: if not isinstance(mask, nib.Nifti1Image): if type(mask) is str: if os.path.isfile(mask): data = nib.load(mask) else: raise ValueError("mask is not a nibabel instance or a valid " "file name") else: mask = nib.load( os.path.join(get_resource_path(), 'MNI152_T1_2mm_brain_mask.nii.gz')) dims = mask.get_data().shape def sphere(r, p, mask): """ create a sphere of given radius at some point p in the brain mask Args: r: radius of the sphere p: point (in coordinates of the brain mask) of the center of the sphere """ dims = mask.shape m = [dims[0] / 2, dims[1] / 2, dims[2] / 2] # JC edit: default value for centers x, y, z = np.ogrid[-m[0]:dims[0] - m[0], -m[1]:dims[1] - m[1], -m[2]:dims[2] - m[2]] #JC edit: creates sphere # x, y, z = np.ogrid[-p[0]:dims[0]-p[0], -p[1]:dims[1]-p[1], -p[2]:dims[2]-p[2]] mask_r = x * x + y * y + z * z <= r * r activation = np.zeros(dims) activation[mask_r] = 1 # JC edit shift mask to proper location translation_affine = np.array([[1, 0, 0, p[0] - m[0]], [0, 1, 0, p[1] - m[1]], [0, 0, 1, p[2] - m[2]], [0, 0, 0, 1]]) # activation = np.multiply(activation, mask.get_data()) # activation = nib.Nifti1Image(activation, affine=np.eye(4)) activation = nib.Nifti1Image(activation, affine=translation_affine) #return the 3D numpy matrix of zeros containing the sphere as a region of ones # return activation.get_data(), translation_affine return activation # Initialize Spheres with options for multiple radii and centers of the spheres (or just an int and a 3D list) # return sphere(radius,coordinates,mask) if type(radius) is int: radius = [radius] if coordinates is None: coordinates = [[dims[0] / 2, dims[1] / 2, dims[2] / 2] * len(radius) ] #default value for centers elif type(coordinates) is list and type( coordinates[0]) is int and len(radius) is 1: coordinates = [coordinates] if (type(radius)) is list and (type(coordinates) is list) and (len(radius) == len(coordinates)): A = np.zeros_like(mask.get_data()) A = Brain_Data(nib.Nifti1Image(A, affine=mask.affine), mask=mask) for i in range(len(radius)): A = A + Brain_Data(sphere(radius[i], coordinates[i], mask), mask=mask) A = A.to_nifti() A.get_data()[A.get_data() > 0.5] = 1 A.get_data()[A.get_data() < 0.5] = 0 return A else: raise ValueError("Data type for sphere or radius(ii) or center(s) " "not recognized.")
predator_difference = np.array([ int(re.search('[-]?[0-9]', i).group()) + int('threat' in i) * 10 + int('B' in i) * 100 for i in beta_map_fnames ]) reordering_idx = np.argsort(predator_difference) beta_map_fnames_ordered = [beta_map_fnames[i] for i in reordering_idx] # Load first level data # Data is represented using Nltools Brain_Data format first_level_betas = Brain_Data([ os.path.join( base_dir, 'first_level/rsa_revised/fsl_betas_smoothed/sub-{0}/{1}'.format( subject, i)) for i in beta_map_fnames_ordered ]) first_level_betas_nii = first_level_betas.to_nifti() # Resample mask # Mask comes from T1 which is higher resolution than functional images brain_mask = resample_to_img(brain_mask, first_level_betas_nii) brain_mask_data = brain_mask.get_data() brain_mask_data = np.round(brain_mask_data, 0).astype(int) brain_mask = new_img_like(brain_mask, brain_mask_data) # Get condition info labels = [ re.search('(?<=blocked_with_decision_)[a-z]+', i).group() + ', ' + re.search('[-]?[0-9](?=.0)', i).group() for i in beta_map_fnames_ordered ] sessions = [ re.search('(?<=-d)[12]', i).group() for i in beta_map_fnames_ordered
def test_brain_data(tmpdir): # Add 3mm to list to test that resolution as well for resolution in ['2mm']: MNI_Template["resolution"] = resolution sim = Simulator() r = 10 sigma = 1 y = [0, 1] n_reps = 3 output_dir = str(tmpdir) dat = sim.create_data(y, sigma, reps=n_reps, output_dir=output_dir) if MNI_Template["resolution"] == '2mm': shape_3d = (91, 109, 91) shape_2d = (6, 238955) elif MNI_Template["resolution"] == '3mm': shape_3d = (60, 72, 60) shape_2d = (6, 71020) y = pd.read_csv(os.path.join(str(tmpdir.join('y.csv'))),header=None, index_col=None) holdout = pd.read_csv(os.path.join(str(tmpdir.join('rep_id.csv'))),header=None,index_col=None) # Test load list of 4D images file_list = [str(tmpdir.join('data.nii.gz')), str(tmpdir.join('data.nii.gz'))] dat = Brain_Data(file_list) dat = Brain_Data([nb.load(x) for x in file_list]) # Test load list dat = Brain_Data(data=str(tmpdir.join('data.nii.gz')), Y=y) # Test concatenate out = Brain_Data([x for x in dat]) assert isinstance(out, Brain_Data) assert len(out)==len(dat) # Test to_nifti d = dat.to_nifti() assert d.shape[0:3] == shape_3d # Test load nibabel assert Brain_Data(d) # Test shape assert dat.shape() == shape_2d # Test Mean assert dat.mean().shape()[0] == shape_2d[1] # Test Std assert dat.std().shape()[0] == shape_2d[1] # Test add new = dat + dat assert new.shape() == shape_2d # Test subtract new = dat - dat assert new.shape() == shape_2d # Test multiply new = dat * dat assert new.shape() == shape_2d # Test Indexing index = [0, 3, 1] assert len(dat[index]) == len(index) index = range(4) assert len(dat[index]) == len(index) index = dat.Y == 1 assert len(dat[index.values.flatten()]) == index.values.sum() assert len(dat[index]) == index.values.sum() assert len(dat[:3]) == 3 # Test Iterator x = [x for x in dat] assert len(x) == len(dat) assert len(x[0].data.shape) == 1 # # Test T-test out = dat.ttest() assert out['t'].shape()[0] == shape_2d[1] # # # Test T-test - permutation method # out = dat.ttest(threshold_dict={'permutation':'tfce','n_permutations':50,'n_jobs':1}) # assert out['t'].shape()[0]==shape_2d[1] # Test Regress dat.X = pd.DataFrame({'Intercept':np.ones(len(dat.Y)), 'X1':np.array(dat.Y).flatten()}, index=None) # Standard OLS out = dat.regress() assert type(out['beta'].data) == np.ndarray assert type(out['t'].data) == np.ndarray assert type(out['p'].data) == np.ndarray assert type(out['residual'].data) == np.ndarray assert type(out['df'].data) == np.ndarray assert out['beta'].shape() == (2, shape_2d[1]) assert out['t'][1].shape()[0] == shape_2d[1] # Robust OLS out = dat.regress(mode='robust') assert type(out['beta'].data) == np.ndarray assert type(out['t'].data) == np.ndarray assert type(out['p'].data) == np.ndarray assert type(out['residual'].data) == np.ndarray assert type(out['df'].data) == np.ndarray assert out['beta'].shape() == (2, shape_2d[1]) assert out['t'][1].shape()[0] == shape_2d[1] # Test threshold i=1 tt = threshold(out['t'][i], out['p'][i], .05) assert isinstance(tt, Brain_Data) # Test write dat.write(os.path.join(str(tmpdir.join('test_write.nii')))) assert Brain_Data(os.path.join(str(tmpdir.join('test_write.nii')))) # Test append assert dat.append(dat).shape()[0] == shape_2d[0]*2 # Test distance distance = dat.distance(method='euclidean') assert isinstance(distance, Adjacency) assert distance.square_shape()[0] == shape_2d[0] # Test predict stats = dat.predict(algorithm='svm', cv_dict={'type': 'kfolds', 'n_folds': 2}, plot=False, **{'kernel':"linear"}) # Support Vector Regression, with 5 fold cross-validation with Platt Scaling # This will output probabilities of each class stats = dat.predict(algorithm='svm', cv_dict=None, plot=False, **{'kernel':'linear', 'probability':True}) assert isinstance(stats['weight_map'], Brain_Data) # Logistic classificiation, with 2 fold cross-validation. stats = dat.predict(algorithm='logistic', cv_dict={'type': 'kfolds', 'n_folds': 2}, plot=False) assert isinstance(stats['weight_map'], Brain_Data) # Ridge classificiation, stats = dat.predict(algorithm='ridgeClassifier', cv_dict=None, plot=False) assert isinstance(stats['weight_map'], Brain_Data) # Ridge stats = dat.predict(algorithm='ridge', cv_dict={'type': 'kfolds', 'n_folds': 2, 'subject_id':holdout}, plot=False, **{'alpha':.1}) # Lasso stats = dat.predict(algorithm='lasso', cv_dict={'type': 'kfolds', 'n_folds': 2, 'stratified':dat.Y}, plot=False, **{'alpha':.1}) # PCR stats = dat.predict(algorithm='pcr', cv_dict=None, plot=False) # Test Similarity r = dat.similarity(stats['weight_map']) assert len(r) == shape_2d[0] r2 = dat.similarity(stats['weight_map'].to_nifti()) assert len(r2) == shape_2d[0] r = dat.similarity(stats['weight_map'], method='dot_product') assert len(r) == shape_2d[0] r = dat.similarity(stats['weight_map'], method='cosine') assert len(r) == shape_2d[0] r = dat.similarity(dat, method='correlation') assert r.shape == (dat.shape()[0],dat.shape()[0]) r = dat.similarity(dat, method='dot_product') assert r.shape == (dat.shape()[0],dat.shape()[0]) r = dat.similarity(dat, method='cosine') assert r.shape == (dat.shape()[0],dat.shape()[0]) # Test apply_mask - might move part of this to test mask suite s1 = create_sphere([12, 10, -8], radius=10) assert isinstance(s1, nb.Nifti1Image) masked_dat = dat.apply_mask(s1) assert masked_dat.shape()[1] == np.sum(s1.get_data() != 0) # Test extract_roi mask = create_sphere([12, 10, -8], radius=10) assert len(dat.extract_roi(mask)) == shape_2d[0] # Test r_to_z z = dat.r_to_z() assert z.shape() == dat.shape() # Test copy d_copy = dat.copy() assert d_copy.shape() == dat.shape() # Test detrend detrend = dat.detrend() assert detrend.shape() == dat.shape() # Test standardize s = dat.standardize() assert s.shape() == dat.shape() assert np.isclose(np.sum(s.mean().data), 0, atol=.1) s = dat.standardize(method='zscore') assert s.shape() == dat.shape() assert np.isclose(np.sum(s.mean().data), 0, atol=.1) # Test Sum s = dat.sum() assert s.shape() == dat[1].shape() # Test Groupby s1 = create_sphere([12, 10, -8], radius=10) s2 = create_sphere([22, -2, -22], radius=10) mask = Brain_Data([s1, s2]) d = dat.groupby(mask) assert isinstance(d, Groupby) # Test Aggregate mn = dat.aggregate(mask, 'mean') assert isinstance(mn, Brain_Data) assert len(mn.shape()) == 1 # Test Threshold s1 = create_sphere([12, 10, -8], radius=10) s2 = create_sphere([22, -2, -22], radius=10) mask = Brain_Data(s1)*5 mask = mask + Brain_Data(s2) m1 = mask.threshold(upper=.5) m2 = mask.threshold(upper=3) m3 = mask.threshold(upper='98%') m4 = Brain_Data(s1)*5 + Brain_Data(s2)*-.5 m4 = mask.threshold(upper=.5,lower=-.3) assert np.sum(m1.data > 0) > np.sum(m2.data > 0) assert np.sum(m1.data > 0) == np.sum(m3.data > 0) assert np.sum(m4.data[(m4.data > -.3) & (m4.data <.5)]) == 0 assert np.sum(m4.data[(m4.data < -.3) | (m4.data >.5)]) > 0 # Test Regions r = mask.regions(min_region_size=10) m1 = Brain_Data(s1) m2 = r.threshold(1, binarize=True) # assert len(r)==2 assert len(np.unique(r.to_nifti().get_data())) == 2 diff = m2-m1 assert np.sum(diff.data) == 0 # Test Bootstrap masked = dat.apply_mask(create_sphere(radius=10, coordinates=[0, 0, 0])) n_samples = 3 b = masked.bootstrap('mean', n_samples=n_samples) assert isinstance(b['Z'], Brain_Data) b = masked.bootstrap('std', n_samples=n_samples) assert isinstance(b['Z'], Brain_Data) b = masked.bootstrap('predict', n_samples=n_samples, plot=False) assert isinstance(b['Z'], Brain_Data) b = masked.bootstrap('predict', n_samples=n_samples, plot=False, cv_dict={'type':'kfolds','n_folds':3}) assert isinstance(b['Z'], Brain_Data) b = masked.bootstrap('predict', n_samples=n_samples, save_weights=True, plot=False) assert len(b['samples'])==n_samples # Test decompose n_components = 3 stats = dat.decompose(algorithm='pca', axis='voxels', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) stats = dat.decompose(algorithm='ica', axis='voxels', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) dat.data = dat.data + 2 dat.data[dat.data<0] = 0 stats = dat.decompose(algorithm='nnmf', axis='voxels', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) stats = dat.decompose(algorithm='fa', axis='voxels', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) stats = dat.decompose(algorithm='pca', axis='images', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) stats = dat.decompose(algorithm='ica', axis='images', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) dat.data = dat.data + 2 dat.data[dat.data<0] = 0 stats = dat.decompose(algorithm='nnmf', axis='images', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) stats = dat.decompose(algorithm='fa', axis='images', n_components=n_components) assert n_components == len(stats['components']) assert stats['weights'].shape == (len(dat), n_components) # Test Hyperalignment Method sim = Simulator() y = [0, 1] n_reps = 10 s1 = create_sphere([0, 0, 0], radius=3) d1 = sim.create_data(y, 1, reps=n_reps, output_dir=None).apply_mask(s1) d2 = sim.create_data(y, 2, reps=n_reps, output_dir=None).apply_mask(s1) d3 = sim.create_data(y, 3, reps=n_reps, output_dir=None).apply_mask(s1) # Test procrustes using align data = [d1, d2, d3] out = align(data, method='procrustes') assert len(data) == len(out['transformed']) assert len(data) == len(out['transformation_matrix']) assert data[0].shape() == out['common_model'].shape() transformed = np.dot(d1.data, out['transformation_matrix'][0]) centered = d1.data - np.mean(d1.data, 0) transformed = (np.dot(centered/np.linalg.norm(centered), out['transformation_matrix'][0])*out['scale'][0]) np.testing.assert_almost_equal(0, np.sum(out['transformed'][0].data - transformed), decimal=5) # Test deterministic brain_data bout = d1.align(out['common_model'], method='deterministic_srm') assert d1.shape() == bout['transformed'].shape() assert d1.shape() == bout['common_model'].shape() assert d1.shape()[1] == bout['transformation_matrix'].shape[0] btransformed = np.dot(d1.data, bout['transformation_matrix']) np.testing.assert_almost_equal(0, np.sum(bout['transformed'].data - btransformed)) # Test deterministic brain_data bout = d1.align(out['common_model'], method='probabilistic_srm') assert d1.shape() == bout['transformed'].shape() assert d1.shape() == bout['common_model'].shape() assert d1.shape()[1] == bout['transformation_matrix'].shape[0] btransformed = np.dot(d1.data, bout['transformation_matrix']) np.testing.assert_almost_equal(0, np.sum(bout['transformed'].data-btransformed)) # Test procrustes brain_data bout = d1.align(out['common_model'], method='procrustes') assert d1.shape() == bout['transformed'].shape() assert d1.shape() == bout['common_model'].shape() assert d1.shape()[1] == bout['transformation_matrix'].shape[0] centered = d1.data - np.mean(d1.data, 0) btransformed = (np.dot(centered/np.linalg.norm(centered), bout['transformation_matrix'])*bout['scale']) np.testing.assert_almost_equal(0, np.sum(bout['transformed'].data-btransformed), decimal=5) np.testing.assert_almost_equal(0, np.sum(out['transformed'][0].data - bout['transformed'].data)) # Test hyperalignment on Brain_Data over time (axis=1) sim = Simulator() y = [0, 1] n_reps = 10 s1 = create_sphere([0, 0, 0], radius=5) d1 = sim.create_data(y, 1, reps=n_reps, output_dir=None).apply_mask(s1) d2 = sim.create_data(y, 2, reps=n_reps, output_dir=None).apply_mask(s1) d3 = sim.create_data(y, 3, reps=n_reps, output_dir=None).apply_mask(s1) data = [d1, d2, d3] out = align(data, method='procrustes', axis=1) assert len(data) == len(out['transformed']) assert len(data) == len(out['transformation_matrix']) assert data[0].shape() == out['common_model'].shape() centered = data[0].data.T-np.mean(data[0].data.T, 0) transformed = (np.dot(centered/np.linalg.norm(centered), out['transformation_matrix'][0])*out['scale'][0]) np.testing.assert_almost_equal(0,np.sum(out['transformed'][0].data-transformed.T), decimal=5) bout = d1.align(out['common_model'], method='deterministic_srm', axis=1) assert d1.shape() == bout['transformed'].shape() assert d1.shape() == bout['common_model'].shape() assert d1.shape()[0] == bout['transformation_matrix'].shape[0] btransformed = np.dot(d1.data.T, bout['transformation_matrix']) np.testing.assert_almost_equal(0, np.sum(bout['transformed'].data-btransformed.T)) bout = d1.align(out['common_model'], method='probabilistic_srm', axis=1) assert d1.shape() == bout['transformed'].shape() assert d1.shape() == bout['common_model'].shape() assert d1.shape()[0] == bout['transformation_matrix'].shape[0] btransformed = np.dot(d1.data.T, bout['transformation_matrix']) np.testing.assert_almost_equal(0, np.sum(bout['transformed'].data-btransformed.T)) bout = d1.align(out['common_model'], method='procrustes', axis=1) assert d1.shape() == bout['transformed'].shape() assert d1.shape() == bout['common_model'].shape() assert d1.shape()[0] == bout['transformation_matrix'].shape[0] centered = d1.data.T-np.mean(d1.data.T, 0) btransformed = (np.dot(centered/np.linalg.norm(centered), bout['transformation_matrix'])*bout['scale']) np.testing.assert_almost_equal(0, np.sum(bout['transformed'].data-btransformed.T), decimal=5) np.testing.assert_almost_equal(0, np.sum(out['transformed'][0].data-bout['transformed'].data))
img_s2 = Brain_Data(neurite2) masked_img_s2 = img_s2.apply_mask(r_mask) masked_img_s2.plot() masked_img_s2.plot(anatomical=anat) len(masked_img_s2.data) sns.distplot(masked_img_s2.data) # ==== Test ground for playing ==== # # %% plotting glass brain /w `nilearn` import nilearn from nilearn import plotting plotting.plot_glass_brain(neurite1) # %% testing smoothing on the image from nilearn import image smoothed_img = image.smooth_img(neurite1, fwhm=5) plotting.plot_glass_brain(smoothed_img) # %% loading image as `Nifti1Image` and header img = image.load_img(neurite1) print(img.header) print(img.shape) # %% visualizing glass brain /w `nltools` from nltools.data import Brain_Data from nilearn.plotting import plot_glass_brain img_s1 = Brain_Data(neurite2) plot_glass_brain(img_s1.to_nifti())
# In[56]: data2 = (data + 10) * 2 # Brain_Data instances can be copied # In[57]: new = data.copy() # Brain_Data instances can be easily converted to nibabel instances, which store the data in a 3D/4D matrix. This is useful for interfacing with other python toolboxes such as [nilearn](http://nilearn.github.io) # # In[58]: data.to_nifti() # Brain_Data instances can be concatenated using the append method # In[59]: new = new.append(data[4]) # Lists of `Brain_Data` instances can also be concatenated by recasting as a `Brain_Data` object. # In[60]: print(type([x for x in data[:4]])) type(Brain_Data([x for x in data[:4]]))
def test_brain_data(tmpdir): sim = Simulator() r = 10 sigma = 1 y = [0, 1] n_reps = 3 output_dir = str(tmpdir) sim.create_data(y, sigma, reps=n_reps, output_dir=output_dir) shape_3d = (91, 109, 91) shape_2d = (6, 238955) y=pd.read_csv(os.path.join(str(tmpdir.join('y.csv'))), header=None,index_col=None).T holdout=pd.read_csv(os.path.join(str(tmpdir.join('rep_id.csv'))),header=None,index_col=None).T flist = glob.glob(str(tmpdir.join('centered*.nii.gz'))) # Test load list dat = Brain_Data(data=flist,Y=y) # Test load file assert Brain_Data(flist[0]) # Test to_nifti d = dat.to_nifti() assert d.shape[0:3] == shape_3d # Test load nibabel assert Brain_Data(d) # Test shape assert dat.shape() == shape_2d # Test Mean assert dat.mean().shape()[0] == shape_2d[1] # Test Std assert dat.std().shape()[0] == shape_2d[1] # Test add new = dat + dat assert new.shape() == shape_2d # Test subtract new = dat - dat assert new.shape() == shape_2d # Test multiply new = dat * dat assert new.shape() == shape_2d # Test Iterator x = [x for x in dat] assert len(x) == len(dat) assert len(x[0].data.shape) == 1 # # Test T-test out = dat.ttest() assert out['t'].shape()[0] == shape_2d[1] # # # Test T-test - permutation method # out = dat.ttest(threshold_dict={'permutation':'tfce','n_permutations':50,'n_jobs':1}) # assert out['t'].shape()[0]==shape_2d[1] # Test Regress dat.X = pd.DataFrame({'Intercept':np.ones(len(dat.Y)), 'X1':np.array(dat.Y).flatten()},index=None) out = dat.regress() assert out['beta'].shape() == (2,shape_2d[1]) # Test indexing assert out['t'][1].shape()[0] == shape_2d[1] # Test threshold i=1 tt = threshold(out['t'][i], out['p'][i], .05) assert isinstance(tt,Brain_Data) # Test write dat.write(os.path.join(str(tmpdir.join('test_write.nii')))) assert Brain_Data(os.path.join(str(tmpdir.join('test_write.nii')))) # Test append assert dat.append(dat).shape()[0]==shape_2d[0]*2 # Test distance distance = dat.distance(method='euclidean') assert isinstance(distance,Adjacency) assert distance.square_shape()[0]==shape_2d[0] # Test predict stats = dat.predict(algorithm='svm', cv_dict={'type': 'kfolds','n_folds': 2}, plot=False,**{'kernel':"linear"}) # Support Vector Regression, with 5 fold cross-validation with Platt Scaling # This will output probabilities of each class stats = dat.predict(algorithm='svm', cv_dict=None, plot=False,**{'kernel':'linear', 'probability':True}) assert isinstance(stats['weight_map'],Brain_Data) # Logistic classificiation, with 2 fold cross-validation. stats = dat.predict(algorithm='logistic', cv_dict={'type': 'kfolds', 'n_folds': 2}, plot=False) assert isinstance(stats['weight_map'],Brain_Data) # Ridge classificiation, stats = dat.predict(algorithm='ridgeClassifier', cv_dict=None,plot=False) assert isinstance(stats['weight_map'],Brain_Data) # Ridge stats = dat.predict(algorithm='ridge', cv_dict={'type': 'kfolds', 'n_folds': 2,'subject_id':holdout}, plot=False,**{'alpha':.1}) # Lasso stats = dat.predict(algorithm='lasso', cv_dict={'type': 'kfolds', 'n_folds': 2,'stratified':dat.Y}, plot=False,**{'alpha':.1}) # PCR stats = dat.predict(algorithm='pcr', cv_dict=None, plot=False) # Test Similarity r = dat.similarity(stats['weight_map']) assert len(r) == shape_2d[0] r2 = dat.similarity(stats['weight_map'].to_nifti()) assert len(r2) == shape_2d[0] # Test apply_mask - might move part of this to test mask suite s1 = create_sphere([12, 10, -8], radius=10) assert isinstance(s1, nb.Nifti1Image) s2 = Brain_Data(s1) masked_dat = dat.apply_mask(s1) assert masked_dat.shape()[1] == np.sum(s2.data != 0) # Test extract_roi mask = create_sphere([12, 10, -8], radius=10) assert len(dat.extract_roi(mask)) == shape_2d[0] # Test r_to_z z = dat.r_to_z() assert z.shape() == dat.shape() # Test copy d_copy = dat.copy() assert d_copy.shape() == dat.shape() # Test detrend detrend = dat.detrend() assert detrend.shape() == dat.shape() # Test standardize s = dat.standardize() assert s.shape() == dat.shape() assert np.isclose(np.sum(s.mean().data), 0, atol=.1) s = dat.standardize(method='zscore') assert s.shape() == dat.shape() assert np.isclose(np.sum(s.mean().data), 0, atol=.1) # Test Sum s = dat.sum() assert s.shape() == dat[1].shape() # Test Groupby s1 = create_sphere([12, 10, -8], radius=10) s2 = create_sphere([22, -2, -22], radius=10) mask = Brain_Data([s1, s2]) d = dat.groupby(mask) assert isinstance(d, Groupby) # Test Aggregate mn = dat.aggregate(mask, 'mean') assert isinstance(mn, Brain_Data) assert len(mn.shape()) == 1 # Test Threshold s1 = create_sphere([12, 10, -8], radius=10) s2 = create_sphere([22, -2, -22], radius=10) mask = Brain_Data(s1)*5 mask = mask + Brain_Data(s2) m1 = mask.threshold(thresh=.5) m2 = mask.threshold(thresh=3) m3 = mask.threshold(thresh='98%') assert np.sum(m1.data > 0) > np.sum(m2.data > 0) assert np.sum(m1.data > 0) == np.sum(m3.data > 0) # Test Regions r = mask.regions(min_region_size=10) m1 = Brain_Data(s1) m2 = r.threshold(1, binarize=True) # assert len(r)==2 assert len(np.unique(r.to_nifti().get_data())) == 2 # JC edit: I think this is what you were trying to do diff = m2-m1 assert np.sum(diff.data) == 0
# Download a Single Image from the Web # ------------------------------------ # # It's possible to load a single image from a web URL using the Brain_Data # load method. The files are downloaded to a temporary directory and will # eventually be erased by your computer so be sure to write it out to a file # if you would like to save it. Here we plot it using nilearn's glass brain # function. from nilearn.plotting import plot_glass_brain mask = Brain_Data( 'http://neurovault.org/media/images/2099/Neurosynth%20Parcellation_0.nii.gz' ) plot_glass_brain(mask.to_nifti()) ######################################################################### # Upload Data to Neurovault # ------------------------- # # There is a method to easily upload a Brain_Data() instance to # `neurovault <http://neurovault.org>`_. This requires using your api key, which can be found # under your account settings. Anything stored in data.X will be uploaded as # image metadata. The required fields include collection_name, the img_type, # img_modality, and analysis_level. See https://github.com/neurolearn/pyneurovault_upload # for additional information about the required fields. (Don't forget to uncomment the line!) api_key = 'your_neurovault_api_key' # mask.upload_neurovault(access_token=api_key, collection_name='Neurosynth Parcellation',
import subprocess subprocess.call('fslmaths {0} -sub {1} {2}'.format(asl2, asl1, diff_fname), shell=True) # !!NEED TO TRY!! `ImCalc` in spm12 # visualizing the difference map - need to check if this is the right way!! fname2 = pathlib.Path(asl2).stem fname1 = pathlib.Path(asl1).stem diff_img = image.index_img(diff_fname + '.nii.gz', 1) diff_img_BD_mask = Brain_Data(diff_img, mask=anat_betmask) plotting.plot_roi( diff_img_BD_mask.to_nifti(), bg_img=anat, display_mode='z', cut_coords=z_cut, threshold=50, dim=-1, # dimming the anatomy background colorbar=True, output_file="{0}_visual.png".format(diff_fname), title="ASL difference map: {0}.nii.gz".format(diff_fname)) # %% visualizing Scan2-Scan1 neurite density difference map sub = image.load_img(sub_img) plotting.plot_roi(sub, bg_img=anat, display_mode='z', dim=-1, threshold=0.5) # %% --- visualize processed CBF maps with no anatomy - using `nilearn::image`