[d15511]: / gen_unfold_template / constrain_surf_to_bbox.py

Download this file

50 lines (38 with data), 1.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import nibabel as nib
import numpy as np
epsilon = 0.01
#load gifti surf
gii = nib.load(snakemake.input.gii)
arr = gii.get_arrays_from_intent('NIFTI_INTENT_POINTSET')[0]
vertices = arr.data
#get ref nii (for defining bbox)
img = nib.load(snakemake.input.ref_nii)
affine = img.affine
print(affine)
lower_coord = np.array([0,0,0,1])
upper_coord = np.hstack([np.subtract(img.shape[0:3],1),1])
print(lower_coord)
print(upper_coord)
#get bounds by taking corners of img and getting phys coords
bounds = np.vstack([affine@lower_coord,affine@upper_coord]);
print(bounds)
#drop the 4-th dim
bounds = bounds[:,0:3]
print(bounds)
#and sort from neg to pos in each dim
bounds = np.sort(bounds,0);
print(bounds)
#replicate to enable comparison with g.vertices
minrep = np.add(np.tile(bounds[0,:],(vertices.shape[0],1)) , epsilon)
maxrep = np.subtract(np.tile(bounds[1,:],(vertices.shape[0],1)) , epsilon)
#get indices where too low or high
too_low = np.where(vertices<minrep);
too_high = np.where(vertices>maxrep);
print(f'surface {snakemake.input.gii}:')
print(f'{too_low[0].size} vertices below minimum')
print(f'{too_high[0].size} vertices above maximum')
#replace with the min or max
vertices[too_low] = minrep[too_low]
vertices[too_high] = maxrep[too_high]
#save gifti
nib.save(gii,snakemake.output.gii)