def test_projector_rois(): td = get_testdir() ins = op.join(td, 'in.surf.gii') outs = op.join(td, 'out.surf.gii') hemi = toblerone.Hemisphere(ins, outs, 'L') spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz')) fracs = nibabel.load(op.join(td, 'sph_fractions.nii.gz')).get_fdata() hemi.pvs = fracs.reshape(-1, 3) puta = Surface.manual(0.25 * hemi.inSurf.points, hemi.inSurf.tris, 'L_Puta') rois = {'L_Puta': puta} proj = toblerone.projection.Projector(hemi, spc, rois=rois, cores=8) ndata = np.ones(proj.n_nodes) ndata[-1] = 2 vdata = proj.node2vol(ndata, True) spc.save_image(vdata, 'n2v.nii.gz') ndata = proj.vol2node(vdata, True) print(ndata)
def load(cls, path): """ Load Projector from path in HDF5 format. This is useful for performing repeated analyses with the same voxel grid and cortical surfaces. """ f = h5py.File(path, 'r') p = cls.__new__(cls) # Recreate the reference ImageSpace first p.spc = ImageSpace.manual(f['ref_spc_vox2world'][()], f['ref_spc_size'][()]) if 'ref_spc_fname' in f: p.spc.fname = f['ref_spc_fname'][()] n_vox = p.spc.size.prod() # Now read out hemisphere specific properties p._hemi_pvs = [] p.vox_tri_mats = [] p.vtx_tri_mats = [] p.hemi_dict = {} p._roi_pvs = {} for s in SIDES: hemi_key = f"{s}_hemi" if hemi_key in f: # Read out the surfaces, create the Hemisphere ins, outs = [ Surface.manual( f[hemi_key][f'{s}{n}S_points'][()], f[hemi_key][f'{s}{n}S_tris'][()], f'{s}{n}S') for n in ['W', 'P'] ] p.hemi_dict[s] = Hemisphere(ins, outs, s) # Read out the PVs array for the hemi p._hemi_pvs.append(f[hemi_key][f"{s}_pvs"][()]) # Recreate the sparse voxtri and vtxtri matrices. # They are stored as a 3 x N array, where top row # is row indices, second is column, then data voxtri = f[hemi_key][f"{s}_vox_tri"][()] assert voxtri.shape[0] == 3, 'expected 3 rows' voxtri = sparse.coo_matrix( (voxtri[2,:], (voxtri[0,:], voxtri[1,:])), shape=(n_vox, ins.tris.shape[0])) p.vox_tri_mats.append(voxtri.tocsr()) # Same convention as above vtxtri = f[hemi_key][f"{s}_vtx_tri"][()] assert vtxtri.shape[0] == 3, 'expected 3 rows' vtxtri = sparse.coo_matrix( (vtxtri[2,:], (vtxtri[0,:], vtxtri[1,:])), shape=(ins.n_points, ins.tris.shape[0])) p.vtx_tri_mats.append(vtxtri.tocsr()) if "subcortical_pvs" in f: g = f["subcortical_pvs"] for k in sorted(g.keys()): p._roi_pvs[k] = g[k][()] return p